import {z} from 'zod' export type GetApiSensorsSensorIdArgs = {sensor_id: number}; export type GetApiSensorsSensorIdOptions = {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 }; /** * Retrieve sensor * * GET /sensors/{sensor_id}/ * * Also accepts auth: PersonalAccessToken */ export const getApiSensorsSensorId = async (args: GetApiSensorsSensorIdArgs, options?: GetApiSensorsSensorIdOptions) => { 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 (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: "GET", headers}) 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()) };