import {z} from 'zod' export type IdCaptureV2Request = {base64Image?: string | undefined, imageUrl?: string | undefined}; export type CreateApiOmniAddBackIdV2Args = {retry?: boolean | undefined, 'api-version': string, body: IdCaptureV2Request}; export type CreateApiOmniAddBackIdV2Options = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, IncodeHardwareIdAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const captureAttemptsLimitDto = z.object({max: z.number().int().optional(), remaining: z.number().int().optional()}); export const addIdResponse = z.object({correctSharpness: z.boolean().optional(), correctGlare: z.boolean().optional(), horizontalResolution: z.number().int().optional(), shadowConfidence: z.number().gte(0).lte(1).optional(), classification: z.boolean().optional(), readability: z.boolean().optional(), typeOfId: z.enum(["Unknown", "Passport", "Visa", "DriversLicense", "IdentificationCard", "Permit", "Currency", "ResidenceDocument", "TravelDocument", "BirthCertificate", "VehicleRegistration", "Other", "WeaponLicense", "TribalIdentification", "VoterIdentification", "Military", "TaxIdentification", "FederalID", "MedicalCard"]).optional(), issueYear: z.number().int().optional(), issueName: z.string().optional(), curpCheck: z.boolean().optional(), sessionStatus: z.enum(["Alive", "Closed", "Deleted"]).optional(), countryCode: z.string().optional(), state: z.string().optional(), failReason: z.enum(["UNKNOWN_DOCUMENT_TYPE", "WRONG_DOCUMENT_SIDE", "WRONG_ONE_SIDED_DOCUMENT", "UNFOLDED_DOCUMENT_PAGE_MISMATCH", "WRONG_UNFOLDED_DOCUMENT", "DOCUMENT_NOT_READABLE", "UNABLE_TO_ALIGN_DOCUMENT", "ID_TYPE_UNACCEPTABLE", "UNEXPECTED_ERROR_OCCURRED", "DIGITAL_ID_REQUESTED_BUT_OTHER_PROVIDED"]).optional(), skipBackIdCapture: z.boolean().optional(), forceFrontIdCapture: z.boolean().optional(), showMandatoryConsent: z.boolean().optional(), regulationType: z.string().optional(), skipGlareFront: z.boolean().optional(), skipGlareBack: z.boolean().optional(), documentIsOnTheEdge: z.boolean().optional(), acceptedDocuments: z.array(z.enum(["Unknown", "Passport", "Visa", "DriversLicense", "IdentificationCard", "Permit", "Currency", "ResidenceDocument", "TravelDocument", "BirthCertificate", "VehicleRegistration", "Other", "WeaponLicense", "TribalIdentification", "VoterIdentification", "Military", "TaxIdentification", "FederalID", "MedicalCard"])).optional(), imageRedacted: z.boolean().optional(), idFaceExtractionSkipped: z.boolean().optional(), captureAttemptsLimit: captureAttemptsLimitDto.optional(), idQualityAttemptApproved: z.boolean().optional(), isDocumentExpired: z.boolean().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 }; /** * Add back side of ID * * POST /omni/add/back-id/v2 */ export const createApiOmniAddBackIdV2 = async (args: CreateApiOmniAddBackIdV2Args, options?: CreateApiOmniAddBackIdV2Options) => { const {baseUrl = "https://demo-api.incodesmile.com", fetch: fetchFn = fetch, ApiKeyAuth, IncodeHardwareIdAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.retry != null) query.append("retry", String(args.retry)) if (args['api-version'] != null) headers.set("api-version", String(args['api-version'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (IncodeHardwareIdAuth !== undefined) headers.set("X-Incode-Hardware-Id", IncodeHardwareIdAuth) const url = `${baseUrl.replace(/\/+$/, '')}/omni/add/back-id/v2${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(addIdResponse, await res.json()) };