-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.fixtures.ts
139 lines (125 loc) · 3.17 KB
/
server.fixtures.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import fastify, { FastifyInstance, FastifyServerOptions } from "fastify";
import { BadRequest, NotFound } from "http-errors";
import { register } from "..";
import { RegisterOptions } from "../FastifyZod";
import { models, TodoItems } from "./models.fixtures";
export const swaggerOptions: RegisterOptions<typeof models>[`swaggerOptions`] =
{
swagger: {
info: {
title: `Fastify Zod Test Server`,
description: `Test Server for Fastify Zod`,
version: `0.0.0`,
},
},
};
export const openApiOptions: RegisterOptions<typeof models>[`swaggerOptions`] =
{
openapi: {
info: {
title: `Fastify Zod Test Server`,
description: `Test Server for Fastify Zod`,
version: `0.0.0`,
},
},
};
export const swaggerUiOptions: RegisterOptions<
typeof models
>[`swaggerUiOptions`] = {
staticCSP: true,
};
export const createTestServer = async (
fastifyOptions: FastifyServerOptions,
registerOptions: RegisterOptions<typeof models>,
): Promise<FastifyInstance> => {
const f = await register(fastify(fastifyOptions), registerOptions);
const state: TodoItems = {
todoItems: [],
};
f.zod.get(
`/item`,
{
operationId: `getTodoItems`,
reply: {
description: `The list of Todo Items`,
key: `TodoItems`,
},
},
async () => state,
);
f.zod.get(
`/item/grouped-by-status`,
{
operationId: `getTodoItemsGroupedByStatus`,
reply: `TodoItemsGroupedByStatus`,
},
async () => ({
done: state.todoItems.filter((item) => item.state === `done`),
inProgress: state.todoItems.filter(
(item) => item.state === `in progress`,
),
todo: state.todoItems.filter((item) => item.state === `todo`),
}),
);
f.zod.post(
`/item`,
{
operationId: `postTodoItem`,
body: `TodoItem`,
reply: `TodoItems`,
},
async ({ body: nextItem }) => {
if (state.todoItems.some((prevItem) => prevItem.id === nextItem.id)) {
throw new BadRequest(`item already exists`);
}
state.todoItems = [...state.todoItems, nextItem];
return state;
},
);
f.zod.get(
`/item/:id`,
{
operationId: `getTodoItem`,
params: `TodoItemId`,
response: {
200: `TodoItem`,
404: `TodoItemNotFoundError`,
},
},
async ({ params: { id } }, reply) => {
const item = state.todoItems.find((item) => item.id === id);
if (item) {
return item;
}
reply.code(404);
return {
id,
message: `item not found`,
};
},
);
f.zod.put(
`/item/:id`,
{
operationId: `putTodoItem`,
body: `TodoItem`,
params: `TodoItemId`,
reply: `TodoItem`,
},
async ({ params: { id }, body: nextItem }) => {
if (!state.todoItems.some((prevItem) => prevItem.id === id)) {
throw new NotFound(`no such item`);
}
state.todoItems = state.todoItems.map((prevItem) =>
prevItem.id === id ? nextItem : prevItem,
);
return nextItem;
},
);
f.zod.get(
`/42`,
{ operationId: `getFortyTwo`, reply: `FortyTwo` },
async () => 42,
);
return f;
};