@tsed/swagger 7.0.0-beta.1 → 7.0.0-beta.10
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/jest.config.js +14 -0
- package/lib/cjs/SwaggerModule.js +19 -22
- package/lib/cjs/SwaggerModule.js.map +1 -1
- package/lib/cjs/index.js +15 -5
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/SwaggerSettings.js.map +1 -1
- package/lib/cjs/interfaces/interfaces.js +3 -0
- package/lib/cjs/interfaces/interfaces.js.map +1 -0
- package/lib/cjs/middlewares/cssMiddleware.js +1 -1
- package/lib/cjs/middlewares/cssMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/jsMiddleware.js +1 -1
- package/lib/cjs/middlewares/jsMiddleware.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/SwaggerService.js +19 -78
- package/lib/cjs/services/SwaggerService.js.map +1 -1
- package/lib/cjs/utils/includeRoute.js +2 -2
- package/lib/cjs/utils/includeRoute.js.map +1 -1
- package/lib/cjs/utils/matchPath.js +1 -1
- package/lib/cjs/utils/matchPath.js.map +1 -1
- package/lib/esm/SwaggerModule.js +12 -15
- package/lib/esm/SwaggerModule.js.map +1 -1
- package/lib/esm/index.js +15 -5
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/SwaggerSettings.js.map +1 -1
- package/lib/esm/interfaces/interfaces.js +2 -0
- package/lib/esm/interfaces/interfaces.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/SwaggerService.js +16 -75
- package/lib/esm/services/SwaggerService.js.map +1 -1
- package/lib/esm/utils/includeRoute.js +1 -1
- package/lib/esm/utils/includeRoute.js.map +1 -1
- package/lib/types/SwaggerModule.d.ts +1 -1
- package/lib/types/index.d.ts +12 -2
- package/lib/types/interfaces/SwaggerSettings.d.ts +0 -3
- package/lib/types/interfaces/{index.d.ts → interfaces.d.ts} +0 -1
- package/lib/types/services/SwaggerService.d.ts +4 -22
- package/lib/types/utils/includeRoute.d.ts +1 -1
- package/package.json +26 -20
- package/readme.md +71 -85
- package/lib/cjs/interfaces/index.js +0 -5
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/utils/getSpecType.js +0 -21
- package/lib/cjs/utils/getSpecType.js.map +0 -1
- package/lib/cjs/utils/index.js +0 -11
- package/lib/cjs/utils/index.js.map +0 -1
- package/lib/cjs/utils/mapOpenSpec.js +0 -40
- package/lib/cjs/utils/mapOpenSpec.js.map +0 -1
- package/lib/cjs/utils/mapOpenSpec2.js +0 -35
- package/lib/cjs/utils/mapOpenSpec2.js.map +0 -1
- package/lib/cjs/utils/mapOpenSpec3.js +0 -26
- package/lib/cjs/utils/mapOpenSpec3.js.map +0 -1
- package/lib/cjs/utils/mapOpenSpecInfo.js +0 -21
- package/lib/cjs/utils/mapOpenSpecInfo.js.map +0 -1
- package/lib/esm/interfaces/index.js +0 -2
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/utils/getSpecType.js +0 -16
- package/lib/esm/utils/getSpecType.js.map +0 -1
- package/lib/esm/utils/index.js +0 -8
- package/lib/esm/utils/index.js.map +0 -1
- package/lib/esm/utils/mapOpenSpec.js +0 -36
- package/lib/esm/utils/mapOpenSpec.js.map +0 -1
- package/lib/esm/utils/mapOpenSpec2.js +0 -31
- package/lib/esm/utils/mapOpenSpec2.js.map +0 -1
- package/lib/esm/utils/mapOpenSpec3.js +0 -22
- package/lib/esm/utils/mapOpenSpec3.js.map +0 -1
- package/lib/esm/utils/mapOpenSpecInfo.js +0 -17
- package/lib/esm/utils/mapOpenSpecInfo.js.map +0 -1
- package/lib/types/utils/getSpecType.d.ts +0 -12
- package/lib/types/utils/index.d.ts +0 -7
- package/lib/types/utils/mapOpenSpec.d.ts +0 -10
- package/lib/types/utils/mapOpenSpec2.d.ts +0 -10
- package/lib/types/utils/mapOpenSpec3.d.ts +0 -8
- package/lib/types/utils/mapOpenSpecInfo.d.ts +0 -6
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "swagger"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
statements: 98.27,
|
|
9
|
+
branches: 80,
|
|
10
|
+
functions: 96.66,
|
|
11
|
+
lines: 98.19
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
package/lib/cjs/SwaggerModule.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SwaggerModule = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@tsed/common");
|
|
6
|
-
const fs_1 =
|
|
6
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
7
7
|
const path_1 = require("path");
|
|
8
8
|
const cssMiddleware_1 = require("./middlewares/cssMiddleware");
|
|
9
9
|
const indexMiddleware_1 = require("./middlewares/indexMiddleware");
|
|
@@ -11,7 +11,8 @@ const jsMiddleware_1 = require("./middlewares/jsMiddleware");
|
|
|
11
11
|
const redirectMiddleware_1 = require("./middlewares/redirectMiddleware");
|
|
12
12
|
const SwaggerService_1 = require("./services/SwaggerService");
|
|
13
13
|
const core_1 = require("@tsed/core");
|
|
14
|
-
const
|
|
14
|
+
const swagger_ui_dist_1 = require("swagger-ui-dist");
|
|
15
|
+
const swaggerUiPath = (0, swagger_ui_dist_1.absolutePath)();
|
|
15
16
|
/**
|
|
16
17
|
* @ignore
|
|
17
18
|
*/
|
|
@@ -43,13 +44,9 @@ let SwaggerModule = class SwaggerModule {
|
|
|
43
44
|
const host = this.configuration.getBestHost();
|
|
44
45
|
const url = host.toString();
|
|
45
46
|
const displayLog = (conf) => {
|
|
46
|
-
const { path = "/", fileName = "swagger.json", doc
|
|
47
|
+
const { path = "/", fileName = "swagger.json", doc } = conf;
|
|
47
48
|
this.injector.logger.info(`[${doc || "default"}] Swagger JSON is available on ${url}${(0, common_1.normalizePath)(path, fileName)}`);
|
|
48
49
|
this.injector.logger.info(`[${doc || "default"}] Swagger UI is available on ${url}${path}/`);
|
|
49
|
-
// istanbul ignore next
|
|
50
|
-
if (specVersion === "2.0") {
|
|
51
|
-
this.injector.logger.warn(`[${doc || "default"}] Swagger 2.0 is now deprecated. Please migrate to OpenSpec 3`);
|
|
52
|
-
}
|
|
53
50
|
};
|
|
54
51
|
this.settings.forEach((conf) => {
|
|
55
52
|
displayLog(conf);
|
|
@@ -58,10 +55,10 @@ let SwaggerModule = class SwaggerModule {
|
|
|
58
55
|
this.generateSpecFiles();
|
|
59
56
|
}
|
|
60
57
|
generateSpecFiles() {
|
|
61
|
-
return Promise.all(this.settings.map((conf) => {
|
|
58
|
+
return Promise.all(this.settings.map(async (conf) => {
|
|
62
59
|
const { outFile } = conf;
|
|
63
60
|
if (this.env === core_1.Env.PROD || outFile) {
|
|
64
|
-
const spec = this.swaggerService.getOpenAPISpec(conf);
|
|
61
|
+
const spec = await this.swaggerService.getOpenAPISpec(conf);
|
|
65
62
|
if (outFile) {
|
|
66
63
|
return fs_1.default.writeFile(outFile, JSON.stringify(spec, null, 2), { encoding: "utf8" }, () => { });
|
|
67
64
|
}
|
|
@@ -101,32 +98,32 @@ let SwaggerModule = class SwaggerModule {
|
|
|
101
98
|
return router;
|
|
102
99
|
}
|
|
103
100
|
middlewareSwaggerJson(conf) {
|
|
104
|
-
return (ctx) => {
|
|
105
|
-
ctx.response.status(200).body(this.swaggerService.getOpenAPISpec(conf));
|
|
101
|
+
return async (ctx) => {
|
|
102
|
+
ctx.response.status(200).body(await this.swaggerService.getOpenAPISpec(conf));
|
|
106
103
|
};
|
|
107
104
|
}
|
|
108
105
|
};
|
|
109
|
-
|
|
106
|
+
tslib_1.__decorate([
|
|
110
107
|
(0, common_1.Inject)(),
|
|
111
|
-
|
|
108
|
+
tslib_1.__metadata("design:type", common_1.InjectorService)
|
|
112
109
|
], SwaggerModule.prototype, "injector", void 0);
|
|
113
|
-
|
|
110
|
+
tslib_1.__decorate([
|
|
114
111
|
(0, common_1.Inject)(),
|
|
115
|
-
|
|
112
|
+
tslib_1.__metadata("design:type", common_1.PlatformApplication)
|
|
116
113
|
], SwaggerModule.prototype, "app", void 0);
|
|
117
|
-
|
|
114
|
+
tslib_1.__decorate([
|
|
118
115
|
(0, common_1.Configuration)(),
|
|
119
|
-
|
|
116
|
+
tslib_1.__metadata("design:type", Object)
|
|
120
117
|
], SwaggerModule.prototype, "configuration", void 0);
|
|
121
|
-
|
|
118
|
+
tslib_1.__decorate([
|
|
122
119
|
(0, common_1.Inject)(),
|
|
123
|
-
|
|
120
|
+
tslib_1.__metadata("design:type", SwaggerService_1.SwaggerService)
|
|
124
121
|
], SwaggerModule.prototype, "swaggerService", void 0);
|
|
125
|
-
|
|
122
|
+
tslib_1.__decorate([
|
|
126
123
|
(0, common_1.Constant)("env"),
|
|
127
|
-
|
|
124
|
+
tslib_1.__metadata("design:type", String)
|
|
128
125
|
], SwaggerModule.prototype, "env", void 0);
|
|
129
|
-
SwaggerModule =
|
|
126
|
+
SwaggerModule = tslib_1.__decorate([
|
|
130
127
|
(0, common_1.Module)()
|
|
131
128
|
], SwaggerModule);
|
|
132
129
|
exports.SwaggerModule = SwaggerModule;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwaggerModule.js","sourceRoot":"","sources":["../../src/SwaggerModule.ts"],"names":[],"mappings":";;;;AAAA,yCAasB;AACtB,
|
|
1
|
+
{"version":3,"file":"SwaggerModule.js","sourceRoot":"","sources":["../../src/SwaggerModule.ts"],"names":[],"mappings":";;;;AAAA,yCAasB;AACtB,oDAAoB;AACpB,+BAA0B;AAE1B,+DAA0D;AAC1D,mEAA8D;AAC9D,6DAAwD;AACxD,yEAAoE;AACpE,8DAAyD;AACz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,IAAA,sBAAa,EAAC,IAAA,iCAAe,EAAC,QAAQ,EAAE,EAAC,IAAI,EAAE,GAAG,IAAI,EAAC,CAAC,CAAC,CAAC,CAAC;YAC3E,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,EAAC,IAAI,EAAE,aAAa,EAAC,CAAC,CAAC;SAC5C;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,qBAAqB,CAAC,IAAqB;QACjD,OAAO,KAAK,EAAE,GAAoB,EAAE,EAAE;YACpC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC;QAChF,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AA3HC;IADC,IAAA,eAAM,GAAE;sCACC,wBAAe;+CAAC;AAG1B;IADC,IAAA,eAAM,GAAE;sCACJ,4BAAmB;0CAAC;AAGzB;IADC,IAAA,sBAAa,GAAE;;oDACa;AAG7B;IADC,IAAA,eAAM,GAAE;sCACO,+BAAc;qDAAC;AAG/B;IADC,IAAA,iBAAQ,EAAC,KAAK,CAAC;;0CACP;AAdE,aAAa;IADzB,IAAA,eAAM,GAAE;GACI,aAAa,CA6HzB;AA7HY,sCAAa","sourcesContent":["import {\n OnRoutesInit,\n Configuration,\n Constant,\n Inject,\n InjectorService,\n Module,\n normalizePath,\n OnReady,\n PlatformApplication,\n PlatformContext,\n PlatformRouter,\n useCtxHandler\n} from \"@tsed/common\";\nimport Fs from \"fs\";\nimport {join} from \"path\";\nimport {SwaggerSettings} from \"./interfaces/SwaggerSettings\";\nimport {cssMiddleware} from \"./middlewares/cssMiddleware\";\nimport {indexMiddleware} from \"./middlewares/indexMiddleware\";\nimport {jsMiddleware} from \"./middlewares/jsMiddleware\";\nimport {redirectMiddleware} from \"./middlewares/redirectMiddleware\";\nimport {SwaggerService} from \"./services/SwaggerService\";\nimport {Env} from \"@tsed/core\";\nimport {absolutePath} from \"swagger-ui-dist\";\n\nconst swaggerUiPath = absolutePath();\n\n/**\n * @ignore\n */\n@Module()\nexport class SwaggerModule implements OnRoutesInit, OnReady {\n @Inject()\n injector: InjectorService;\n\n @Inject()\n app: PlatformApplication;\n\n @Configuration()\n configuration: Configuration;\n\n @Inject()\n swaggerService: SwaggerService;\n\n @Constant(\"env\")\n env: Env;\n\n private loaded = false;\n\n get settings() {\n return ([] as SwaggerSettings[]).concat(this.configuration.get<SwaggerSettings[]>(\"swagger\")).filter((o) => !!o);\n }\n\n /**\n *\n */\n $onRoutesInit() {\n if (this.loaded) {\n return;\n }\n\n const urls: any[] = this.getUrls();\n\n this.settings.forEach((conf: SwaggerSettings) => {\n const {path = \"/\"} = conf;\n\n this.app.get(path, useCtxHandler(redirectMiddleware(path)));\n this.app.use(path, this.createRouter(conf, urls));\n });\n\n this.loaded = true;\n }\n\n $onReady() {\n // istanbul ignore next\n if (this.configuration.getBestHost) {\n const host = this.configuration.getBestHost();\n const url = host.toString();\n\n const displayLog = (conf: SwaggerSettings) => {\n const {path = \"/\", fileName = \"swagger.json\", doc} = conf;\n\n this.injector.logger.info(`[${doc || \"default\"}] Swagger JSON is available on ${url}${normalizePath(path, fileName)}`);\n this.injector.logger.info(`[${doc || \"default\"}] Swagger UI is available on ${url}${path}/`);\n };\n\n this.settings.forEach((conf) => {\n displayLog(conf);\n });\n }\n\n this.generateSpecFiles();\n }\n\n generateSpecFiles() {\n return Promise.all(\n this.settings.map(async (conf) => {\n const {outFile} = conf;\n\n if (this.env === Env.PROD || outFile) {\n const spec = await this.swaggerService.getOpenAPISpec(conf);\n\n if (outFile) {\n return Fs.writeFile(outFile, JSON.stringify(spec, null, 2), {encoding: \"utf8\"}, () => {});\n }\n }\n })\n );\n }\n\n private getUrls() {\n return this.settings.reduce((acc: any[], conf) => {\n const {path = \"/\", fileName = \"swagger.json\", doc, hidden} = conf;\n\n if (!hidden) {\n acc.push({url: normalizePath(path, fileName), name: doc || path});\n }\n\n return acc;\n }, []);\n }\n\n /**\n *\n * @param conf\n * @param urls\n */\n private createRouter(conf: SwaggerSettings, urls: string[]) {\n const {disableSpec = false, fileName = \"swagger.json\", cssPath, jsPath, viewPath = join(__dirname, \"../views/index.ejs\")} = conf;\n const router = PlatformRouter.create(this.injector);\n\n if (!disableSpec) {\n router.get(normalizePath(\"/\", fileName), useCtxHandler(this.middlewareSwaggerJson(conf)));\n }\n\n if (viewPath) {\n if (cssPath) {\n router.get(\"/main.css\", useCtxHandler(cssMiddleware(cssPath)));\n }\n\n if (jsPath) {\n router.get(\"/main.js\", useCtxHandler(jsMiddleware(jsPath)));\n }\n\n router.get(\"/\", useCtxHandler(indexMiddleware(viewPath, {urls, ...conf})));\n router.statics(\"/\", {root: swaggerUiPath});\n }\n\n return router;\n }\n\n private middlewareSwaggerJson(conf: SwaggerSettings) {\n return async (ctx: PlatformContext) => {\n ctx.response.status(200).body(await this.swaggerService.getOpenAPISpec(conf));\n };\n }\n}\n"]}
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Automatically generated by barrelsby.
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const tslib_1 = require("tslib");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
tslib_1.__exportStar(require("./SwaggerModule"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./decorators/docs"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./decorators/hidden"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./interfaces/SwaggerSettings"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./interfaces/interfaces"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./middlewares/cssMiddleware"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./middlewares/indexMiddleware"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./middlewares/jsMiddleware"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./middlewares/redirectMiddleware"), exports);
|
|
16
|
+
tslib_1.__exportStar(require("./services/SwaggerService"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./utils/includeRoute"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./utils/matchPath"), exports);
|
|
9
19
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,0DAAgC;AAChC,4DAAkC;AAClC,8DAAoC;AACpC,uEAA6C;AAC7C,kEAAwC;AACxC,sEAA4C;AAC5C,wEAA8C;AAC9C,qEAA2C;AAC3C,2EAAiD;AACjD,oEAA0C;AAC1C,+DAAqC;AACrC,4DAAkC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./SwaggerModule\";\nexport * from \"./decorators/docs\";\nexport * from \"./decorators/hidden\";\nexport * from \"./interfaces/SwaggerSettings\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./middlewares/cssMiddleware\";\nexport * from \"./middlewares/indexMiddleware\";\nexport * from \"./middlewares/jsMiddleware\";\nexport * from \"./middlewares/redirectMiddleware\";\nexport * from \"./services/SwaggerService\";\nexport * from \"./utils/includeRoute\";\nexport * from \"./utils/matchPath\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwaggerSettings.js","sourceRoot":"","sources":["../../../src/interfaces/SwaggerSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {OpenSpec2, OpenSpec3, OpenSpecVersions, OS2Versions, OS3Versions} from \"@tsed/openspec\";\n\nexport interface SwaggerUIOptions {\n configUrl?: string;\n url?: string;\n urls?: {url: string; name: string; primaryName?: string}[];\n layout?: string;\n validatorUrl?: string;\n oauth?: any;\n authorize?: any;\n\n [key: string]: any;\n}\n\nexport interface SwaggerSettingsBase {\n /**\n * The url subpath to access to the documentation.\n */\n path: string;\n /**\n * Specify the spec version you want to generate.\n */\n specVersion?: OpenSpecVersions;\n /**\n * Swagger file name. By default swagger.json\n */\n fileName?: string;\n /**\n * Hidden documentation. Use this to hide documentation link in the dropdown.\n */\n hidden?: string;\n /**\n * The documentation key used by `@Docs` decorator to create several swagger documentations.\n */\n doc?: string;\n /**\n * The path to the CSS file.\n */\n cssPath?: string;\n /**\n * The path to the JS file.\n */\n jsPath?: string;\n /**\n * The path to the ejs file to create html page.\n */\n viewPath?: string | false;\n /**\n * SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md)\n */\n options?: SwaggerUIOptions;\n /**\n * Display the search field in the navbar.\n */\n showExplorer?: boolean;\n /**\n * Load the base spec documentation from the specified path.\n */\n specPath?: string;\n /**\n * Write the `swagger.json` spec documentation on the specified path.\n */\n outFile?: string;\n /**\n * A function to generate the operationId.\n */\n operationIdFormatter?: (name: string, propertyKey: string, path: string) => string;\n /**\n * A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name).\n */\n operationIdPattern?: string;\n /**\n *\n */\n disableSpec?: boolean;\n /**\n * Include only controllers whose paths match the pattern list provided.\n */\n pathPatterns?: string[];\n}\n\
|
|
1
|
+
{"version":3,"file":"SwaggerSettings.js","sourceRoot":"","sources":["../../../src/interfaces/SwaggerSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {OpenSpec2, OpenSpec3, OpenSpecVersions, OS2Versions, OS3Versions} from \"@tsed/openspec\";\n\nexport interface SwaggerUIOptions {\n configUrl?: string;\n url?: string;\n urls?: {url: string; name: string; primaryName?: string}[];\n layout?: string;\n validatorUrl?: string;\n oauth?: any;\n authorize?: any;\n\n [key: string]: any;\n}\n\nexport interface SwaggerSettingsBase {\n /**\n * The url subpath to access to the documentation.\n */\n path: string;\n /**\n * Specify the spec version you want to generate.\n */\n specVersion?: OpenSpecVersions;\n /**\n * Swagger file name. By default swagger.json\n */\n fileName?: string;\n /**\n * Hidden documentation. Use this to hide documentation link in the dropdown.\n */\n hidden?: string;\n /**\n * The documentation key used by `@Docs` decorator to create several swagger documentations.\n */\n doc?: string;\n /**\n * The path to the CSS file.\n */\n cssPath?: string;\n /**\n * The path to the JS file.\n */\n jsPath?: string;\n /**\n * The path to the ejs file to create html page.\n */\n viewPath?: string | false;\n /**\n * SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md)\n */\n options?: SwaggerUIOptions;\n /**\n * Display the search field in the navbar.\n */\n showExplorer?: boolean;\n /**\n * Load the base spec documentation from the specified path.\n */\n specPath?: string;\n /**\n * Write the `swagger.json` spec documentation on the specified path.\n */\n outFile?: string;\n /**\n * A function to generate the operationId.\n */\n operationIdFormatter?: (name: string, propertyKey: string, path: string) => string;\n /**\n * A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name).\n */\n operationIdPattern?: string;\n /**\n *\n */\n disableSpec?: boolean;\n /**\n * Include only controllers whose paths match the pattern list provided.\n */\n pathPatterns?: string[];\n}\n\nexport interface SwaggerOS2Settings extends SwaggerSettingsBase {\n specVersion?: OS2Versions;\n /**\n * OpenSpec 2\n */\n spec?: Partial<OpenSpec2>;\n}\n\nexport interface SwaggerOS3Settings extends SwaggerSettingsBase {\n specVersion?: OS3Versions;\n /**\n * OpenSpec 3\n */\n spec?: Partial<OpenSpec3>;\n}\n\nexport type SwaggerSettings = SwaggerOS2Settings | SwaggerOS3Settings;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import {SwaggerSettings} from \"./SwaggerSettings\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n swagger: SwaggerSettings[];\n }\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cssMiddleware = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const fs_1 =
|
|
5
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
6
|
const path_1 = require("path");
|
|
7
7
|
/**
|
|
8
8
|
* @ignore
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cssMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/cssMiddleware.ts"],"names":[],"mappings":";;;;AACA,
|
|
1
|
+
{"version":3,"file":"cssMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/cssMiddleware.ts"],"names":[],"mappings":";;;;AACA,oDAAoB;AACpB,+BAA6B;AAE7B;;;GAGG;AACH,SAAgB,aAAa,CAAC,IAAY;IACxC,OAAO,CAAC,GAAoB,EAAE,EAAE;QAC9B,MAAM,OAAO,GAAG,YAAE,CAAC,YAAY,CAAC,IAAA,cAAO,EAAC,IAAI,CAAC,EAAE,EAAC,QAAQ,EAAE,MAAM,EAAC,CAAC,CAAC;QACnE,GAAG,CAAC,QAAQ;aACT,UAAU,CAAC;YACV,cAAc,EAAE,UAAU;SAC3B,CAAC;aACD,MAAM,CAAC,GAAG,CAAC;aACX,IAAI,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC;AAVD,sCAUC","sourcesContent":["import {PlatformContext} from \"@tsed/common\";\nimport Fs from \"fs\";\nimport {resolve} from \"path\";\n\n/**\n * @ignore\n * @param path\n */\nexport function cssMiddleware(path: string) {\n return (ctx: PlatformContext) => {\n const content = Fs.readFileSync(resolve(path), {encoding: \"utf8\"});\n ctx.response\n .setHeaders({\n \"Content-Type\": \"text/css\"\n })\n .status(200)\n .body(content);\n };\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jsMiddleware = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const fs_1 =
|
|
5
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
6
6
|
const path_1 = require("path");
|
|
7
7
|
/**
|
|
8
8
|
* @ignore
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/jsMiddleware.ts"],"names":[],"mappings":";;;;AACA,
|
|
1
|
+
{"version":3,"file":"jsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/jsMiddleware.ts"],"names":[],"mappings":";;;;AACA,oDAAoB;AACpB,+BAA6B;AAE7B;;;GAGG;AACH,SAAgB,YAAY,CAAC,IAAY;IACvC,OAAO,CAAC,GAAoB,EAAE,EAAE;QAC9B,MAAM,OAAO,GAAG,YAAE,CAAC,YAAY,CAAC,IAAA,cAAO,EAAC,IAAI,CAAC,EAAE,EAAC,QAAQ,EAAE,MAAM,EAAC,CAAC,CAAC;QACnE,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAC,cAAc,EAAE,wBAAwB,EAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAChG,CAAC,CAAC;AACJ,CAAC;AALD,oCAKC","sourcesContent":["import {PlatformContext} from \"@tsed/common\";\nimport Fs from \"fs\";\nimport {resolve} from \"path\";\n\n/**\n * @ignore\n * @param path\n */\nexport function jsMiddleware(path: string) {\n return (ctx: PlatformContext) => {\n const content = Fs.readFileSync(resolve(path), {encoding: \"utf8\"});\n ctx.response.setHeaders({\"Content-Type\": \"application/javascript\"}).status(200).body(content);\n };\n}\n"]}
|
|
@@ -4,12 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.SwaggerService = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const common_1 = require("@tsed/common");
|
|
7
|
-
const core_1 = require("@tsed/core");
|
|
8
7
|
const schema_1 = require("@tsed/schema");
|
|
9
|
-
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
|
|
10
|
-
const getSpecType_1 = require("../utils/getSpecType");
|
|
11
8
|
const includeRoute_1 = require("../utils/includeRoute");
|
|
12
|
-
const mapOpenSpec_1 = require("../utils/mapOpenSpec");
|
|
13
9
|
let SwaggerService = class SwaggerService {
|
|
14
10
|
constructor(injectorService, platform, configuration) {
|
|
15
11
|
this.injectorService = injectorService;
|
|
@@ -17,86 +13,31 @@ let SwaggerService = class SwaggerService {
|
|
|
17
13
|
this.configuration = configuration;
|
|
18
14
|
_SwaggerService_specs.set(this, new Map());
|
|
19
15
|
}
|
|
20
|
-
getOpenAPISpec(conf) {
|
|
21
|
-
if (!
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
this.platform.getMountedControllers().forEach(({ route, provider }) => {
|
|
37
|
-
if ((0, includeRoute_1.includeRoute)(route, provider, conf)) {
|
|
38
|
-
const spec = this.buildRoutes(provider, {
|
|
39
|
-
...options,
|
|
40
|
-
rootPath: route.replace(provider.path, "")
|
|
41
|
-
});
|
|
42
|
-
options.append(spec);
|
|
43
|
-
}
|
|
16
|
+
async getOpenAPISpec(conf) {
|
|
17
|
+
if (!tslib_1.__classPrivateFieldGet(this, _SwaggerService_specs, "f").has(conf.path)) {
|
|
18
|
+
const { version = "1.0.0", acceptMimes } = this.configuration;
|
|
19
|
+
const specPath = conf.specPath ? this.configuration.resolve(conf.specPath) : conf.specPath;
|
|
20
|
+
const tokens = this.platform
|
|
21
|
+
.getMountedControllers()
|
|
22
|
+
.filter(({ route, provider }) => (0, includeRoute_1.includeRoute)(route, provider, conf))
|
|
23
|
+
.map(({ route, provider }) => ({ token: provider.token, rootPath: route.replace(provider.path, "") }));
|
|
24
|
+
const spec = await (0, schema_1.generateSpec)({
|
|
25
|
+
tokens,
|
|
26
|
+
...conf,
|
|
27
|
+
specPath,
|
|
28
|
+
version,
|
|
29
|
+
acceptMimes
|
|
44
30
|
});
|
|
45
|
-
|
|
31
|
+
tslib_1.__classPrivateFieldGet(this, _SwaggerService_specs, "f").set(conf.path, spec);
|
|
46
32
|
}
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Return the global api information.
|
|
51
|
-
*/
|
|
52
|
-
getDefaultSpec(conf) {
|
|
53
|
-
const { version = "1.0.0", acceptMimes } = this.configuration;
|
|
54
|
-
const { specPath, specVersion } = conf;
|
|
55
|
-
const fileSpec = specPath ? this.readSpecPath(specPath) : {};
|
|
56
|
-
return (0, mapOpenSpec_1.mapOpenSpec)((0, core_1.getValue)(conf, "spec", {}), {
|
|
57
|
-
fileSpec,
|
|
58
|
-
version,
|
|
59
|
-
specVersion,
|
|
60
|
-
acceptMimes
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
readSpecPath(path) {
|
|
64
|
-
path = this.configuration.resolve(path);
|
|
65
|
-
if (fs_1.default.existsSync(path)) {
|
|
66
|
-
const json = fs_1.default.readFileSync(path, { encoding: "utf8" });
|
|
67
|
-
/* istanbul ignore else */
|
|
68
|
-
if (json !== "") {
|
|
69
|
-
return JSON.parse(json);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return {};
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
*
|
|
76
|
-
* @param ctrl
|
|
77
|
-
* @param options
|
|
78
|
-
*/
|
|
79
|
-
buildRoutes(ctrl, options) {
|
|
80
|
-
const rootPath = options.rootPath + ctrl.path;
|
|
81
|
-
ctrl.children
|
|
82
|
-
.map((ctrl) => this.injectorService.getProvider(ctrl))
|
|
83
|
-
.forEach((provider) => {
|
|
84
|
-
if (!provider.store.get("hidden")) {
|
|
85
|
-
const spec = this.buildRoutes(provider, {
|
|
86
|
-
...options,
|
|
87
|
-
rootPath
|
|
88
|
-
});
|
|
89
|
-
options.append(spec);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
return (0, schema_1.getSpec)(ctrl.token, options);
|
|
33
|
+
return tslib_1.__classPrivateFieldGet(this, _SwaggerService_specs, "f").get(conf.path);
|
|
93
34
|
}
|
|
94
35
|
};
|
|
95
36
|
_SwaggerService_specs = new WeakMap();
|
|
96
|
-
SwaggerService =
|
|
37
|
+
SwaggerService = tslib_1.__decorate([
|
|
97
38
|
(0, common_1.Injectable)(),
|
|
98
|
-
|
|
99
|
-
|
|
39
|
+
tslib_1.__param(2, (0, common_1.Configuration)()),
|
|
40
|
+
tslib_1.__metadata("design:paramtypes", [common_1.InjectorService,
|
|
100
41
|
common_1.Platform, Object])
|
|
101
42
|
], SwaggerService);
|
|
102
43
|
exports.SwaggerService = SwaggerService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwaggerService.js","sourceRoot":"","sources":["../../../src/services/SwaggerService.ts"],"names":[],"mappings":";;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"SwaggerService.js","sourceRoot":"","sources":["../../../src/services/SwaggerService.ts"],"names":[],"mappings":";;;;;AAAA,yCAAkF;AAElF,yCAA0C;AAE1C,wDAAmD;AAGnD,IAAa,cAAc,GAA3B,MAAa,cAAc;IAGzB,YACU,eAAgC,EAChC,QAAkB,EACD,aAA4B;QAF7C,oBAAe,GAAf,eAAe,CAAiB;QAChC,aAAQ,GAAR,QAAQ,CAAU;QACD,kBAAa,GAAb,aAAa,CAAe;QALvD,gCAA6C,IAAI,GAAG,EAAE,EAAC;IAMpD,CAAC;IASG,KAAK,CAAC,cAAc,CAAC,IAAqB;QAC/C,IAAI,CAAC,+BAAA,IAAI,6BAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YAC/B,MAAM,EAAC,OAAO,GA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\"@tsed/schema\";\nimport {SwaggerOS2Settings, SwaggerOS3Settings, SwaggerSettings} from \"../interfaces/SwaggerSettings\";\nimport {includeRoute} from \"../utils/includeRoute\";\n\n@Injectable()\nexport class SwaggerService {\n #specs: Map<string, OpenSpec3 | OpenSpec2> = new Map();\n\n constructor(\n private injectorService: InjectorService,\n private platform: Platform,\n @Configuration() private configuration: Configuration\n ) {}\n\n /**\n * Generate Spec for the given configuration\n * @returns {Spec}\n */\n public async getOpenAPISpec(conf: SwaggerOS3Settings): Promise<OpenSpec3>;\n public async getOpenAPISpec(conf: SwaggerOS2Settings): Promise<OpenSpec2>;\n public async getOpenAPISpec(conf: SwaggerSettings): Promise<OpenSpec2>;\n public async getOpenAPISpec(conf: SwaggerSettings) {\n if (!this.#specs.has(conf.path)) {\n const {version = \"1.0.0\", acceptMimes} = this.configuration;\n const specPath = conf.specPath ? this.configuration.resolve(conf.specPath) : conf.specPath;\n\n const tokens = this.platform\n .getMountedControllers()\n .filter(({route, provider}) => includeRoute(route, provider, conf))\n .map(({route, provider}) => ({token: provider.token, rootPath: route.replace(provider.path, \"\")}));\n\n const spec = await generateSpec({\n tokens,\n ...conf,\n specPath,\n version,\n acceptMimes\n });\n\n this.#specs.set(conf.path, spec);\n }\n\n return this.#specs.get(conf.path);\n }\n}\n"]}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.includeRoute = void 0;
|
|
4
|
-
const
|
|
4
|
+
const matchPath_1 = require("./matchPath");
|
|
5
5
|
function includeRoute(route, provider, conf) {
|
|
6
6
|
const hidden = provider.store.get("hidden");
|
|
7
7
|
const docs = provider.store.get("docs") || [];
|
|
8
8
|
const { doc, pathPatterns } = conf;
|
|
9
9
|
const inDoc = doc && docs.indexOf(doc) > -1;
|
|
10
|
-
const pathPatternMatch = pathPatterns && (0,
|
|
10
|
+
const pathPatternMatch = pathPatterns && (0, matchPath_1.matchPath)(route, pathPatterns);
|
|
11
11
|
if (hidden) {
|
|
12
12
|
return false;
|
|
13
13
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"includeRoute.js","sourceRoot":"","sources":["../../../src/utils/includeRoute.ts"],"names":[],"mappings":";;;AACA,
|
|
1
|
+
{"version":3,"file":"includeRoute.js","sourceRoot":"","sources":["../../../src/utils/includeRoute.ts"],"names":[],"mappings":";;;AACA,2CAAsC;AAGtC,SAAgB,YAAY,CAAC,KAAa,EAAE,QAA4B,EAAE,IAAqB;IAC7F,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC9C,MAAM,EAAC,GAAG,EAAE,YAAY,EAAC,GAAG,IAAI,CAAC;IACjC,MAAM,KAAK,GAAG,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,MAAM,gBAAgB,GAAG,YAAY,IAAI,IAAA,qBAAS,EAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IAExE,IAAI,MAAM,EAAE;QACV,OAAO,KAAK,CAAC;KACd;IAED,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,EAAE;QACzB,OAAO,IAAI,CAAC;KACb;IAED,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,gBAAgB,CAAC,CAAC;AACvC,CAAC;AAhBD,oCAgBC","sourcesContent":["import {ControllerProvider} from \"@tsed/common\";\nimport {matchPath} from \"./matchPath\";\nimport {SwaggerSettings} from \"../interfaces/SwaggerSettings\";\n\nexport function includeRoute(route: string, provider: ControllerProvider, conf: SwaggerSettings): boolean {\n const hidden = provider.store.get(\"hidden\");\n const docs = provider.store.get(\"docs\") || [];\n const {doc, pathPatterns} = conf;\n const inDoc = doc && docs.indexOf(doc) > -1;\n const pathPatternMatch = pathPatterns && matchPath(route, pathPatterns);\n\n if (hidden) {\n return false;\n }\n\n if (!doc && !pathPatterns) {\n return true;\n }\n\n return !!(inDoc || pathPatternMatch);\n}\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.matchPath = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const micromatch_1 =
|
|
5
|
+
const micromatch_1 = tslib_1.__importDefault(require("micromatch"));
|
|
6
6
|
function matchPath(path, patterns) {
|
|
7
7
|
return patterns && patterns.length ? micromatch_1.default.isMatch(path, patterns) : true;
|
|
8
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matchPath.js","sourceRoot":"","sources":["../../../src/utils/matchPath.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"matchPath.js","sourceRoot":"","sources":["../../../src/utils/matchPath.ts"],"names":[],"mappings":";;;;AAAA,oEAAoC;AAEpC,SAAgB,SAAS,CAAC,IAAY,EAAE,QAAmB;IACzD,OAAO,QAAQ,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,oBAAU,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACjF,CAAC;AAFD,8BAEC","sourcesContent":["import micromatch from \"micromatch\";\n\nexport function matchPath(path: string, patterns?: string[]) {\n return patterns && patterns.length ? micromatch.isMatch(path, patterns) : true;\n}\n"]}
|
package/lib/esm/SwaggerModule.js
CHANGED
|
@@ -2,13 +2,14 @@ import { __decorate, __metadata } from "tslib";
|
|
|
2
2
|
import { Configuration, Constant, Inject, InjectorService, Module, normalizePath, PlatformApplication, PlatformRouter, useCtxHandler } from "@tsed/common";
|
|
3
3
|
import Fs from "fs";
|
|
4
4
|
import { join } from "path";
|
|
5
|
-
import { cssMiddleware } from "./middlewares/cssMiddleware";
|
|
6
|
-
import { indexMiddleware } from "./middlewares/indexMiddleware";
|
|
7
|
-
import { jsMiddleware } from "./middlewares/jsMiddleware";
|
|
8
|
-
import { redirectMiddleware } from "./middlewares/redirectMiddleware";
|
|
9
|
-
import { SwaggerService } from "./services/SwaggerService";
|
|
5
|
+
import { cssMiddleware } from "./middlewares/cssMiddleware.js";
|
|
6
|
+
import { indexMiddleware } from "./middlewares/indexMiddleware.js";
|
|
7
|
+
import { jsMiddleware } from "./middlewares/jsMiddleware.js";
|
|
8
|
+
import { redirectMiddleware } from "./middlewares/redirectMiddleware.js";
|
|
9
|
+
import { SwaggerService } from "./services/SwaggerService.js";
|
|
10
10
|
import { Env } from "@tsed/core";
|
|
11
|
-
|
|
11
|
+
import { absolutePath } from "swagger-ui-dist";
|
|
12
|
+
const swaggerUiPath = absolutePath();
|
|
12
13
|
/**
|
|
13
14
|
* @ignore
|
|
14
15
|
*/
|
|
@@ -40,13 +41,9 @@ let SwaggerModule = class SwaggerModule {
|
|
|
40
41
|
const host = this.configuration.getBestHost();
|
|
41
42
|
const url = host.toString();
|
|
42
43
|
const displayLog = (conf) => {
|
|
43
|
-
const { path = "/", fileName = "swagger.json", doc
|
|
44
|
+
const { path = "/", fileName = "swagger.json", doc } = conf;
|
|
44
45
|
this.injector.logger.info(`[${doc || "default"}] Swagger JSON is available on ${url}${normalizePath(path, fileName)}`);
|
|
45
46
|
this.injector.logger.info(`[${doc || "default"}] Swagger UI is available on ${url}${path}/`);
|
|
46
|
-
// istanbul ignore next
|
|
47
|
-
if (specVersion === "2.0") {
|
|
48
|
-
this.injector.logger.warn(`[${doc || "default"}] Swagger 2.0 is now deprecated. Please migrate to OpenSpec 3`);
|
|
49
|
-
}
|
|
50
47
|
};
|
|
51
48
|
this.settings.forEach((conf) => {
|
|
52
49
|
displayLog(conf);
|
|
@@ -55,10 +52,10 @@ let SwaggerModule = class SwaggerModule {
|
|
|
55
52
|
this.generateSpecFiles();
|
|
56
53
|
}
|
|
57
54
|
generateSpecFiles() {
|
|
58
|
-
return Promise.all(this.settings.map((conf) => {
|
|
55
|
+
return Promise.all(this.settings.map(async (conf) => {
|
|
59
56
|
const { outFile } = conf;
|
|
60
57
|
if (this.env === Env.PROD || outFile) {
|
|
61
|
-
const spec = this.swaggerService.getOpenAPISpec(conf);
|
|
58
|
+
const spec = await this.swaggerService.getOpenAPISpec(conf);
|
|
62
59
|
if (outFile) {
|
|
63
60
|
return Fs.writeFile(outFile, JSON.stringify(spec, null, 2), { encoding: "utf8" }, () => { });
|
|
64
61
|
}
|
|
@@ -98,8 +95,8 @@ let SwaggerModule = class SwaggerModule {
|
|
|
98
95
|
return router;
|
|
99
96
|
}
|
|
100
97
|
middlewareSwaggerJson(conf) {
|
|
101
|
-
return (ctx) => {
|
|
102
|
-
ctx.response.status(200).body(this.swaggerService.getOpenAPISpec(conf));
|
|
98
|
+
return async (ctx) => {
|
|
99
|
+
ctx.response.status(200).body(await this.swaggerService.getOpenAPISpec(conf));
|
|
103
100
|
};
|
|
104
101
|
}
|
|
105
102
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwaggerModule.js","sourceRoot":"","sources":["../../src/SwaggerModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAEL,aAAa,EACb,QAAQ,EACR,MAAM,EACN,eAAe,EACf,MAAM,EACN,aAAa,EAEb,mBAAmB,EAEnB,cAAc,EACd,aAAa,EACd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAC,IAAI,EAAC,MAAM,MAAM,CAAC;AAE1B,OAAO,EAAC,aAAa,EAAC,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAC,eAAe,EAAC,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAC,YAAY,EAAC,MAAM,4BAA4B,CAAC;AACxD,OAAO,EAAC,kBAAkB,EAAC,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAC,cAAc,EAAC,MAAM,2BAA2B,CA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8BACO,cAAc;qDAAC;AAG/B;IADC,QAAQ,CAAC,KAAK,CAAC;;0CACP;AAdE,aAAa;IADzB,MAAM,EAAE;GACI,aAAa,CA6HzB;SA7HY,aAAa","sourcesContent":["import {\n OnRoutesInit,\n Configuration,\n Constant,\n Inject,\n InjectorService,\n Module,\n normalizePath,\n OnReady,\n PlatformApplication,\n PlatformContext,\n PlatformRouter,\n useCtxHandler\n} from \"@tsed/common\";\nimport Fs from \"fs\";\nimport {join} from \"path\";\nimport {SwaggerSettings} from \"./interfaces/SwaggerSettings\";\nimport {cssMiddleware} from \"./middlewares/cssMiddleware\";\nimport {indexMiddleware} from \"./middlewares/indexMiddleware\";\nimport {jsMiddleware} from \"./middlewares/jsMiddleware\";\nimport {redirectMiddleware} from \"./middlewares/redirectMiddleware\";\nimport {SwaggerService} from \"./services/SwaggerService\";\nimport {Env} from \"@tsed/core\";\nimport {absolutePath} from \"swagger-ui-dist\";\n\nconst swaggerUiPath = absolutePath();\n\n/**\n * @ignore\n */\n@Module()\nexport class SwaggerModule implements OnRoutesInit, OnReady {\n @Inject()\n injector: InjectorService;\n\n @Inject()\n app: PlatformApplication;\n\n @Configuration()\n configuration: Configuration;\n\n @Inject()\n swaggerService: SwaggerService;\n\n @Constant(\"env\")\n env: Env;\n\n private loaded = false;\n\n get settings() {\n return ([] as SwaggerSettings[]).concat(this.configuration.get<SwaggerSettings[]>(\"swagger\")).filter((o) => !!o);\n }\n\n /**\n *\n */\n $onRoutesInit() {\n if (this.loaded) {\n return;\n }\n\n const urls: any[] = this.getUrls();\n\n this.settings.forEach((conf: SwaggerSettings) => {\n const {path = \"/\"} = conf;\n\n this.app.get(path, useCtxHandler(redirectMiddleware(path)));\n this.app.use(path, this.createRouter(conf, urls));\n });\n\n this.loaded = true;\n }\n\n $onReady() {\n // istanbul ignore next\n if (this.configuration.getBestHost) {\n const host = this.configuration.getBestHost();\n const url = host.toString();\n\n const displayLog = (conf: SwaggerSettings) => {\n const {path = \"/\", fileName = \"swagger.json\", doc} = conf;\n\n this.injector.logger.info(`[${doc || \"default\"}] Swagger JSON is available on ${url}${normalizePath(path, fileName)}`);\n this.injector.logger.info(`[${doc || \"default\"}] Swagger UI is available on ${url}${path}/`);\n };\n\n this.settings.forEach((conf) => {\n displayLog(conf);\n });\n }\n\n this.generateSpecFiles();\n }\n\n generateSpecFiles() {\n return Promise.all(\n this.settings.map(async (conf) => {\n const {outFile} = conf;\n\n if (this.env === Env.PROD || outFile) {\n const spec = await this.swaggerService.getOpenAPISpec(conf);\n\n if (outFile) {\n return Fs.writeFile(outFile, JSON.stringify(spec, null, 2), {encoding: \"utf8\"}, () => {});\n }\n }\n })\n );\n }\n\n private getUrls() {\n return this.settings.reduce((acc: any[], conf) => {\n const {path = \"/\", fileName = \"swagger.json\", doc, hidden} = conf;\n\n if (!hidden) {\n acc.push({url: normalizePath(path, fileName), name: doc || path});\n }\n\n return acc;\n }, []);\n }\n\n /**\n *\n * @param conf\n * @param urls\n */\n private createRouter(conf: SwaggerSettings, urls: string[]) {\n const {disableSpec = false, fileName = \"swagger.json\", cssPath, jsPath, viewPath = join(__dirname, \"../views/index.ejs\")} = conf;\n const router = PlatformRouter.create(this.injector);\n\n if (!disableSpec) {\n router.get(normalizePath(\"/\", fileName), useCtxHandler(this.middlewareSwaggerJson(conf)));\n }\n\n if (viewPath) {\n if (cssPath) {\n router.get(\"/main.css\", useCtxHandler(cssMiddleware(cssPath)));\n }\n\n if (jsPath) {\n router.get(\"/main.js\", useCtxHandler(jsMiddleware(jsPath)));\n }\n\n router.get(\"/\", useCtxHandler(indexMiddleware(viewPath, {urls, ...conf})));\n router.statics(\"/\", {root: swaggerUiPath});\n }\n\n return router;\n }\n\n private middlewareSwaggerJson(conf: SwaggerSettings) {\n return async (ctx: PlatformContext) => {\n ctx.response.status(200).body(await this.swaggerService.getOpenAPISpec(conf));\n };\n }\n}\n"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./decorators/
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./SwaggerModule.js";
|
|
5
|
+
export * from "./decorators/docs.js";
|
|
6
|
+
export * from "./decorators/hidden.js";
|
|
7
|
+
export * from "./interfaces/SwaggerSettings.js";
|
|
8
|
+
export * from "./interfaces/interfaces.js";
|
|
9
|
+
export * from "./middlewares/cssMiddleware.js";
|
|
10
|
+
export * from "./middlewares/indexMiddleware.js";
|
|
11
|
+
export * from "./middlewares/jsMiddleware.js";
|
|
12
|
+
export * from "./middlewares/redirectMiddleware.js";
|
|
13
|
+
export * from "./services/SwaggerService.js";
|
|
14
|
+
export * from "./utils/includeRoute.js";
|
|
15
|
+
export * from "./utils/matchPath.js";
|
|
6
16
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kCAAkC,CAAC;AACjD,cAAc,2BAA2B,CAAC;AAC1C,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./SwaggerModule\";\nexport * from \"./decorators/docs\";\nexport * from \"./decorators/hidden\";\nexport * from \"./interfaces/SwaggerSettings\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./middlewares/cssMiddleware\";\nexport * from \"./middlewares/indexMiddleware\";\nexport * from \"./middlewares/jsMiddleware\";\nexport * from \"./middlewares/redirectMiddleware\";\nexport * from \"./services/SwaggerService\";\nexport * from \"./utils/includeRoute\";\nexport * from \"./utils/matchPath\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SwaggerSettings.js","sourceRoot":"","sources":["../../../src/interfaces/SwaggerSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {OpenSpec2, OpenSpec3, OpenSpecVersions, OS2Versions, OS3Versions} from \"@tsed/openspec\";\n\nexport interface SwaggerUIOptions {\n configUrl?: string;\n url?: string;\n urls?: {url: string; name: string; primaryName?: string}[];\n layout?: string;\n validatorUrl?: string;\n oauth?: any;\n authorize?: any;\n\n [key: string]: any;\n}\n\nexport interface SwaggerSettingsBase {\n /**\n * The url subpath to access to the documentation.\n */\n path: string;\n /**\n * Specify the spec version you want to generate.\n */\n specVersion?: OpenSpecVersions;\n /**\n * Swagger file name. By default swagger.json\n */\n fileName?: string;\n /**\n * Hidden documentation. Use this to hide documentation link in the dropdown.\n */\n hidden?: string;\n /**\n * The documentation key used by `@Docs` decorator to create several swagger documentations.\n */\n doc?: string;\n /**\n * The path to the CSS file.\n */\n cssPath?: string;\n /**\n * The path to the JS file.\n */\n jsPath?: string;\n /**\n * The path to the ejs file to create html page.\n */\n viewPath?: string | false;\n /**\n * SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md)\n */\n options?: SwaggerUIOptions;\n /**\n * Display the search field in the navbar.\n */\n showExplorer?: boolean;\n /**\n * Load the base spec documentation from the specified path.\n */\n specPath?: string;\n /**\n * Write the `swagger.json` spec documentation on the specified path.\n */\n outFile?: string;\n /**\n * A function to generate the operationId.\n */\n operationIdFormatter?: (name: string, propertyKey: string, path: string) => string;\n /**\n * A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name).\n */\n operationIdPattern?: string;\n /**\n *\n */\n disableSpec?: boolean;\n /**\n * Include only controllers whose paths match the pattern list provided.\n */\n pathPatterns?: string[];\n}\n\
|
|
1
|
+
{"version":3,"file":"SwaggerSettings.js","sourceRoot":"","sources":["../../../src/interfaces/SwaggerSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {OpenSpec2, OpenSpec3, OpenSpecVersions, OS2Versions, OS3Versions} from \"@tsed/openspec\";\n\nexport interface SwaggerUIOptions {\n configUrl?: string;\n url?: string;\n urls?: {url: string; name: string; primaryName?: string}[];\n layout?: string;\n validatorUrl?: string;\n oauth?: any;\n authorize?: any;\n\n [key: string]: any;\n}\n\nexport interface SwaggerSettingsBase {\n /**\n * The url subpath to access to the documentation.\n */\n path: string;\n /**\n * Specify the spec version you want to generate.\n */\n specVersion?: OpenSpecVersions;\n /**\n * Swagger file name. By default swagger.json\n */\n fileName?: string;\n /**\n * Hidden documentation. Use this to hide documentation link in the dropdown.\n */\n hidden?: string;\n /**\n * The documentation key used by `@Docs` decorator to create several swagger documentations.\n */\n doc?: string;\n /**\n * The path to the CSS file.\n */\n cssPath?: string;\n /**\n * The path to the JS file.\n */\n jsPath?: string;\n /**\n * The path to the ejs file to create html page.\n */\n viewPath?: string | false;\n /**\n * SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md)\n */\n options?: SwaggerUIOptions;\n /**\n * Display the search field in the navbar.\n */\n showExplorer?: boolean;\n /**\n * Load the base spec documentation from the specified path.\n */\n specPath?: string;\n /**\n * Write the `swagger.json` spec documentation on the specified path.\n */\n outFile?: string;\n /**\n * A function to generate the operationId.\n */\n operationIdFormatter?: (name: string, propertyKey: string, path: string) => string;\n /**\n * A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name).\n */\n operationIdPattern?: string;\n /**\n *\n */\n disableSpec?: boolean;\n /**\n * Include only controllers whose paths match the pattern list provided.\n */\n pathPatterns?: string[];\n}\n\nexport interface SwaggerOS2Settings extends SwaggerSettingsBase {\n specVersion?: OS2Versions;\n /**\n * OpenSpec 2\n */\n spec?: Partial<OpenSpec2>;\n}\n\nexport interface SwaggerOS3Settings extends SwaggerSettingsBase {\n specVersion?: OS3Versions;\n /**\n * OpenSpec 3\n */\n spec?: Partial<OpenSpec3>;\n}\n\nexport type SwaggerSettings = SwaggerOS2Settings | SwaggerOS3Settings;\n"]}
|