@moostjs/swagger 0.3.43 → 0.4.0
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 +40 -7
- package/dist/index.d.ts +9 -2
- package/dist/index.mjs +40 -7
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var moost = require('moost');
|
|
4
4
|
var eventHttp = require('@moostjs/event-http');
|
|
5
5
|
var zod = require('@moostjs/zod');
|
|
6
|
+
var eventHttp$1 = require('@wooksjs/event-http');
|
|
6
7
|
var httpStatic = require('@wooksjs/http-static');
|
|
7
8
|
var Path = require('path');
|
|
8
9
|
var swaggerUiDist = require('swagger-ui-dist');
|
|
@@ -120,6 +121,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
120
121
|
if (hh.type !== 'HTTP' || hmeta?.swaggerExclude || handler.registeredAs.length === 0) {
|
|
121
122
|
continue;
|
|
122
123
|
}
|
|
124
|
+
const uniqueParams = {};
|
|
123
125
|
const handlerPath = handler.registeredAs[0].path;
|
|
124
126
|
const handlerMethod = hh.method?.toLowerCase() || 'get';
|
|
125
127
|
const handlerDescription = hmeta?.description;
|
|
@@ -197,8 +199,29 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
197
199
|
responses,
|
|
198
200
|
};
|
|
199
201
|
const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
|
|
202
|
+
function addParam(param) {
|
|
203
|
+
const key = `${param.in}//${param.name}`;
|
|
204
|
+
if (uniqueParams[key]) {
|
|
205
|
+
uniqueParams[key].description = param.description ?? uniqueParams[key].description;
|
|
206
|
+
uniqueParams[key].required = param.required;
|
|
207
|
+
uniqueParams[key].schema = param.schema ?? uniqueParams[key].schema;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
uniqueParams[key] = param;
|
|
211
|
+
endpointSpec.parameters.push(param);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const param of cmeta?.swaggerParams || []) {
|
|
215
|
+
addParam({
|
|
216
|
+
name: param.name,
|
|
217
|
+
in: param.in,
|
|
218
|
+
description: param.description,
|
|
219
|
+
required: !!param.required,
|
|
220
|
+
schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
|
|
221
|
+
});
|
|
222
|
+
}
|
|
200
223
|
for (const param of hmeta?.swaggerParams || []) {
|
|
201
|
-
|
|
224
|
+
addParam({
|
|
202
225
|
name: param.name,
|
|
203
226
|
in: param.in,
|
|
204
227
|
description: param.description,
|
|
@@ -223,7 +246,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
223
246
|
parsed = myParseZod(zodType);
|
|
224
247
|
schema = getSwaggerSchema(parsed, true);
|
|
225
248
|
}
|
|
226
|
-
|
|
249
|
+
addParam({
|
|
227
250
|
name: paramName,
|
|
228
251
|
in: 'path',
|
|
229
252
|
description: paramMeta ? paramMeta.description : undefined,
|
|
@@ -581,11 +604,18 @@ function getSwaggerSchemaFromSwaggerConfigType(type) {
|
|
|
581
604
|
}
|
|
582
605
|
|
|
583
606
|
exports.SwaggerController = class SwaggerController {
|
|
584
|
-
constructor(
|
|
585
|
-
this.
|
|
607
|
+
constructor(opts = { title: 'Moost API' }) {
|
|
608
|
+
this.opts = opts;
|
|
586
609
|
this['assetPath'] = swaggerUiDist.getAbsoluteFSPath();
|
|
587
610
|
}
|
|
611
|
+
'processCors'() {
|
|
612
|
+
if (this.opts.cors) {
|
|
613
|
+
const { enableCors } = eventHttp$1.useSetHeaders();
|
|
614
|
+
enableCors(this.opts.cors === true ? undefined : this.opts.cors);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
588
617
|
'serveIndex'(url, location, status) {
|
|
618
|
+
this.processCors();
|
|
589
619
|
if (!url.endsWith('index.html') && !url.endsWith('/')) {
|
|
590
620
|
status.value = 302;
|
|
591
621
|
location.value = Path.join(url, '/');
|
|
@@ -595,7 +625,7 @@ exports.SwaggerController = class SwaggerController {
|
|
|
595
625
|
<html lang="en">
|
|
596
626
|
<head>
|
|
597
627
|
<meta charset="UTF-8">
|
|
598
|
-
<title>${this.title}</title>
|
|
628
|
+
<title>${this.opts.title}</title>
|
|
599
629
|
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
|
|
600
630
|
<link rel="stylesheet" type="text/css" href="index.css" />
|
|
601
631
|
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
|
|
@@ -611,6 +641,7 @@ exports.SwaggerController = class SwaggerController {
|
|
|
611
641
|
</html>`;
|
|
612
642
|
}
|
|
613
643
|
'swagger-initializer.js'() {
|
|
644
|
+
this.processCors();
|
|
614
645
|
return `window.onload = function() {
|
|
615
646
|
window.ui = SwaggerUIBundle({
|
|
616
647
|
url: "./spec.json",
|
|
@@ -628,15 +659,17 @@ exports.SwaggerController = class SwaggerController {
|
|
|
628
659
|
};`;
|
|
629
660
|
}
|
|
630
661
|
async 'spec.json'() {
|
|
662
|
+
this.processCors();
|
|
631
663
|
const logger = moost.useEventLogger('@moostjs/zod');
|
|
632
664
|
if (!this.spec) {
|
|
633
665
|
const { instantiate } = moost.useControllerContext();
|
|
634
666
|
const moost$1 = await instantiate(moost.Moost);
|
|
635
|
-
this.spec = mapToSwaggerSpec(moost$1.getControllersOverview(),
|
|
667
|
+
this.spec = mapToSwaggerSpec(moost$1.getControllersOverview(), this.opts, logger);
|
|
636
668
|
}
|
|
637
669
|
return this.spec;
|
|
638
670
|
}
|
|
639
671
|
'files'(url) {
|
|
672
|
+
this.processCors();
|
|
640
673
|
return this.serve(url.split('/').pop());
|
|
641
674
|
}
|
|
642
675
|
'serve'(path) {
|
|
@@ -688,7 +721,7 @@ exports.SwaggerController = __decorate([
|
|
|
688
721
|
SwaggerExclude(),
|
|
689
722
|
zod.ZodSkip(),
|
|
690
723
|
moost.Controller('api-docs'),
|
|
691
|
-
__param(0, moost.Const('Moost API')),
|
|
724
|
+
__param(0, moost.Const({ title: 'Moost API' })),
|
|
692
725
|
__metadata("design:paramtypes", [Object])
|
|
693
726
|
], exports.SwaggerController);
|
|
694
727
|
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,12 @@ type TFunction = Function;
|
|
|
7
7
|
interface TEmpty {
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
interface TSwaggerOptions {
|
|
11
|
+
title?: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
version?: string;
|
|
14
|
+
cors?: boolean | string;
|
|
15
|
+
}
|
|
10
16
|
interface TSwaggerSchema {
|
|
11
17
|
type?: string;
|
|
12
18
|
$ref?: string;
|
|
@@ -81,9 +87,10 @@ declare function SwaggerParam(opts: TSwaggerMate['swaggerParams'][number]): Meth
|
|
|
81
87
|
declare function SwaggerExample(example: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
82
88
|
|
|
83
89
|
declare class SwaggerController {
|
|
84
|
-
protected
|
|
85
|
-
constructor(
|
|
90
|
+
protected opts: TSwaggerOptions;
|
|
91
|
+
constructor(opts?: TSwaggerOptions);
|
|
86
92
|
'assetPath': string;
|
|
93
|
+
protected 'processCors'(): void;
|
|
87
94
|
'serveIndex'(url: string, location: THeaderHook, status: TStatusHook): string;
|
|
88
95
|
'swagger-initializer.js'(): string;
|
|
89
96
|
'spec'?: Record<string, unknown>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getMoostMate, Controller, Const, useEventLogger, useControllerContext, Moost } from 'moost';
|
|
2
2
|
import { Get, SetHeader, Url, HeaderHook, StatusHook } from '@moostjs/event-http';
|
|
3
3
|
import { getZodType, z, getZodTypeForProp, ZodSkip } from '@moostjs/zod';
|
|
4
|
+
import { useSetHeaders } from '@wooksjs/event-http';
|
|
4
5
|
import { serveFile } from '@wooksjs/http-static';
|
|
5
6
|
import Path from 'path';
|
|
6
7
|
import { getAbsoluteFSPath } from 'swagger-ui-dist';
|
|
@@ -118,6 +119,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
118
119
|
if (hh.type !== 'HTTP' || hmeta?.swaggerExclude || handler.registeredAs.length === 0) {
|
|
119
120
|
continue;
|
|
120
121
|
}
|
|
122
|
+
const uniqueParams = {};
|
|
121
123
|
const handlerPath = handler.registeredAs[0].path;
|
|
122
124
|
const handlerMethod = hh.method?.toLowerCase() || 'get';
|
|
123
125
|
const handlerDescription = hmeta?.description;
|
|
@@ -195,8 +197,29 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
195
197
|
responses,
|
|
196
198
|
};
|
|
197
199
|
const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
|
|
200
|
+
function addParam(param) {
|
|
201
|
+
const key = `${param.in}//${param.name}`;
|
|
202
|
+
if (uniqueParams[key]) {
|
|
203
|
+
uniqueParams[key].description = param.description ?? uniqueParams[key].description;
|
|
204
|
+
uniqueParams[key].required = param.required;
|
|
205
|
+
uniqueParams[key].schema = param.schema ?? uniqueParams[key].schema;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
uniqueParams[key] = param;
|
|
209
|
+
endpointSpec.parameters.push(param);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for (const param of cmeta?.swaggerParams || []) {
|
|
213
|
+
addParam({
|
|
214
|
+
name: param.name,
|
|
215
|
+
in: param.in,
|
|
216
|
+
description: param.description,
|
|
217
|
+
required: !!param.required,
|
|
218
|
+
schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
|
|
219
|
+
});
|
|
220
|
+
}
|
|
198
221
|
for (const param of hmeta?.swaggerParams || []) {
|
|
199
|
-
|
|
222
|
+
addParam({
|
|
200
223
|
name: param.name,
|
|
201
224
|
in: param.in,
|
|
202
225
|
description: param.description,
|
|
@@ -221,7 +244,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
221
244
|
parsed = myParseZod(zodType);
|
|
222
245
|
schema = getSwaggerSchema(parsed, true);
|
|
223
246
|
}
|
|
224
|
-
|
|
247
|
+
addParam({
|
|
225
248
|
name: paramName,
|
|
226
249
|
in: 'path',
|
|
227
250
|
description: paramMeta ? paramMeta.description : undefined,
|
|
@@ -579,11 +602,18 @@ function getSwaggerSchemaFromSwaggerConfigType(type) {
|
|
|
579
602
|
}
|
|
580
603
|
|
|
581
604
|
let SwaggerController = class SwaggerController {
|
|
582
|
-
constructor(
|
|
583
|
-
this.
|
|
605
|
+
constructor(opts = { title: 'Moost API' }) {
|
|
606
|
+
this.opts = opts;
|
|
584
607
|
this['assetPath'] = getAbsoluteFSPath();
|
|
585
608
|
}
|
|
609
|
+
'processCors'() {
|
|
610
|
+
if (this.opts.cors) {
|
|
611
|
+
const { enableCors } = useSetHeaders();
|
|
612
|
+
enableCors(this.opts.cors === true ? undefined : this.opts.cors);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
586
615
|
'serveIndex'(url, location, status) {
|
|
616
|
+
this.processCors();
|
|
587
617
|
if (!url.endsWith('index.html') && !url.endsWith('/')) {
|
|
588
618
|
status.value = 302;
|
|
589
619
|
location.value = Path.join(url, '/');
|
|
@@ -593,7 +623,7 @@ let SwaggerController = class SwaggerController {
|
|
|
593
623
|
<html lang="en">
|
|
594
624
|
<head>
|
|
595
625
|
<meta charset="UTF-8">
|
|
596
|
-
<title>${this.title}</title>
|
|
626
|
+
<title>${this.opts.title}</title>
|
|
597
627
|
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
|
|
598
628
|
<link rel="stylesheet" type="text/css" href="index.css" />
|
|
599
629
|
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
|
|
@@ -609,6 +639,7 @@ let SwaggerController = class SwaggerController {
|
|
|
609
639
|
</html>`;
|
|
610
640
|
}
|
|
611
641
|
'swagger-initializer.js'() {
|
|
642
|
+
this.processCors();
|
|
612
643
|
return `window.onload = function() {
|
|
613
644
|
window.ui = SwaggerUIBundle({
|
|
614
645
|
url: "./spec.json",
|
|
@@ -626,15 +657,17 @@ let SwaggerController = class SwaggerController {
|
|
|
626
657
|
};`;
|
|
627
658
|
}
|
|
628
659
|
async 'spec.json'() {
|
|
660
|
+
this.processCors();
|
|
629
661
|
const logger = useEventLogger('@moostjs/zod');
|
|
630
662
|
if (!this.spec) {
|
|
631
663
|
const { instantiate } = useControllerContext();
|
|
632
664
|
const moost = await instantiate(Moost);
|
|
633
|
-
this.spec = mapToSwaggerSpec(moost.getControllersOverview(),
|
|
665
|
+
this.spec = mapToSwaggerSpec(moost.getControllersOverview(), this.opts, logger);
|
|
634
666
|
}
|
|
635
667
|
return this.spec;
|
|
636
668
|
}
|
|
637
669
|
'files'(url) {
|
|
670
|
+
this.processCors();
|
|
638
671
|
return this.serve(url.split('/').pop());
|
|
639
672
|
}
|
|
640
673
|
'serve'(path) {
|
|
@@ -686,7 +719,7 @@ SwaggerController = __decorate([
|
|
|
686
719
|
SwaggerExclude(),
|
|
687
720
|
ZodSkip(),
|
|
688
721
|
Controller('api-docs'),
|
|
689
|
-
__param(0, Const('Moost API')),
|
|
722
|
+
__param(0, Const({ title: 'Moost API' })),
|
|
690
723
|
__metadata("design:paramtypes", [Object])
|
|
691
724
|
], SwaggerController);
|
|
692
725
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/swagger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "@moostjs/swagger",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -37,15 +37,15 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/swagger#readme",
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"moost": "0.
|
|
41
|
-
"@moostjs/event-http": "0.
|
|
40
|
+
"moost": "0.4.0",
|
|
41
|
+
"@moostjs/event-http": "0.4.0"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@wooksjs/event-http": "^0.
|
|
44
|
+
"@wooksjs/event-http": "^0.5.0",
|
|
45
45
|
"swagger-ui-dist": "^5.10.5",
|
|
46
46
|
"zod-parser": "^0.0.3",
|
|
47
|
-
"@moostjs/zod": "0.
|
|
48
|
-
"@wooksjs/http-static": "^0.
|
|
47
|
+
"@moostjs/zod": "0.4.0",
|
|
48
|
+
"@wooksjs/http-static": "^0.5.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@types/swagger-ui-dist": ""
|