import {z} from 'zod' export type VerifyIDRequest = {session_id: string}; export type CreateApiLivenessVerifyIdArgs = {body: VerifyIDRequest}; export type CreateApiLivenessVerifyIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const verifyIDResponse = z.object({metadata: z.object({project_id: z.string().optional(), session_id: z.string().optional(), account_id: z.string().nullable().optional(), referring_session_id: z.string().nullable().optional(), request_id: z.string().optional(), timestamp: z.string().optional()}).optional(), decision: z.enum(["Fake", "Suspicious", "Real"]).optional(), risk_score: z.number().gte(0).lte(1).optional(), risk_flags: z.array(z.string()).optional(), document_signals: z.object({id_age: z.number().int().optional(), face_age_estimate: z.enum(["under8", "over8", "over13", "over16", "over18", "over21", "over25", "over30", "over40", "over50", "over65"]).nullable().optional(), id_face_match_score: z.number().gte(0).lte(1).optional(), id_barcode_status: z.enum(["no_barcode_specificed_by_template", "barcode_requested_but_not_found", "barcode_requested_but_error_reading", "success", "barcode_requested_and_read_but_could_not_parse"]).optional(), id_face_status: z.enum(["not_available", "likely_original_face", "cannot_confirm_id_is_authentic", "ocr_template_does_not_support_detection"]).optional(), id_text_status: z.enum(["not_available", "likely_original_text", "cannot_confirm_id_is_authentic"]).optional(), is_id_digital_spoof: z.enum(["likely_physical_id", "could_not_confidently_determine_physical_id_user_needs_to_retry"]).optional(), is_full_id_captured: z.enum(["full_id_detected", "could_not_confidently_determine_full_id_user_needs_to_retry"]).optional(), id_validity: z.enum(["likely_authentic_id", "likely_fake_id", "cannot_confirm_id_is_authentic"]).optional()}).optional(), document_data: z.object({template_info: z.object({document_country_code: z.string().optional(), document_state: z.string().optional(), template_type: z.enum(["passport", "driver_license", "state_id", "national_id", "residence_permit", "other", "unknown"]).optional()}).optional(), user_data: z.object({first_name: z.string().optional(), last_name: z.string().optional(), date_of_birth: z.string().optional(), date_of_expiration: z.string().optional(), date_of_issue: z.string().optional(), id_number: z.string().optional(), id_number2: z.string().optional(), address: z.object({city: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional(), state: z.string().optional(), street: z.string().optional()}).optional()}).optional()}).optional(), device_network_signals: z.object({device_risk: z.number().gte(0).lte(1).optional(), proxy: z.number().gte(0).lte(1).optional(), vpn: z.number().gte(0).lte(1).optional(), datacenter: z.number().gte(0).lte(1).optional(), tor: z.number().gte(0).lte(1).optional(), spoofed_ip: z.number().gte(0).lte(1).optional(), recent_fraud_ip: z.number().gte(0).lte(1).optional(), device_network_mismatch: z.number().gte(0).lte(1).optional(), location_spoofing: z.number().gte(0).lte(1).optional()}).optional(), referring_session_signals: z.object({impossible_travel: z.number().gte(0).lte(1).optional(), ip_mismatch: z.number().gte(0).lte(1).optional(), user_agent_mismatch: z.number().gte(0).lte(1).optional(), device_timezone_mismatch: z.number().gte(0).lte(1).optional(), ip_timezone_mismatch: z.number().gte(0).lte(1).optional()}).optional(), photo_urls: z.object({face: z.string().optional()}).optional(), video_urls: z.array(z.string()).optional(), session_data: z.object({true_country_code: z.string().optional(), network: z.object({ip_address: z.string().optional(), service_provider: z.string().optional(), connection_type: z.string().optional()}).optional(), location: z.object({continent: z.string().optional(), country_code: z.string().optional(), state: z.string().optional(), city: z.string().optional(), zip_code: z.string().optional(), timezone: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional()}).optional(), browser: z.object({type: z.string().optional(), version: z.string().optional(), language: z.string().optional(), user_agent: z.string().optional(), timezone: z.string().optional()}).optional(), device: z.object({category: z.enum(["desktop", "mobile", "tablet", "console", "smarttv", "wearable", "embedded", "other"]).optional(), type: z.string().optional(), os: z.string().optional(), cpu_cores: z.number().int().optional(), memory: z.number().int().optional(), gpu: z.string().optional()}).optional()}).optional(), matches: z.object({num_accounts_linked: z.number().int().optional(), accounts_linked: z.array(z.object({account_id: z.string().optional(), match_types: z.array(z.enum(["face", "ip_address", "browser", "document_number", "document_name"])).optional()})).optional()}).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 ID document * * POST /liveness/verify-id */ export const createApiLivenessVerifyId = async (args: CreateApiLivenessVerifyIdArgs, options?: CreateApiLivenessVerifyIdOptions) => { const {baseUrl = "https://api.sandbox.verisoul.ai", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/liveness/verify-id${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(verifyIDResponse, await res.json()) };