import { withUser } from "@/lib/api"; import { usageRecordsForExport, usageCsv, usageQueryFromUrl } from "@/lib/usage/service"; export const dynamic = "force-dynamic"; /** GET /api/usage/export.csv? → `text/csv` attachment of the raw usage records (max 50 000 rows). */ export const GET = withUser(async ({ req, user }) => { const { range, rows } = await usageRecordsForExport(user.id, usageQueryFromUrl(req.url)); const from = range.from ? range.from.toISOString().slice(0, 10) : "all"; const to = new Date(range.to.getTime() - 1).toISOString().slice(0, 10); const body = usageCsv(rows); return new Response(body, { status: 200, headers: { "Content-Type": "text/csv; charset=utf-8", "Content-Disposition": `attachment; filename="polyllm-usage-${from}_${to}.csv"`, "Cache-Control": "no-store", "X-Row-Count": String(rows.length), }, }); });