import { projectRef } from "@/lib/projects"; import { EXPORT_FORMATS, exportProject } from "@/lib/report/export.mjs"; export const dynamic = "force-dynamic"; // GET ?project=&format=toc-bbcode|toc-markdown|description|chapters[&variant=] // Plain text, so CopyButton can fetch it straight onto the clipboard. export async function GET(request: Request) { const url = new URL(request.url); const project = await projectRef(url.searchParams.get("project") ?? ""); if (!project) return new Response("no such project", { status: 404 }); const format = url.searchParams.get("format") ?? ""; if (!EXPORT_FORMATS.includes(format)) { return new Response(`format must be one of ${EXPORT_FORMATS.join(", ")}`, { status: 400 }); } const variant = url.searchParams.get("variant") || undefined; try { const r = await exportProject(project.dir, format, variant ? { variant } : {}); return new Response(r.text, { headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store", "x-offsets": r.offsets.source, }, }); } catch (e) { return new Response(e instanceof Error ? e.message : String(e), { status: 409 }); } }