-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathapp.ts
More file actions
46 lines (43 loc) · 1.13 KB
/
app.ts
File metadata and controls
46 lines (43 loc) · 1.13 KB
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
import { createRouter, Response } from 'fets';
import { MeshFetch } from '@graphql-mesh/types';
import { fetch as defaultFetch } from '@whatwg-node/fetch';
export function createApi(fetch: MeshFetch = defaultFetch) {
let todos = [];
const app = createRouter()
.route({
path: '/todos',
method: 'GET',
handler: () => Response.json(todos),
})
.route({
path: '/todo',
method: 'POST',
async handler(request) {
const reqBody = await request.json();
const todo = {
id: todos.length,
...reqBody,
};
todos.push(todo);
try {
const res = await fetch('http://127.0.0.1:4000/webhooks/todo_added', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(todo),
});
console.log('Webhook response', await res.text());
} catch (e) {
console.error('Failed to send webhook', e);
}
return Response.json(todo);
},
});
return {
app,
resetTodos() {
todos = [];
},
};
}