import {z} from 'zod' export type GetApiApiInvoicesArgs = {link?: string | undefined, page?: number | undefined, page_size?: number | undefined, link__in?: Array | undefined, omit?: string | undefined, fields?: string | undefined, id?: string | undefined, id__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, invoice_date?: string | undefined, invoice_date__lt?: string | undefined, invoice_date__lte?: string | undefined, invoice_date__gt?: string | undefined, invoice_date__gte?: string | undefined, invoice_date__range?: Array | undefined, invoice_identification?: string | undefined, invoice_identification__in?: Array | undefined, status?: string | undefined, status__in?: Array | undefined, type?: ('OUTFLOW' | 'INFLOW') | undefined, type__in?: (Array<'OUTFLOW' | 'INFLOW'>) | undefined, total_amount?: number | undefined, total_amount__lt?: number | undefined, total_amount__lte?: number | undefined, total_amount__gt?: number | undefined, total_amount__gte?: number | undefined, total_amount__range?: Array | undefined}; export type GetApiApiInvoicesOptions = {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 enumInvoiceSatInvoiceType = z.enum(["Egreso", "Ingreso", "NĂ³mina", "Pago", "Traslado"]).nullable(); export const enumInvoiceType = z.enum(["OUTFLOW", "INFLOW", "null"]).nullable(); export const taxDetailsInvoicesSat = z.object({total_tax_retained: z.number().optional(), total_tax_transferred: z.number().optional(), retained_taxes: z.array(z.object({tax_type: z.string().nullable().optional(), tax: z.string().optional(), tax_percentage: z.number().nullable().optional(), pre_tax_amount: z.number().nullable().optional(), tax_amount: z.number().optional()})).optional(), transferred_taxes: z.array(z.object({tax_type: z.string().nullable().optional(), tax: z.string().optional(), tax_percentage: z.number().optional(), pre_tax_amount: z.number().optional(), tax_amount: z.number().optional()})).optional()}); export const enumInvoiceSatPaymentMethod = z.enum(["PUE", "PPD", "null"]).nullable(); export const invoiceDetailRetainedTaxSat = z.object({collected_at: collectedAt.optional(), tax_type: z.string().nullable().optional(), tax: z.string().nullable(), tax_percentage: z.number().nullable(), retained_tax_amount: z.number().nullable()}); export const invoiceDetailTransferredTaxSat = z.object({collected_at: collectedAt.optional(), tax_type: z.string().nullable().optional(), tax: z.string().nullable(), tax_percentage: z.number().nullable(), transferred_tax_amount: z.number().nullable()}); export const invoiceDetailSat = z.object({collected_at: collectedAt.optional(), description: z.string().nullable(), product_identification: z.string().nullable(), quantity: z.number().nullable(), unit_code: z.string().nullable(), unit_description: z.string().nullable(), unit_amount: z.number().nullable(), discount: z.number().nullable().optional(), tax_subject_code: z.string().nullable().optional(), identifier_number: z.string().nullable().optional(), pre_tax_amount: z.number().nullable(), tax_percentage: z.number().nullable(), tax_amount: z.number().nullable(), total_amount: z.number().nullable(), retained_taxes: z.array(invoiceDetailRetainedTaxSat).optional(), transferred_taxes: z.array(invoiceDetailTransferredTaxSat).optional()}); export const relatedInvoiceSat = z.object({relationship_type: z.string().nullable().optional(), related_invoice_identification: z.string().nullable().optional()}); export const invoicesPaymentsRelatedDocumentsSat = z.object({invoice_identification: z.string().nullable(), currency: z.string().nullable(), payment_method: z.string().nullable(), partiality_number: z.number().int().optional(), previous_balance: z.number().nullable(), amount_paid: z.number().nullable(), outstanding_balance: z.number().nullable()}); export const invoicesPaymentsSat = z.object({date: z.string().nullable(), payment_type: z.string().nullable(), currency: z.string().nullable(), exchange_rate: z.string().nullable(), amount: z.number().nullable(), operation_number: z.string().nullable(), beneficiary_rfc: z.string().nullable().optional(), beneficiary_account_number: z.string().nullable(), payer_rfc: z.string().nullable(), payer_account_number: z.string().nullable(), payer_bank_name: z.string().nullable(), related_documents: z.array(invoicesPaymentsRelatedDocumentsSat)}); export const enumInvoicePayrollPeriodicity = z.enum(["DAILY", "WEEKLY", "TENTH_DAY", "FOURTEENTH_DAY", "FIFTEENTH_DAY", "MONTHLY", "BIMONTHLY", "PER_TASK", "COMMISSION", "ONE_OFF", "OTHER_PERIODICITY", "null"]).nullable(); export const invoicePayrollEarningsBreakdown = z.object({type: z.string().nullable().optional(), taxable_amount: z.number().optional(), vat_free_amount: z.number().optional()}).nullable(); export const invoicePayrollTaxDeductions = z.object({type: z.string().nullable().optional(), amount: z.number().optional()}).nullable(); export const invoicePayrollOtherPayments = z.object({type: z.string().nullable().optional(), amount: z.number().optional()}).nullable(); export const invoicesPayrollSat = z.object({days: z.number().int().nullable(), type: z.string().nullable(), amount: z.number(), version: z.string(), date_from: z.string().nullable(), date_to: z.string().nullable(), collected_at: collectedAt.optional(), payment_date: z.string(), periodicity: enumInvoicePayrollPeriodicity.optional(), earnings_breakdown: z.array(invoicePayrollEarningsBreakdown).nullable().optional(), tax_deductions: z.array(invoicePayrollTaxDeductions).nullable().optional(), other_payments: z.array(invoicePayrollOtherPayments).nullable().optional()}).nullable(); export const invoiceWarningsSat = z.object({code: z.string().nullable(), message: z.string().nullable()}).nullable(); export const invoiceWithIdSat = z.object({id: id.optional(), link: link.optional(), collected_at: collectedAt, created_at: createdAt.optional(), invoice_identification: z.string().nullable(), invoice_date: z.string().nullable(), status: z.string().nullable(), invoice_type: enumInvoiceSatInvoiceType, type: enumInvoiceType, tax_details: taxDetailsInvoicesSat.optional(), sender_id: z.string().nullable(), sender_fiscal_regime: z.string().nullable().optional(), sender_name: z.string().nullable(), sender_tax_fraud_status: z.string().nullable().optional(), receiver_id: z.string().nullable(), receiver_postal_code: z.string().optional(), receiver_fiscal_regime: z.string().nullable().optional(), receiver_name: z.string().nullable(), receiver_tax_fraud_status: z.string().nullable().optional(), cancelation_status: z.string().nullable(), cancelation_update_date: z.string().nullable(), certification_date: z.string().nullable(), certification_authority: z.string().nullable(), payment_type: z.string().nullable(), payment_type_description: z.string().nullable(), payment_method: enumInvoiceSatPaymentMethod.optional(), payment_method_description: z.string().nullable().optional(), usage: z.string().nullable().optional(), version: z.string().nullable().optional(), place_of_issue: z.string().nullable().optional(), invoice_details: z.array(invoiceDetailSat), currency: z.string().nullable(), subtotal_amount: z.number().nullable(), exchange_rate: z.number().nullable(), tax_amount: z.number().nullable(), discount_amount: z.number().nullable(), total_amount: z.number().nullable(), related_invoices: z.array(relatedInvoiceSat).optional(), payments: z.array(invoicesPaymentsSat), payroll: invoicesPayrollSat, folio: z.string().nullable().optional(), series: z.string().nullable().optional(), export_type: z.string().nullable().optional(), xml: z.string().nullable().optional(), warnings: invoiceWarningsSat.optional(), sender_blacklist_status: z.string().nullable().optional(), receiver_blacklist_status: z.string().nullable().optional()}); export const getApiApiInvoicesResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(invoiceWithIdSat).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 invoices * * GET /api/invoices/ */ export const getApiApiInvoices = async (args: GetApiApiInvoicesArgs, options?: GetApiApiInvoicesOptions) => { 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 != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.link__in != null) query.append("link__in", args.link__in.join(',')) if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.id != null) query.append("id", String(args.id)) if (args.id__in != null) query.append("id__in", args.id__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 (args.invoice_date != null) query.append("invoice_date", String(args.invoice_date)) if (args.invoice_date__lt != null) query.append("invoice_date__lt", String(args.invoice_date__lt)) if (args.invoice_date__lte != null) query.append("invoice_date__lte", String(args.invoice_date__lte)) if (args.invoice_date__gt != null) query.append("invoice_date__gt", String(args.invoice_date__gt)) if (args.invoice_date__gte != null) query.append("invoice_date__gte", String(args.invoice_date__gte)) if (args.invoice_date__range != null) query.append("invoice_date__range", args.invoice_date__range.join(',')) if (args.invoice_identification != null) query.append("invoice_identification", String(args.invoice_identification)) if (args.invoice_identification__in != null) query.append("invoice_identification__in", args.invoice_identification__in.join(',')) if (args.status != null) query.append("status", String(args.status)) if (args.status__in != null) query.append("status__in", args.status__in.join(',')) if (args.type != null) query.append("type", String(args.type)) if (args.type__in != null) query.append("type__in", args.type__in.join(',')) if (args.total_amount != null) query.append("total_amount", String(args.total_amount)) if (args.total_amount__lt != null) query.append("total_amount__lt", String(args.total_amount__lt)) if (args.total_amount__lte != null) query.append("total_amount__lte", String(args.total_amount__lte)) if (args.total_amount__gt != null) query.append("total_amount__gt", String(args.total_amount__gt)) if (args.total_amount__gte != null) query.append("total_amount__gte", String(args.total_amount__gte)) if (args.total_amount__range != null) query.append("total_amount__range", args.total_amount__range.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/invoices/${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(getApiApiInvoicesResponse, await res.json()) };