@moostjs/swagger 0.3.10 → 0.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +511 -525
- package/dist/index.d.ts +82 -94
- package/dist/index.mjs +511 -525
- package/package.json +7 -6
package/dist/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
20
20
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
21
21
|
PERFORMANCE OF THIS SOFTWARE.
|
|
22
22
|
***************************************************************************** */
|
|
23
|
-
/* global Reflect, Promise */
|
|
23
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
function __decorate(decorators, target, key, desc) {
|
|
@@ -38,467 +38,455 @@ function __metadata(metadataKey, metadataValue) {
|
|
|
38
38
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
46
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
47
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
48
|
-
});
|
|
49
|
-
}
|
|
41
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
42
|
+
var e = new Error(message);
|
|
43
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
44
|
+
};
|
|
50
45
|
|
|
51
|
-
function getSwaggerMate() {
|
|
52
|
-
return getMoostMate();
|
|
46
|
+
function getSwaggerMate() {
|
|
47
|
+
return getMoostMate();
|
|
53
48
|
}
|
|
54
49
|
|
|
55
|
-
const SwaggerTag = (tag) => getSwaggerMate().decorate('swaggerTags', tag, true);
|
|
56
|
-
const SwaggerExclude = () => getSwaggerMate().decorate('swaggerExclude', true);
|
|
57
|
-
const SwaggerDescription = (descr) => getSwaggerMate().decorate('swaggerDescription', descr);
|
|
58
|
-
function SwaggerResponse(code, opts) {
|
|
59
|
-
return getSwaggerMate().decorate((meta) => {
|
|
60
|
-
meta.swaggerResponses = meta.swaggerResponses || {};
|
|
61
|
-
const keyCode = typeof code === 'number' ? code : 0;
|
|
62
|
-
const opt = typeof code === 'number' ? opts : code;
|
|
63
|
-
const contentType = typeof opt.contentType === 'string' ? opt.contentType : '*/*';
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
meta.swaggerResponses[keyCode]
|
|
67
|
-
meta
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return meta;
|
|
79
|
-
});
|
|
50
|
+
const SwaggerTag = (tag) => getSwaggerMate().decorate('swaggerTags', tag, true);
|
|
51
|
+
const SwaggerExclude = () => getSwaggerMate().decorate('swaggerExclude', true);
|
|
52
|
+
const SwaggerDescription = (descr) => getSwaggerMate().decorate('swaggerDescription', descr);
|
|
53
|
+
function SwaggerResponse(code, opts) {
|
|
54
|
+
return getSwaggerMate().decorate((meta) => {
|
|
55
|
+
meta.swaggerResponses = meta.swaggerResponses || {};
|
|
56
|
+
const keyCode = typeof code === 'number' ? code : 0;
|
|
57
|
+
const opt = typeof code === 'number' ? opts : code;
|
|
58
|
+
const contentType = typeof opt.contentType === 'string' ? opt.contentType : '*/*';
|
|
59
|
+
const response = (['object', 'function'].includes(typeof opt.response) ? opt.response : opt);
|
|
60
|
+
meta.swaggerResponses[keyCode] = meta.swaggerResponses[keyCode] || {};
|
|
61
|
+
meta.swaggerResponses[keyCode][contentType] = response;
|
|
62
|
+
return meta;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function SwaggerRequestBody(opt) {
|
|
66
|
+
return getSwaggerMate().decorate((meta) => {
|
|
67
|
+
meta.swaggerRequestBody = meta.swaggerRequestBody || {};
|
|
68
|
+
const contentType = typeof opt.contentType === 'string' ? opt.contentType : 'application/json';
|
|
69
|
+
const response = (typeof opt.contentType === 'string' ? opt.response : opt);
|
|
70
|
+
meta.swaggerRequestBody[contentType] = response;
|
|
71
|
+
return meta;
|
|
72
|
+
});
|
|
80
73
|
}
|
|
81
74
|
|
|
82
|
-
function mapToSwaggerSpec(metadata, options, logger) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
responses =
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
responses =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
let
|
|
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
|
-
const
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
let
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
case '
|
|
268
|
-
case '
|
|
269
|
-
case '
|
|
270
|
-
case '
|
|
271
|
-
case '
|
|
272
|
-
case '
|
|
273
|
-
case '
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (
|
|
300
|
-
return
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
case '
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
case '
|
|
398
|
-
case '
|
|
399
|
-
case '
|
|
400
|
-
case '
|
|
401
|
-
case '
|
|
402
|
-
|
|
403
|
-
case '
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
schema.
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
schema.
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
schema.
|
|
416
|
-
schema.
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
case '
|
|
437
|
-
|
|
438
|
-
schema.
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
PUT: 200,
|
|
485
|
-
POST: 201,
|
|
486
|
-
DELETE: 204,
|
|
487
|
-
};
|
|
488
|
-
return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
|
|
75
|
+
function mapToSwaggerSpec(metadata, options, logger) {
|
|
76
|
+
const swaggerSpec = {
|
|
77
|
+
openapi: '3.0.0',
|
|
78
|
+
info: {
|
|
79
|
+
title: options?.title || 'API Documentation',
|
|
80
|
+
version: options?.version || '1.0.0',
|
|
81
|
+
},
|
|
82
|
+
paths: {},
|
|
83
|
+
tags: [],
|
|
84
|
+
components: {
|
|
85
|
+
schemas: globalSchemas,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
for (const controller of metadata) {
|
|
89
|
+
const cmeta = controller.meta;
|
|
90
|
+
if (cmeta?.swaggerExclude)
|
|
91
|
+
continue;
|
|
92
|
+
const controllerTags = cmeta?.swaggerTags || [];
|
|
93
|
+
for (const handler of controller.handlers) {
|
|
94
|
+
const hmeta = handler.meta;
|
|
95
|
+
const hh = handler.handler;
|
|
96
|
+
if (hh.type !== 'HTTP' || hmeta?.swaggerExclude || !handler.registeredAs.length)
|
|
97
|
+
continue;
|
|
98
|
+
const handlerPath = handler.registeredAs[0].path;
|
|
99
|
+
const handlerMethod = hh.method?.toLowerCase() || 'get';
|
|
100
|
+
const handlerDescription = hmeta?.description;
|
|
101
|
+
const handlerTags = [...controllerTags, ...(hmeta?.swaggerTags || [])];
|
|
102
|
+
if (!swaggerSpec.paths[handlerPath]) {
|
|
103
|
+
swaggerSpec.paths[handlerPath] = {};
|
|
104
|
+
}
|
|
105
|
+
let responses = undefined;
|
|
106
|
+
if (hmeta?.swaggerResponses) {
|
|
107
|
+
for (const [code, responseConfigs] of Object.entries(hmeta.swaggerResponses)) {
|
|
108
|
+
const newCode = code === '0' ? getDefaultStatusCode(handlerMethod) : code;
|
|
109
|
+
for (const [contentType, type] of Object.entries(responseConfigs)) {
|
|
110
|
+
let schema = undefined;
|
|
111
|
+
let zt = undefined;
|
|
112
|
+
if (type instanceof z.ZodType) {
|
|
113
|
+
zt = type;
|
|
114
|
+
}
|
|
115
|
+
else if (typeof type === 'function') {
|
|
116
|
+
zt = getZodType({
|
|
117
|
+
type,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (zt) {
|
|
121
|
+
const parsed = myParseZod(zt);
|
|
122
|
+
if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
|
|
123
|
+
schema = getSwaggerSchema(parsed);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (schema) {
|
|
127
|
+
responses = responses || {};
|
|
128
|
+
responses[newCode] = { content: { [contentType]: { schema } } };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (hmeta?.returnType) {
|
|
134
|
+
const parsed = myParseZod(getZodType({
|
|
135
|
+
type: hmeta.returnType,
|
|
136
|
+
}));
|
|
137
|
+
if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
|
|
138
|
+
const schema = getSwaggerSchema(parsed);
|
|
139
|
+
if (schema) {
|
|
140
|
+
responses = responses || {};
|
|
141
|
+
responses[getDefaultStatusCode(handlerMethod)] = {
|
|
142
|
+
content: {
|
|
143
|
+
'*/*': {
|
|
144
|
+
schema,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
let reqBodyRequired = true;
|
|
152
|
+
const bodyConfig = {};
|
|
153
|
+
if (hmeta?.swaggerRequestBody) {
|
|
154
|
+
for (const [contentType, type] of Object.entries(hmeta?.swaggerRequestBody)) {
|
|
155
|
+
let zt;
|
|
156
|
+
let schema;
|
|
157
|
+
if (type instanceof z.ZodType) {
|
|
158
|
+
zt = type;
|
|
159
|
+
}
|
|
160
|
+
else if (typeof type === 'function') {
|
|
161
|
+
zt = getZodType({
|
|
162
|
+
type,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
if (zt) {
|
|
166
|
+
const parsed = myParseZod(zt);
|
|
167
|
+
if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
|
|
168
|
+
schema = getSwaggerSchema(parsed);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
bodyConfig[contentType] = { schema };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
swaggerSpec.paths[handlerPath][handlerMethod] = {
|
|
175
|
+
summary: handlerDescription,
|
|
176
|
+
operationId: handlerMethod.toUpperCase() + '_' + handlerPath.replace(/\//g, '_').replace(/[\{\}]/g, '__').replace(/[^A-Za-z0-9]/g, '_'),
|
|
177
|
+
tags: handlerTags,
|
|
178
|
+
parameters: [],
|
|
179
|
+
responses,
|
|
180
|
+
};
|
|
181
|
+
const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
|
|
182
|
+
for (const paramName of handler.registeredAs[0].args) {
|
|
183
|
+
const paramIndex = handler.meta.params.findIndex(param => param.paramSource === 'ROUTE' && param.paramName === paramName);
|
|
184
|
+
const paramMeta = handler.meta.params[paramIndex];
|
|
185
|
+
let schema;
|
|
186
|
+
let parsed;
|
|
187
|
+
if (paramMeta) {
|
|
188
|
+
const zodType = getZodTypeForProp({
|
|
189
|
+
type: controller.type,
|
|
190
|
+
key: handler.method,
|
|
191
|
+
index: paramIndex,
|
|
192
|
+
}, {
|
|
193
|
+
type: paramMeta.type,
|
|
194
|
+
additionalMeta: paramMeta,
|
|
195
|
+
}, undefined, logger);
|
|
196
|
+
parsed = myParseZod(zodType);
|
|
197
|
+
schema = getSwaggerSchema(parsed, true);
|
|
198
|
+
}
|
|
199
|
+
endpointSpec.parameters.push({
|
|
200
|
+
name: paramName,
|
|
201
|
+
in: 'path',
|
|
202
|
+
description: paramMeta ? paramMeta.description : undefined,
|
|
203
|
+
required: !paramMeta?.optional && !parsed?.$optional,
|
|
204
|
+
schema: schema || { type: 'string' },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
for (let i = 0; i < handler.meta.params.length; i++) {
|
|
208
|
+
const paramMeta = handler.meta.params[i];
|
|
209
|
+
if (paramMeta.paramSource && ['QUERY_ITEM', 'QUERY'].includes(paramMeta.paramSource)) {
|
|
210
|
+
const zodType = getZodTypeForProp({
|
|
211
|
+
type: controller.type,
|
|
212
|
+
key: handler.method,
|
|
213
|
+
index: i,
|
|
214
|
+
}, {
|
|
215
|
+
type: paramMeta.type,
|
|
216
|
+
additionalMeta: paramMeta,
|
|
217
|
+
}, undefined, logger);
|
|
218
|
+
const parsed = myParseZod(zodType);
|
|
219
|
+
const schema = getSwaggerSchema(parsed, true);
|
|
220
|
+
if (paramMeta.paramSource == 'QUERY_ITEM') {
|
|
221
|
+
endpointSpec.parameters.push({
|
|
222
|
+
name: paramMeta.paramName || '',
|
|
223
|
+
in: 'query',
|
|
224
|
+
description: paramMeta.description,
|
|
225
|
+
required: !paramMeta?.optional && !parsed?.$optional,
|
|
226
|
+
schema: schema || { type: 'string' },
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
else if (paramMeta.paramSource == 'QUERY') {
|
|
230
|
+
if (parsed.$type === 'ZodObject') {
|
|
231
|
+
for (const [key, value] of Object.entries(parsed.$inner)) {
|
|
232
|
+
const schema = getSwaggerSchema(value, true);
|
|
233
|
+
if (schema) {
|
|
234
|
+
const swaggerSchema = {
|
|
235
|
+
name: key,
|
|
236
|
+
in: 'query',
|
|
237
|
+
description: value.description,
|
|
238
|
+
required: !parsed.$optional && !value.$optional,
|
|
239
|
+
schema,
|
|
240
|
+
};
|
|
241
|
+
endpointSpec.parameters.push(swaggerSchema);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (paramMeta?.paramSource === 'BODY') {
|
|
248
|
+
const zodType = getZodTypeForProp({
|
|
249
|
+
type: controller.type,
|
|
250
|
+
key: handler.method,
|
|
251
|
+
index: i,
|
|
252
|
+
}, {
|
|
253
|
+
type: paramMeta.type,
|
|
254
|
+
additionalMeta: paramMeta,
|
|
255
|
+
}, undefined, logger);
|
|
256
|
+
const parsed = myParseZod(zodType);
|
|
257
|
+
let contentType = '';
|
|
258
|
+
switch (parsed.$type) {
|
|
259
|
+
case 'ZodString':
|
|
260
|
+
case 'ZodNumber':
|
|
261
|
+
case 'ZodBigInt':
|
|
262
|
+
case 'ZodBoolean':
|
|
263
|
+
case 'ZodDate':
|
|
264
|
+
case 'ZodEnum':
|
|
265
|
+
case 'ZodNativeEnum':
|
|
266
|
+
case 'ZodLiteral':
|
|
267
|
+
contentType = 'text/plan';
|
|
268
|
+
break;
|
|
269
|
+
default: contentType = 'application/json';
|
|
270
|
+
}
|
|
271
|
+
if (!bodyConfig[contentType]) {
|
|
272
|
+
bodyConfig[contentType] = { schema: getSwaggerSchema(parsed) };
|
|
273
|
+
}
|
|
274
|
+
reqBodyRequired = !zodType.isOptional() && !paramMeta.optional;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (bodyConfig && Object.entries(bodyConfig).filter(e => !!e[1]).length > 0) {
|
|
278
|
+
swaggerSpec.paths[handlerPath][handlerMethod].requestBody = {
|
|
279
|
+
content: bodyConfig,
|
|
280
|
+
required: reqBodyRequired,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return swaggerSpec;
|
|
286
|
+
}
|
|
287
|
+
const globalSchemas = {};
|
|
288
|
+
function getSwaggerSchema(parsed, forParam) {
|
|
289
|
+
const zodType = parsed.$ref;
|
|
290
|
+
if (!forParam && zodType.__type_ref && globalSchemas[zodType.__type_ref.name])
|
|
291
|
+
return { $ref: `#/components/schemas/${zodType.__type_ref.name}` };
|
|
292
|
+
if (forParam && zodType.__type_ref && globalSchemas[zodType.__type_ref.name])
|
|
293
|
+
return globalSchemas[zodType.__type_ref.name];
|
|
294
|
+
const schema = {};
|
|
295
|
+
if (!forParam && zodType.__type_ref) {
|
|
296
|
+
globalSchemas[zodType.__type_ref.name] = schema;
|
|
297
|
+
}
|
|
298
|
+
function asString() {
|
|
299
|
+
schema.type = 'string';
|
|
300
|
+
if (parsed.$checks) {
|
|
301
|
+
const { regex } = parsed.$checks;
|
|
302
|
+
if (regex) {
|
|
303
|
+
schema.pattern = regex.source;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function asLiteral() {
|
|
308
|
+
if (parsed.$type === 'ZodLiteral') {
|
|
309
|
+
schema.type = 'string';
|
|
310
|
+
schema.enum = [parsed.$value];
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function asEnum() {
|
|
314
|
+
if (parsed.$type === 'ZodEnum') {
|
|
315
|
+
schema.type = 'string';
|
|
316
|
+
schema.enum = parsed.$value;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function asNativeEnum() {
|
|
320
|
+
if (parsed.$type === 'ZodNativeEnum') {
|
|
321
|
+
schema.type = 'string';
|
|
322
|
+
schema.enum = Object.keys(parsed.$value);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (forParam) {
|
|
326
|
+
switch (parsed.$type) {
|
|
327
|
+
case 'ZodAny':
|
|
328
|
+
case 'ZodUnknown':
|
|
329
|
+
case 'ZodString':
|
|
330
|
+
asString();
|
|
331
|
+
break;
|
|
332
|
+
case 'ZodNumber':
|
|
333
|
+
schema.type = 'number';
|
|
334
|
+
break;
|
|
335
|
+
case 'ZodBigInt':
|
|
336
|
+
schema.type = 'integer';
|
|
337
|
+
break;
|
|
338
|
+
case 'ZodBoolean':
|
|
339
|
+
schema.type = 'boolean';
|
|
340
|
+
break;
|
|
341
|
+
case 'ZodLiteral':
|
|
342
|
+
asLiteral();
|
|
343
|
+
break;
|
|
344
|
+
case 'ZodEnum':
|
|
345
|
+
asEnum();
|
|
346
|
+
break;
|
|
347
|
+
case 'ZodNativeEnum':
|
|
348
|
+
asNativeEnum();
|
|
349
|
+
break;
|
|
350
|
+
case 'ZodDate':
|
|
351
|
+
schema.type = 'string';
|
|
352
|
+
break;
|
|
353
|
+
case 'ZodNull':
|
|
354
|
+
schema.type = 'null';
|
|
355
|
+
break;
|
|
356
|
+
default: return undefined;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
switch (parsed.$type) {
|
|
361
|
+
case 'ZodString':
|
|
362
|
+
asString();
|
|
363
|
+
break;
|
|
364
|
+
case 'ZodNumber':
|
|
365
|
+
schema.type = 'number';
|
|
366
|
+
break;
|
|
367
|
+
case 'ZodBigInt':
|
|
368
|
+
schema.type = 'integer';
|
|
369
|
+
break;
|
|
370
|
+
case 'ZodBoolean':
|
|
371
|
+
schema.type = 'boolean';
|
|
372
|
+
break;
|
|
373
|
+
case 'ZodLiteral':
|
|
374
|
+
asLiteral();
|
|
375
|
+
break;
|
|
376
|
+
case 'ZodEnum':
|
|
377
|
+
asEnum();
|
|
378
|
+
break;
|
|
379
|
+
case 'ZodNativeEnum':
|
|
380
|
+
asNativeEnum();
|
|
381
|
+
break;
|
|
382
|
+
case 'ZodDate':
|
|
383
|
+
schema.type = 'string';
|
|
384
|
+
break;
|
|
385
|
+
case 'ZodNull':
|
|
386
|
+
schema.type = 'null';
|
|
387
|
+
break;
|
|
388
|
+
case 'ZodFunction':
|
|
389
|
+
case 'ZodSymbol':
|
|
390
|
+
case 'ZodUndefined':
|
|
391
|
+
case 'ZodUnknown':
|
|
392
|
+
case 'ZodNever':
|
|
393
|
+
case 'ZodVoid':
|
|
394
|
+
case 'ZodNaN':
|
|
395
|
+
return undefined;
|
|
396
|
+
case 'ZodArray':
|
|
397
|
+
schema.type = 'array';
|
|
398
|
+
schema.minItems = parsed.$checks?.minLength || undefined;
|
|
399
|
+
schema.maxItems = parsed.$checks?.maxLength || undefined;
|
|
400
|
+
schema.items = getSwaggerSchema(parsed.$inner);
|
|
401
|
+
break;
|
|
402
|
+
case 'ZodTuple':
|
|
403
|
+
schema.type = 'array';
|
|
404
|
+
schema.items = parsed.$inner.map(t => getSwaggerSchema(t)).filter(t => !!t);
|
|
405
|
+
break;
|
|
406
|
+
case 'ZodObject':
|
|
407
|
+
schema.type = 'object';
|
|
408
|
+
schema.properties = {};
|
|
409
|
+
schema.required = [];
|
|
410
|
+
if (zodType._def.unknownKeys === 'passthrough')
|
|
411
|
+
schema.additionalProperties = {};
|
|
412
|
+
for (const [key, val] of Object.entries(parsed.$inner)) {
|
|
413
|
+
const prop = getSwaggerSchema(val);
|
|
414
|
+
if (prop) {
|
|
415
|
+
schema.properties[key] = prop;
|
|
416
|
+
if (!val.$optional)
|
|
417
|
+
schema.required.push(key);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
break;
|
|
421
|
+
case 'ZodPromise':
|
|
422
|
+
case 'ZodRecord':
|
|
423
|
+
case 'ZodMap':
|
|
424
|
+
case 'ZodSet':
|
|
425
|
+
schema.type = 'object';
|
|
426
|
+
schema.properties = {};
|
|
427
|
+
schema.additionalProperties = parsed.$type === 'ZodRecord' ? {} : undefined;
|
|
428
|
+
break;
|
|
429
|
+
case 'ZodUnion':
|
|
430
|
+
case 'ZodDiscriminatedUnion':
|
|
431
|
+
schema.oneOf = parsed.$inner.map(t => getSwaggerSchema(t)).filter(t => !!t);
|
|
432
|
+
break;
|
|
433
|
+
case 'ZodIntersection':
|
|
434
|
+
schema.allOf = parsed.$inner.map(t => getSwaggerSchema(t)).filter(t => !!t);
|
|
435
|
+
break;
|
|
436
|
+
case 'ZodLazy':
|
|
437
|
+
return getSwaggerSchema(parsed.$get());
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (parsed.$nullable) {
|
|
441
|
+
schema.nullable = parsed.$nullable;
|
|
442
|
+
}
|
|
443
|
+
if (parsed.$ref._def.description) {
|
|
444
|
+
schema.description = parsed.$ref._def.description;
|
|
445
|
+
}
|
|
446
|
+
if (parsed.$checks) {
|
|
447
|
+
const checks = parsed.$checks;
|
|
448
|
+
if (parsed.$type === 'ZodString') {
|
|
449
|
+
if (typeof checks.min === 'number')
|
|
450
|
+
schema.minLength = checks.min;
|
|
451
|
+
if (typeof checks.max === 'number')
|
|
452
|
+
schema.maxLength = checks.max;
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
if (typeof checks.min === 'number')
|
|
456
|
+
schema.minimum = checks.min;
|
|
457
|
+
if (typeof checks.max === 'number')
|
|
458
|
+
schema.maximum = checks.max;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (!forParam && zodType.__type_ref) {
|
|
462
|
+
return { $ref: `#/components/schemas/${zodType.__type_ref.name}` };
|
|
463
|
+
}
|
|
464
|
+
return schema;
|
|
465
|
+
}
|
|
466
|
+
function myParseZod(schema) {
|
|
467
|
+
return parseZodType(schema);
|
|
468
|
+
}
|
|
469
|
+
function getDefaultStatusCode(httpMethod) {
|
|
470
|
+
const defaultStatusCodes = {
|
|
471
|
+
GET: 200,
|
|
472
|
+
PUT: 200,
|
|
473
|
+
POST: 201,
|
|
474
|
+
DELETE: 204,
|
|
475
|
+
};
|
|
476
|
+
return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
|
|
489
477
|
}
|
|
490
478
|
|
|
491
|
-
let SwaggerController = class SwaggerController {
|
|
492
|
-
constructor(title = 'Moost API') {
|
|
493
|
-
this.title = title;
|
|
494
|
-
this.assetPath = getAbsoluteFSPath();
|
|
495
|
-
}
|
|
496
|
-
serveIndex(url, location, status) {
|
|
497
|
-
if (!url.endsWith('index.html') && !url.endsWith('/')) {
|
|
498
|
-
status.value = 302;
|
|
499
|
-
location.value = join(url, '/');
|
|
500
|
-
return '';
|
|
501
|
-
}
|
|
479
|
+
let SwaggerController = class SwaggerController {
|
|
480
|
+
constructor(title = 'Moost API') {
|
|
481
|
+
this.title = title;
|
|
482
|
+
this.assetPath = getAbsoluteFSPath();
|
|
483
|
+
}
|
|
484
|
+
serveIndex(url, location, status) {
|
|
485
|
+
if (!url.endsWith('index.html') && !url.endsWith('/')) {
|
|
486
|
+
status.value = 302;
|
|
487
|
+
location.value = join(url, '/');
|
|
488
|
+
return '';
|
|
489
|
+
}
|
|
502
490
|
return `<!DOCTYPE html>
|
|
503
491
|
<html lang="en">
|
|
504
492
|
<head>
|
|
@@ -516,9 +504,9 @@ let SwaggerController = class SwaggerController {
|
|
|
516
504
|
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
|
|
517
505
|
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
|
|
518
506
|
</body>
|
|
519
|
-
</html>`;
|
|
520
|
-
}
|
|
521
|
-
'swagger-initializer.js'() {
|
|
507
|
+
</html>`;
|
|
508
|
+
}
|
|
509
|
+
'swagger-initializer.js'() {
|
|
522
510
|
return `window.onload = function() {
|
|
523
511
|
window.ui = SwaggerUIBundle({
|
|
524
512
|
url: "./spec.json",
|
|
@@ -533,73 +521,71 @@ let SwaggerController = class SwaggerController {
|
|
|
533
521
|
],
|
|
534
522
|
layout: "BaseLayout"
|
|
535
523
|
});
|
|
536
|
-
};`;
|
|
537
|
-
}
|
|
538
|
-
'spec.json'() {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
Get(''),
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
__param(
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
__metadata("design:
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
__metadata("design:
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
__metadata("design:
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
Get('swagger-ui
|
|
589
|
-
Get('
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
__metadata("design:
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
__param(0, Const('Moost API')),
|
|
602
|
-
__metadata("design:paramtypes", [Object])
|
|
524
|
+
};`;
|
|
525
|
+
}
|
|
526
|
+
async 'spec.json'() {
|
|
527
|
+
const logger = useEventLogger('@moostjs/zod');
|
|
528
|
+
if (!this.spec) {
|
|
529
|
+
const { instantiate } = useControllerContext();
|
|
530
|
+
const moost = await instantiate(Moost);
|
|
531
|
+
this.spec = mapToSwaggerSpec(moost.getControllersOverview(), { title: this.title }, logger);
|
|
532
|
+
}
|
|
533
|
+
return this.spec;
|
|
534
|
+
}
|
|
535
|
+
files(url) {
|
|
536
|
+
return this.serve(url.split('/').pop());
|
|
537
|
+
}
|
|
538
|
+
serve(path) {
|
|
539
|
+
return serveFile(path, {
|
|
540
|
+
baseDir: this.assetPath,
|
|
541
|
+
cacheControl: {
|
|
542
|
+
public: true,
|
|
543
|
+
maxAge: '1w',
|
|
544
|
+
},
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
__decorate([
|
|
549
|
+
Get(''),
|
|
550
|
+
Get('//'),
|
|
551
|
+
Get('index.html'),
|
|
552
|
+
SetHeader('content-type', 'text/html'),
|
|
553
|
+
__param(0, Url()),
|
|
554
|
+
__param(1, HeaderHook('location')),
|
|
555
|
+
__param(2, StatusHook()),
|
|
556
|
+
__metadata("design:type", Function),
|
|
557
|
+
__metadata("design:paramtypes", [String, Object, Object]),
|
|
558
|
+
__metadata("design:returntype", void 0)
|
|
559
|
+
], SwaggerController.prototype, "serveIndex", null);
|
|
560
|
+
__decorate([
|
|
561
|
+
Get(),
|
|
562
|
+
SetHeader('content-type', 'application/javascript'),
|
|
563
|
+
__metadata("design:type", Function),
|
|
564
|
+
__metadata("design:paramtypes", []),
|
|
565
|
+
__metadata("design:returntype", void 0)
|
|
566
|
+
], SwaggerController.prototype, "swagger-initializer.js", null);
|
|
567
|
+
__decorate([
|
|
568
|
+
Get(),
|
|
569
|
+
__metadata("design:type", Function),
|
|
570
|
+
__metadata("design:paramtypes", []),
|
|
571
|
+
__metadata("design:returntype", Promise)
|
|
572
|
+
], SwaggerController.prototype, "spec.json", null);
|
|
573
|
+
__decorate([
|
|
574
|
+
Get('swagger-ui-bundle.*(js|js\\.map)'),
|
|
575
|
+
Get('swagger-ui-standalone-preset.*(js|js\\.map)'),
|
|
576
|
+
Get('swagger-ui.*(css|css\\.map)'),
|
|
577
|
+
Get('index.*(css|css\\.map)'),
|
|
578
|
+
__param(0, Url()),
|
|
579
|
+
__metadata("design:type", Function),
|
|
580
|
+
__metadata("design:paramtypes", [String]),
|
|
581
|
+
__metadata("design:returntype", void 0)
|
|
582
|
+
], SwaggerController.prototype, "files", null);
|
|
583
|
+
SwaggerController = __decorate([
|
|
584
|
+
SwaggerExclude(),
|
|
585
|
+
ZodSkip(),
|
|
586
|
+
Controller('api-docs'),
|
|
587
|
+
__param(0, Const('Moost API')),
|
|
588
|
+
__metadata("design:paramtypes", [Object])
|
|
603
589
|
], SwaggerController);
|
|
604
590
|
|
|
605
591
|
export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
|