import {z} from 'zod' export type GetApiSnapshotsSnapshotIdHealthRefactoringPlansArgs = {snapshot_id: string, type?: (string | null) | undefined, min_confidence?: (string | null) | undefined}; export type GetApiSnapshotsSnapshotIdHealthRefactoringPlansOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const refactoringTypeCount = z.object({type: z.string(), count: z.number().int()}); export const refactoringSummary = z.object({total: z.number().int(), by_type: z.array(refactoringTypeCount).optional(), files_total: z.number().int().nullable().optional(), structural_total: z.number().int().nullable().optional(), performance_total: z.number().int().nullable().optional(), small_effort_total: z.number().int().nullable().optional(), health_recovery_total: z.number().int().nullable().optional(), negligible_health_total: z.number().int().nullable().optional(), best_health_gain: z.number().nullable().optional()}); export const refactoringPlan = z.object({id: z.string(), refactoring_type: z.string(), file_path: z.string(), target_symbol: z.string(), line_start: z.number().int().nullable().optional(), line_end: z.number().int().nullable().optional(), plan: z.record(z.string(), z.unknown()).optional(), evidence: z.record(z.string(), z.unknown()).optional(), impact_delta: z.number().optional(), effort_bucket: z.string().optional(), blast_radius: z.record(z.string(), z.unknown()).optional(), confidence: z.string().optional(), source_biomarker: z.string().optional(), rank_score: z.number().optional(), dependents: z.number().int().optional(), file_nloc: z.number().int().optional(), benefit: z.number().optional(), leverage: z.number().optional(), cost: z.number().optional(), risk: z.number().optional(), file_weighted_deficit: z.number().int().optional(), validation: z.record(z.string(), z.unknown()).optional()}); export const refactoringPlansResponse = z.object({summary: refactoringSummary.optional(), plans: z.array(refactoringPlan).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 }; /** * Refactoring Plans * * GET /snapshots/{snapshot_id}/health/refactoring-plans */ export const getApiSnapshotsSnapshotIdHealthRefactoringPlans = async (args: GetApiSnapshotsSnapshotIdHealthRefactoringPlansArgs, options: GetApiSnapshotsSnapshotIdHealthRefactoringPlansOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.min_confidence != null) query.append("min_confidence", String(args.min_confidence)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/snapshots/${encodeURIComponent(String(args.snapshot_id))}/health/refactoring-plans${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(refactoringPlansResponse, await res.json()) };