import {z} from 'zod' export type UUID = string; export type GetApiInvoicesCaptureIdArgs = {captureId: UUID}; export type GetApiInvoicesCaptureIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(36).max(36); export const amountDTO = z.object({gross: z.number().gte(0.01), net: z.number().gte(0.01), tax: z.number().gte(0)}); export const paymentMethodType = z.enum(["bank_transfer", "direct_debit"]); export const tinyText = z.string().max(255); export const dateTime = z.string(); export const paymentMethod = z.object({type: paymentMethodType.optional(), data: z.object({iban: tinyText.optional(), bic: tinyText.optional(), bank_name: z.string().nullable().optional(), bank_giro: z.string().nullable().optional(), sort_code: z.string().nullable().optional(), account_number: z.string().nullable().optional(), bank_account_owner: z.string().optional(), mandate_reference: z.string().nullable().optional(), mandate_execution_date: dateTime.optional(), creditor_identification: z.string().nullable().optional()}).optional()}); export const paymentMethodCollection = z.array(paymentMethod); export const getInvoiceResponse = z.object({uuid: uUID.optional(), invoice_number: z.string().nullable().optional(), duration: z.number().optional(), payout_amount: z.number().optional(), amount: amountDTO.optional(), outstanding_amount: z.number().optional(), fee_amount: z.number().optional(), fee_rate: z.number().optional(), created_at: z.string().optional(), due_date: z.string().optional(), state: z.string().optional(), selected_payment_method: z.enum(["bank_transfer", "direct_debit"]).nullable().optional(), payment_methods: paymentMethodCollection.optional()}); 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 }; /** * Get Invoice * * GET /invoices/{captureId} */ export const getApiInvoicesCaptureId = async (args: GetApiInvoicesCaptureIdArgs, options?: GetApiInvoicesCaptureIdOptions) => { const {baseUrl = "https://paella-sandbox.billie.io/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/invoices/${encodeURIComponent(String(args.captureId))}${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(getInvoiceResponse, await res.json()) };