import {z} from 'zod' export type GetApiApiV1TelemetryStatsArgs = {metricNames?: string | undefined, profile?: string | undefined, bypassCache?: boolean | undefined}; export type GetApiApiV1TelemetryStatsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const metricValue = z.object({name: z.string().optional(), description: z.string().optional(), category: z.enum(["DIFFERENTIATING_FEATURES", "PAID_FEATURES", "PLATFORM_SPECIFIC_CUSTOMIZATION", "SOPHISTICATED_CONTENT_ARCHITECTURE", "POSITIVE_USER_EXPERIENCE", "PLATFORM_SPECIFIC_DEVELOPMENT", "RECENT_ACTIVITY", "HISTORY_LEGACY_DEPRECATED_FEATURES"]).optional(), feature: z.enum(["CONTENTLETS", "LANGUAGES", "CONTENT_TYPES", "SITES", "URL_MAPS", "WORKFLOW", "USERS", "SITE_SEARCH", "IMAGE_API", "LAYOUT", "CONTENT_TYPE_FIELDS", "AI", "EXPERIMENTS"]).optional(), value: z.object({}).optional()}); export const metric = z.object({name: z.string().optional(), description: z.string().optional(), category: z.enum(["DIFFERENTIATING_FEATURES", "PAID_FEATURES", "PLATFORM_SPECIFIC_CUSTOMIZATION", "SOPHISTICATED_CONTENT_ARCHITECTURE", "POSITIVE_USER_EXPERIENCE", "PLATFORM_SPECIFIC_DEVELOPMENT", "RECENT_ACTIVITY", "HISTORY_LEGACY_DEPRECATED_FEATURES"]).optional(), feature: z.enum(["CONTENTLETS", "LANGUAGES", "CONTENT_TYPES", "SITES", "URL_MAPS", "WORKFLOW", "USERS", "SITE_SEARCH", "IMAGE_API", "LAYOUT", "CONTENT_TYPE_FIELDS", "AI", "EXPERIMENTS"]).optional()}); export const metricCalculationError = z.object({name: z.string().optional(), description: z.string().optional(), category: z.enum(["DIFFERENTIATING_FEATURES", "PAID_FEATURES", "PLATFORM_SPECIFIC_CUSTOMIZATION", "SOPHISTICATED_CONTENT_ARCHITECTURE", "POSITIVE_USER_EXPERIENCE", "PLATFORM_SPECIFIC_DEVELOPMENT", "RECENT_ACTIVITY", "HISTORY_LEGACY_DEPRECATED_FEATURES"]).optional(), feature: z.enum(["CONTENTLETS", "LANGUAGES", "CONTENT_TYPES", "SITES", "URL_MAPS", "WORKFLOW", "USERS", "SITE_SEARCH", "IMAGE_API", "LAYOUT", "CONTENT_TYPE_FIELDS", "AI", "EXPERIMENTS"]).optional(), error: z.string().optional(), type: metric.optional()}); export const metricTiming = z.object({metricName: z.string().optional(), durationMs: z.number().int().optional(), cacheHit: z.boolean().optional(), computationMs: z.number().int().optional(), timedOut: z.boolean().optional(), slow: z.boolean().optional()}); export const metricsSnapshot = z.object({stats: z.array(metricValue).optional(), errors: z.array(metricCalculationError).optional(), timings: z.array(metricTiming).optional(), notNumericMetricValues: z.array(metricValue).optional()}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityMetricsSnapshotView = z.object({errors: z.array(errorEntity).optional(), entity: metricsSnapshot.optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 }; /** * Retrieves dotCMS usage data * * GET /api/v1/telemetry/stats */ export const getApiApiV1TelemetryStats = async (args: GetApiApiV1TelemetryStatsArgs, options: GetApiApiV1TelemetryStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metricNames != null) query.append("metricNames", String(args.metricNames)) if (args.profile != null) query.append("profile", String(args.profile)) if (args.bypassCache != null) query.append("bypassCache", String(args.bypassCache)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/telemetry/stats${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(responseEntityMetricsSnapshotView, await res.json()) };