import {z} from 'zod' export type GetApiApiV1AdminInvoicesArgs = {merchant_id?: string | undefined, status?: ('draft' | 'issued' | 'paid' | 'void') | undefined, period_start_from?: string | undefined, period_start_to?: string | undefined, limit?: string | undefined, offset?: string | undefined}; export type GetApiApiV1AdminInvoicesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceListResponse = z.object({success: z.boolean(), data: z.array(z.object({id: z.string(), invoice_number: z.string(), payment_reference: z.string(), merchant_id: z.string(), status: z.union([z.literal("draft"), z.literal("issued"), z.literal("paid"), z.literal("void")]), currency: z.string().min(3).max(3), period_start: z.string(), period_end: z.string(), total_amount: z.string(), amount_paid: z.string(), outstanding_amount: z.string(), transaction_count: z.number(), due_date: z.string().nullable(), issued_at: z.string().nullable(), issued_by_user_id: z.string().nullable(), paid_at: z.string().nullable(), voided_at: z.string().nullable(), voided_reason: z.string().nullable(), voided_by_user_id: z.string().nullable(), notes: z.string().nullable(), created_by_user_id: z.string().nullable(), created_at: z.string(), updated_at: z.string(), payments: z.array(z.object({id: z.string(), amount: z.string(), currency: z.string().min(3).max(3), payment_method: z.union([z.literal("manual_bank_transfer"), z.literal("immediately_settled"), z.literal("issue_credit")]), external_reference: z.string().nullable(), paid_at: z.string(), recorded_by_user_id: z.string().nullable(), recorded_at: z.string(), notes: z.string().nullable()})), fx_rates: z.array(z.object({source_currency: z.string().min(3).max(3), rate: z.string(), captured_at: z.string(), fetched_at: z.string().nullable(), provider: z.string()}))}))}); 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 invoices across all billables with optional filters * * GET /api/v1/admin/invoices */ export const getApiApiV1AdminInvoices = async (args: GetApiApiV1AdminInvoicesArgs, options?: GetApiApiV1AdminInvoicesOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchant_id != null) query.append("merchant_id", String(args.merchant_id)) if (args.status != null) query.append("status", String(args.status)) if (args.period_start_from != null) query.append("period_start_from", String(args.period_start_from)) if (args.period_start_to != null) query.append("period_start_to", String(args.period_start_to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/invoices${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(invoiceListResponse, await res.json()) };