import {z} from 'zod' export type NotificationRuleChangeObject = {notification_method: number, message_type: 'my_incident' | 'shared_incident' | 'arming' | 'trigger_action' | 'open_incident_verification' | 'verify_incident' | 'close_incident' | 'arrow_status_online' | 'arrow_status_offline' | 'camera_status_online' | 'camera_status_offline', allowed_cameras?: Array | undefined, allowed_arrow_clients?: Array | undefined}; export type CreateApiRtsNotificationsRulesArgs = {body: NotificationRuleChangeObject}; export type CreateApiRtsNotificationsRulesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRuleObject = z.object({id: z.number().int().gte(1), notification_method: z.number().int(), message_type: z.enum(["my_incident", "shared_incident", "arming", "trigger_action", "open_incident_verification", "verify_incident", "close_incident", "arrow_status_online", "arrow_status_offline", "camera_status_online", "camera_status_offline"]), allowed_cameras: z.array(z.number().int()).optional(), allowed_arrow_clients: z.array(z.string()).optional()}); 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 notification rule * * POST /rts/notifications/rules/ * * Also accepts auth: PersonalAccessToken */ export const createApiRtsNotificationsRules = async (args: CreateApiRtsNotificationsRulesArgs, options?: CreateApiRtsNotificationsRulesOptions) => { 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(/\/+$/, '')}/rts/notifications/rules/${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(notificationRuleObject, await res.json()) };