import {z} from 'zod' export type AccountType = 'us_ach' | 'us_wire' | 'iban'; export type GetApiBankAccountsArgs = {before?: string | undefined, after?: string | undefined, perPage?: number | undefined, accountType?: AccountType | undefined}; export type GetApiBankAccountsOptions = {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 paginatedBankExternalAccountResponse = z.object({items: z.array(bankExternalAccountResponse), startCursor: z.string().optional(), endCursor: z.string().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 }; /** * List external bank accounts * * GET /bank-accounts */ export const getApiBankAccounts = async (args: GetApiBankAccountsArgs, options?: GetApiBankAccountsOptions) => { const {baseUrl = "https://api.enterprise.airtm.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (args.accountType != null) query.append("accountType", String(args.accountType)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedBankExternalAccountResponse, await res.json()) };