import {z} from 'zod' export type Configv1MutingRuleLabelMatcherMatcherType = 'EXACT' | 'REGEX' | 'NOT_EXACT' | 'NOT_REGEXP'; export type Configv1MutingRuleLabelMatcher = {type?: Configv1MutingRuleLabelMatcherMatcherType | undefined, name?: string | undefined, value?: string | undefined}; export type Configv1MutingRule = {slug?: string | undefined, name?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, label_matchers?: Array | undefined, starts_at?: string | undefined, ends_at?: string | undefined, comment?: string | undefined}; export type Configv1CreateMutingRuleRequest = {muting_rule?: Configv1MutingRule | undefined, dry_run?: boolean | undefined}; export type CreateApiApiV1ConfigMutingRulesArgs = {body: Configv1CreateMutingRuleRequest}; export type CreateApiApiV1ConfigMutingRulesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1MutingRuleLabelMatcherMatcherType = z.enum(["EXACT", "REGEX", "NOT_EXACT", "NOT_REGEXP"]); export const configv1MutingRuleLabelMatcher = z.object({type: configv1MutingRuleLabelMatcherMatcherType.optional(), name: z.string().optional(), value: z.string().optional()}); export const configv1MutingRule = z.object({slug: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), label_matchers: z.array(configv1MutingRuleLabelMatcher).optional(), starts_at: z.string().optional(), ends_at: z.string().optional(), comment: z.string().optional()}); export const configv1CreateMutingRuleResponse = z.object({muting_rule: configv1MutingRule.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/muting-rules */ export const createApiApiV1ConfigMutingRules = async (args: CreateApiApiV1ConfigMutingRulesArgs, options: CreateApiApiV1ConfigMutingRulesOptions) => { 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/muting-rules${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(configv1CreateMutingRuleResponse, await res.json()) };