import {z} from 'zod' export type GetApiV1ReceiptsIdArgs = {id: number}; export type GetApiV1ReceiptsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const receiptLinkType = z.enum(["Commitment", "Invoice", "Credit"]); export const receiptLink = z.object({amount: z.number().nullable().optional(), id: z.number().int().optional(), type: receiptLinkType.optional()}); export const paymentMethod = z.enum(["Transfer", "Order", "Check", "Cash", "Card", "Bill", "Usec", "Other"]); export const paymentStatus = z.enum(["Success", "Pending", "Failed"]); export const receipt = z.object({amount: z.number().optional(), customerId: z.number().int().nullable().optional(), customerInternalId: z.string().nullable().optional(), customerName: z.string().nullable().optional(), customerNumber: z.string().nullable().optional(), date: z.string().optional(), id: z.number().int().optional(), internalId: z.string().nullable().optional(), links: z.array(receiptLink).nullable().optional(), metadata: z.record(z.string(), z.string().nullable()).nullable().optional(), notes: z.string().nullable().optional(), paymentMethod: paymentMethod.optional(), reference: z.string().nullable().optional(), status: paymentStatus.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 a receipt by ID * * GET /v1/receipts/{id} */ export const getApiV1ReceiptsId = async (args: GetApiV1ReceiptsIdArgs, options?: GetApiV1ReceiptsIdOptions) => { const {baseUrl = "https://api.altoviz.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/receipts/${encodeURIComponent(String(args.id))}${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(receipt, await res.json()) };