-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy path[...slug].ts
More file actions
87 lines (83 loc) · 1.54 KB
/
Copy path[...slug].ts
File metadata and controls
87 lines (83 loc) · 1.54 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
import { createRouter, FromSchema, Response } from 'fets';
const TODO_SCHEMA = {
type: 'object',
properties: {
id: {
type: 'string',
},
content: {
type: 'string',
},
},
required: ['id', 'content'],
additionalProperties: false,
} as const;
export type Todo = FromSchema<typeof TODO_SCHEMA>;
const todos: Todo[] = [
{
id: '1',
content: 'Buy milk',
},
{
id: '2',
content: 'Buy eggs',
},
{
id: '3',
content: 'Buy bread',
},
];
export default createRouter({
swaggerUI: {
endpoint: '/api/docs',
},
openAPI: {
endpoint: '/api/openapi.json',
},
plugins: [],
})
.route({
method: 'GET',
path: '/api/todos',
schemas: {
responses: {
200: {
type: 'array',
items: TODO_SCHEMA,
},
},
} as const,
handler: () => Response.json(todos),
})
.route({
method: 'POST',
path: '/api/add-todo',
schemas: {
request: {
json: {
type: 'object',
properties: {
content: {
type: 'string',
},
},
required: ['content'],
additionalProperties: false,
},
},
responses: {
201: TODO_SCHEMA,
},
} as const,
handler: async req => {
const input = await req.json();
const todo = {
id: Math.random().toString(36).substring(7),
content: input.content,
};
todos.push(todo);
return Response.json(todo, {
status: 201,
});
},
});