import {z} from 'zod' export type ClientAddressRequest = {streetAddress?: (string | null) | undefined, addressLine2?: (string | null) | undefined, city?: (string | null) | undefined, state?: (string | null) | undefined, zip?: (string | null) | undefined}; export type ClientProfileRequest = {phone?: (string | null) | undefined, address?: (ClientAddressRequest | null) | undefined}; export type PatchApiClientArgs = {'Idempotency-Key'?: (string | null) | undefined, 'X-Hevn-Account': string, body: ClientProfileRequest}; export type PatchApiClientOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const platformClientStatus = z.enum(["provisioning", "ready", "failed"]); export const platformKybStatus = z.enum(["notStarted", "pending", "approved", "rfi", "rejected"]); export const platformClientFailure = z.enum(["provisioning_failed", "request_rejected", "request_canceled"]); export const clientFailureResponse = z.object({code: platformClientFailure, message: z.string(), retriable: z.boolean().optional()}); export const transferPolicyResponse = z.object({allowFiat: z.boolean(), allowCrypto: z.boolean(), allowThirdParty: z.boolean(), allowEscrowThirdParty: z.boolean(), allowSwap: z.boolean(), allowPartnerGroupPayments: z.boolean(), dualApproval: z.boolean(), isFrozen: z.boolean(), recoveryModeActivated: z.boolean()}); export const clientResponse = z.object({id: z.string(), status: platformClientStatus, name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), baseSmartWallet: z.string().nullable().optional(), kybStatus: platformKybStatus.optional(), kybApplicationId: z.string().nullable().optional(), verifiedAt: z.string().nullable().optional(), createdAt: z.string(), failure: clientFailureResponse.nullable().optional(), pollUrl: z.string().nullable().optional(), transferPolicy: transferPolicyResponse.nullable().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 }; /** * Update a client profile * * PATCH /client */ export const patchApiClient = async (args: PatchApiClientArgs, options: PatchApiClientOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) 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(/\/+$/, '')}/client${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(clientResponse, await res.json()) };