import {z} from 'zod' export type Operation = {op?: string | undefined, value?: Record | undefined, path?: string | undefined}; export type JsonPatchDocument = Array; export type PatchApiAlertsIdArgs = {id: string, body: JsonPatchDocument}; export type PatchApiAlertsIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const alertRuleSummary = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), rule_set: z.string().nullable().optional(), is_enabled: z.boolean().nullable().optional()}); export const alertDevice = z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional()}); export const alert = z.object({id: z.string().nullable().optional(), created_on: z.string().optional(), updated_on: z.string().nullable().optional(), snoozed_until: z.string().nullable().optional(), resolved_on: z.string().nullable().optional(), alert_rule: alertRuleSummary.optional(), devices: z.array(alertDevice).nullable().optional(), is_active: z.boolean().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 }; /** * Partially updates an alert using JSON Patch. * Uses the JSON Patch syntax (http://jsonpatch.com) * * PATCH /alerts/{id} * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const patchApiAlertsId = async (args: PatchApiAlertsIdArgs, options?: PatchApiAlertsIdOptions) => { 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-patch+json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/alerts/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(alert, await res.json()) };