import {z} from 'zod' export type Address = {addressLine1: string, addressLine2?: string | undefined, city: string, stateProvinceRegion: string, postalCode: string, country: string}; export type IndividualUserV2Update = {type?: 'individual' | undefined, firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined, dateOfBirth?: string | undefined, address?: Address | undefined, ipAddress?: string | undefined, signedAgreementId?: string | undefined}; export type UltimateBeneficialOwnerV2 = {firstName: string, lastName: string, dateOfBirth: string, hasControl: boolean, isSigner: boolean}; export type BusinessUserV2Update = {type?: 'business' | undefined, businessName?: string | undefined, email?: string | undefined, address?: Address | undefined, ipAddress?: string | undefined, ultimateBeneficialOwners?: Array | undefined, signedAgreementId?: string | undefined}; export type CreateApiV2UsersUserIdArgs = {userId: string, body: IndividualUserV2Update | BusinessUserV2Update}; export type CreateApiV2UsersUserIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const walletAddressObject = z.object({address: z.string()}); export const individualUserV2Object = z.object({id: z.string().optional(), createdAt: z.string().optional(), type: z.string().optional(), email: z.string().optional(), name: z.string().optional(), wallets: z.object({INDIVIDUAL: z.object({ETHEREUM: walletAddressObject.optional(), POLYGON: walletAddressObject.optional()}).optional()}).optional()}); export const businessUserV2Object = z.object({id: z.string().optional(), createdAt: z.string().optional(), type: z.string().optional(), email: z.string().optional(), name: z.string().optional(), wallets: z.object({INDIVIDUAL: z.object({ETHEREUM: walletAddressObject.optional(), POLYGON: walletAddressObject.optional()}).optional()}).optional()}); export const createApiV2UsersUserIdResponse = z.union([individualUserV2Object, businessUserV2Object]); 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 user * * POST /v2/users/{userId} */ export const createApiV2UsersUserId = async (args: CreateApiV2UsersUserIdArgs, options?: CreateApiV2UsersUserIdOptions) => { const {baseUrl = "https://production.hifibridge.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/users/${encodeURIComponent(String(args.userId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiV2UsersUserIdResponse, await res.json()) };