route.ts 649 B

12345678910111213141516171819202122232425262728
  1. import { getWidgets, createWidget } from "@/lib/db/store";
  2. export async function GET() {
  3. try {
  4. const widgetList = await getWidgets();
  5. return Response.json({ data: widgetList, total: widgetList.length });
  6. } catch {
  7. return Response.json({ data: [], total: 0 });
  8. }
  9. }
  10. export async function POST(req: Request) {
  11. const body = await req.json();
  12. if (!body.prompt || !body.spec) {
  13. return Response.json(
  14. { error: "prompt and spec are required" },
  15. { status: 400 },
  16. );
  17. }
  18. const widget = await createWidget({
  19. prompt: body.prompt,
  20. spec: body.spec,
  21. });
  22. return Response.json(widget, { status: 201 });
  23. }