import {z} from 'zod' export type Decimal = number; export type PayoutStatesEnum = 'created' | 'initiated' | 'pending' | 'failed' | 'settled' | 'cancelled'; export type Currency = string; export type GetApiPayoutTransferArgs = {external_id?: string | undefined, external_provider_id?: string | undefined, timestamp?: string | undefined, timestamp_from?: string | undefined, timestamp_to?: string | undefined, amount?: Decimal | undefined, amount_min?: Decimal | undefined, amount_max?: Decimal | undefined, state?: PayoutStatesEnum | undefined, currency?: Currency | undefined, origin_financial_institution?: string | undefined, origin_account?: string | undefined, customer_account?: string | undefined, name?: string | undefined, customer_tax_id?: string | undefined, ordering?: ('timestamp' | 'amount' | 'fee') | undefined, page?: number | undefined}; export type GetApiPayoutTransferOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const decimal = z.number(); export const country = z.string(); export const bank = z.object({code: z.string().optional(), bicfi: z.string().optional(), name: z.string().max(50).optional(), country: country.optional()}); export const withdrawalAccountDetailsResponse = z.object({id: z.string().optional(), account_format: z.enum(["iban", "clabe", "cci", "pix", "bre_b", "alias", "cbu", "cvu"]).optional(), account_number: z.string(), description: z.string().max(50).optional(), selected: z.boolean().optional(), branch: z.string().optional(), bank: bank.optional()}); export const payoutCustomer = z.object({name: z.string().max(50).optional(), tax_id_type: z.enum(["rfc", "cnpj", "cpf", "ruc", "curp", "le", "dni", "lm", "pas", "ce", "nit", "cc", "pa", "ti", "nuip", "ppt", "pep", "cuil", "cuit", "other"]).optional(), tax_id: z.string().optional(), external_id: z.string(), withdrawal_account: withdrawalAccountDetailsResponse.optional()}); export const payoutStatesEnum = z.enum(["created", "initiated", "pending", "failed", "settled", "cancelled"]).max(50); export const payoutTransferState = z.object({state: payoutStatesEnum.optional(), timestamp: z.string().optional(), message: z.string().optional()}); export const payoutTransfer = z.object({id: z.string().optional(), origin: z.string().optional(), description: z.string().max(100).optional(), external_id: z.string().max(50).optional(), amount: decimal.optional(), fee: decimal.optional(), total_amount: decimal.optional(), reference: z.string().max(50).optional(), timestamp: z.string().optional(), customer: payoutCustomer.optional(), events: z.array(payoutTransferState).optional()}); export const getApiPayoutTransferResponse = z.object({count: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), results: z.array(payoutTransfer).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 payout * * GET /payout/transfer */ export const getApiPayoutTransfer = async (args: GetApiPayoutTransferArgs, options: GetApiPayoutTransferOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.external_provider_id != null) query.append("external_provider_id", String(args.external_provider_id)) if (args.timestamp != null) query.append("timestamp", String(args.timestamp)) if (args.timestamp_from != null) query.append("timestamp_from", String(args.timestamp_from)) if (args.timestamp_to != null) query.append("timestamp_to", String(args.timestamp_to)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.amount_min != null) query.append("amount_min", String(args.amount_min)) if (args.amount_max != null) query.append("amount_max", String(args.amount_max)) if (args.state != null) query.append("state", String(args.state)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.origin_financial_institution != null) query.append("origin_financial_institution", String(args.origin_financial_institution)) if (args.origin_account != null) query.append("origin_account", String(args.origin_account)) if (args.customer_account != null) query.append("customer_account", String(args.customer_account)) if (args.name != null) query.append("name", String(args.name)) if (args.customer_tax_id != null) query.append("customer_tax_id", String(args.customer_tax_id)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.page != null) query.append("page", String(args.page)) const url = `${baseUrl.replace(/\/+$/, '')}/payout/transfer${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(getApiPayoutTransferResponse, await res.json()) };