-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (74 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { Router } from 'itty-router'
import { customAlphabet } from 'nanoid/non-secure'
const nanoid = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz', 10)
// Create a new router
const router = Router()
router.get("/items", async () => {
const list = await kv.list()
console.log(JSON.stringify(list))
let postPromises = list.keys.map(key => kv.get(key.name))
let getPosts = await Promise.all(postPromises)
list.keys.forEach((element, index) => {
getPosts[index] = JSON.parse(getPosts[index])
getPosts[index].id = element.name
})
return new Response(JSON.stringify(getPosts), {
headers: {
'content-type': 'application/json;charset=UTF-8',
'access-control-allow-origin': '*'
}
})
})
router.post("/additem", async request => {
let post = await request.json()
let id
if (post.id) {
id = post.id
delete post.id
} else {
id = nanoid()
}
let serializedPost = JSON.stringify(post)
await kv.put(id, serializedPost)
post.id = id
return new Response(JSON.stringify(post), {
headers: {
'access-control-allow-origin': '*'
}
})
})
router.delete("/deleteitem", async request => {
let post = await request.json()
await kv.delete(post.id)
return new Response('success', {
headers: {
'access-control-allow-origin': '*'
}
})
})
router.options("/additem", () => new Response('Success', {
headers: {
'access-control-allow-origin': '*',
'access-control-allow-headers': '*'
}
}))
router.options("/deleteitem", () => new Response('Success', {
headers: {
'access-control-allow-origin': '*',
'access-control-allow-headers': '*',
'access-control-allow-methods': 'DELETE'
}
}))
/*
This is the last route we define, it will match anything that hasn't hit a route we've defined
above, therefore it's useful as a 404 (and avoids us hitting worker exceptions, so make sure to include it!).
Visit any page that doesn't exist (e.g. /foobar) to see it in action.
*/
router.all("*", () => new Response("404, not found!", { status: 404 }))
/*
This snippet ties our worker to the router we deifned above, all incoming requests
are passed to the router where your routes are called and the response is sent.
*/
addEventListener('fetch', (e) => {
e.respondWith(router.handle(e.request))
})