import {z} from 'zod' export type CreateApiApiAiImagesV2FaceDetectionArgs = {body: BodyInit}; export type CreateApiApiAiImagesV2FaceDetectionOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, OrgKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emotion = z.object({type: z.string().optional(), confidence: z.number().optional()}); export const landmark = z.object({type: z.string().optional(), x: z.number().optional(), y: z.number().optional()}); export const faceData = z.object({bounding_box: z.object({width: z.number().optional(), height: z.number().optional(), left: z.number().optional(), top: z.number().optional()}).optional(), age_range: z.object({low: z.number().int().optional(), high: z.number().int().optional()}).optional(), smile: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), eyeglasses: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), sunglasses: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), gender: z.object({value: z.string().optional(), confidence: z.number().optional()}).optional(), beard: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), mustache: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), eyes_open: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), mouth_open: z.object({value: z.boolean().optional(), confidence: z.number().optional()}).optional(), emotions: z.array(emotion).optional(), landmarks: z.array(landmark).optional(), pose: z.object({roll: z.number().optional(), yaw: z.number().optional(), pitch: z.number().optional()}).optional(), quality: z.object({brightness: z.number().optional(), sharpness: z.number().optional()}).optional(), confidence: z.number().optional()}); export const faceDetectionResponse = z.object({id: z.string().optional(), status: z.string().optional(), timestamp: z.number().int().optional(), analysed_data: z.array(faceData).optional(), processing_count: z.number().int().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 }; /** * Modify Image V2 * * POST /api/ai/images/v2/face-detection */ export const createApiApiAiImagesV2FaceDetection = async (args: CreateApiApiAiImagesV2FaceDetectionArgs, options?: CreateApiApiAiImagesV2FaceDetectionOptions) => { const {baseUrl = "https://api.worqhat.com", fetch: fetchFn = fetch, ApiKeyAuth, OrgKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) if (OrgKeyAuth !== undefined) headers.set("x-org-key", OrgKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/ai/images/v2/face-detection${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(faceDetectionResponse, await res.json()) };