Spaces:
No application file
No application file
| import type { NextApiRequest, NextApiResponse } from "next"; | |
| const library: any[] = []; | |
| export default function handler(req: NextApiRequest, res: NextApiResponse) { | |
| res.setHeader("Content-Type", "application/json"); | |
| if (req.method === "GET") { | |
| return res.status(200).json({ items: library }); | |
| } | |
| if (req.method === "POST") { | |
| try { | |
| const item = req.body; | |
| if (!item || !item.id) { | |
| return res.status(400).json({ error: "Item with id is required" }); | |
| } | |
| library.push(item); | |
| return res.status(201).json({ ok: true, item }); | |
| } catch { | |
| return res.status(400).json({ error: "Invalid JSON body" }); | |
| } | |
| } | |
| return res.status(405).json({ error: "Method not allowed" }); | |
| } | |