import {z} from 'zod' export type PhonePrefix = unknown; export type DomesticPhoneNumber = unknown; export type PhoneNumber = {country_code: PhonePrefix, number: DomesticPhoneNumber}; export type CountryCodeIso2 = unknown; export type LanguageCode = unknown; export type FaceAttemptClientInformation = {pre_selected_residence_country?: CountryCodeIso2 | undefined, pre_selected_language?: LanguageCode | undefined}; export type FaceAttemptBase = {phone_number?: PhoneNumber | undefined, client_information?: FaceAttemptClientInformation | undefined, redirect_url?: string | undefined, mobile_handoff_url?: (string | null) | undefined}; export type FaceAuthenticationAttemptInputPost = FaceAttemptBase; export type CreateApiV2FaceAuthenticationsFaceAuthenticationIdAttemptsArgs = {face_authentication_id: string, body: FaceAuthenticationAttemptInputPost}; export type CreateApiV2FaceAuthenticationsFaceAuthenticationIdAttemptsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const attemptStates = z.enum(["pending_redirection", "capture_in_progress", "checks_in_progress", "completed", "expired", "capture_aborted", "capture_refused", "checks_inconclusive", "terminated"]); export const responseCode = z.number().int().gte(10000).lte(69999); export const responseSummary = z.string(); export const responseCodes = z.object({code: responseCode, summary: responseSummary}); export const initialDevices = z.enum(["mobile", "desktop"]); export const faceAuthenticationApplicantSessionInformation = z.object({ip_address: z.string().optional(), number_of_sessions: z.number().int().optional(), user_agent: z.string().optional(), initial_device: initialDevices.optional()}); export const halLink = z.object({href: z.string()}); export const attemptLinks = z.object({verification_url: halLink, self: halLink}); export const phonePrefix = z.string(); export const domesticPhoneNumber = z.string(); export const phoneNumber = z.object({country_code: phonePrefix, number: domesticPhoneNumber}); export const countryCodeIso2 = z.string(); export const languageCode = z.string(); export const faceAttemptClientInformation = z.object({pre_selected_residence_country: countryCodeIso2.optional(), pre_selected_language: languageCode.optional()}); export const faceAuthenticationAttemptOutputPost = z.object({id: z.string().optional(), status: attemptStates.optional(), response_codes: z.array(responseCodes).optional(), applicant_session_information: faceAuthenticationApplicantSessionInformation.optional(), _links: attemptLinks.optional(), created_on: z.string(), modified_on: z.string(), phone_number: phoneNumber.optional(), client_information: faceAttemptClientInformation.optional(), redirect_url: z.string().optional(), mobile_handoff_url: z.string().max(512).nullable().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 }; /** * Create an attempt * * POST /v2/face-authentications/{face_authentication_id}/attempts */ export const createApiV2FaceAuthenticationsFaceAuthenticationIdAttempts = async (args: CreateApiV2FaceAuthenticationsFaceAuthenticationIdAttemptsArgs, options?: CreateApiV2FaceAuthenticationsFaceAuthenticationIdAttemptsOptions) => { const {baseUrl = "https://api.ubble.ai", 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(/\/+$/, '')}/v2/face-authentications/${encodeURIComponent(String(args.face_authentication_id))}/attempts${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(faceAuthenticationAttemptOutputPost, await res.json()) };