import {z} from 'zod' export type ApiSilencingRule = {account_id?: string | undefined, alert_contexts?: Array | undefined, alert_instances?: Array | undefined, alert_names?: Array | undefined, alert_roles?: Array | undefined, delete_on_expiry?: boolean | undefined, disabled?: boolean | undefined, host_labels?: {[key: string]: string} | undefined, id?: string | undefined, integration_ids?: Array | undefined, lasts_until?: string | undefined, name?: string | undefined, node_ids?: Array | undefined, notification_options?: (Array<'CLEAR' | 'WARNING' | 'CRITICAL' | 'REACHABLE' | 'UNREACHABLE'>) | undefined, read_only?: boolean | undefined, room_ids?: Array | undefined, rrule?: string | undefined, starts_at?: string | undefined, state?: ('INACTIVE' | 'ACTIVE' | 'SCHEDULED') | undefined, timezone?: string | undefined}; export type CreateApiApiV2SpacesSpaceIDNotificationsSilencingRuleArgs = {spaceID: string, body: ApiSilencingRule}; export type CreateApiApiV2SpacesSpaceIDNotificationsSilencingRuleOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const apiCreator = z.object({avatar_url: z.string().optional(), email: z.string().optional(), id: z.string().optional(), name: z.string().optional()}); export const apiSilencingRuleRes = z.object({account_id: z.string().optional(), alert_contexts: z.array(z.string()).optional(), alert_instances: z.array(z.string()).optional(), alert_names: z.array(z.string()).optional(), alert_roles: z.array(z.string()).optional(), created_at: z.string().optional(), creator: apiCreator.optional(), delete_on_expiry: z.boolean().optional(), disabled: z.boolean().optional(), host_labels: z.record(z.string(), z.string()).optional(), id: z.string().optional(), integration_ids: z.array(z.string()).optional(), lasts_until: z.string().optional(), name: z.string().optional(), node_ids: z.array(z.string()).optional(), notification_options: z.array(z.enum(["CLEAR", "WARNING", "CRITICAL", "REACHABLE", "UNREACHABLE"])).optional(), read_only: z.boolean().optional(), room_ids: z.array(z.string()).optional(), rrule: z.string().optional(), starts_at: z.string().optional(), state: z.enum(["INACTIVE", "ACTIVE", "SCHEDULED"]).optional(), timezone: z.string().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 }; /** * Create a new Alert Notification Silencing Rule * * POST /api/v2/spaces/{spaceID}/notifications/silencing/rule */ export const createApiApiV2SpacesSpaceIDNotificationsSilencingRule = async (args: CreateApiApiV2SpacesSpaceIDNotificationsSilencingRuleArgs, options: CreateApiApiV2SpacesSpaceIDNotificationsSilencingRuleOptions) => { const {baseUrl, 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) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/spaces/${encodeURIComponent(String(args.spaceID))}/notifications/silencing/rule${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(apiSilencingRuleRes, await res.json()) };