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