-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (102 loc) · 2.53 KB
/
Copy pathindex.ts
File metadata and controls
107 lines (102 loc) · 2.53 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { createServer } from 'http';
import { createRouter, Response } from 'fets';
import { z } from 'zod';
const TodoSchema = z.object({
id: z.string(),
content: z.string(),
});
type Todo = z.infer<typeof TodoSchema>;
const todos: Todo[] = [];
export const router = createRouter()
.route({
description: 'Get all todos',
method: 'GET',
path: '/todos',
handler: () => Response.json(todos),
})
.route({
description: 'Get a todo',
method: 'GET',
path: '/todo/:id',
schemas: {
request: {
params: z.object({
id: z.string(),
}),
},
},
handler: async request => {
const { id } = request.params;
const todo = todos.find(todo => todo.id === id);
if (!todo) {
return Response.json(
{
message: `Todo with id ${id} not found`,
},
{
status: 404,
},
);
}
return Response.json(todo);
},
})
.route({
description: 'Add a todo',
method: 'PUT',
path: '/todo',
schemas: {
request: {
json: z.object({
content: z.string(),
}),
},
},
handler: async request => {
const input = await request.json();
const todo: Todo = {
id: crypto.randomUUID(),
content: input.content,
};
todos.push(todo);
return Response.json(todo);
},
})
.route({
description: 'Delete a todo',
method: 'DELETE',
path: '/todo/:id',
schemas: {
request: {
params: z.object({
id: z.string(),
}),
},
},
handler: async request => {
const { id } = request.params;
const index = todos.findIndex(todo => todo.id === id);
if (index === -1) {
return Response.json(
{ error: 'not found' },
{
status: 404,
},
);
}
const todo = todos[index];
todos.splice(index, 1);
return Response.json({
id: todo.id,
});
},
});
// TODO: Type 'IncomingMessage' is not assignable to type 'NodeRequest' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
// Types of property 'url' are incompatible.
// Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
createServer(router).listen(3000, () => {
console.log('SwaggerUI is served at http://localhost:3000/docs');
});