import {z} from 'zod' export type RailsEnum = 'USD' | 'SOUTH_AMERICA_STANDARD' | 'AFRICA_GENERAL' | 'AFRICA_NIGERIA' | 'GLOBAL_NETWORK'; export type KycStatusEnum = 'INACTIVE' | 'CREATED' | 'PENDING' | 'INCOMPLETE' | 'RFI_PENDING' | 'ACTIVE' | 'REJECTED' | 'CONTACT_SUPPORT'; export type GetApiV2KycStatusArgs = {userId?: string | undefined, userName?: string | undefined, userEmail?: string | undefined, rails?: RailsEnum | undefined, status?: Array | undefined, limit?: string | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined}; export type GetApiV2KycStatusOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const railsEnum = z.enum(["USD", "SOUTH_AMERICA_STANDARD", "AFRICA_GENERAL", "AFRICA_NIGERIA", "GLOBAL_NETWORK"]); export const kycStatusEnum = z.enum(["INACTIVE", "CREATED", "PENDING", "INCOMPLETE", "RFI_PENDING", "ACTIVE", "REJECTED", "CONTACT_SUPPORT"]); export const allKycStatusesObject = z.array(z.object({user: z.object({id: z.string().optional(), type: z.enum(["individual", "business"]).optional(), name: z.string().optional(), email: z.string().optional()}).optional(), rails: railsEnum.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), status: kycStatusEnum.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 KYC statuses * * GET /v2/kyc/status */ export const getApiV2KycStatus = async (args: GetApiV2KycStatusArgs, options?: GetApiV2KycStatusOptions) => { 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.userId != null) query.append("userId", String(args.userId)) if (args.userName != null) query.append("userName", String(args.userName)) if (args.userEmail != null) query.append("userEmail", String(args.userEmail)) if (args.rails != null) query.append("rails", String(args.rails)) for (const item of args.status ?? []) query.append("status", String(item)) 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/kyc/status${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(allKycStatusesObject, await res.json()) };