import {z} from 'zod' export type StringNoEdgeSpaces = string; export type CountryCodeForRequest = string; export type SubdivisionCodeForRequest = string; export type GetApiCoreCapabilitiesArgs = {'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 GetApiCoreCapabilitiesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const capabilityCode = z.enum(["crypto-withdrawals", "card-withdrawals", "bank-withdrawals", "apple-pay-withdrawals", "paypal-withdrawals", "venmo-withdrawals", "crypto-deposits", "bank-deposits", "deposits", "receives", "sends", "trades", "cards", "apple-pay", "google-pay", "paypal", "venmo", "unique-account-number-viban"]); export const capability = z.object({code: capabilityCode, name: z.string(), enabled: z.boolean(), requirements: z.array(z.enum(["user-must-submit-crypto-risk-assessment-form", "user-must-submit-self-categorization-form", "user-must-submit-residential-address", "user-must-submit-customer-due-diligence", "user-must-submit-enhanced-due-diligence", "user-must-wait-for-the-email-cooldown-period-to-expire", "user-must-submit-full-name", "user-must-submit-identity", "user-must-submit-identity-via-physical-document", "user-must-submit-phone-number", "user-must-submit-proof-of-address", "user-must-submit-tax-details", "user-must-submit-tax-identification-number", "user-must-submit-w9-form", "user-must-accept-unique-account-number-viban-terms-of-service", "user-must-delete-card"])), restrictions: z.array(z.enum(["crypto-risk-assessment-form-not-valid", "financial-promotion-cooldown-running", "phone-country-not-supported", "self-categorization-statement-form-not-valid", "temporarily-disabled", "user-residential-address-not-valid", "user-cannot-receive-funds", "user-country-not-supported", "user-enhanced-due-diligence-not-valid", "user-identity-not-valid", "user-proof-of-address-not-valid", "user-screening-running", "user-state-not-supported", "user-status-not-valid", "user-type-not-supported", "user-w9-form-not-valid", "user-unique-card-limit-reached"]))}); export const getApiCoreCapabilitiesResponse = z.object({capabilities: z.array(capability)}); 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 capabilities * * GET /core/capabilities */ export const getApiCoreCapabilities = async (args: GetApiCoreCapabilitiesArgs, options?: GetApiCoreCapabilitiesOptions) => { 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['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/capabilities${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(getApiCoreCapabilitiesResponse, await res.json()) };