@moostjs/swagger 0.3.42 → 0.3.44

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
@@ -120,6 +120,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
120
120
  if (hh.type !== 'HTTP' || hmeta?.swaggerExclude || handler.registeredAs.length === 0) {
121
121
  continue;
122
122
  }
123
+ const uniqueParams = {};
123
124
  const handlerPath = handler.registeredAs[0].path;
124
125
  const handlerMethod = hh.method?.toLowerCase() || 'get';
125
126
  const handlerDescription = hmeta?.description;
@@ -197,8 +198,29 @@ function mapToSwaggerSpec(metadata, options, logger) {
197
198
  responses,
198
199
  };
199
200
  const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
201
+ function addParam(param) {
202
+ const key = `${param.in}//${param.name}`;
203
+ if (uniqueParams[key]) {
204
+ uniqueParams[key].description = param.description ?? uniqueParams[key].description;
205
+ uniqueParams[key].required = param.required;
206
+ uniqueParams[key].schema = param.schema ?? uniqueParams[key].schema;
207
+ }
208
+ else {
209
+ uniqueParams[key] = param;
210
+ endpointSpec.parameters.push(param);
211
+ }
212
+ }
213
+ for (const param of cmeta?.swaggerParams || []) {
214
+ addParam({
215
+ name: param.name,
216
+ in: param.in,
217
+ description: param.description,
218
+ required: !!param.required,
219
+ schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
220
+ });
221
+ }
200
222
  for (const param of hmeta?.swaggerParams || []) {
201
- endpointSpec.parameters.push({
223
+ addParam({
202
224
  name: param.name,
203
225
  in: param.in,
204
226
  description: param.description,
@@ -223,7 +245,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
223
245
  parsed = myParseZod(zodType);
224
246
  schema = getSwaggerSchema(parsed, true);
225
247
  }
226
- endpointSpec.parameters.push({
248
+ addParam({
227
249
  name: paramName,
228
250
  in: 'path',
229
251
  description: paramMeta ? paramMeta.description : undefined,
@@ -323,6 +345,17 @@ function getSwaggerSchema(parsed, forParam) {
323
345
  return globalSchemas[zodType.__type_ref.name];
324
346
  }
325
347
  const schema = {};
348
+ if (meta) {
349
+ if (meta.swaggerExample) {
350
+ schema.example = meta.swaggerExample;
351
+ }
352
+ if (meta.label || meta.id) {
353
+ schema.title = meta.label || meta.id;
354
+ }
355
+ if (meta.description) {
356
+ schema.description = meta.description;
357
+ }
358
+ }
326
359
  if (!forParam && zodType.__type_ref) {
327
360
  globalSchemas[zodType.__type_ref.name] = schema;
328
361
  }
@@ -353,9 +386,6 @@ function getSwaggerSchema(parsed, forParam) {
353
386
  schema.enum = Object.keys(parsed.$value);
354
387
  }
355
388
  }
356
- if (meta?.swaggerExample) {
357
- schema.example = meta.swaggerExample;
358
- }
359
389
  if (forParam) {
360
390
  switch (parsed.$type) {
361
391
  case 'ZodAny':
@@ -573,8 +603,8 @@ function getSwaggerSchemaFromSwaggerConfigType(type) {
573
603
  }
574
604
 
575
605
  exports.SwaggerController = class SwaggerController {
576
- constructor(title = 'Moost API') {
577
- this.title = title;
606
+ constructor(opts = { title: 'Moost API' }) {
607
+ this.opts = opts;
578
608
  this['assetPath'] = swaggerUiDist.getAbsoluteFSPath();
579
609
  }
580
610
  'serveIndex'(url, location, status) {
@@ -587,7 +617,7 @@ exports.SwaggerController = class SwaggerController {
587
617
  <html lang="en">
588
618
  <head>
589
619
  <meta charset="UTF-8">
590
- <title>${this.title}</title>
620
+ <title>${this.opts.title}</title>
591
621
  <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
592
622
  <link rel="stylesheet" type="text/css" href="index.css" />
593
623
  <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
@@ -624,7 +654,7 @@ exports.SwaggerController = class SwaggerController {
624
654
  if (!this.spec) {
625
655
  const { instantiate } = moost.useControllerContext();
626
656
  const moost$1 = await instantiate(moost.Moost);
627
- this.spec = mapToSwaggerSpec(moost$1.getControllersOverview(), { title: this.title }, logger);
657
+ this.spec = mapToSwaggerSpec(moost$1.getControllersOverview(), this.opts, logger);
628
658
  }
629
659
  return this.spec;
630
660
  }
@@ -680,7 +710,7 @@ exports.SwaggerController = __decorate([
680
710
  SwaggerExclude(),
681
711
  zod.ZodSkip(),
682
712
  moost.Controller('api-docs'),
683
- __param(0, moost.Const('Moost API')),
713
+ __param(0, moost.Const({ title: 'Moost API' })),
684
714
  __metadata("design:paramtypes", [Object])
685
715
  ], exports.SwaggerController);
686
716
 
package/dist/index.d.ts CHANGED
@@ -7,6 +7,11 @@ type TFunction = Function;
7
7
  interface TEmpty {
8
8
  }
9
9
 
10
+ interface TSwaggerOptions {
11
+ title?: string;
12
+ description?: string;
13
+ version?: string;
14
+ }
10
15
  interface TSwaggerSchema {
11
16
  type?: string;
12
17
  $ref?: string;
@@ -81,8 +86,8 @@ declare function SwaggerParam(opts: TSwaggerMate['swaggerParams'][number]): Meth
81
86
  declare function SwaggerExample(example: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
82
87
 
83
88
  declare class SwaggerController {
84
- protected title: string;
85
- constructor(title?: string);
89
+ protected opts: TSwaggerOptions;
90
+ constructor(opts?: TSwaggerOptions);
86
91
  'assetPath': string;
87
92
  'serveIndex'(url: string, location: THeaderHook, status: TStatusHook): string;
88
93
  'swagger-initializer.js'(): string;
package/dist/index.mjs CHANGED
@@ -118,6 +118,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
118
118
  if (hh.type !== 'HTTP' || hmeta?.swaggerExclude || handler.registeredAs.length === 0) {
119
119
  continue;
120
120
  }
121
+ const uniqueParams = {};
121
122
  const handlerPath = handler.registeredAs[0].path;
122
123
  const handlerMethod = hh.method?.toLowerCase() || 'get';
123
124
  const handlerDescription = hmeta?.description;
@@ -195,8 +196,29 @@ function mapToSwaggerSpec(metadata, options, logger) {
195
196
  responses,
196
197
  };
197
198
  const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
199
+ function addParam(param) {
200
+ const key = `${param.in}//${param.name}`;
201
+ if (uniqueParams[key]) {
202
+ uniqueParams[key].description = param.description ?? uniqueParams[key].description;
203
+ uniqueParams[key].required = param.required;
204
+ uniqueParams[key].schema = param.schema ?? uniqueParams[key].schema;
205
+ }
206
+ else {
207
+ uniqueParams[key] = param;
208
+ endpointSpec.parameters.push(param);
209
+ }
210
+ }
211
+ for (const param of cmeta?.swaggerParams || []) {
212
+ addParam({
213
+ name: param.name,
214
+ in: param.in,
215
+ description: param.description,
216
+ required: !!param.required,
217
+ schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
218
+ });
219
+ }
198
220
  for (const param of hmeta?.swaggerParams || []) {
199
- endpointSpec.parameters.push({
221
+ addParam({
200
222
  name: param.name,
201
223
  in: param.in,
202
224
  description: param.description,
@@ -221,7 +243,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
221
243
  parsed = myParseZod(zodType);
222
244
  schema = getSwaggerSchema(parsed, true);
223
245
  }
224
- endpointSpec.parameters.push({
246
+ addParam({
225
247
  name: paramName,
226
248
  in: 'path',
227
249
  description: paramMeta ? paramMeta.description : undefined,
@@ -321,6 +343,17 @@ function getSwaggerSchema(parsed, forParam) {
321
343
  return globalSchemas[zodType.__type_ref.name];
322
344
  }
323
345
  const schema = {};
346
+ if (meta) {
347
+ if (meta.swaggerExample) {
348
+ schema.example = meta.swaggerExample;
349
+ }
350
+ if (meta.label || meta.id) {
351
+ schema.title = meta.label || meta.id;
352
+ }
353
+ if (meta.description) {
354
+ schema.description = meta.description;
355
+ }
356
+ }
324
357
  if (!forParam && zodType.__type_ref) {
325
358
  globalSchemas[zodType.__type_ref.name] = schema;
326
359
  }
@@ -351,9 +384,6 @@ function getSwaggerSchema(parsed, forParam) {
351
384
  schema.enum = Object.keys(parsed.$value);
352
385
  }
353
386
  }
354
- if (meta?.swaggerExample) {
355
- schema.example = meta.swaggerExample;
356
- }
357
387
  if (forParam) {
358
388
  switch (parsed.$type) {
359
389
  case 'ZodAny':
@@ -571,8 +601,8 @@ function getSwaggerSchemaFromSwaggerConfigType(type) {
571
601
  }
572
602
 
573
603
  let SwaggerController = class SwaggerController {
574
- constructor(title = 'Moost API') {
575
- this.title = title;
604
+ constructor(opts = { title: 'Moost API' }) {
605
+ this.opts = opts;
576
606
  this['assetPath'] = getAbsoluteFSPath();
577
607
  }
578
608
  'serveIndex'(url, location, status) {
@@ -585,7 +615,7 @@ let SwaggerController = class SwaggerController {
585
615
  <html lang="en">
586
616
  <head>
587
617
  <meta charset="UTF-8">
588
- <title>${this.title}</title>
618
+ <title>${this.opts.title}</title>
589
619
  <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
590
620
  <link rel="stylesheet" type="text/css" href="index.css" />
591
621
  <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
@@ -622,7 +652,7 @@ let SwaggerController = class SwaggerController {
622
652
  if (!this.spec) {
623
653
  const { instantiate } = useControllerContext();
624
654
  const moost = await instantiate(Moost);
625
- this.spec = mapToSwaggerSpec(moost.getControllersOverview(), { title: this.title }, logger);
655
+ this.spec = mapToSwaggerSpec(moost.getControllersOverview(), this.opts, logger);
626
656
  }
627
657
  return this.spec;
628
658
  }
@@ -678,7 +708,7 @@ SwaggerController = __decorate([
678
708
  SwaggerExclude(),
679
709
  ZodSkip(),
680
710
  Controller('api-docs'),
681
- __param(0, Const('Moost API')),
711
+ __param(0, Const({ title: 'Moost API' })),
682
712
  __metadata("design:paramtypes", [Object])
683
713
  ], SwaggerController);
684
714
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/swagger",
3
- "version": "0.3.42",
3
+ "version": "0.3.44",
4
4
  "description": "@moostjs/swagger",
5
5
  "sideEffects": false,
6
6
  "main": "dist/index.cjs",
@@ -37,14 +37,14 @@
37
37
  },
38
38
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/swagger#readme",
39
39
  "peerDependencies": {
40
- "moost": "0.3.42",
41
- "@moostjs/event-http": "0.3.42"
40
+ "moost": "0.3.44",
41
+ "@moostjs/event-http": "0.3.44"
42
42
  },
43
43
  "dependencies": {
44
44
  "@wooksjs/event-http": "^0.4.35",
45
45
  "swagger-ui-dist": "^5.10.5",
46
46
  "zod-parser": "^0.0.3",
47
- "@moostjs/zod": "0.3.42",
47
+ "@moostjs/zod": "0.3.44",
48
48
  "@wooksjs/http-static": "^0.4.35"
49
49
  },
50
50
  "devDependencies": {