import {z} from 'zod' export type GetApiApiV1AdminInvoicesInvoiceIdDocumentArgs = {invoiceId: string}; export type GetApiApiV1AdminInvoicesInvoiceIdDocumentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceDocumentBundleResponse = z.object({success: z.boolean(), data: z.object({invoice: 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(), paid_at: z.string().nullable(), created_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()}))}), line_items: z.array(z.object({transaction_id: z.string(), transaction_number: z.string(), transaction_created_at: z.string(), deposit_amount_minor: z.number(), deposit_currency: z.string().min(3).max(3), payout_currency: z.string().min(3).max(3), exchange_rate: z.number(), fee_amount_minor: z.number(), fee_currency: z.string().min(3).max(3), fee_rate_type: z.string(), fee_rate_value: z.number(), fee_rate_unit: z.string()})), currency_subtotals: z.array(z.object({source_currency: z.string().min(3).max(3), subtotal_source_minor: z.number(), subtotal_invoice_minor: z.number().nullable(), rate: z.string().nullable(), rate_source: z.union([z.literal("same_currency"), z.literal("stored"), z.literal("snapshot"), z.literal("estimate"), z.literal("pending")]), rate_captured_at: z.string().nullable(), rate_fetched_at: z.string().nullable(), rate_provider: z.string().nullable()})), grand_total_minor: z.number().nullable(), grand_total_is_estimate: z.boolean(), merchant: z.object({id: z.string(), name: z.string(), address_lines: z.array(z.string())}), issuer: z.object({name: z.string(), address_lines: z.array(z.string()), country: z.string(), vat_number: z.string().nullable(), contact_email: z.string(), contact_url: z.string()}), fee_account: z.object({currency: z.string().min(3).max(3), provider: z.string(), account_holder_name: z.string().nullable().optional(), iban: z.string().nullable().optional(), swift: z.string().nullable().optional(), account_number: z.string().nullable().optional(), sort_code: z.string().nullable().optional(), routing_number: z.string().nullable().optional(), bsb: z.string().nullable().optional()}).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 }; /** * Returns the JSON document bundle that drives the PDF/CSV renderers — line items, per-currency subtotals, fee account, issuer block, FX snapshots. Powers the Console preview. * * GET /api/v1/admin/invoices/{invoiceId}/document */ export const getApiApiV1AdminInvoicesInvoiceIdDocument = async (args: GetApiApiV1AdminInvoicesInvoiceIdDocumentArgs, options?: GetApiApiV1AdminInvoicesInvoiceIdDocumentOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/admin/invoices/${encodeURIComponent(String(args.invoiceId))}/document${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(invoiceDocumentBundleResponse, await res.json()) };