import {z} from 'zod' export type ApplicantId = string; export type IdvUserJourneyId = string; export type FavVerificationStatuses = 'approved' | 'capture_in_progress' | 'checks_in_progress' | 'created' | 'declined' | 'inconclusive' | 'pending' | 'refused' | 'retry_required'; export type IdvResponseCodeCode = number; export type IdvResponseCodeSummary = string; export type IdvResponseCode = {code: IdvResponseCodeCode, summary: IdvResponseCodeSummary}; export type IdvResponseCodes = Array; export type IdvRiskLabel = 'multiple_faces_detected' | 'mcc_not_confident' | 'risky_document_format'; export type IdvRiskLabels = Array; export type IdvPreSignedUrl = string; export type IdvFace = {image_signed_url: IdvPreSignedUrl}; export type IdvHalLink = {href: string}; export type FavFaceAuthenticationLinks = {self: IdvHalLink, applicant: IdvHalLink}; export type FavFaceAuthenticationId = string; export type FavFaceAuthentication = {created_on: string, modified_on: string, applicant_id: ApplicantId, user_journey_id: IdvUserJourneyId, status: FavVerificationStatuses, response_codes: IdvResponseCodes, risk_labels: IdvRiskLabels, face?: IdvFace | undefined, _links: FavFaceAuthenticationLinks, id: FavFaceAuthenticationId}; export type CreateApiFaceAuthenticationsArgs = {body: FavFaceAuthentication}; export type CreateApiFaceAuthenticationsOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const applicantId = z.string(); export const idvUserJourneyId = z.string(); export const favVerificationStatuses = z.enum(["approved", "capture_in_progress", "checks_in_progress", "created", "declined", "inconclusive", "pending", "refused", "retry_required"]); export const idvPreSignedUrl = z.string(); export const idvFace = z.object({image_signed_url: idvPreSignedUrl}); export const idvResponseCodeCode = z.number().int().gte(10000).lte(69999); export const idvResponseCodeSummary = z.string(); export const idvResponseCode = z.object({code: idvResponseCodeCode, summary: idvResponseCodeSummary}); export const idvResponseCodes = z.array(idvResponseCode); export const idvRiskLabel = z.enum(["multiple_faces_detected", "mcc_not_confident", "risky_document_format"]); export const idvRiskLabels = z.array(idvRiskLabel); export const idvHalLink = z.object({href: z.string()}); export const favFaceAuthenticationLinks = z.object({self: idvHalLink, applicant: idvHalLink}); export const favFaceAuthenticationId = z.string(); export const favFaceAuthenticationOutput = z.object({created_on: z.string(), modified_on: z.string(), applicant_id: applicantId, user_journey_id: idvUserJourneyId.optional(), status: favVerificationStatuses, face: idvFace.optional(), response_codes: idvResponseCodes, risk_labels: idvRiskLabels, _links: favFaceAuthenticationLinks, id: favFaceAuthenticationId}); 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 }; /** * Create a face authentication * * POST /face-authentications * * Also accepts auth: ApiSecretKey */ export const createApiFaceAuthentications = async (args: CreateApiFaceAuthenticationsArgs, options: CreateApiFaceAuthenticationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/face-authentications${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(favFaceAuthenticationOutput, await res.json()) };