import {z} from 'zod' export type ExternalAccountType = 'apm' | 'bank' | 'card' | 'crypto'; export type StringNoEdgeSpaces = string; export type CountryCodeForRequest = string; export type SubdivisionCodeForRequest = string; export type GetApiCoreExternalAccountsArgs = {type?: Array | undefined, 'X-On-Behalf-Of'?: string | undefined, 'X-Uphold-User-Ip'?: (string | string) | undefined, 'X-Uphold-User-Agent'?: StringNoEdgeSpaces | undefined, 'X-Uphold-User-Origin'?: StringNoEdgeSpaces | undefined, 'X-Uphold-User-Country'?: CountryCodeForRequest | undefined, 'X-Uphold-User-Subdivision'?: SubdivisionCodeForRequest | undefined, 'X-Uphold-User-City'?: StringNoEdgeSpaces | undefined}; export type GetApiCoreExternalAccountsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const externalAccountType = z.enum(["apm", "bank", "card", "crypto"]); export const externalAccountStatusReason = z.enum(["card-data-invalid", "card-declined-by-bank", "card-expired", "card-permanently-declined-by-bank", "card-unauthorized", "card-unsupported", "bank-account-holder-name-mismatch", "velocity", "under-review", "unspecified-error"]); export const externalAccountDetailsCard = z.object({type: z.enum(["credit", "debit"]).optional(), last4Digits: z.string(), expiryDate: z.object({month: z.number().int(), year: z.number().int()}), octSupport: z.enum(["unknown", "supported", "unsupported"])}); export const address = z.object({country: z.string().optional(), subdivision: z.string().optional(), city: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), postalCode: z.string().optional()}); export const externalAccountDetailsBankAch = z.object({address: address, routingNumber: z.string(), accountNumber: z.string(), accountType: z.string(), secondaryNetworks: z.object({deposit: z.array(z.string()).optional(), withdraw: z.array(z.string()).optional()}).optional()}); export const externalAccountDetailsBankFps = z.object({accountNumber: z.string(), sortCode: z.string()}); export const externalAccountDetailsBankSepa = z.object({bic: z.string(), iban: z.string()}); export const externalAccountDetailsApm = z.object({identifier: z.string()}); export const externalAccountDetailsCrypto = z.object({address: z.string(), reference: z.string().optional()}); export const externalAccountVerification = z.object({via: z.literal("request-for-information"), level: z.enum(["declared", "verified"]), status: z.enum(["pending", "completed", "failed"]), completedAt: z.string().optional()}); export const externalAccount = z.object({id: z.string(), ownerId: z.string(), type: externalAccountType, status: z.enum(["processing", "ok", "expired", "failed", "restricted", "blocked"]), statusDetails: z.object({reason: externalAccountStatusReason}).optional(), label: z.string(), asset: z.string(), network: z.string().optional(), features: z.array(z.enum(["withdraw", "deposit"])), details: z.union([externalAccountDetailsCard, externalAccountDetailsBankAch, externalAccountDetailsBankFps, externalAccountDetailsBankSepa, externalAccountDetailsApm, externalAccountDetailsCrypto]), createdAt: z.string(), updatedAt: z.string(), verifications: z.array(externalAccountVerification).optional()}); export const getApiCoreExternalAccountsResponse = z.object({externalAccounts: z.array(externalAccount)}); 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 accounts * * GET /core/external-accounts */ export const getApiCoreExternalAccounts = async (args: GetApiCoreExternalAccountsArgs, options?: GetApiCoreExternalAccountsOptions) => { const {baseUrl = "https://api.enterprise.sandbox.uphold.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", args.type.join(',')) if (args['X-On-Behalf-Of'] != null) headers.set("X-On-Behalf-Of", String(args['X-On-Behalf-Of'])) if (args['X-Uphold-User-Ip'] != null) headers.set("X-Uphold-User-Ip", String(args['X-Uphold-User-Ip'])) if (args['X-Uphold-User-Agent'] != null) headers.set("X-Uphold-User-Agent", String(args['X-Uphold-User-Agent'])) if (args['X-Uphold-User-Origin'] != null) headers.set("X-Uphold-User-Origin", String(args['X-Uphold-User-Origin'])) if (args['X-Uphold-User-Country'] != null) headers.set("X-Uphold-User-Country", String(args['X-Uphold-User-Country'])) if (args['X-Uphold-User-Subdivision'] != null) headers.set("X-Uphold-User-Subdivision", String(args['X-Uphold-User-Subdivision'])) if (args['X-Uphold-User-City'] != null) headers.set("X-Uphold-User-City", String(args['X-Uphold-User-City'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/core/external-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(getApiCoreExternalAccountsResponse, await res.json()) };