import {z} from 'zod' export type EsperCloudApiUpdateAlarmRule = {id?: string | undefined, enterprise: string, devices?: Array | undefined, groups?: Array | undefined, name: string, description?: string | undefined, metric: string, conditions: {value?: number | undefined, unit?: string | undefined, comparator?: string | undefined, toggle_state?: boolean | undefined}, action_emails?: Array | undefined, actions?: Array | undefined, alert_channels?: Array | undefined, created_on?: string | undefined, updated_on?: string | undefined, is_active?: boolean | undefined, realert_interval: number, repeat_times?: number | undefined, trigger_count?: number | undefined, daily_trigger_count?: number | undefined}; export type UpdateApiV1EnterpriseEnterpriseIdAlarmrulesAlarmIdArgs = {enterprise_id: string, alarm_id: string, body: EsperCloudApiUpdateAlarmRule}; export type UpdateApiV1EnterpriseEnterpriseIdAlarmrulesAlarmIdOptions = {baseUrl?: string | undefined, esperCloudApiApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const esperCloudApiAlarmRule = z.object({id: z.string().optional(), enterprise: z.string().optional(), devices: z.object({id: z.string().optional(), device_name: z.string().optional(), alias_name: z.string().nullable().optional()}).optional(), groups: z.array(z.string()).optional(), name: z.string().optional(), description: z.string().optional(), metric: z.string().optional(), conditions: z.object({value: z.number().optional(), unit: z.string().optional(), comparator: z.string().optional(), toggle_state: z.boolean().optional()}).optional(), action_emails: z.array(z.string()).optional(), actions: z.array(z.string()).optional(), alert_channels: z.array(z.string()).optional(), created_on: z.string().optional(), updated_on: z.string().optional(), is_active: z.boolean().optional(), realert_interval: z.number().int().optional(), repeat_times: z.number().int().optional(), trigger_count: z.number().int().optional(), daily_trigger_count: z.number().int().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 }; /** * Update alarm rules * * PUT /v1/enterprise/{enterprise_id}/alarmrules/{alarm_id}/ */ export const updateApiV1EnterpriseEnterpriseIdAlarmrulesAlarmId = async (args: UpdateApiV1EnterpriseEnterpriseIdAlarmrulesAlarmIdArgs, options?: UpdateApiV1EnterpriseEnterpriseIdAlarmrulesAlarmIdOptions) => { const {baseUrl = "https://develop-api.esper.cloud/api", fetch: fetchFn = fetch, esperCloudApiApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (esperCloudApiApiKey !== undefined) headers.set('authorization', `Bearer ${esperCloudApiApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/enterprise/${encodeURIComponent(String(args.enterprise_id))}/alarmrules/${encodeURIComponent(String(args.alarm_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(esperCloudApiAlarmRule, await res.json()) };