import {z} from 'zod' export type GetApiV1SettingsAccountsArgs = {page?: number | undefined, page_size?: number | undefined}; export type GetApiV1SettingsAccountsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sharedSelfLink = z.string(); export const sharedListLinks = z.object({self: sharedSelfLink, next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const accountsAccountLedgerMeta = z.object({id: z.string().nullable(), payable_id: z.string().nullable(), receivable_id: z.string().nullable()}); export const accountsAccountLinks = z.object({self: sharedSelfLink}); export const accountsAccountBankTypeDetails = z.object({account_number: z.string(), account_type: z.enum(["checking", "savings"]), institution_name: z.string(), logo: z.string().optional(), primary_color: z.string().min(4).max(7).optional(), routing_number: z.string().min(9).max(9)}); export const accountsAccountBalanceTypeDetails = z.object({available_amount: z.string(), is_usable: z.boolean(), pending_amount: z.string()}); export const accountsAccountExternalTypeDetails = z.object({}); export const accountsAccountPayPalTypeDetails = z.object({}); export const accountsAccount = z.discriminatedUnion("type", [z.object({object: z.literal("Account"), id: z.string(), created_at: z.string(), is_deleted: z.boolean(), is_disabled: z.boolean(), is_usable_for_payables: z.boolean(), is_usable_for_receivables: z.boolean(), is_valid: z.boolean(), ledger: accountsAccountLedgerMeta, name: z.string().max(128), type: z.literal("bank"), links: accountsAccountLinks, type_details: accountsAccountBankTypeDetails}), z.object({object: z.literal("Account"), id: z.string(), created_at: z.string(), is_deleted: z.boolean(), is_disabled: z.boolean(), is_usable_for_payables: z.boolean(), is_usable_for_receivables: z.boolean(), is_valid: z.boolean(), ledger: accountsAccountLedgerMeta, name: z.string().max(128), type: z.literal("balance"), links: accountsAccountLinks, type_details: accountsAccountBalanceTypeDetails}), z.object({object: z.literal("Account"), id: z.string(), created_at: z.string(), is_deleted: z.boolean(), is_disabled: z.boolean(), is_usable_for_payables: z.boolean(), is_usable_for_receivables: z.boolean(), is_valid: z.boolean(), ledger: accountsAccountLedgerMeta, name: z.string().max(128), type: z.literal("external"), links: accountsAccountLinks, type_details: accountsAccountExternalTypeDetails}), z.object({object: z.literal("Account"), id: z.string(), created_at: z.string(), is_deleted: z.boolean(), is_disabled: z.boolean(), is_usable_for_payables: z.boolean(), is_usable_for_receivables: z.boolean(), is_valid: z.boolean(), ledger: accountsAccountLedgerMeta, name: z.string().max(128), type: z.literal("paypal"), links: accountsAccountLinks, type_details: accountsAccountPayPalTypeDetails})]); export const accountsAccountList = z.object({object: z.literal("List"), links: sharedListLinks, results: z.array(accountsAccount)}); 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 Accounts * * GET /v1/settings/accounts */ export const getApiV1SettingsAccounts = async (args: GetApiV1SettingsAccountsArgs, options?: GetApiV1SettingsAccountsOptions) => { const {baseUrl = "https://api.routable.com", fetch: fetchFn = fetch, BearerAuth} = 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.page_size != null) query.append("page_size", String(args.page_size)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/settings/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(accountsAccountList, await res.json()) };