import {z} from 'zod' export type PatchBankAccountActive = {active: boolean}; export type PatchApiPaymentsBrBankAccountsIdArgs = {'X-Belvo-API-Resource-Version'?: 'Payments-BR.V2' | undefined, id: string, body: PatchBankAccountActive}; export type PatchApiPaymentsBrBankAccountsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const createdAt = z.string(); export const updatedAt = z.string(); export const externalIdPayments = z.string(); export const metadataPayments = z.record(z.string(), z.string().max(500)); export const bankAccountV2 = z.object({id: id, created_at: createdAt, updated_at: updatedAt, active: z.boolean(), holder: z.object({name: z.string().max(120), identifier: z.string().min(11).max(14)}), details: z.object({institution: z.string(), account_type: z.enum(["CHECKINGS", "SAVINGS", "PAYMENTS"]), agency: z.string().min(1).max(4), number: z.string().min(1).max(20)}), external_id: externalIdPayments, metadata: metadataPayments}); export const createdBy = z.string(); export const enumBankAccountPixAccountTypeOfpi = z.enum(["CHECKINGS", "SAVINGS", "SALARY", "PAYMENTS"]); export const bankAccountDetailsOfpi = z.object({account_type: enumBankAccountPixAccountTypeOfpi, agency: z.string().min(1).max(4), number: z.string().min(1).max(20)}); export const holderInformationIndividualOfpiResponse = z.object({first_name: z.string().min(1).max(256), last_name: z.string().min(1).max(256), identifier_type: z.literal("CPF"), identifier: z.string().min(11).max(11)}); export const holderResponseOfpiIndividual = z.object({type: z.literal("INDIVIDUAL"), information: holderInformationIndividualOfpiResponse}); export const holderInformationBusinessOfpiResponse = z.object({name: z.string().min(1).max(256), identifier_type: z.literal("CNPJ"), identifier: z.string().min(14).max(14)}); export const holderResponseOfpiBusiness = z.object({type: z.literal("BUSINESS"), information: holderInformationBusinessOfpiResponse}); export const bankAccountOfpiResponse = z.object({id: id, created_at: createdAt, created_by: createdBy, customer: z.string().nullable(), external_id: externalIdPayments.optional(), institution: z.string().nullable(), details: bankAccountDetailsOfpi, holder: z.union([holderResponseOfpiIndividual, holderResponseOfpiBusiness])}); export const patchApiPaymentsBrBankAccountsIdResponse = z.union([bankAccountV2, bankAccountOfpiResponse]); 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 }; /** * Activate or deactivate a bank account * * PATCH /payments/br/bank-accounts/{id}/ */ export const patchApiPaymentsBrBankAccountsId = async (args: PatchApiPaymentsBrBankAccountsIdArgs, options?: PatchApiPaymentsBrBankAccountsIdOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Belvo-API-Resource-Version'] != null) headers.set("X-Belvo-API-Resource-Version", String(args['X-Belvo-API-Resource-Version'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/br/bank-accounts/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patchApiPaymentsBrBankAccountsIdResponse, await res.json()) };