import {z} from 'zod' export type AnalysisProfileSchema = {analysis_service_account_id: string, enabled?: boolean | undefined, id?: string | undefined, is_default?: boolean | undefined, media_type: 'image' | 'video' | 'transcription' | 'face_image' | 'face_video', name: string, service_type?: ('GOOGLE_VISION' | 'GOOGLE_VIDEO_INTELLIGENCE' | 'AMAZON_REKOGNITION' | 'REV_AI' | 'ICONIK' | 'ICONIK_FACE_RECOGNITION') | undefined, settings?: {[key: string]: unknown} | undefined}; export type CreateApiV1AnalysisProfilesArgs = {'App-ID': string, 'Auth-Token': string, body: AnalysisProfileSchema}; export type CreateApiV1AnalysisProfilesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const analysisProfileSchema = z.object({analysis_service_account_id: z.string(), enabled: z.boolean().optional(), id: z.string().optional(), is_default: z.boolean().optional(), media_type: z.enum(["image", "video", "transcription", "face_image", "face_video"]), name: z.string(), service_type: z.enum(["GOOGLE_VISION", "GOOGLE_VIDEO_INTELLIGENCE", "AMAZON_REKOGNITION", "REV_AI", "ICONIK", "ICONIK_FACE_RECOGNITION"]).optional(), settings: z.record(z.string(), z.unknown()).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 a new analysis profile * * POST /v1/analysis/profiles/ */ export const createApiV1AnalysisProfiles = async (args: CreateApiV1AnalysisProfilesArgs, options: CreateApiV1AnalysisProfilesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analysis/profiles/${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(analysisProfileSchema, await res.json()) };