import {z} from 'zod' export type GetApiCamerasCameraIdArgs = {camera_id: number, refresh_rate?: number | undefined, max_width?: number | undefined, max_height?: number | undefined, stream_expires_at?: string | undefined}; export type GetApiCamerasCameraIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const snapshotObject = z.object({url: z.string().nullable(), created_at: z.string()}).nullable(); export const streamObject = z.object({url: z.string(), format: z.enum(["hls", "mp4", "mpegts", "mjpeg"]), refresh_rate: z.number().optional()}); export const applicationObject = z.object({code: z.enum(["LSA", "CRA", "VVA", "VPA", "TLA"])}); export const cameraObject = z.object({id: z.number().int().gte(1), name: z.string(), location: z.number().int().nullable(), site: z.number().int().nullable(), type: z.enum(["h264", "h265", "mjpeg"]), snapshot: snapshotObject, status: z.enum(["offline", "online", "unknown"]), event_api_hash: z.string().optional(), live_snapshot: z.string().nullable(), streams: z.array(streamObject), applications: z.array(applicationObject), low_latency_enabled: z.boolean(), audio_enabled: z.boolean(), enable_status_checks: z.boolean(), enable_periodical_snapshots: z.boolean(), tags: z.array(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 }; /** * Retrieve camera * * GET /cameras/{camera_id}/ * * Also accepts auth: PersonalAccessToken */ export const getApiCamerasCameraId = async (args: GetApiCamerasCameraIdArgs, options?: GetApiCamerasCameraIdOptions) => { const {baseUrl = "https://api.angelcam.com/v1", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.refresh_rate != null) query.append("refresh_rate", String(args.refresh_rate)) if (args.max_width != null) query.append("max_width", String(args.max_width)) if (args.max_height != null) query.append("max_height", String(args.max_height)) if (args.stream_expires_at != null) query.append("stream_expires_at", String(args.stream_expires_at)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/cameras/${encodeURIComponent(String(args.camera_id))}/${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(cameraObject, await res.json()) };