New-Riffusion / pages /api /library.ts
Gertie2013's picture
Create pages/api/library.ts
8e07754 verified
Raw
History Blame Contribute Delete
733 Bytes
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" });
}