import {z} from 'zod' export type BankAccountAddress = {street_line_1: string, street_line_2?: (string | null) | undefined, city: string, state?: (string | null) | undefined, postal_code?: (string | null) | undefined, country: string}; export type AccountOwnerType = 'individual' | 'business'; export type FiatCurrency = 'usd' | 'eur' | 'mxn' | 'brl' | 'gbp' | 'cop'; export type BankAccountType = 'us' | 'iban' | 'swift' | 'clabe' | 'pix' | 'gb' | 'bre_b' | 'co_bank_transfer'; export type CheckingOrSavings = 'checking' | 'savings'; export type BankAccount = {account_number?: string | undefined, routing_number?: (string | null) | undefined, sort_code?: (string | null) | undefined, bre_b_key?: (string | null) | undefined, document_type?: (string | null) | undefined, document_number?: (string | null) | undefined, bank_code?: (string | null) | undefined, account_type_detail?: (string | null) | undefined, phone_number?: (string | null) | undefined, checking_or_savings?: (CheckingOrSavings | null) | undefined}; export type IBANBankAccount = {account_number: string, country: string, bic?: (string | null) | undefined}; export type SWIFTAccountInfo = {account_number?: (string | null) | undefined, country?: (string | null) | undefined, bic?: (string | null) | undefined}; export type SwiftCategory = 'client' | 'parent_company' | 'subsidiary' | 'supplier'; export type SWIFTBankAccount = {account: SWIFTAccountInfo, address: BankAccountAddress, category: SwiftCategory, purpose_of_funds: Array, short_business_description: string}; export type CLABEBankAccount = {account_number: string}; export type PIXBankAccount = {document_number?: (string | null) | undefined, pix_key?: (string | null) | undefined, br_code?: (string | null) | undefined}; export type AddBankAccountRequest = {account_name?: string | undefined, account_owner_name: string, address: BankAccountAddress, bank_name?: string | undefined, account_owner_type?: AccountOwnerType | undefined, first_name?: string | undefined, last_name?: string | undefined, business_name?: string | undefined, currency: FiatCurrency, account_type: BankAccountType, account?: (BankAccount | null) | undefined, iban?: (IBANBankAccount | null) | undefined, swift?: (SWIFTBankAccount | null) | undefined, clabe?: (CLABEBankAccount | null) | undefined, pix?: (PIXBankAccount | null) | undefined}; export type CreateApiOffRampBankAccountsArgs = {'Authorization-Signature': string, body: AddBankAccountRequest}; export type CreateApiOffRampBankAccountsOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const bankAccountType = z.enum(["us", "iban", "swift", "clabe", "pix", "gb", "bre_b", "co_bank_transfer"]); export const accountOwnerType = z.enum(["individual", "business"]); export const accountDetails = z.object({bank_name: z.string().nullable().optional(), routing_number: z.string().nullable().optional(), sort_code: z.string().nullable().optional(), account_number: z.string(), checking_or_savings: z.string().nullable().optional(), bic: z.string().nullable().optional(), pix_key: z.string().nullable().optional(), bank_code: z.string().nullable().optional(), account_type_detail: z.string().nullable().optional()}); export const fiatCurrency = z.enum(["usd", "eur", "mxn", "brl", "gbp", "cop"]); export const bankAccountDetails = z.object({id: z.string(), account_name: z.string().nullable().optional(), bank_name: z.string().nullable().optional(), account_type: bankAccountType, account_owner_type: accountOwnerType.nullable().optional(), account_details: accountDetails, currency: fiatCurrency, active: z.boolean(), beneficiary_address_valid: z.boolean().nullable().optional(), deactivation_reason: z.string().nullable().optional(), deactivation_details: z.string().nullable().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 }; /** * Add Bank Account * * POST /off-ramp/bank-accounts */ export const createApiOffRampBankAccounts = async (args: CreateApiOffRampBankAccountsArgs, options?: CreateApiOffRampBankAccountsOptions) => { const {baseUrl = "https://api.coinvoyage.io/v3", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Authorization-Signature'] != null) headers.set("Authorization-Signature", String(args['Authorization-Signature'])) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/off-ramp/bank-accounts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(bankAccountDetails, await res.json()) };