import {z} from 'zod' export type Configv1LabelFilter = {name?: string | undefined, value_glob?: string | undefined}; export type ConstructedLabelValueDefinition = {value?: string | undefined, filters?: Array | undefined}; export type MetricLabelConstructedLabel = {value_definitions?: Array | undefined}; export type MappingLabelValueMapping = {source_value_globs?: Array | undefined, target_value?: string | undefined}; export type MappingLabelNameMapping = {filters?: Array | undefined, source_label?: string | undefined, value_mappings?: Array | undefined}; export type MetricLabelMappingLabel = {name_mappings?: Array | undefined, value_mappings?: Array | undefined}; export type DerivedLabelMetricLabel = {constructed_label?: MetricLabelConstructedLabel | undefined, mapping_label?: MetricLabelMappingLabel | undefined}; export type Configv1DerivedLabelLabelPolicy = 'KEEP' | 'OVERRIDE'; export type Configv1DerivedLabel = {name?: string | undefined, slug?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, label_name?: string | undefined, description?: string | undefined, metric_label?: DerivedLabelMetricLabel | undefined, existing_label_policy?: Configv1DerivedLabelLabelPolicy | undefined}; export type ConfigV1UpdateDerivedLabelBody = {derived_label?: Configv1DerivedLabel | undefined, create_if_missing?: boolean | undefined, dry_run?: boolean | undefined}; export type UpdateApiApiV1ConfigDerivedLabelsSlugArgs = {slug: string, body: ConfigV1UpdateDerivedLabelBody}; export type UpdateApiApiV1ConfigDerivedLabelsSlugOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1LabelFilter = z.object({name: z.string().optional(), value_glob: z.string().optional()}); export const constructedLabelValueDefinition = z.object({value: z.string().optional(), filters: z.array(configv1LabelFilter).optional()}); export const metricLabelConstructedLabel = z.object({value_definitions: z.array(constructedLabelValueDefinition).optional()}); export const mappingLabelValueMapping = z.object({source_value_globs: z.array(z.string()).optional(), target_value: z.string().optional()}); export const mappingLabelNameMapping = z.object({filters: z.array(configv1LabelFilter).optional(), source_label: z.string().optional(), value_mappings: z.array(mappingLabelValueMapping).optional()}); export const metricLabelMappingLabel = z.object({name_mappings: z.array(mappingLabelNameMapping).optional(), value_mappings: z.array(mappingLabelValueMapping).optional()}); export const derivedLabelMetricLabel = z.object({constructed_label: metricLabelConstructedLabel.optional(), mapping_label: metricLabelMappingLabel.optional()}); export const configv1DerivedLabelLabelPolicy = z.enum(["KEEP", "OVERRIDE"]); export const configv1DerivedLabel = z.object({name: z.string().optional(), slug: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), label_name: z.string().optional(), description: z.string().optional(), metric_label: derivedLabelMetricLabel.optional(), existing_label_policy: configv1DerivedLabelLabelPolicy.optional()}); export const configv1UpdateDerivedLabelResponse = z.object({derived_label: configv1DerivedLabel.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-labels/{slug} */ export const updateApiApiV1ConfigDerivedLabelsSlug = async (args: UpdateApiApiV1ConfigDerivedLabelsSlugArgs, options: UpdateApiApiV1ConfigDerivedLabelsSlugOptions) => { 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-labels/${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(configv1UpdateDerivedLabelResponse, await res.json()) };