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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { isAuthorized } from "../../../../lib/auth";
|
||||
import { pullChanges, SyncInputError } from "../../../../lib/sync";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
if (!isAuthorized(request.headers.get("authorization"))) {
|
||||
return Response.json({ error: "unauthorized" }, { status: 401 });
|
||||
}
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "invalid json" }, { status: 400 });
|
||||
}
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
return Response.json({ error: "invalid body" }, { status: 400 });
|
||||
}
|
||||
const cursors = (body as { cursors?: unknown }).cursors;
|
||||
try {
|
||||
const result = await pullChanges(cursors);
|
||||
return Response.json(result);
|
||||
} catch (error) {
|
||||
if (error instanceof SyncInputError) {
|
||||
return Response.json({ error: error.message }, { status: 400 });
|
||||
}
|
||||
console.error("sync pull failed");
|
||||
return Response.json({ error: "sync failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { isAuthorized } from "../../../../lib/auth";
|
||||
import { pushChanges, SyncInputError } from "../../../../lib/sync";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
if (!isAuthorized(request.headers.get("authorization"))) {
|
||||
return Response.json({ error: "unauthorized" }, { status: 401 });
|
||||
}
|
||||
let body: unknown;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "invalid json" }, { status: 400 });
|
||||
}
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
||||
return Response.json({ error: "invalid body" }, { status: 400 });
|
||||
}
|
||||
const tables = (body as { tables?: unknown }).tables ?? {};
|
||||
try {
|
||||
const result = await pushChanges(tables);
|
||||
return Response.json(result);
|
||||
} catch (error) {
|
||||
if (error instanceof SyncInputError) {
|
||||
return Response.json({ error: error.message }, { status: 400 });
|
||||
}
|
||||
console.error("sync push failed");
|
||||
return Response.json({ error: "sync failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user