import {z} from 'zod' export type CreateApiV3CompareArgs = {'SmileID-Source-SDK'?: string | undefined, 'SmileID-Source-SDK-Version'?: string | undefined, 'SmileID-Timestamp'?: string | undefined, 'SmileID-Request-Signature'?: string | undefined, 'User-ID'?: string | undefined, body: BodyInit}; export type CreateApiV3CompareOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const acceptedResponse = z.object({status: z.literal("accepted"), message: z.string(), job_id: z.string(), user_id: z.string(), created_at: z.string()}); 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 }; /** * Submit smart selfie compare * * POST /v3/compare */ export const createApiV3Compare = async (args: CreateApiV3CompareArgs, options?: CreateApiV3CompareOptions) => { const {baseUrl = "https://testapi.smileidentity.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['SmileID-Source-SDK'] != null) headers.set("SmileID-Source-SDK", String(args['SmileID-Source-SDK'])) if (args['SmileID-Source-SDK-Version'] != null) headers.set("SmileID-Source-SDK-Version", String(args['SmileID-Source-SDK-Version'])) if (args['SmileID-Timestamp'] != null) headers.set("SmileID-Timestamp", String(args['SmileID-Timestamp'])) if (args['SmileID-Request-Signature'] != null) headers.set("SmileID-Request-Signature", String(args['SmileID-Request-Signature'])) if (args['User-ID'] != null) headers.set("User-ID", String(args['User-ID'])) const url = `${baseUrl.replace(/\/+$/, '')}/v3/compare${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(acceptedResponse, await res.json()) };