-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
37 lines (32 loc) · 913 Bytes
/
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
import { Router } from 'itty-router';
import { getCoordinates } from './src/coordinates';
const router = Router();
router.get('/coordinates', async (request) => {
const json = await getCoordinates(request);
if (json === null) {
return new Response(JSON.stringify({ error: 'Bad Request' }), {
status: 400,
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
}
return new Response(json, {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
});
router
.get('/redirect', async (request) => {
let json = await getCoordinates(request);
json = JSON.parse(json);
const link = json.url.geo;
return Response.redirect(link);
})
.get('*', async (request) => {
return new Response(JSON.stringify({ error: 'hey' }));
});
addEventListener('fetch', (e) => {
e.respondWith(router.handle(e.request));
});