import {z} from 'zod' export type GetApiBankAccountsAccountIdentifierOrTokenArgs = {account_identifier_or_token: string, page?: number | undefined, per_page?: number | undefined}; export type GetApiBankAccountsAccountIdentifierOrTokenOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBankAccountsAccountIdentifierOrTokenResponse = z.object({account_token: z.string().optional(), timestamp: z.string().optional(), external_account_id: z.string().optional(), external_entity_id: z.string().optional(), external_session_id: z.string().max(256).nullable().optional(), entity_token: z.string().nullable().optional(), account_class: z.enum(["credit", "deposit", "brokerage", "loan"]).nullable().optional(), account_name: z.string().nullable().optional(), account_number: z.string().optional(), routing_number: z.string().nullable().optional(), opening_date: z.string().nullable().optional(), maturity_date: z.string().nullable().optional(), closing_date: z.string().nullable().optional(), cif_number: z.string().nullable().optional(), card_network: z.string().nullable().optional(), country_code: z.string().nullable().optional(), currency: z.string().nullable().optional(), product_code: z.string().nullable().optional(), product_name: z.string().nullable().optional(), status: z.string().nullable().optional(), status_detail: z.string().nullable().optional(), credit_limit: z.number().nullable().optional(), account_balance: z.number().nullable().optional(), account_type_status: z.enum(["Written Off", "Active", "Refinanced", "Past Due", "Paid Off", "Cancelled"]).nullable().optional(), status_fraud: z.enum(["fraud", "not fraud"]).nullable().optional(), entity_group_token: z.string().nullable().optional(), external_entity_group_id: z.string().nullable().optional(), entities: z.array(z.union([z.object({account_holder_type: z.enum(["primary", "secondary", "power_of_attorney", "authorized_signer", "authorized_user", "beneficiary", "joint_owner", "custodian", "guardian", "trustee"]).optional(), external_entity_id: z.string(), entity_token: z.string().optional()}), z.object({account_holder_type: z.enum(["primary", "secondary", "power_of_attorney", "authorized_signer", "authorized_user", "beneficiary", "joint_owner", "custodian", "guardian", "trustee"]).optional(), external_entity_id: z.string().optional(), entity_token: z.string()})])).optional(), history: z.array(z.object({timestamp: z.string().optional(), status: z.string().optional(), status_detail: z.string().optional(), credit_limit: z.number().optional(), account_balance: z.number().optional(), status_fraud: z.enum(["fraud", "not fraud"]).optional()})).optional(), entity_groups: z.array(z.object({entity_group_token: z.string().optional(), entity_group_name: z.string().optional(), entity_group_type: z.enum(["business", "joint_account", "membership", "merchant"]).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 a single bank account, with historical and entity data, by unique token or identifier. * * GET /bank-accounts/{account_identifier_or_token} * * Also accepts auth: basic */ export const getApiBankAccountsAccountIdentifierOrToken = async (args: GetApiBankAccountsAccountIdentifierOrTokenArgs, options?: GetApiBankAccountsAccountIdentifierOrTokenOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/bank-accounts/${encodeURIComponent(String(args.account_identifier_or_token))}${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(getApiBankAccountsAccountIdentifierOrTokenResponse, await res.json()) };