import {z} from 'zod' export type GetApiValidationBankAccountsCountryCurrencyArgs = {country: string, currency: string, 'account-holder-type'?: ('individual' | 'corporate' | 'government') | undefined, 'payment-network'?: ('local' | 'sepa' | 'fps' | 'ach' | 'fedwire' | 'swift') | undefined}; export type GetApiValidationBankAccountsCountryCurrencyOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountFields = z.object({sections: z.array(z.object({name: z.string(), fields: z.array(z.object({id: z.string(), section: z.string().optional(), display: z.string(), help_text: z.string().optional(), type: z.string(), required: z.boolean(), validation_regex: z.string().optional(), min_length: z.number().int().optional(), max_length: z.number().int().optional(), allowed_options: z.array(z.object({id: z.string().optional(), display: z.string().optional()})).optional(), dependencies: z.array(z.object({field_id: z.string().optional(), value: z.string().optional()})).optional()})).optional()})).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 }; /** * Get bank account field formatting * * GET /validation/bank-accounts/{country}/{currency} */ export const getApiValidationBankAccountsCountryCurrency = async (args: GetApiValidationBankAccountsCountryCurrencyArgs, options: GetApiValidationBankAccountsCountryCurrencyOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['account-holder-type'] != null) query.append("account-holder-type", String(args['account-holder-type'])) if (args['payment-network'] != null) query.append("payment-network", String(args['payment-network'])) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/validation/bank-accounts/${encodeURIComponent(String(args.country))}/${encodeURIComponent(String(args.currency))}${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(bankAccountFields, await res.json()) };