import {z} from 'zod' export type UpdateApiSensorsSensorIdArgs = {sensor_id: number, body: {name?: string | undefined, type?: ('motion' | 'camera_motion' | 'door_window' | 'smoke' | 'fire' | 'glass_break' | 'shock_vibration' | 'water' | 'perimeter_entry') | undefined, placement?: ('indoors' | 'outdoors') | undefined, manufacturer?: string | undefined, model?: string | undefined, note?: string | undefined, enabled?: boolean | undefined, override_arming?: boolean | undefined, camera?: number | undefined, enable_cloud_recording_for?: ('PT30S' | 'PT1M' | 'PT2M' | 'PT5M') | undefined, incident_delay?: unknown | undefined}}; export type UpdateApiSensorsSensorIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const sensorObject = z.object({id: z.number().int().gte(1), user: z.number().int().gte(1), name: z.string(), type: z.enum(["motion", "camera_motion", "door_window", "smoke", "fire", "glass_break", "shock_vibration", "water", "perimeter_entry"]), placement: z.enum(["indoors", "outdoors"]), manufacturer: z.string(), model: z.string(), note: z.string(), hash: z.string(), email: z.string(), enabled: z.boolean(), override_arming: z.boolean(), camera: z.number().int().gte(1), enable_cloud_recording_for: z.enum(["PT30S", "PT1M", "PT2M", "PT5M"]), incident_delay: z.string(), created_at: z.string(), updated_at: z.string(), events_count_ifttt: z.number().int(), events_count_email: z.number().int(), events_count_api: z.number().int()}); 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 }; /** * Update sensor * * PUT /sensors/{sensor_id}/ * * Also accepts auth: PersonalAccessToken */ export const updateApiSensorsSensorId = async (args: UpdateApiSensorsSensorIdArgs, options?: UpdateApiSensorsSensorIdOptions) => { const {baseUrl = "https://api.angelcam.com/v1", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/sensors/${encodeURIComponent(String(args.sensor_id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(sensorObject, await res.json()) };