init
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { BILL_BODY_LIMIT, readLimited } from "../../../../lib/body";
|
||||
import { BillStoreError, readBillPayload, storePublishedBill } from "../../../../lib/bills";
|
||||
import { isUuid } from "../../../../lib/ids";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type RouteContext = { params: Promise<{ token: string }> };
|
||||
|
||||
export async function GET(_request: Request, context: RouteContext) {
|
||||
const { token } = await context.params;
|
||||
if (!isUuid(token)) return Response.json({ error: "not found" }, { status: 404 });
|
||||
try {
|
||||
const payload = await readBillPayload(token);
|
||||
if (!payload) return Response.json({ error: "not found" }, { status: 404 });
|
||||
return new Response(payload, {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json; charset=utf-8" },
|
||||
});
|
||||
} catch {
|
||||
console.error("bill read failed");
|
||||
return Response.json({ error: "not found" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request, context: RouteContext) {
|
||||
const { token } = await context.params;
|
||||
if (!isUuid(token)) return Response.json({ error: "invalid token" }, { status: 400 });
|
||||
const raw = await readLimited(request, BILL_BODY_LIMIT);
|
||||
if (raw === null) return Response.json({ error: "body too large" }, { status: 413 });
|
||||
try {
|
||||
await storePublishedBill(token, raw);
|
||||
return Response.json({ ok: true }, { status: 201 });
|
||||
} catch (error) {
|
||||
if (error instanceof BillStoreError) {
|
||||
return Response.json({ error: error.message }, { status: error.status });
|
||||
}
|
||||
console.error("bill publish failed");
|
||||
return Response.json({ error: "publish failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user