(req: NextRequest)
| 25 | }; |
| 26 | |
| 27 | export async function POST(req: NextRequest) { |
| 28 | let body: ExportRequest; |
| 29 | try { |
| 30 | body = await req.json(); |
| 31 | } catch { |
| 32 | return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); |
| 33 | } |
| 34 | |
| 35 | const { conversation, options } = body; |
| 36 | if (!conversation || !options) { |
| 37 | return NextResponse.json( |
| 38 | { error: "Missing conversation or options" }, |
| 39 | { status: 400 } |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | const { format } = options; |
| 44 | |
| 45 | if (format === "pdf") { |
| 46 | // PDF is handled client-side via window.print() |
| 47 | return NextResponse.json( |
| 48 | { error: "PDF export is handled client-side" }, |
| 49 | { status: 400 } |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | let content: string; |
| 54 | switch (format) { |
| 55 | case "markdown": |
| 56 | content = toMarkdown(conversation, options); |
| 57 | break; |
| 58 | case "json": |
| 59 | content = toJSON(conversation, options); |
| 60 | break; |
| 61 | case "html": |
| 62 | content = toHTML(conversation, options); |
| 63 | break; |
| 64 | case "plaintext": |
| 65 | content = toPlainText(conversation, options); |
| 66 | break; |
| 67 | default: |
| 68 | return NextResponse.json({ error: `Unknown format: ${format}` }, { status: 400 }); |
| 69 | } |
| 70 | |
| 71 | const slug = conversation.title |
| 72 | .toLowerCase() |
| 73 | .replace(/[^a-z0-9]+/g, "-") |
| 74 | .replace(/^-|-$/g, "") |
| 75 | .slice(0, 50); |
| 76 | const filename = `${slug || "conversation"}.${EXT[format]}`; |
| 77 | |
| 78 | return new NextResponse(content, { |
| 79 | status: 200, |
| 80 | headers: { |
| 81 | "Content-Type": MIME[format], |
| 82 | "Content-Disposition": `attachment; filename="${filename}"`, |
| 83 | }, |
| 84 | }); |
nothing calls this directly
no test coverage detected