import {z} from 'zod' export type Type = 'id_card' | 'passport' | 'driver_license' | 'foreign_card' | 'invoice' | 'pdf'; export type Model = string; export type Files = Array; export type IsCropped = boolean; export type ForceDetection = boolean; export type RetrieveImages = boolean; export type ProcessBody = {type?: Type | undefined, model?: Model | undefined, files?: Files | undefined, isCropped?: IsCropped | undefined, forceDetection?: ForceDetection | undefined, retrieveImages?: RetrieveImages | undefined}; export type CreateApiApiV1ProcessMultiArgs = {body: ProcessBody}; export type CreateApiApiV1ProcessMultiOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const uRSourceType = z.enum(["viz", "encoded", "nfc", "mrz"]); export const uROCRSource = z.object({type: uRSourceType, index: z.number().int()}); export const uROCRField = z.object({value: z.string(), confidence: z.number().gte(0).lte(1).optional(), source: uROCRSource.optional()}); export const uRImageType = z.enum(["portrait", "ghost", "signature", "fullFrame", "document", "fingerprint"]); export const uRImageContent = z.object({data: z.string(), length: z.number().int(), mime_type: z.string().optional()}); export const uRCoordinates = z.object({x: z.number(), y: z.number()}); export const uRRegion = z.object({top_left: uRCoordinates, top_right: uRCoordinates, bottom_left: uRCoordinates, bottom_right: uRCoordinates}); export const uRImage = z.object({type: uRImageType, side: z.number().int().optional(), source: uRSourceType, content: uRImageContent.optional(), region: uRRegion.optional()}); export const uRViz = z.object({field: z.string(), value: z.string(), side: z.number().int(), confidence: z.number().gte(0).lte(1).optional(), alphabet: z.string().optional(), region: uRRegion.optional()}); export const uRMRZ = z.object({side: z.number().int(), confidence: z.number().gte(0).lte(1).optional(), raw: z.string(), fields: z.record(z.string(), z.string()), region: uRRegion.optional()}); export const uREncodedType = z.enum(["qr", "barcode", "pdf"]); export const uREncoded = z.object({fields: z.record(z.string(), z.string()), type: uREncodedType, side: z.number().int(), raw: z.string(), rawBinary: z.string(), region: uRRegion.optional()}); export const uRNFC = z.object({field: z.string(), value: z.string(), alphabet: z.string().optional()}); export const fieldInconsistency = z.object({field: z.string(), values: z.array(z.string()).optional(), distance: z.number().int()}); export const uRChecks = z.object({field_inconsistency: z.array(fieldInconsistency).optional()}); export const processResultUniversal = z.object({version: z.number().int(), type: z.string(), model: z.string(), ocr: z.record(z.string(), uROCRField), images: z.array(uRImage).optional(), viz: z.array(uRViz), mrz: z.array(uRMRZ).optional(), encoded: z.array(uREncoded).optional(), nfc: z.array(uRNFC).optional(), checks: uRChecks}); 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 }; /** * Procesa mĂșltiples alfabetos mediante OCR * * POST /api/v1/process_multi * * Also accepts auth: ApiKeyAuth */ export const createApiApiV1ProcessMulti = async (args: CreateApiApiV1ProcessMultiArgs, options: CreateApiApiV1ProcessMultiOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/process_multi${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(processResultUniversal, await res.json()) };