import {z} from 'zod' export type LiveviewId = string; export type LiveviewModelKey = 'liveview'; export type UserId = string; export type CameraId = string; export type Liveview = {id: LiveviewId, modelKey: LiveviewModelKey, name: string, isDefault: boolean, isGlobal: boolean, owner: UserId, layout: number, slots: Array<{cameras: Array, cycleMode: 'motion' | 'time', cycleInterval: number}>}; export type CreateApiV1LiveviewsArgs = {body: Liveview}; export type CreateApiV1LiveviewsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const liveviewId = z.string(); export const liveviewModelKey = z.literal("liveview"); export const userId = z.string(); export const cameraId = z.string(); export const liveview = z.object({id: liveviewId, modelKey: liveviewModelKey, name: z.string(), isDefault: z.boolean(), isGlobal: z.boolean(), owner: userId, layout: z.number().gte(1).lte(26), slots: z.array(z.object({cameras: z.array(cameraId), cycleMode: z.enum(["motion", "time"]), cycleInterval: z.number()}))}); 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 live view * * POST /v1/liveviews */ export const createApiV1Liveviews = async (args: CreateApiV1LiveviewsArgs, options?: CreateApiV1LiveviewsOptions) => { const {baseUrl = "https://api.ui.com/v1/connector/consoles/942A6FF664C0000000000964970E0000000009E710560000000068A9873F:9999999999/proxy/protect/integration", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/liveviews${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(liveview, await res.json()) };