import {z} from 'zod' export type GetApiApiTaxRetentionsArgs = {link?: string | undefined, page_size?: number | undefined, page?: number | undefined, omit?: string | undefined, fields?: string | undefined, link__in?: Array | undefined, created_at?: string | undefined, created_at__gt?: string | undefined, created_at__gte?: string | undefined, created_at__lt?: string | undefined, created_at__lte?: string | undefined, created_at__range?: Array | undefined}; export type GetApiApiTaxRetentionsOptions = {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 getApiApiTaxRetentionsResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(taxRetentions).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 }; /** * List tax retentions * * GET /api/tax-retentions/ */ export const getApiApiTaxRetentions = async (args: GetApiApiTaxRetentionsArgs, options?: GetApiApiTaxRetentionsOptions) => { 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.link != null) query.append("link", String(args.link)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.link__in != null) query.append("link__in", args.link__in.join(',')) if (args.created_at != null) query.append("created_at", String(args.created_at)) if (args.created_at__gt != null) query.append("created_at__gt", String(args.created_at__gt)) if (args.created_at__gte != null) query.append("created_at__gte", String(args.created_at__gte)) if (args.created_at__lt != null) query.append("created_at__lt", String(args.created_at__lt)) if (args.created_at__lte != null) query.append("created_at__lte", String(args.created_at__lte)) if (args.created_at__range != null) query.append("created_at__range", args.created_at__range.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/tax-retentions/${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(getApiApiTaxRetentionsResponse, await res.json()) };