import {z} from 'zod' export type Configv1DerivedMetricLabelMatcherMatcherType = 'EXACT'; export type Configv1DerivedMetricLabelMatcher = {type?: Configv1DerivedMetricLabelMatcherMatcherType | undefined, name?: string | undefined, value?: string | undefined}; export type DerivedMetricSelector = {labels?: Array | undefined}; export type DerivedMetricVariable = {name?: string | undefined, default_prometheus_selector?: string | undefined}; export type DerivedMetricQuery = {prometheus_expr?: string | undefined, variables?: Array | undefined}; export type DerivedMetricSelectorQuery = {selector?: DerivedMetricSelector | undefined, query?: DerivedMetricQuery | undefined}; export type Configv1DerivedMetric = {name?: string | undefined, slug?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, metric_name?: string | undefined, description?: string | undefined, queries?: Array | undefined}; export type ConfigV1UpdateDerivedMetricBody = {derived_metric?: Configv1DerivedMetric | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigDerivedMetricsSlugArgs = {slug: string, body: ConfigV1UpdateDerivedMetricBody}; export type UpdateApiApiV1ConfigDerivedMetricsSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1DerivedMetricLabelMatcherMatcherType = z.literal("EXACT"); export const configv1DerivedMetricLabelMatcher = z.object({type: configv1DerivedMetricLabelMatcherMatcherType.optional(), name: z.string().optional(), value: z.string().optional()}); export const derivedMetricSelector = z.object({labels: z.array(configv1DerivedMetricLabelMatcher).optional()}); export const derivedMetricVariable = z.object({name: z.string().optional(), default_prometheus_selector: z.string().optional()}); export const derivedMetricQuery = z.object({prometheus_expr: z.string().optional(), variables: z.array(derivedMetricVariable).optional()}); export const derivedMetricSelectorQuery = z.object({selector: derivedMetricSelector.optional(), query: derivedMetricQuery.optional()}); export const configv1DerivedMetric = z.object({name: z.string().optional(), slug: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), metric_name: z.string().optional(), description: z.string().optional(), queries: z.array(derivedMetricSelectorQuery).optional()}); export const configv1UpdateDerivedMetricResponse = z.object({derived_metric: configv1DerivedMetric.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/derived-metrics/{slug} */ export const updateApiApiV1ConfigDerivedMetricsSlug = async (args: UpdateApiApiV1ConfigDerivedMetricsSlugArgs, options: UpdateApiApiV1ConfigDerivedMetricsSlugOptions) => { 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/derived-metrics/${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(configv1UpdateDerivedMetricResponse, await res.json()) };