import {z} from 'zod' export type GetApiApiTaxRetentionsIdArgs = {id: string, omit?: string | undefined, fields?: string | undefined}; export type GetApiApiTaxRetentionsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const link = z.string().nullable(); export const collectedAt = z.string(); export const createdAt = z.string(); export const enumTaxRetentionReceiverNationality = z.enum(["NATIONAL", "FOREIGN"]).nullable(); export const enumTaxRetentionPaymentStatus = z.enum(["PAID", "PROVISIONED"]).nullable(); export const retentionBreakdown = z.object({base_amount: z.number().nullable(), tax_type: z.string().nullable(), retained_amount: z.number().nullable(), payment_status: enumTaxRetentionPaymentStatus}); export const taxRetentions = z.object({id: id.optional(), link: link.optional(), collected_at: collectedAt, created_at: createdAt.optional(), invoice_identification: z.string().nullable(), version: z.string().nullable(), code: z.number().int().nullable(), issued_at: z.string().nullable(), certified_at: z.string().nullable(), cancelled_at: z.string().nullable(), sender_id: z.string().nullable(), sender_name: z.string().nullable(), receiver_nationality: enumTaxRetentionReceiverNationality, receiver_id: z.string().nullable(), receiver_name: z.string().nullable(), total_invoice_amount: z.number().nullable(), total_exempt_amount: z.number().nullable(), total_retained_amount: z.number().nullable(), total_taxable_amount: z.number().nullable(), retention_breakdown: z.array(retentionBreakdown).nullable(), xml: z.string().nullable()}); 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 tax retention's details * * GET /api/tax-retentions/{id}/ */ export const getApiApiTaxRetentionsId = async (args: GetApiApiTaxRetentionsIdArgs, options?: GetApiApiTaxRetentionsIdOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/tax-retentions/${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(taxRetentions, await res.json()) };