import {z} from 'zod' export type GetApiApiV1ConfigRollupRulesSlugArgs = {slug: string}; export type GetApiApiV1ConfigRollupRulesSlugOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const configv1AggregationType = z.enum(["LAST", "MIN", "MAX", "MEAN", "MEDIAN", "COUNT", "SUM", "SUMSQ", "STDEV", "P10", "P20", "P30", "P40", "P50", "P60", "P70", "P80", "P90", "P95", "P99", "P999", "P9999", "P25", "P75", "COUNT_SAMPLES", "HISTOGRAM"]); export const configv1RollupRule = z.object({add_metric_type_label: z.boolean().optional(), aggregation: configv1AggregationType.optional(), bucket_slug: z.string().optional(), created_at: z.string().optional(), drop_raw: z.boolean().optional(), expansive_match: z.boolean().optional(), filters: z.unknown()}); export const configv1ReadRollupRuleResponse = z.object({rollup_rule: configv1RollupRule.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/rollup-rules/{slug} */ export const getApiApiV1ConfigRollupRulesSlug = async (args: GetApiApiV1ConfigRollupRulesSlugArgs, options?: GetApiApiV1ConfigRollupRulesSlugOptions) => { const {baseUrl = "https://tenant.chronosphere.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/rollup-rules/${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(configv1ReadRollupRuleResponse, await res.json()) };