import {z} from 'zod' export type ValidationRequestARCBU = {country_code: 'AR', account_number: string}; export type ValidationRequestARLocal = {country_code: 'AR', account_number: string, bank_code: '000' | '005' | '007' | '011' | '014' | '015' | '016' | '017' | '018' | '020' | '027' | '029' | '030' | '034' | '044' | '045' | '046' | '060' | '065' | '072' | '083' | '086' | '093' | '094' | '097' | '131' | '143' | '147' | '150' | '158' | '165' | '191' | '198' | '247' | '254' | '259' | '262' | '266' | '268' | '269' | '277' | '281' | '285' | '295' | '299' | '300' | '301' | '305' | '309' | '310' | '311' | '312' | '315' | '319' | '321' | '322' | '325' | '330' | '331' | '332' | '336' | '338' | '339' | '340' | '341' | '384' | '386' | '389' | '405' | '406' | '408' | '413' | '415' | '426' | '428' | '431' | '432' | '434' | '435' | '437' | '438' | '440' | '441' | '442' | '443' | '444' | '445' | '446' | '448' | '453' | '901' | '992'}; export type ValidationRequestBRIBAN = {account_number: string, country_code: 'BR', document_number?: string | undefined, beneficiary_name?: string | undefined}; export type ValidationRequestBRLocal = {account_number: string, country_code: 'BR', document_number?: string | undefined, beneficiary_name?: string | undefined, branch_code: string, bank_code: '001' | '003' | '004' | '010' | '012' | '016' | '021' | '033' | '037' | '041' | '047' | '069' | '070' | '077' | '079' | '082' | '084' | '085' | '089' | '095' | '096' | '097' | '099' | '104' | '119' | '121' | '125' | '130' | '133' | '136' | '144' | '159' | '174' | '183' | '190' | '197' | '208' | '212' | '213' | '218' | '224' | '237' | '246' | '254' | '260' | '269' | '271' | '273' | '274' | '276' | '279' | '280' | '281' | '290' | '299' | '301' | '310' | '312' | '318' | '322' | '323' | '324' | '326' | '329' | '330' | '332' | '335' | '336' | '341' | '348' | '349' | '350' | '358' | '359' | '362' | '363' | '364' | '365' | '368' | '373' | '376' | '377' | '378' | '379' | '380' | '382' | '383' | '384' | '385' | '389' | '391' | '395' | '396' | '397' | '401' | '402' | '403' | '404' | '406' | '407' | '408' | '410' | '412' | '413' | '419' | '421' | '422' | '425' | '427' | '428' | '430' | '435' | '439' | '440' | '444' | '448' | '450' | '451' | '452' | '457' | '460' | '461' | '462' | '465' | '470' | '471' | '478' | '481' | '482' | '509' | '510' | '511' | '512' | '513' | '517' | '520' | '523' | '526' | '527' | '529' | '530' | '531' | '533' | '534' | '535' | '536' | '537' | '540' | '542' | '546' | '547' | '550' | '552' | '555' | '557' | '559' | '560' | '561' | '562' | '563' | '566' | '568' | '569' | '572' | '574' | '575' | '576' | '586' | '588' | '589' | '590' | '592' | '593' | '594' | '595' | '597' | '600' | '604' | '610' | '611' | '612' | '613' | '614' | '615' | '619' | '620' | '623' | '632' | '633' | '634' | '637' | '643' | '646' | '651' | '655' | '659' | '660' | '662' | '665' | '669' | '670' | '671' | '673' | '674' | '675' | '679' | '680' | '681' | '683' | '685' | '686' | '687' | '688' | '690' | '692' | '694' | '695' | '699' | '700' | '701' | '707' | '712' | '741' | '743' | '745' | '748' | '752' | '753' | '755' | '756' | '757' | '761' | '764' | '766' | '769' | '771' | '772' | '773' | '775' | '783' | '788' | '790' | '792', account_type: 'SAVINGS' | 'CHECKING' | 'PAYMENTS' | 'EASY' | 'PUBLIC_ENTITY'}; export type ValidationRequestBRPix = {account_number: string, country_code: 'BR', account_type: 'PIX_KEY'}; export type ValidationRequestCLLocal = {account_number: string, bank_code: '0001' | '0009' | '0012' | '0014' | '0016' | '0028' | '0031' | '0037' | '0039' | '0041' | '0049' | '0051' | '0053' | '0055' | '0672' | '0729' | '0730' | '0732' | '0875', country_code: 'CL', document_type: 'RUT', account_type: 'SAVINGS' | 'CHECKING' | 'DEMAND', document_number: string}; export type ValidationRequestCOLocal = {account_number: string, country_code: 'CO', document_number: string, document_type: 'CC' | 'NIT', bank_code: '001' | '006' | '007' | '012' | '042' | '030' | '039' | '049' | '053' | '054' | '056' | '057' | '063' | '065' | '117' | '118' | '124' | '131' | '150' | '198' | '208' | '261' | '264' | '304' | '345', account_type: 'SAVINGS' | 'CHECKING'}; export type ValidationRequestCONEQUI = {account_number: string, country_code: 'CO', document_number: string, document_type: 'CC' | 'NIT' | 'CD' | 'CE' | 'TI' | 'PAS' | 'IEPN' | 'IEPJ' | 'FD' | 'RC', account_type: 'NEQUI_WALLET'}; export type ValidationRequestCOBREBKEY = {account_number: string, country_code: 'CO', account_type: 'BREB_KEY'}; export type ValidationRequestECLocal = {account_number: string, country_code: 'EC', document_number: string, document_type: 'CC' | 'RUC' | 'PAS', bank_code: '0010' | '0017' | '0029' | '0030' | '0032' | '0035' | '0036' | '0037' | '0042' | '0059' | '0060' | '0201' | '0232', account_type: 'SAVINGS' | 'CHECKING'}; export type ValidationRequestMXCLABE = {account_number: string, country_code: 'MX'}; export type ValidationRequestMXLocal = {account_number: string, branch_code: string, bank_code: '002' | '006' | '009' | '012' | '014' | '019' | '021' | '030' | '036' | '042' | '044' | '058' | '059' | '060' | '062' | '072' | '106' | '108' | '110' | '112' | '113' | '124' | '127' | '128' | '129' | '130' | '132' | '133' | '135' | '136' | '137' | '138' | '140' | '141' | '143' | '145' | '147' | '148' | '150' | '151' | '152' | '154' | '155' | '156' | '157' | '158' | '159' | '160' | '166' | '167' | '168' | '600' | '601' | '602' | '605' | '616' | '617' | '620' | '630' | '631' | '634' | '638' | '646' | '652' | '653' | '656' | '659' | '661' | '670' | '677' | '680' | '683' | '684' | '685' | '688' | '699' | '703' | '706' | '710' | '715' | '720' | '721' | '722' | '723' | '725' | '727' | '728' | '729' | '732' | '734' | '738' | '901' | '902', country_code: 'MX'}; export type ValidationRequestMXDebitCard = {account_number: string, bank_code?: string | undefined, country_code: 'MX'}; export type ValidationRequestPECCI = {account_number: string, country_code: 'PE'}; export type ValidationRequestPELocal = {account_number: string, bank_code: '002' | '003' | '009' | '011' | '018', country_code: 'PE'}; export type ValidationRequestUSLocal = {account_number: string, country_code: 'US', bank_code: string, beneficiary_name?: string | undefined}; export type ValidationRequestUYLocal = {account_number: string, bank_code: '001' | '091' | '110' | '113' | '128' | '137' | '153' | '157' | '162' | '205' | '246' | '452' | '815' | '6917', country_code: 'UY'}; export type ValidationRequestPALocal = {account_number: string, bank_code: '0001' | '0007' | '0115', country_code: 'PA'}; export type CreateApiValidateAccountArgs = {body: ValidationRequestARCBU | ValidationRequestARLocal | ValidationRequestBRIBAN | ValidationRequestBRLocal | ValidationRequestBRPix | ValidationRequestCLLocal | ValidationRequestCOLocal | ValidationRequestCONEQUI | ValidationRequestCOBREBKEY | ValidationRequestECLocal | ValidationRequestMXCLABE | ValidationRequestMXLocal | ValidationRequestMXDebitCard | ValidationRequestPECCI | ValidationRequestPELocal | ValidationRequestUSLocal | ValidationRequestUYLocal | ValidationRequestPALocal}; export type CreateApiValidateAccountOptions = {baseUrl: string, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const country = z.string(); export const currency = z.string(); export const validAccount = z.object({valid: z.boolean().optional(), country_code: country.optional(), account: z.string().optional(), bank: z.string().optional(), result: z.object({valid: z.boolean().optional(), message: z.string().optional(), data: z.object({document_type: z.string().optional(), document_number: z.string().optional(), beneficiary_name: z.string().optional(), account_currency: currency.optional(), account_type: z.string().optional()}).optional()}).optional()}); export const errorMessage = z.object({status: z.string().optional(), message: z.string().optional()}); export const createApiValidateAccountResponse = z.object({data: validAccount.optional(), errors: errorMessage.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 }; /** * Retorna detalles de la cuenta consultada. * * POST /validate-account/ */ export const createApiValidateAccount = async (args: CreateApiValidateAccountArgs, options: CreateApiValidateAccountOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (APIKeyAuth !== undefined) headers.set("X-API-Key", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/validate-account/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiValidateAccountResponse, await res.json()) };