import {z} from 'zod' export type InvoiceCreateInput = {merchant_id: string, period_start: string, period_end: string, currency: string, notes?: (string | null) | undefined}; export type CreateApiApiV1AdminInvoicesArgs = {body: InvoiceCreateInput}; export type CreateApiApiV1AdminInvoicesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceResponse = z.object({success: z.boolean(), data: 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 }; /** * Create a draft invoice for a merchant over a (period, currency) window. Claims eligible transactions atomically; pre-settled fees produce an immediately_settled payment row. * * POST /api/v1/admin/invoices */ export const createApiApiV1AdminInvoices = async (args: CreateApiApiV1AdminInvoicesArgs, options?: CreateApiApiV1AdminInvoicesOptions) => { const {baseUrl = "https://core.quidkey.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(invoiceResponse, await res.json()) };