import {z} from 'zod' export type AlertMuteRequest = {all_groups?: boolean | undefined, ends_at?: (number | null) | undefined, firing_started_at?: (number | null) | undefined, folder_id?: (string | null) | undefined, group_keys?: (Array | null) | undefined, minutes?: (number | null) | undefined, reason?: (string | null) | undefined, until_clear?: boolean | undefined}; export type CreateApiApiV2OrgIdAlertsAlertIdMuteArgs = {org_id: string, alert_id: string, folder?: string | undefined, body: AlertMuteRequest}; export type CreateApiApiV2OrgIdAlertsAlertIdMuteOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const alertGroupRef = z.object({alert_id: z.string(), group_key: z.string().optional(), group_labels: z.record(z.string(), z.string()).nullable().optional()}); export const endOnClear = z.object({alert_id: z.string(), group_key: z.string().nullable().optional()}); export const mutedPage = z.object({refused: z.string().nullable().optional(), response_id: z.string(), snoozed_until: z.number().int().nullable().optional(), team: z.string()}); export const alertMuteResponse = z.object({alert_groups: z.array(alertGroupRef).nullable().optional(), downtime_id: z.string(), end_on_clear: endOnClear.nullable().optional(), ends_at: z.number().int(), pages: z.array(mutedPage).optional(), version: 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 }; /** * Mute an alert or some of its groups * * POST /api/v2/{org_id}/alerts/{alert_id}/mute */ export const createApiApiV2OrgIdAlertsAlertIdMute = async (args: CreateApiApiV2OrgIdAlertsAlertIdMuteArgs, options: CreateApiApiV2OrgIdAlertsAlertIdMuteOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.folder != null) query.append("folder", String(args.folder)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/alerts/${encodeURIComponent(String(args.alert_id))}/mute${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(alertMuteResponse, await res.json()) };