import {z} from 'zod' export type DestinationEmailTarget = {addresses?: Array | undefined}; export type DestinationOpsGenieTarget = {external_connection_slug?: string | undefined}; export type DestinationPagerdutyTarget = {external_connection_slug?: string | undefined}; export type DestinationSlackTarget = {channels?: Array | undefined, external_connection_slug?: string | undefined}; export type DestinationVictorOpsTarget = {external_connection_slug?: string | undefined, routing_keys?: Array | undefined}; export type WebhookTargetQueryParameter = {key?: string | undefined, value?: string | undefined}; export type DestinationWebhookTarget = {external_connection_slug?: string | undefined, query_parameters?: Array | undefined}; export type RoutesDestination = {disable_resolves?: boolean | undefined, email?: DestinationEmailTarget | undefined, ops_genie?: DestinationOpsGenieTarget | undefined, pagerduty?: DestinationPagerdutyTarget | undefined, slack?: DestinationSlackTarget | undefined, victor_ops?: DestinationVictorOpsTarget | undefined, webhook?: DestinationWebhookTarget | undefined}; export type RoutesNotifierList = {destinations?: Array | undefined, disable_repeat?: boolean | undefined, notifier_slugs?: Array | undefined, repeat_interval_secs?: number | undefined}; export type RoutesSeverityNotifiers = {critical?: RoutesNotifierList | undefined, warn?: RoutesNotifierList | undefined}; export type Configv1LabelMatcherMatcherType = 'EXACT' | 'REGEX'; export type Configv1LabelMatcher = {name?: string | undefined, type?: Configv1LabelMatcherMatcherType | undefined, value?: string | undefined}; export type NotificationPolicyRoutesOverride = {alert_label_matchers?: Array | undefined, notifiers?: RoutesSeverityNotifiers | undefined}; export type NotificationPolicyRoutes = {defaults: RoutesSeverityNotifiers, overrides?: Array | undefined}; export type Configv1NotificationPolicy = {bucket_slug?: string | undefined, created_at?: string | undefined, name: string, routes?: NotificationPolicyRoutes | undefined, slug?: string | undefined, team_slug?: string | undefined, updated_at?: string | undefined}; export type Configv1CreateNotificationPolicyRequest = {dry_run?: boolean | undefined, notification_policy?: Configv1NotificationPolicy | undefined}; export type CreateApiApiV1ConfigNotificationPoliciesArgs = {body: Configv1CreateNotificationPolicyRequest}; export type CreateApiApiV1ConfigNotificationPoliciesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const destinationEmailTarget = z.object({addresses: z.array(z.string()).optional()}); export const destinationOpsGenieTarget = z.object({external_connection_slug: z.string().optional()}); export const destinationPagerdutyTarget = z.object({external_connection_slug: z.string().optional()}); export const destinationSlackTarget = z.object({channels: z.array(z.string()).optional(), external_connection_slug: z.string().optional()}); export const destinationVictorOpsTarget = z.object({external_connection_slug: z.string().optional(), routing_keys: z.array(z.string()).optional()}); export const webhookTargetQueryParameter = z.object({key: z.string().optional(), value: z.string().optional()}); export const destinationWebhookTarget = z.object({external_connection_slug: z.string().optional(), query_parameters: z.array(webhookTargetQueryParameter).optional()}); export const routesDestination = z.object({disable_resolves: z.boolean().optional(), email: destinationEmailTarget.optional(), ops_genie: destinationOpsGenieTarget.optional(), pagerduty: destinationPagerdutyTarget.optional(), slack: destinationSlackTarget.optional(), victor_ops: destinationVictorOpsTarget.optional(), webhook: destinationWebhookTarget.optional()}); export const routesNotifierList = z.object({destinations: z.array(routesDestination).optional(), disable_repeat: z.boolean().optional(), notifier_slugs: z.array(z.string()).optional(), repeat_interval_secs: z.number().int().optional()}); export const routesSeverityNotifiers = z.object({critical: routesNotifierList.optional(), warn: routesNotifierList.optional()}); export const configv1LabelMatcherMatcherType = z.enum(["EXACT", "REGEX"]); export const configv1LabelMatcher = z.object({name: z.string().optional(), type: configv1LabelMatcherMatcherType.optional(), value: z.string().optional()}); export const notificationPolicyRoutesOverride = z.object({alert_label_matchers: z.array(configv1LabelMatcher).optional(), notifiers: routesSeverityNotifiers.optional()}); export const notificationPolicyRoutes = z.object({defaults: routesSeverityNotifiers, overrides: z.array(notificationPolicyRoutesOverride).optional()}); export const configv1NotificationPolicy = z.object({bucket_slug: z.string().optional(), created_at: z.string().optional(), name: z.string(), routes: notificationPolicyRoutes.optional(), slug: z.string().optional(), team_slug: z.string().optional(), updated_at: z.string().optional()}); export const configv1CreateNotificationPolicyResponse = z.object({notification_policy: configv1NotificationPolicy.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 }; /** * POST /api/v1/config/notification-policies */ export const createApiApiV1ConfigNotificationPolicies = async (args: CreateApiApiV1ConfigNotificationPoliciesArgs, options?: CreateApiApiV1ConfigNotificationPoliciesOptions) => { const {baseUrl = "https://tenant.chronosphere.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/notification-policies${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(configv1CreateNotificationPolicyResponse, await res.json()) };