-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathopenapi.ts
More file actions
198 lines (193 loc) · 4.63 KB
/
openapi.ts
File metadata and controls
198 lines (193 loc) · 4.63 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { ExtendedSimulationStore } from "./store.ts";
import type { SimulationHandlers } from "../../src/index.ts";
const openapiSchemaFromRealEndpoint = {
openapi: "3.0.0",
info: {
title: "API",
version: "1.0.0",
},
paths: {
"/dogs": {
get: {
summary: "Get the dogs",
operationId: "getDogs",
responses: {
200: {
description: "All of the dogs",
},
404: {
description: "The dogs have gone missing!",
},
},
},
},
"/trigger-webhook": {
post: {
summary: "Trigger a webhook",
operationId: "triggerWebhook",
responses: {
200: {
description: "webhook sent",
},
},
},
},
},
};
const openapiSchemaWithModificationsForSimulation = {
openapi: "3.0.0",
info: {
title: "API",
version: "1.0.0",
},
paths: {
"/pets": {
get: {
operationId: "getPets",
responses: {
200: {
$ref: "#/components/responses/PetList",
},
},
},
},
"/dogs": {
get: {
operationId: "getDogs",
},
},
"/more-dogs": {
get: {
operationId: "putDogs",
responses: {
200: {
description: "All of the dogs",
},
},
},
},
"/puppies": {
// would more likely be `post`,
// but easier to test a get from the brower
get: {
operationId: "putPuppies",
parameters: [
{
in: "query",
name: "quantity",
schema: {
type: "integer",
},
required: true,
description:
"The comma separated list of numbers that which we use to determine the perfect quantity",
},
],
responses: {
200: {
description: "You had puppies!",
},
},
},
},
"/perfect-number-of-dogs": {
get: {
operationId: "perfectDogQuantity",
parameters: [
{
in: "query",
name: "numbers",
schema: {
type: "string",
},
required: true,
description:
"The list of numbers that which we use to determine the perfect quantity",
},
],
responses: {
200: {
description: "Do you need more dogs?",
},
},
},
},
},
components: {
responses: {
PetList: {
description: "you would love them",
content: {
"application/json": {
example: [
{ id: 1, name: "Garfield" },
{ id: 2, name: "Odie" },
],
},
},
},
},
},
};
let document = [
openapiSchemaFromRealEndpoint,
openapiSchemaWithModificationsForSimulation,
];
function handlers(
simulationStore: ExtendedSimulationStore
): SimulationHandlers {
return {
getDogs: (_c, _request, response, _next, routeMetadata) => {
let dogs = simulationStore.schema.dogs.select(
simulationStore.store.getState()
);
if (routeMetadata.defaultCode === 200) {
response.status(200).json({ dogs });
} else {
response.sendStatus(routeMetadata.defaultCode);
}
},
putDogs: (_c, _req, response) => {
simulationStore.store.dispatch(
simulationStore.actions.batchUpdater([
simulationStore.schema.dogs.increment(),
])
);
response.status(200).send(`added 1 dog`);
},
putPuppies: (_c, request, response) => {
let rawQuantity = request.query.quantity as string;
let quantity = parseInt(rawQuantity, 10);
console.dir({ quantity });
simulationStore.store.dispatch(
simulationStore.actions.addLotsOfDogs({ quantity })
);
response
.status(200)
.send(`added ${quantity} ${quantity === 1 ? "dog" : "dogs"}`);
},
perfectDogQuantity: (_c, request, response) => {
let numbers = (
((request?.query?.numbers as string) ?? "").split(",") ?? []
).map((n) => parseInt(n, 10));
let dogs = simulationStore.selectors.booleanSpecificNumbers(
simulationStore.store.getState(),
numbers
);
response.status(200).json({ dogs });
},
triggerWebhook: (c, _request, response) => {
simulationStore.store.dispatch(
simulationStore.actions.webhooks.onTest(c.request.body)
);
response.status(200).json({ status: "ok" });
},
};
}
export const openapi = [
{
document,
handlers,
apiRoot: "/api",
},
];