import {z} from 'zod' export type Configv1RecordingRuleLabelPolicy = {add?: {[key: string]: string} | undefined}; export type Configv1RecordingRule = {slug?: string | undefined, name?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, bucket_slug?: string | undefined, label_policy?: Configv1RecordingRuleLabelPolicy | undefined, interval_secs?: number | undefined, metric_name?: string | undefined, prometheus_expr?: string | undefined, execution_group?: string | undefined}; export type ConfigV1UpdateRecordingRuleBody = {recording_rule?: Configv1RecordingRule | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigRecordingRulesSlugArgs = {slug: string, body: ConfigV1UpdateRecordingRuleBody}; export type UpdateApiApiV1ConfigRecordingRulesSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1RecordingRuleLabelPolicy = z.object({add: z.record(z.string(), z.string()).optional()}); export const configv1RecordingRule = 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(), label_policy: configv1RecordingRuleLabelPolicy.optional(), interval_secs: z.number().int().optional(), metric_name: z.string().optional(), prometheus_expr: z.string().optional(), execution_group: z.string().optional()}); export const configv1UpdateRecordingRuleResponse = z.object({recording_rule: configv1RecordingRule.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/recording-rules/{slug} */ export const updateApiApiV1ConfigRecordingRulesSlug = async (args: UpdateApiApiV1ConfigRecordingRulesSlugArgs, options: UpdateApiApiV1ConfigRecordingRulesSlugOptions) => { 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/recording-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(configv1UpdateRecordingRuleResponse, await res.json()) };