import {z} from 'zod' export type PaymentReceipt = {id?: number | undefined, sources: Array, document?: ('MX22' | null) | undefined, number?: number | undefined, date?: (string | null) | undefined, custom_change?: (number | null) | undefined, metadata?: Record | undefined, tax_settings?: Record | undefined, status?: ('draft' | 'finalized') | undefined, biller_status?: string | undefined, tax_agency?: Record | undefined, local_file?: (string | null) | undefined, created?: string | undefined, updated?: string | undefined}; export type CreateApiPaymentReceiptsArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, body: PaymentReceipt}; export type CreateApiPaymentReceiptsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentReceipt = z.object({id: z.number().int().optional(), sources: z.array(z.string()), document: z.literal("MX22").nullable().optional(), number: z.number().int().optional(), date: z.string().nullable().optional(), custom_change: z.number().gte(1).nullable().optional(), metadata: z.object({}).optional(), tax_settings: z.object({}).optional(), status: z.enum(["draft", "finalized"]).optional(), biller_status: z.string().optional(), tax_agency: z.object({}).optional(), local_file: z.string().nullable().optional(), created: z.string().optional(), updated: z.string().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 }; /** * Create payment receipt (draft) * * POST /payment-receipts/ */ export const createApiPaymentReceipts = async (args: CreateApiPaymentReceiptsArgs, options?: CreateApiPaymentReceiptsOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/payment-receipts/${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(paymentReceipt, await res.json()) };