40 lines
No EOL
1.3 KiB
TypeScript
40 lines
No EOL
1.3 KiB
TypeScript
// app/api/notes/route.ts
|
|
// POST /api/notes — create a new note.
|
|
// Hardcoded collection = "ysterbaard_notes". Field-allowlisted.
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
import { createRecord } from "@/lib/pb";
|
|
import type { YsterbaardNote } from "@/lib/types";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const COLLECTION = "ysterbaard_notes";
|
|
const FIELD_ALLOWLIST = ["title", "content", "is_done", "done_at"] as const;
|
|
|
|
export async function POST(req: NextRequest) {
|
|
let body: Record<string, unknown>;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const title = typeof body.title === "string" ? body.title.trim() : "";
|
|
const content = typeof body.content === "string" ? body.content.trim() : "";
|
|
|
|
if (!title) {
|
|
return NextResponse.json({ error: "title is required" }, { status: 400 });
|
|
}
|
|
|
|
const sanitised: Record<string, unknown> = { title };
|
|
if (content) sanitised.content = content;
|
|
|
|
try {
|
|
const record = await createRecord<YsterbaardNote>(COLLECTION, sanitised);
|
|
return NextResponse.json(record, { status: 200 });
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : "Unknown PB error";
|
|
console.error("[notes create]", message);
|
|
return NextResponse.json({ error: message }, { status: 502 });
|
|
}
|
|
} |