import {z} from 'zod' export type GetApiSensorsArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiSensorsOptions = {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 sensorListResponse = z.object({count: z.number().int(), next: z.string().nullable(), previous: z.string().nullable(), results: z.array(sensorObject)}); 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 sensor list * * GET /sensors/ * * Also accepts auth: PersonalAccessToken */ export const getApiSensors = async (args: GetApiSensorsArgs, options?: GetApiSensorsOptions) => { 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.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/sensors/${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(sensorListResponse, await res.json()) };