import {z} from 'zod' export type GetApiBillingBalanceStatementArgs = {period_start_at: string, period_end_at: string}; export type GetApiBillingBalanceStatementOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionBalanceStatementUsage = z.object({amount_microdollars: z.number().int(), count: z.number().int(), product: z.string()}); export const subscriptionBalanceStatement = z.object({added_microdollars: z.number().int(), ending_balance_microdollars: z.number().int(), expired_microdollars: z.number().int(), other_microdollars: z.number().int(), period_end_at: z.string(), period_start_at: z.string(), refunded_microdollars: z.number().int(), starting_balance_microdollars: z.number().int(), usage: z.array(subscriptionBalanceStatementUsage).nullable()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get balance statement * * GET /billing/balance/statement */ export const getApiBillingBalanceStatement = async (args: GetApiBillingBalanceStatementArgs, options?: GetApiBillingBalanceStatementOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_start_at != null) query.append("period_start_at", String(args.period_start_at)) if (args.period_end_at != null) query.append("period_end_at", String(args.period_end_at)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/billing/balance/statement${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(subscriptionBalanceStatement, await res.json()) };