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 Configv1CreateDerivedMetricRequest = {derived_metric?: Configv1DerivedMetric | undefined, dry_run?: boolean | undefined}; export type CreateApiApiV1ConfigDerivedMetricsArgs = {body: Configv1CreateDerivedMetricRequest}; export type CreateApiApiV1ConfigDerivedMetricsOptions = {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 configv1CreateDerivedMetricResponse = 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 }; /** * POST /api/v1/config/derived-metrics */ export const createApiApiV1ConfigDerivedMetrics = async (args: CreateApiApiV1ConfigDerivedMetricsArgs, options: CreateApiApiV1ConfigDerivedMetricsOptions) => { 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${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(configv1CreateDerivedMetricResponse, await res.json()) };