import {z} from 'zod' export type AudioBuffer = string; export type AudioTemplate = string; export type CheckLiveness = boolean; export type CheckQuality = boolean; export type LivenessThreshold = number; export type MinimumSnrDb = number; export type MinimumAudioLengthMs = number; export type MinimumSpeechRelativeLength = number; export type MaximumMultipleSpeakersDetectorScore = number; export type ExtraData = string; export type AuthenticationBody = {audio: AudioBuffer, template: AudioTemplate, check_liveness?: CheckLiveness | undefined, check_quality?: CheckQuality | undefined, liveness_threshold?: LivenessThreshold | undefined, minimum_snr_db?: MinimumSnrDb | undefined, minimum_speech_duration_ms?: MinimumAudioLengthMs | undefined, minimum_speech_relative_length?: MinimumSpeechRelativeLength | undefined, maximum_multiple_speakers_detector_score?: MaximumMultipleSpeakersDetectorScore | undefined, extra_data?: ExtraData | undefined}; export type CreateApiApiV1FacialAuthenticationArgs = {body: AuthenticationBody}; export type CreateApiApiV1FacialAuthenticationOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const operationResult = z.number(); export const match = z.boolean(); export const livenessScore = z.number(); export const matchingScore = z.number(); export const snrDbDetected = z.number(); export const speechLengthMsDetected = z.number(); export const speechRelativeLengthDetected = z.number(); export const multipleSpeakersScoreDetected = z.number(); export const trackingStatus = z.number(); export const trackingMessage = z.string(); export const authenticationResponse = z.object({operation_result: operationResult.optional(), match: match.optional(), liveness_score: livenessScore.optional(), matching_score: matchingScore.optional(), snr_db_detected: snrDbDetected.optional(), speech_length_ms_detected: speechLengthMsDetected.optional(), speech_relative_length_detected: speechRelativeLengthDetected.optional(), multiple_speakers_score_detected: multipleSpeakersScoreDetected.optional(), tracking_status: trackingStatus.optional(), tracking_message: trackingMessage.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 }; /** * Este endpoint se utiliza para autenticar dos imágenes de fotos. * * POST /api/v1/facial/authentication * * Also accepts auth: ApiKeyAuth */ export const createApiApiV1FacialAuthentication = async (args: CreateApiApiV1FacialAuthenticationArgs, options: CreateApiApiV1FacialAuthenticationOptions) => { 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/facial/authentication${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(authenticationResponse, await res.json()) };