import {z} from 'zod' export type AccountSnapshot = {timestamp: string, meta?: Record | undefined, details?: ({status?: ('null' | 'Active' | 'Deactivated' | 'Frozen' | 'Pending' | 'Inactive' | null) | undefined, status_detail?: string | undefined, loan_credit_limit?: number | undefined, interest_rate?: number | undefined, original_interest_rate?: number | undefined, original_amount?: number | undefined, ach_push_limit?: number | undefined, ach_pull_limit?: number | undefined, name_first?: string | undefined, name_last?: string | undefined, email_address?: string | undefined, phone_number?: string | undefined, mailing_address_line1?: string | undefined, mailing_address_line2?: string | undefined, mailing_address_city?: string | undefined, mailing_address_state?: string | undefined, mailing_address_postal_code?: string | undefined, mailing_address_country_code?: string | undefined, primary_address_line1?: string | undefined, primary_address_line2?: string | undefined, primary_address_city?: string | undefined, primary_address_state?: string | undefined, primary_address_postal_code?: string | undefined, primary_address_country_code?: string | undefined}) | undefined, account_view?: ({account_balance?: number | undefined, date_last_activity?: string | undefined, is_good_standing?: boolean | undefined, past_due_days?: number | undefined}) | undefined}; export type BankAccountBody = {bank_account_token?: string | undefined, external_account_id?: string | undefined, account_number?: string | undefined, account_class?: ('deposit' | 'credit' | 'brokerage') | undefined, account_name?: string | undefined, routing_number?: string | undefined, account_type?: ('merchant account' | 'person' | null) | undefined, is_linked_account?: (boolean | null) | undefined, link_timestamp?: (string | null) | undefined, unlink_timestamp?: (string | null) | undefined, opening_date?: (string | null) | undefined, country_code?: (string | null) | undefined, account_snapshot?: AccountSnapshot | undefined, product_code?: (string | null) | undefined, product_name?: (string | null) | undefined, product_number?: (string | null) | undefined, maturity_date?: (string | null) | undefined, closing_date?: (string | null) | undefined, cif_number?: (string | null) | undefined, card_network?: (string | null) | undefined, expiration_date?: (string | null) | undefined, tokenization_method?: (string | null) | undefined, merchant_category_code?: (string | null) | undefined, name_on_card?: (string | null) | undefined, account_number_last4?: (string | null) | undefined, avs?: (string | null) | undefined, currency?: (string | null) | undefined, meta?: (Record | null) | undefined}; export type CreateBankAccountBody = BankAccountBody; export type CreateApiAccountsArgs = {body: CreateBankAccountBody}; export type CreateApiAccountsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const accountSnapshot = z.object({timestamp: z.string(), meta: z.object({}).optional(), details: z.object({status: z.enum(["null", "Active", "Deactivated", "Frozen", "Pending", "Inactive"]).nullable().optional(), status_detail: z.string().optional(), loan_credit_limit: z.number().int().optional(), interest_rate: z.number().optional(), original_interest_rate: z.number().optional(), original_amount: z.number().int().optional(), ach_push_limit: z.number().int().optional(), ach_pull_limit: z.number().int().optional(), name_first: z.string().optional(), name_last: z.string().optional(), email_address: z.string().optional(), phone_number: z.string().optional(), mailing_address_line1: z.string().optional(), mailing_address_line2: z.string().optional(), mailing_address_city: z.string().optional(), mailing_address_state: z.string().optional(), mailing_address_postal_code: z.string().optional(), mailing_address_country_code: z.string().optional(), primary_address_line1: z.string().optional(), primary_address_line2: z.string().optional(), primary_address_city: z.string().optional(), primary_address_state: z.string().optional(), primary_address_postal_code: z.string().optional(), primary_address_country_code: z.string().max(3).optional()}).optional(), account_view: z.object({account_balance: z.number().int().optional(), date_last_activity: z.string().optional(), is_good_standing: z.boolean().optional(), past_due_days: z.number().int().optional()}).optional()}); export const bankAccountBody = z.object({bank_account_token: z.string().optional(), external_account_id: z.string().max(60).optional(), account_number: z.string().optional(), account_class: z.enum(["deposit", "credit", "brokerage"]).optional(), account_name: z.string().optional(), routing_number: z.string().optional(), account_type: z.enum(["merchant account", "person"]).nullable().optional(), is_linked_account: z.boolean().nullable().optional(), link_timestamp: z.string().nullable().optional(), unlink_timestamp: z.string().nullable().optional(), opening_date: z.string().nullable().optional(), country_code: z.string().max(2).nullable().optional(), account_snapshot: accountSnapshot.optional(), product_code: z.string().nullable().optional(), product_name: z.string().nullable().optional(), product_number: 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().max(20).nullable().optional(), expiration_date: z.string().nullable().optional(), tokenization_method: z.string().max(20).nullable().optional(), merchant_category_code: z.string().max(20).nullable().optional(), name_on_card: z.string().max(100).nullable().optional(), account_number_last4: z.string().max(4).nullable().optional(), avs: z.string().max(20).nullable().optional(), currency: z.string().max(4).nullable().optional(), meta: z.object({}).nullable().optional()}); export const accountPostResponseBody = bankAccountBody; 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 an account * * POST /accounts * * Also accepts auth: basic */ export const createApiAccounts = async (args: CreateApiAccountsArgs, options?: CreateApiAccountsOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(accountPostResponseBody, await res.json()) };