Gertie2013 commited on
Commit
8e07754
·
verified ·
1 Parent(s): 3fd0bae

Create pages/api/library.ts

Browse files
Files changed (1) hide show
  1. pages/api/library.ts +26 -0
pages/api/library.ts ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { NextApiRequest, NextApiResponse } from "next";
2
+
3
+ const library: any[] = [];
4
+
5
+ export default function handler(req: NextApiRequest, res: NextApiResponse) {
6
+ res.setHeader("Content-Type", "application/json");
7
+
8
+ if (req.method === "GET") {
9
+ return res.status(200).json({ items: library });
10
+ }
11
+
12
+ if (req.method === "POST") {
13
+ try {
14
+ const item = req.body;
15
+ if (!item || !item.id) {
16
+ return res.status(400).json({ error: "Item with id is required" });
17
+ }
18
+ library.push(item);
19
+ return res.status(201).json({ ok: true, item });
20
+ } catch {
21
+ return res.status(400).json({ error: "Invalid JSON body" });
22
+ }
23
+ }
24
+
25
+ return res.status(405).json({ error: "Method not allowed" });
26
+ }