import {z} from 'zod' export type GetApiSharedCamerasCameraIdArgs = {camera_id: number, refresh_rate?: number | undefined, max_width?: number | undefined, max_height?: number | undefined}; export type GetApiSharedCamerasCameraIdOptions = {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 sharedApplicationObject = z.object({code: z.literal("CRA")}); export const ownerObject = z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string()}); export const sharedCameraObject = z.object({id: z.number().int().gte(1), name: z.string(), type: z.enum(["h264", "h265", "mjpeg"]), snapshot: snapshotObject, status: z.enum(["offline", "online", "unknown"]), live_snapshot: z.string().nullable(), streams: z.array(streamObject), applications: z.array(sharedApplicationObject), owner: ownerObject, has_recording: z.boolean(), has_notifications: z.boolean()}); 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 shared camera * * GET /shared-cameras/{camera_id}/ * * Also accepts auth: PersonalAccessToken */ export const getApiSharedCamerasCameraId = async (args: GetApiSharedCamerasCameraIdArgs, options?: GetApiSharedCamerasCameraIdOptions) => { 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 (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/shared-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(sharedCameraObject, await res.json()) };