import {z} from 'zod' export type GetApiApiV2OrgIdAlertsAlertIdGroupsArgs = {org_id: string, alert_id: string}; export type GetApiApiV2OrgIdAlertsAlertIdGroupsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const ackSource = z.enum(["ui", "api", "slack", "oncall", "incident"]); export const ackView = z.object({ack_source: ackSource.optional(), acked_at: z.number().int(), acked_by: z.string(), firing_started_at: z.number().int(), group_key: z.string().optional(), taken_over_from: z.string().nullable().optional()}); export const alertGroupLabel = z.object({name: z.string(), value: z.string()}); export const alertGroupResponseItem = z.object({ack: ackView.nullable().optional(), firing_started_at: z.number().int().nullable().optional(), group_key: z.string(), group_labels: z.string().nullable().optional(), labels: z.array(alertGroupLabel), last_notified_level: z.string().nullable().optional(), last_outcome: z.string().nullable().optional(), last_outcome_at: z.number().int().nullable().optional(), last_seen: z.number().int().nullable().optional(), level: z.string().nullable().optional(), level_since: z.number().int().nullable().optional(), silenced_until: z.number().int().nullable().optional()}); export const getApiApiV2OrgIdAlertsAlertIdGroupsResponse = z.object({capped: z.boolean(), group_cap: z.number().int().gte(0), groups_firing: z.number().int().nullable().optional(), groups_firing_is_lower_bound: z.boolean().nullable().optional(), groups_observed: z.number().int().nullable().optional(), groups_observed_is_lower_bound: z.boolean().nullable().optional(), list: z.array(alertGroupResponseItem)}); 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 }; /** * List a multi-alert's tracked groups * * GET /api/v2/{org_id}/alerts/{alert_id}/groups */ export const getApiApiV2OrgIdAlertsAlertIdGroups = async (args: GetApiApiV2OrgIdAlertsAlertIdGroupsArgs, options: GetApiApiV2OrgIdAlertsAlertIdGroupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/alerts/${encodeURIComponent(String(args.alert_id))}/groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV2OrgIdAlertsAlertIdGroupsResponse, await res.json()) };