import {z} from 'zod' export type GetApiContactsContactIdCapabilitiesArgs = {contactId: string, amount?: (string | null) | undefined, 'X-Hevn-Account'?: (string | null) | undefined}; export type GetApiContactsContactIdCapabilitiesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const contactBlockerCode = z.enum(["contact_details_missing", "contact_address_incomplete", "contact_country_restricted", "contact_holder_type_unsupported", "account_type_unsupported", "routing_unresolved", "receiving_account_unavailable", "payment_method_unavailable", "currency_mismatch", "transfer_feature_disabled", "transfer_feature_on_request", "channel_not_configured", "sender_residency_ineligible", "sender_category_restricted", "endorsement_required", "channel_unavailable", "amount_below_minimum", "amount_above_maximum"]); export const contactBlockerView = z.object({code: contactBlockerCode, message: z.string(), field: z.string().nullable().optional()}); export const contactCapabilityOption = z.object({bankId: z.string().nullable().optional(), method: z.string(), currency: z.string(), available: z.boolean(), minAmount: z.string(), maxAmount: z.string().nullable().optional(), feeBps: z.number().int().optional(), fixedFee: z.string().nullable().optional(), purposeRequired: z.boolean().optional(), memoRequired: z.boolean().optional(), documentsRequired: z.boolean().optional(), blockers: z.array(contactBlockerView).optional()}); export const contactCapabilitiesResponse = z.object({contactId: z.string(), available: z.boolean(), options: z.array(contactCapabilityOption).optional(), blockers: z.array(contactBlockerView).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 }; /** * Retrieve contact channels * * GET /contacts/{contactId}/capabilities */ export const getApiContactsContactIdCapabilities = async (args: GetApiContactsContactIdCapabilitiesArgs, options: GetApiContactsContactIdCapabilitiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.amount != null) query.append("amount", String(args.amount)) if (args['X-Hevn-Account'] != null) headers.set("X-Hevn-Account", String(args['X-Hevn-Account'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/${encodeURIComponent(String(args.contactId))}/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(contactCapabilitiesResponse, await res.json()) };