import {z} from 'zod' export type KeyIdentifier = {key?: string | undefined, value?: string | undefined}; export type BiometricDemand = {securityLevel?: ('HIGH' | 'MEDIUM' | 'LOW') | undefined, biometricFields?: (Array<'FACE' | 'FINGERPRINT' | 'FULL_NAME' | 'DATE_OF_BIRTH' | 'PERSONAL_EMAIL' | 'WORK_EMAIL' | 'OTHER_EMAIL' | 'ADDRESS' | 'COUNTRY' | 'GENDER' | 'NATIONALITY' | 'MOBILE_NUMBER'>) | undefined, otc?: number | undefined, notificationEndpoint?: string | undefined, organizationName?: string | undefined, callback?: string | undefined, userEmail?: string | undefined, keyIdentifier?: KeyIdentifier | undefined, qualityThreshold?: number | undefined}; export type CreateRequisitionRequest = {data?: BiometricDemand | undefined}; export type CreateApiCheckidArgs = {body: CreateRequisitionRequest}; export type CreateApiCheckidOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const gUID = z.object({guid: z.string().optional()}); export const createRequisitionResponse = z.object({transactionID: gUID.optional(), dynamicLink: z.string().optional(), status: z.enum(["AWAITING_DATA_COLLECTION", "DATA_COLLECTED", "FINISHED", "DENIED", "ERROR"]).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 }; /** * createUserDemand * * POST /checkid */ export const createApiCheckid = async (args: CreateApiCheckidArgs, options?: CreateApiCheckidOptions) => { const {baseUrl = "http://:8085/gbds/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/checkid${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(createRequisitionResponse, await res.json()) };