import {z} from 'zod' export type GetApiBillingBalanceTransactionsArgs = {limit?: number | undefined, offset?: number | undefined, type?: (Array<'opening_balance' | 'unrecorded' | 'deposit' | 'usage' | 'plan_grant' | 'plan_lapse' | 'expiry' | 'adjustment' | 'refund_reversal' | 'dispute_reversal' | 'dispute_reinstated' | 'refund_failed'> | null) | undefined, created_after?: string | undefined, created_before?: string | undefined, order_by?: string | undefined}; export type GetApiBillingBalanceTransactionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionBalanceTransaction = z.object({amount_display: z.string(), amount_microdollars: z.number().int(), balance_after_microdollars: z.number().int(), created_at: z.string(), description: z.string(), id: z.string(), source_id: z.string().optional(), source_type: z.enum(["session", "inference"]).optional(), type: z.enum(["opening_balance", "unrecorded", "deposit", "usage", "plan_grant", "plan_lapse", "expiry", "adjustment", "refund_reversal", "dispute_reversal", "dispute_reinstated", "refund_failed"])}); export const subscriptionBalanceTransactionsResponse = z.object({items: z.array(subscriptionBalanceTransaction).nullable(), limit: z.number().int(), offset: z.number().int(), total: z.number().int()}); 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 }; /** * List balance transactions * * GET /billing/balance/transactions */ export const getApiBillingBalanceTransactions = async (args: GetApiBillingBalanceTransactionsArgs, options?: GetApiBillingBalanceTransactionsOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/billing/balance/transactions${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(subscriptionBalanceTransactionsResponse, await res.json()) };