-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
38 lines (33 loc) · 999 Bytes
/
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
import querystring from 'querystring'
import getMeta from './lib'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const { url, pretty, format = pretty !== undefined } = querystring.parse(
(new URL(request.url).search || '').substring(1),
)
if (url) {
try {
const result = await getMeta(url, {
lang: request.headers.get('accept-language'),
})
return new Response(JSON.stringify(result, null, format ? 2 : 0), {
headers: {
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET'
},
})
} catch ({ message, status }) {
return new Response(message, { status })
}
}
return new Response(
'url is required parameter. e.g try /?url=http://github.com&pretty',
{
status: 422,
headers: { 'content-type': 'application/json' },
},
)
}