31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
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 });
|
|
}
|
|
}
|