import {z} from 'zod' export type GetApiApiV1CameraInfoOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const videoFormat = z.enum(["MJPG", "YUYV", "H264", "UYVY"]); export const cameraFormatDetail = z.object({format: videoFormat, width: z.number().int().gt(0), height: z.number().int().gt(0), fps: z.number().gt(0)}); export const activeCameraStatus = z.object({device_path: z.string(), streaming: z.boolean(), current_format: cameraFormatDetail.nullable().optional(), settings: z.record(z.string(), z.unknown()).nullable().optional()}); export const resolutionDetail = z.object({width: z.number().int().gt(0), height: z.number().int().gt(0), framerates: z.array(z.number())}); export const supportedFormat = z.object({format: videoFormat, description: z.string(), resolutions: z.array(resolutionDetail)}); export const cameraDevice = z.object({device_name: z.string(), bus_info: z.string(), device_path: z.string(), card_name: z.string(), driver_name: z.string(), supported_formats: z.array(supportedFormat).optional(), best_format: cameraFormatDetail.nullable().optional()}); export const cameraInfoResponse = z.object({connected: z.boolean(), message: z.string(), active_camera: activeCameraStatus.nullable().optional(), all_cameras: z.array(cameraDevice).nullable().optional(), cameras: z.array(cameraDevice).nullable().optional(), primary_camera: cameraDevice.nullable().optional(), total_cameras: z.number().int().gte(0).nullable().optional(), cameras_found: z.array(cameraDevice).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 }; /** * Get Detailed Camera Information * * GET /api/v1/camera/info */ export const getApiApiV1CameraInfo = async (options: GetApiApiV1CameraInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/camera/info${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cameraInfoResponse, await res.json()) };