-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (50 loc) · 1.43 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
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400',
}
addEventListener('fetch', (event) => {
const request = event.request
if (request.method === 'OPTIONS') {
event.respondWith(handleOptions(request))
} else {
event.respondWith(handleRequest(request))
}
})
const handleOptions = async (request) => {
let headers = request.headers
if (
headers.get('Origin') !== null &&
headers.get('Access-Control-Request-Method') !== null &&
headers.get('Access-Control-Request-Headers') !== null
) {
let respHeaders = {
...corsHeaders,
'Access-Control-Allow-Headers': request.headers.get(
'Access-Control-Request-Headers',
),
}
return new Response(null, {
headers: respHeaders,
})
} else {
return new Response(null, {
headers: {
Allow: 'GET, HEAD, POST, OPTIONS',
},
})
}
}
const handleRequest = async (request) => {
const { pathname, search } = new URL(request.url)
const target = `https://opendata.ecdc.europa.eu/${pathname}${search}`
const resp = await fetch(target)
const newResponse = new Response(resp.body)
newResponse.headers.append('Content-Type', 'application/json')
newResponse.headers.append(
'Access-Control-Allow-Origin',
request.headers.get('ORIGIN'),
)
newResponse.headers.append('Vary', 'Origin')
return newResponse
}