import { invalidateProjects, projectRef } from "@/lib/projects"; import { readManifest } from "@/lib/projects/report.mjs"; import { diffManifests } from "@/lib/report/manifest-diff.mjs"; import { createSnapshot, listSnapshots, readSnapshot } from "@/lib/report/snapshots.mjs"; export const dynamic = "force-dynamic"; // GET ?project= every snapshot, newest first // GET ?project=&diff= that snapshot diffed against the current manifest // POST { project, label? } copy the manifest into revisions/ export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); const headers = { "cache-control": "no-store" }; const rel = url.searchParams.get("diff"); if (rel) { try { const [before, after] = await Promise.all([readSnapshot(project.dir, rel), readManifest(project.dir)]); return Response.json({ snapshot: rel, ...diffManifests(before, after) }, { headers }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } } return Response.json({ snapshots: await listSnapshots(project.dir) }, { headers }); } export async function POST(request: Request) { const body = (await request.json().catch(() => ({}))) as { project?: string; label?: string | null }; const project = await projectRef(String(body.project ?? "")); if (!project) return Response.json({ error: "no such project" }, { status: 404 }); try { const r = await createSnapshot(project.dir, { label: body.label ? String(body.label) : null }); invalidateProjects(); return Response.json({ ok: true, ...r }, { headers: { "cache-control": "no-store" } }); } catch (e) { return Response.json({ error: e instanceof Error ? e.message : String(e) }, { status: 400 }); } }