import {z} from 'zod' export type CreateApiV1InvoicesListArgs = {'x-api-version': string, body: {start_cursor?: string | undefined, limit?: number | undefined, customer_id?: string | undefined, entity_id?: string | undefined, status?: (Array<'draft' | 'open' | 'void' | 'paid' | 'uncollectible'>) | undefined, processor_types?: (Array<'stripe' | 'revenuecat'>) | undefined}}; export type CreateApiV1InvoicesListOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1InvoicesListResponse = z.object({list: z.array(z.object({plan_ids: z.array(z.string()), stripe_id: z.string(), processor_type: z.enum(["stripe", "revenuecat"]).optional(), status: z.string(), total: z.number(), currency: z.string(), created_at: z.number(), hosted_invoice_url: z.string().nullable().optional(), id: z.string(), customer_id: z.string().nullable(), entity_id: z.string().nullable(), amount_paid: z.number().nullable(), refunded_amount: z.number()})), next_cursor: z.string().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 }; /** * Lists invoices with cursor pagination and optional filters (customer, entity, status, processor). Pass `start_cursor: ""` (or omit) for the first page; use `next_cursor` from a prior response for subsequent pages. * * POST /v1/invoices.list */ export const createApiV1InvoicesList = async (args: CreateApiV1InvoicesListArgs, options?: CreateApiV1InvoicesListOptions) => { const {baseUrl = "https://api.useautumn.com", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/invoices.list${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1InvoicesListResponse, await res.json()) };