~repos /edge-city
git clone https://pyrossh.dev/repos/edge-city.git
edge-city is a next level meta-framework for react that runs only on edge runtimes
e3c9eca1
—
Peter John 2 years ago
add api folder
- .gitignore +1 -0
- api/health.js +3 -0
- api/todos/[id]/index.js +37 -0
- api/todos/index.js +28 -0
- routes/api/todos/index.js +0 -5
.gitignore
CHANGED
|
@@ -167,4 +167,5 @@ dist
|
|
|
167
167
|
.yarn/build-state.yml
|
|
168
168
|
.yarn/install-state.gz
|
|
169
169
|
.pnp.\*
|
|
170
|
+
.cache
|
|
170
171
|
dist
|
api/health.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export const onGet = (req) => {
|
|
2
|
+
return new Response("ok");
|
|
3
|
+
}
|
api/todos/[id]/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import tigrisDB from "./db.js";
|
|
2
|
+
|
|
3
|
+
export const todosCollection = tigrisDB.getCollection("todoItems");
|
|
4
|
+
|
|
5
|
+
export const onGet = async (req) => {
|
|
6
|
+
const { id } = req.params;
|
|
7
|
+
const item = await todosCollection.findOne({
|
|
8
|
+
filter: { id },
|
|
9
|
+
});
|
|
10
|
+
const data = JSON.stringify(item);
|
|
11
|
+
return new Response(data);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const onPut = async (req) => {
|
|
15
|
+
const updated = await todosCollection.insertOrReplaceOne(item);
|
|
16
|
+
const data = JSON.stringify(updated);
|
|
17
|
+
return new Response(data, {
|
|
18
|
+
headers: {
|
|
19
|
+
"Content-Type": "application/json",
|
|
20
|
+
},
|
|
21
|
+
status: 200,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const onDelete = async (req) => {
|
|
26
|
+
const { id } = req.params;
|
|
27
|
+
const res = await todosCollection.deleteOne({
|
|
28
|
+
filter: { id },
|
|
29
|
+
});
|
|
30
|
+
const data = JSON.stringify(res);
|
|
31
|
+
return new Response(data, {
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
},
|
|
35
|
+
status: 200,
|
|
36
|
+
});
|
|
37
|
+
}
|
api/todos/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import tigrisDB from "./db.js";
|
|
2
|
+
|
|
3
|
+
export const todosCollection = tigrisDB.getCollection("todoItems");
|
|
4
|
+
|
|
5
|
+
export const onGet = async (req) => {
|
|
6
|
+
const cursor = todosCollection.findMany({});
|
|
7
|
+
const items = await cursor.toArray();
|
|
8
|
+
const data = JSON.stringify(items);
|
|
9
|
+
return new Response(data, {
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/json",
|
|
12
|
+
},
|
|
13
|
+
status: 200,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const onPost = async (req) => {
|
|
18
|
+
const body = await req.body();
|
|
19
|
+
const item = JSON.parse(body);
|
|
20
|
+
const inserted = await todosCollection.insertOne(item);
|
|
21
|
+
const data = JSON.stringify(inserted);
|
|
22
|
+
return new Response(data, {
|
|
23
|
+
headers: {
|
|
24
|
+
"Content-Type": "application/json",
|
|
25
|
+
},
|
|
26
|
+
status: 200,
|
|
27
|
+
});
|
|
28
|
+
}
|
routes/api/todos/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { json } from "@/utils";
|
|
2
|
-
|
|
3
|
-
export default async () => {
|
|
4
|
-
return json([])
|
|
5
|
-
};
|