import {z} from 'zod' export type GetApiV2UsersArgs = {type?: ('individual' | 'business') | undefined, firstName?: string | undefined, lastName?: string | undefined, businessName?: string | undefined, email?: string | undefined, limit?: string | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined}; export type GetApiV2UsersOptions = {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 allUserV2Objects = z.object({count: z.number().int().optional(), users: z.array(z.union([individualUserV2Object, businessUserV2Object])).optional(), nextCursor: z.string().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 }; /** * List all users * * GET /v2/users */ export const getApiV2Users = async (args: GetApiV2UsersArgs, options?: GetApiV2UsersOptions) => { const {baseUrl = "https://production.hifibridge.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.firstName != null) query.append("firstName", String(args.firstName)) if (args.lastName != null) query.append("lastName", String(args.lastName)) if (args.businessName != null) query.append("businessName", String(args.businessName)) if (args.email != null) query.append("email", String(args.email)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/users${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(allUserV2Objects, await res.json()) };