import {z} from 'zod' export type GetApiBillingHistoryArgs = {limit?: number | undefined, offset?: number | undefined, search?: string | undefined, billing_cycle?: ('daily' | 'weekly' | 'monthly' | 'yearly') | undefined, status?: ('paid' | 'pending' | 'failed' | 'void') | undefined, invoiced_after?: string | undefined, invoiced_before?: string | undefined, plan_name?: string | undefined, phone_id?: string | undefined, order_by?: string | undefined}; export type GetApiBillingHistoryOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billingHistoryItem = z.object({amount_display: z.string(), amount_microdollars: z.number().int(), billing_cycle: z.enum(["daily", "weekly", "monthly", "yearly"]), currency: z.string(), description: z.string(), due_at: z.string().optional(), hosted_invoice_url: z.string().optional(), id: z.string(), invoice_number: z.string(), invoice_pdf_url: z.string().optional(), invoiced_at: z.string(), paid_at: z.string().optional(), plan_name: z.string(), status: z.enum(["paid", "pending", "failed", "void"])}); export const billingHistoryResponse = z.object({$schema: z.string().optional(), items: z.array(billingHistoryItem).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 billing history * * GET /billing/history */ export const getApiBillingHistory = async (args: GetApiBillingHistoryArgs, options: GetApiBillingHistoryOptions) => { const {baseUrl, 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)) if (args.search != null) query.append("search", String(args.search)) if (args.billing_cycle != null) query.append("billing_cycle", String(args.billing_cycle)) if (args.status != null) query.append("status", String(args.status)) if (args.invoiced_after != null) query.append("invoiced_after", String(args.invoiced_after)) if (args.invoiced_before != null) query.append("invoiced_before", String(args.invoiced_before)) if (args.plan_name != null) query.append("plan_name", String(args.plan_name)) if (args.phone_id != null) query.append("phone_id", String(args.phone_id)) 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/history${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(billingHistoryResponse, await res.json()) };