import {z} from 'zod' export type Address = {addressLine1: string, addressLine2?: string | undefined, city: string, stateProvinceRegion: string, postalCode: string, country: string}; export type IndividualUserV2Create = {requestId?: string | undefined, type: 'individual', chains?: (Array<'POLYGON' | 'ETHEREUM' | 'SOLANA' | 'BASE'>) | undefined, firstName: string, lastName: string, email: string, dateOfBirth: string, address?: Address | undefined, ipAddress?: string | undefined, signedAgreementId: string}; export type UltimateBeneficialOwnerV2 = {firstName: string, lastName: string, dateOfBirth: string, hasControl: boolean, isSigner: boolean}; export type BusinessUserV2Create = {requestId?: string | undefined, type: 'business', businessName: string, chains?: (Array<'POLYGON' | 'ETHEREUM' | 'SOLANA' | 'BASE'>) | undefined, email: string, address?: Address | undefined, ipAddress?: string | undefined, ultimateBeneficialOwners: Array, signedAgreementId: string}; export type CreateApiV2UsersArgs = {body: IndividualUserV2Create | BusinessUserV2Create}; export type CreateApiV2UsersOptions = {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 createApiV2UsersResponse = 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 }; /** * Create a user * * POST /v2/users */ export const createApiV2Users = async (args: CreateApiV2UsersArgs, options?: CreateApiV2UsersOptions) => { 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${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(createApiV2UsersResponse, await res.json()) };