-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (34 loc) · 1.05 KB
/
index.ts
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
import { AutoRouter, IRequest, cors, error } from 'itty-router'
import { getStreamerIndex, getStreamerPhoto } from './notion'
const { preflight, corsify } = cors()
const cache = caches.default
const router = AutoRouter<IRequest, [Env]>({
before: [preflight],
catch: (err) => {
console.error(err)
return error(500)
},
})
router.get('/streamers/index.json', async ({ url }, env) => {
const response = await getStreamerIndex(url, env)
return corsify(response)
})
router.get(
'/streamers/:pageId/:imgPath+',
async ({ params: { pageId, imgPath } }, env) => {
const cacheKey = `https://photo/${pageId}/${imgPath}`
const cachedResponse = await cache.match(cacheKey)
if (cachedResponse) {
return cachedResponse
}
const imgURL = await getStreamerPhoto(pageId, env)
if (!imgURL) {
return error(404)
}
const response = await fetch(imgURL)
// @ts-expect-error https://github.com/cloudflare/workerd/issues/1383
await cache.put(cacheKey, response.clone())
return response
},
)
export default { ...router }