import {z} from 'zod' export type LightId = string; export type Name = string; export type IsLightForceEnabled = boolean; export type LightMode = 'always' | 'motion' | 'off'; export type EnableAt = 'fulltime' | 'dark'; export type LightModeSettings = {mode?: LightMode | undefined, enableAt?: EnableAt | undefined}; export type LightDeviceSettings = {isIndicatorEnabled?: boolean | undefined, pirDuration?: number | undefined, pirSensitivity?: number | undefined, ledLevel?: number | undefined}; export type PatchApiV1LightsIdArgs = {id: LightId, body: {name?: Name | undefined, isLightForceEnabled?: IsLightForceEnabled | undefined, lightModeSettings?: LightModeSettings | undefined, lightDeviceSettings?: LightDeviceSettings | undefined}}; export type PatchApiV1LightsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const lightId = z.string(); export const lightModelKey = z.literal("light"); export const deviceState = z.enum(["CONNECTED", "CONNECTING", "DISCONNECTED"]); export const name = z.string(); export const mac = z.string(); export const lightMode = z.union([z.literal("always"), z.literal("motion"), z.literal("off")]); export const enableAt = z.union([z.literal("fulltime"), z.literal("dark")]); export const lightModeSettings = z.object({mode: lightMode.optional(), enableAt: enableAt.optional()}); export const lightDeviceSettings = z.object({isIndicatorEnabled: z.boolean().optional(), pirDuration: z.number().gte(0).optional(), pirSensitivity: z.number().gte(0).lte(100).optional(), ledLevel: z.number().gte(1).lte(6).optional()}); export const isDark = z.boolean(); export const isLightOn = z.boolean(); export const isLightForceEnabled = z.boolean(); export const lastMotion = z.number().nullable(); export const isPirMotionDetected = z.boolean(); export const cameraId = z.string(); export const light = z.object({id: lightId, modelKey: lightModelKey, state: deviceState, name: name.nullable(), mac: mac, lightModeSettings: lightModeSettings, lightDeviceSettings: lightDeviceSettings, isDark: isDark, isLightOn: isLightOn, isLightForceEnabled: isLightForceEnabled, lastMotion: lastMotion, isPirMotionDetected: isPirMotionDetected, camera: cameraId.nullable()}); 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 }; /** * Patch light settings * * PATCH /v1/lights/{id} */ export const patchApiV1LightsId = async (args: PatchApiV1LightsIdArgs, options?: PatchApiV1LightsIdOptions) => { 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/lights/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(light, await res.json()) };