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 ConfigV1UpdateMutingRuleBody = {muting_rule?: Configv1MutingRule | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigMutingRulesSlugArgs = {slug: string, body: ConfigV1UpdateMutingRuleBody}; export type UpdateApiApiV1ConfigMutingRulesSlugOptions = {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 configv1UpdateMutingRuleResponse = 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 }; /** * PUT /api/v1/config/muting-rules/{slug} */ export const updateApiApiV1ConfigMutingRulesSlug = async (args: UpdateApiApiV1ConfigMutingRulesSlugArgs, options: UpdateApiApiV1ConfigMutingRulesSlugOptions) => { 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/${encodeURIComponent(String(args.slug))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(configv1UpdateMutingRuleResponse, await res.json()) };