import {z} from 'zod' export type GetApiSnapshotsSnapshotIdHealthFilesBreakdownArgs = {snapshot_id: string, path: string, scope?: string | undefined, counts?: string | undefined}; export type GetApiSnapshotsSnapshotIdHealthFilesBreakdownOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const healthBreakdownCategory = z.object({category: z.string(), cap: z.number(), raw_deduction: z.number(), applied_deduction: z.number(), capped: z.boolean().optional(), finding_count: z.number().int().optional()}); export const biomarkerFinding = z.object({id: z.string(), biomarker_type: z.string(), severity: z.string(), file_path: z.string(), function_name: z.string().nullable().optional(), line_start: z.number().int().nullable().optional(), line_end: z.number().int().nullable().optional(), details: z.record(z.string(), z.unknown()).optional(), health_impact: z.number().optional(), reason: z.string().optional(), status: z.string().optional(), suggestion: z.string().nullable().optional(), dimension: z.string().optional(), symbol_id: z.string().nullable().optional(), is_test: z.boolean().optional(), verification: z.string().nullable().optional()}); export const healthFileBreakdownResponse = z.object({file_path: z.string(), score: z.number(), structure_deduction: z.number().nullable().optional(), history_deduction: z.number().nullable().optional(), unclamped_score: z.number().nullable().optional(), band: z.string().nullable().optional(), max_ccn: z.number().int().nullable().optional(), max_nesting: z.number().int().nullable().optional(), nloc: z.number().int().nullable().optional(), duplication_pct: z.number().nullable().optional(), has_test_file: z.boolean().nullable().optional(), line_coverage_pct: z.number().nullable().optional(), branch_coverage_pct: z.number().nullable().optional(), module: z.string().nullable().optional(), defect_score: z.number().nullable().optional(), maintainability_score: z.number().nullable().optional(), performance_score: z.number().nullable().optional(), total_deduction: z.number().nullable().optional(), categories: z.array(healthBreakdownCategory).optional(), deductions_by_category: z.record(z.string(), z.number()).optional(), findings: z.array(biomarkerFinding).optional(), suggestion: z.string().nullable().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 File Breakdown * * GET /snapshots/{snapshot_id}/health/files/breakdown */ export const getApiSnapshotsSnapshotIdHealthFilesBreakdown = async (args: GetApiSnapshotsSnapshotIdHealthFilesBreakdownArgs, options: GetApiSnapshotsSnapshotIdHealthFilesBreakdownOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.path != null) query.append("path", String(args.path)) 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/files/breakdown${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(healthFileBreakdownResponse, await res.json()) };