import {z} from 'zod' export type RailType = 'crypto' | 'card' | 'bank' | 'apm'; export type RailMethod = 'crypto-transaction' | 'credit-card' | 'debit-card' | 'bank-transfer' | 'apple-pay' | 'google-pay' | 'paypal' | 'venmo'; export type StringNoEdgeSpaces = string; export type CountryCodeForRequest = string; export type SubdivisionCodeForRequest = string; export type GetApiCoreRailsArgs = {type?: Array | undefined, network?: Array | undefined, method?: Array | undefined, asset?: 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 GetApiCoreRailsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const railBankConstraint = z.object({rule: z.literal("allowed-deposit-accounts"), allowed: z.literal("default-only")}); export const rail = z.discriminatedUnion("type", [z.object({type: z.literal("crypto"), network: z.string(), method: z.literal("crypto-transaction"), asset: z.string(), decimals: z.number().int().gte(0), features: z.array(z.enum(["withdraw", "deposit"])), contractAddress: z.string().optional()}), z.object({type: z.literal("card"), network: z.string(), method: z.enum(["credit-card", "debit-card"]), asset: z.string(), decimals: z.number().int().gte(0), features: z.array(z.enum(["withdraw", "deposit"]))}), z.object({type: z.literal("bank"), network: z.string(), method: z.literal("bank-transfer"), asset: z.string(), decimals: z.number().int().gte(0), features: z.array(z.enum(["withdraw", "deposit"])), constraints: z.array(railBankConstraint).optional()}), z.object({type: z.literal("apm"), network: z.string(), method: z.enum(["apple-pay", "google-pay", "paypal", "venmo"]), asset: z.string(), decimals: z.number().int().gte(0), features: z.array(z.enum(["withdraw", "deposit"]))})]); export const getApiCoreRailsResponse = z.object({rails: z.array(rail).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 rails * * GET /core/rails */ export const getApiCoreRails = async (args: GetApiCoreRailsArgs, options?: GetApiCoreRailsOptions) => { 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.network != null) query.append("network", args.network.join(',')) if (args.method != null) query.append("method", args.method.join(',')) if (args.asset != null) query.append("asset", args.asset.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/rails${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(getApiCoreRailsResponse, await res.json()) };