import {z} from 'zod' export type GetApiSnapshotsSnapshotIdHealthTrendArgs = {snapshot_id: string, limit?: number | undefined, scope?: string | undefined, counts?: string | undefined}; export type GetApiSnapshotsSnapshotIdHealthTrendOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const healthTrendPoint = z.object({taken_at: z.string().nullable().optional(), hotspot_health: z.number().nullable().optional(), average_health: z.number(), worst_performer_path: z.string().nullable().optional(), worst_performer_score: z.number().nullable().optional(), structure_average: z.number().nullable().optional(), history_average: z.number().nullable().optional(), maintainability_average: z.number().nullable().optional(), code_shape_average: z.number().nullable().optional(), production_code_shape_average: z.number().nullable().optional()}); export const healthTrendSummary = z.object({current_hotspot_health: z.number().nullable().optional(), current_average_health: z.number().optional(), previous_hotspot_health: z.number().nullable().optional(), previous_average_health: z.number().nullable().optional(), hotspot_delta: z.number().nullable().optional(), average_delta: z.number().nullable().optional(), current_structure_deduction: z.number().nullable().optional(), current_history_deduction: z.number().nullable().optional()}); export const healthTrendAlert = z.object({kind: z.string(), metric: z.string(), current: z.number(), baseline: z.number().nullable().optional(), delta: z.number(), message: z.string(), driver: z.string().nullable().optional(), structure_delta: z.number().nullable().optional(), history_delta: z.number().nullable().optional()}); export const healthTrendResponse = z.object({points: z.array(healthTrendPoint).optional(), history: z.array(healthTrendPoint).optional(), summary: healthTrendSummary.nullable().optional(), alerts: z.array(healthTrendAlert).optional(), file_deltas: z.array(z.record(z.string(), z.unknown())).optional(), file_deltas_total: z.number().int().optional(), snapshot_count: z.number().int().optional(), scope: z.string().optional(), counts: z.string().optional(), unscored_snapshots: z.number().int().optional(), history_drag: z.boolean().optional(), declining: z.boolean().optional(), predicted_decline: z.boolean().optional(), state: z.string().optional(), reason: z.string().nullable().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 }; /** * Health Trend * * GET /snapshots/{snapshot_id}/health/trend */ export const getApiSnapshotsSnapshotIdHealthTrend = async (args: GetApiSnapshotsSnapshotIdHealthTrendArgs, options: GetApiSnapshotsSnapshotIdHealthTrendOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.counts != null) query.append("counts", String(args.counts)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/snapshots/${encodeURIComponent(String(args.snapshot_id))}/health/trend${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(healthTrendResponse, await res.json()) };