init
This commit is contained in:
@@ -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