import {z} from 'zod' export type NotificationPolicyRoutesGroupBy = {label_names?: Array | undefined}; export type RoutesNotifierList = {notifier_slugs?: Array | undefined, repeat_interval_secs?: number | undefined, group_by?: NotificationPolicyRoutesGroupBy | undefined}; export type RoutesSeverityNotifiers = {warn?: RoutesNotifierList | undefined, critical?: RoutesNotifierList | undefined}; export type Configv1LabelMatcherMatcherType = 'EXACT' | 'REGEX'; export type Configv1LabelMatcher = {type?: Configv1LabelMatcherMatcherType | undefined, name?: string | undefined, value?: string | undefined}; export type NotificationPolicyRoutesOverride = {alert_label_matchers?: Array | undefined, notifiers?: RoutesSeverityNotifiers | undefined}; export type NotificationPolicyRoutes = {defaults?: RoutesSeverityNotifiers | undefined, overrides?: Array | undefined}; export type Configv1NotificationPolicy = {slug?: string | undefined, name?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, bucket_slug?: string | undefined, team_slug?: string | undefined, routes?: NotificationPolicyRoutes | undefined}; export type Configv1CreateNotificationPolicyRequest = {notification_policy?: Configv1NotificationPolicy | undefined, dry_run?: boolean | undefined}; export type CreateApiApiV1ConfigNotificationPoliciesArgs = {body: Configv1CreateNotificationPolicyRequest}; export type CreateApiApiV1ConfigNotificationPoliciesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const notificationPolicyRoutesGroupBy = z.object({label_names: z.array(z.string()).optional()}); export const routesNotifierList = z.object({notifier_slugs: z.array(z.string()).optional(), repeat_interval_secs: z.number().int().optional(), group_by: notificationPolicyRoutesGroupBy.optional()}); export const routesSeverityNotifiers = z.object({warn: routesNotifierList.optional(), critical: routesNotifierList.optional()}); export const configv1LabelMatcherMatcherType = z.enum(["EXACT", "REGEX"]); export const configv1LabelMatcher = z.object({type: configv1LabelMatcherMatcherType.optional(), name: z.string().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.optional(), overrides: z.array(notificationPolicyRoutesOverride).optional()}); export const configv1NotificationPolicy = z.object({slug: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), bucket_slug: z.string().optional(), team_slug: z.string().optional(), routes: notificationPolicyRoutes.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, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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()) };