import {z} from 'zod' export type Biometric = {index?: number | undefined, content?: string | undefined, width?: number | undefined, height?: number | undefined, resolution?: number | undefined}; export type MissingBiometric = {index?: number | undefined, type?: ('AMPUTATED' | 'UNAVAILABLE') | undefined, reason?: string | undefined}; export type UserTransaction = {biometrics?: Array | undefined, missingBiometrics?: Array | undefined, userId?: string | undefined, operatorId?: string | undefined, timestamp?: number | undefined}; export type UserTransactionRequest = {data?: UserTransaction | undefined}; export type CreateApiGbsSpidServerVerifyArgs = {body: UserTransactionRequest}; export type CreateApiGbsSpidServerVerifyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const externalResult = z.object({id: z.string().optional(), message: z.string().optional(), status: z.enum(["OK", "SEARCH_PENDING", "SEARCH_ENQUEUED", "SEARCH_MATCH", "SEARCH_NOT_MATCH", "SEARCH_FAILED", "ENROLL_PENDING", "ENROLL_ENQUEUED", "ENROLL_CACHE_OK", "ENROLL_OK", "ENROLL_FAILED", "ENROLL_EXCEPTION", "DELETE_OK", "DELETE_FAILED", "DELETE_PENDING", "REMOTE_CAPTURE_ACCEPTED", "REMOTE_CAPTURE_REFUSED", "REMOTE_CAPTURE_PENDING", "REMOTE_CAPTURE_AVAILABLE", "REMOTE_CAPTURE_STORED", "FRAUD", "ERROR", "ENROLL_REJECTED", "PERSON_NOT_FOUND"]).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 }; /** * verify * * POST /gbs-spid-server/verify */ export const createApiGbsSpidServerVerify = async (args: CreateApiGbsSpidServerVerifyArgs, options?: CreateApiGbsSpidServerVerifyOptions) => { 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(/\/+$/, '')}/gbs-spid-server/verify${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(externalResult, await res.json()) };