import {z} from 'zod' export type AlertRuleRequest = {name?: (string | null) | undefined, rule_set?: (string | null) | undefined, is_enabled?: (boolean | null) | undefined, period?: (number | null) | undefined, threshold?: (number | null) | undefined, webhook_url?: (string | null) | undefined, all_devices?: (boolean | null) | undefined, all_users?: (boolean | null) | undefined, device_ids?: (Array | null) | undefined, user_ids?: (Array | null) | undefined}; export type UpdateApiAlertsRulesIdArgs = {id: string, body: AlertRuleRequest}; export type UpdateApiAlertsRulesIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const alertRule = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), rule_set: z.string().nullable().optional(), is_enabled: z.boolean().nullable().optional(), period: z.number().int().nullable().optional(), threshold: z.number().int().nullable().optional(), webhook_url: z.string().nullable().optional(), all_devices: z.boolean().nullable().optional(), all_users: z.boolean().nullable().optional(), created_on: z.string().nullable().optional(), updated_on: z.string().nullable().optional(), device_ids: z.array(z.string()).nullable().optional(), user_ids: z.array(z.string()).nullable().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 }; /** * Updates an existing alert rule. * * PUT /alerts/rules/{id} * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const updateApiAlertsRulesId = async (args: UpdateApiAlertsRulesIdArgs, options?: UpdateApiAlertsRulesIdOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/alerts/rules/${encodeURIComponent(String(args.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(alertRule, await res.json()) };