import {z} from 'zod' export type Key = {id?: string | undefined, value?: string | undefined}; export type Biographic = {key?: string | undefined, type?: ('TEXT' | 'FACE') | undefined, value?: string | undefined}; export type Biometric = {index?: number | undefined, content?: string | undefined, width?: number | undefined, height?: number | undefined, resolution?: number | undefined}; export type HistoryEvent = {tguid?: string | undefined, timestamp?: string | undefined, type?: ('ENROLL' | 'UPDATE' | 'DISABLE_TRANSACTION' | 'DELETE_BIOMETRIC' | 'INCORRECT_ENROLL_TREATMENT') | undefined}; export type History = {events?: Array | undefined}; export type BiographicBaseStatus = 'UNAVAILABLE' | 'TIMEOUT' | 'UNAUTHORIZED' | 'INVALID_DATA' | 'NOT_FOUND' | 'OK'; export type Person = {pguid?: string | undefined, lastEnrollTguid?: string | undefined, timestamp?: number | undefined, keys?: Array | undefined, biographics?: Array | undefined, biometric?: Array | undefined, auxiliaries?: Array | undefined, metadata?: Array | undefined, labels?: Array | undefined, history?: History | undefined, biographicBaseStatus?: BiographicBaseStatus | undefined}; export type ExtractionRequest = {verify?: boolean | undefined, data?: Person | undefined}; export type CreateApiExtractArgs = {body: ExtractionRequest}; export type CreateApiExtractOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const key = z.object({id: z.string().optional(), value: z.string().optional()}); export const biographic = z.object({key: z.string().optional(), type: z.enum(["TEXT", "FACE"]).optional(), value: z.string().optional()}); export const biometric = z.object({index: z.number().int().optional(), content: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), resolution: z.number().int().optional()}); export const historyEvent = z.object({tguid: z.string().optional(), timestamp: z.string().optional(), type: z.enum(["ENROLL", "UPDATE", "DISABLE_TRANSACTION", "DELETE_BIOMETRIC", "INCORRECT_ENROLL_TREATMENT"]).optional()}); export const history = z.object({events: z.array(historyEvent).optional()}); export const biographicBaseStatus = z.enum(["UNAVAILABLE", "TIMEOUT", "UNAUTHORIZED", "INVALID_DATA", "NOT_FOUND", "OK"]); export const person = z.object({pguid: z.string().optional(), lastEnrollTguid: z.string().optional(), timestamp: z.number().int().optional(), keys: z.array(key).optional(), biographics: z.array(biographic).optional(), biometric: z.array(biometric).optional(), auxiliaries: z.array(biometric).optional(), metadata: z.array(z.string()).optional(), labels: z.array(z.string()).optional(), history: history.optional(), biographicBaseStatus: biographicBaseStatus.optional()}); export const extractionResponse = z.object({data: person.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 }; /** * Extract biometric template * * POST /extract */ export const createApiExtract = async (args: CreateApiExtractArgs, options?: CreateApiExtractOptions) => { 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(/\/+$/, '')}/extract${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(extractionResponse, await res.json()) };