import {z} from 'zod' export type GetApiApiV1ConfigDerivedLabelsSlugArgs = {slug: string}; export type GetApiApiV1ConfigDerivedLabelsSlugOptions = {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 configv1ReadDerivedLabelResponse = 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 }; /** * GET /api/v1/config/derived-labels/{slug} */ export const getApiApiV1ConfigDerivedLabelsSlug = async (args: GetApiApiV1ConfigDerivedLabelsSlugArgs, options: GetApiApiV1ConfigDerivedLabelsSlugOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(configv1ReadDerivedLabelResponse, await res.json()) };