import { kitRequest } from './client.ts'
import { parseAuthInput } from './auth.ts'
import type { JsonRecord, KitAuthInput } from './types.ts'
import { requireRecord } from './types.ts'
export type KitAccountInfo = {
id: number | null
name: string | null
planType: string | null
}
function asAccount(body: unknown): KitAccountInfo {
const record =
body && typeof body === 'object' && (body as { account?: unknown }).account
? ((body as { account: JsonRecord }).account ?? {})
: body && typeof body === 'object'
? (body as JsonRecord)
: {}
return {
id: typeof record.id === 'number' ? record.id : null,
name: typeof record.name === 'string' ? record.name : null,
planType: typeof record.plan_type === 'string' ? record.plan_type : null,
}
}
/**
* Read the current Kit creator account.
* @example
* import { getAccount } from 'kody:@kody/kit/account'
* const account = await getAccount()
*/
export async function getAccount(input: KitAuthInput = {}): Promise<KitAccountInfo> {
const result = await kitRequest<JsonRecord>({
...input,
method: 'GET',
path: '/account',
})
return asAccount(result.data)
}
/**
* Read account email stats.
* @example
* import { getAccountEmailStats } from 'kody:@kody/kit/account'
* const stats = await getAccountEmailStats()
*/
export async function getAccountEmailStats(input: KitAuthInput = {}) {
const result = await kitRequest({
...input,
method: 'GET',
path: '/account/email_stats',
})
return result.data
}
/**
* Read account growth stats.
* @example
* import { getAccountGrowthStats } from 'kody:@kody/kit/account'
* const stats = await getAccountGrowthStats()
*/
export async function getAccountGrowthStats(input: KitAuthInput = {}) {
const result = await kitRequest({
...input,
method: 'GET',
path: '/account/growth_stats',
})
return result.data
}
/**
* Read the current Kit creator account.
* @example
* import getAccount from 'kody:@kody/kit/account'
* const account = await getAccount()
*/
export default async function accountEntrypoint(
params: KitAuthInput & Record<string, unknown> = {},
) {
return getAccount(parseAuthInput(requireRecord(params, 'account')))
}