import {z} from 'zod' export type AccountType = 'us_ach' | 'us_wire' | 'iban'; export type Currency = 'USD' | 'EUR'; export type CreateBankExternalAccountRequest = {accountType: AccountType, currency: Currency, bankName: string, accountOwnerName: string, businessName: string, address?: ({country: string, postalCode?: string | undefined, state: string, city: string, streetLine2?: string | undefined, streetLine1: string}) | undefined, usAccount?: ({checkingOrSavings?: ('checking' | 'savings') | undefined, routingNumber: string, accountNumber: string}) | undefined, iban?: {country: string, bic: string, accountNumber: string} | undefined, swift?: ({shortBusinessDescription: string, purposeOfFunds: Array, category: 'client' | 'parent_company' | 'subsidiary' | 'supplier', address: {country: string, postalCode?: string | undefined, state: string, city: string, streetLine2?: string | undefined, streetLine1: string}, account: {country: string, bic: string, accountNumber: string}}) | undefined}; export type CreateApiBankAccountsArgs = {body: CreateBankExternalAccountRequest}; export type CreateApiBankAccountsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const accountType = z.enum(["us_ach", "us_wire", "iban"]); export const currency = z.enum(["USD", "EUR"]); export const bankExternalAccountResponse = z.object({id: z.string(), accountType: accountType, currency: currency, bankName: z.string(), accountOwnerName: z.string(), businessName: z.string(), address: z.object({country: z.string(), postalCode: z.string().optional(), state: z.string(), city: z.string(), streetLine2: z.string().optional(), streetLine1: z.string()}).optional(), usAccount: z.object({checkingOrSavings: z.string().optional(), routingNumber: z.string(), accountNumber: z.string()}).optional(), iban: z.object({country: z.string(), bic: z.string(), accountNumber: z.string()}).optional(), swift: z.object({shortBusinessDescription: z.string(), purposeOfFunds: z.array(z.string()), category: z.string(), address: z.object({country: z.string(), postalCode: z.string().optional(), state: z.string(), city: z.string(), streetLine2: z.string().optional(), streetLine1: z.string()}), account: z.object({country: z.string(), bic: z.string(), accountNumber: z.string()})}).optional(), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Create a bank external account * * POST /bank-accounts */ export const createApiBankAccounts = async (args: CreateApiBankAccountsArgs, options?: CreateApiBankAccountsOptions) => { const {baseUrl = "https://api.enterprise.airtm.com/v2", 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 (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(bankExternalAccountResponse, await res.json()) };