Browse Source

v1.1.6: Swagger security settings

CrazyDoctor 3 weeks ago
parent
commit
b3bb887d55

+ 2 - 0
dist/server/Server.d.ts

@@ -23,6 +23,8 @@ declare class Server {
     private readonly viewEngine?;
     private readonly viewsPath?;
     private readonly options?;
+    private readonly swaggerComponents?;
+    private readonly swaggerSecurity?;
     private readonly swaggerDocsPath?;
     private readonly swaggerTitle?;
     private readonly swaggerDescription?;

+ 8 - 2
dist/server/Server.js

@@ -55,7 +55,7 @@ const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
 /** @sealed */
 class Server {
     constructor(properties) {
-        var _a, _b, _c, _d, _e;
+        var _a, _b, _c, _d, _e, _f, _g;
         this.instance = (0, express_1.default)();
         this.httpServer = http_2.default.createServer(this.instance);
         this.port = properties.port;
@@ -79,6 +79,8 @@ class Server {
             this.swaggerDescription = ((_c = properties.swagger) === null || _c === void 0 ? void 0 : _c.description) || 'API Documentation';
             this.swaggerApiVersion = ((_d = properties.swagger) === null || _d === void 0 ? void 0 : _d.version) || '1.0.0';
             this.swaggerRoute = ((_e = properties.swagger) === null || _e === void 0 ? void 0 : _e.route) || '/api-docs';
+            this.swaggerComponents = (_f = properties.swagger) === null || _f === void 0 ? void 0 : _f.components;
+            this.swaggerSecurity = (_g = properties.swagger) === null || _g === void 0 ? void 0 : _g.security;
         }
         this.initialized = false;
     }
@@ -225,7 +227,7 @@ class Server {
             return this;
         const swaggerOptions = {
             swaggerDefinition: {
-                openapi: '3.0.0',
+                openapi: '3.0.1',
                 info: {
                     title: this.swaggerTitle,
                     version: this.swaggerApiVersion,
@@ -237,6 +239,10 @@ class Server {
             },
             apis: [this.swaggerDocsPath],
         };
+        if (this.swaggerComponents)
+            swaggerOptions.swaggerDefinition.components = this.swaggerComponents;
+        if (this.swaggerSecurity)
+            swaggerOptions.swaggerDefinition.security = this.swaggerSecurity;
         const swaggerDocs = (0, swagger_jsdoc_1.default)(swaggerOptions);
         this.instance.use(this.swaggerRoute, swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swaggerDocs));
         return this;

File diff suppressed because it is too large
+ 0 - 0
dist/server/Server.js.map


+ 2 - 0
dist/server/ServerProperties.d.ts

@@ -14,6 +14,8 @@ type ServerProperties = {
         version?: string;
         route?: string;
         description?: string;
+        components?: object;
+        security?: object;
     };
     wsHandlers?: {
         [url: string]: WebSocketHandler;

+ 13 - 4
lib/server/Server.ts

@@ -13,7 +13,7 @@ import { InvalidMiddlewareException } from '../base/exceptions';
 import { InvalidRouteException } from '../base/exceptions';
 import { ServerNotInitializedException } from '../base/exceptions';
 import { WebSocketHandler } from '../base/websocket';
-import swaggerJsDoc from 'swagger-jsdoc';
+import swaggerJsDoc, { OAS3Options, Options, SecurityRequirement } from 'swagger-jsdoc';
 import swaggerUi from 'swagger-ui-express';
 
 /** @sealed */
@@ -39,6 +39,8 @@ class Server {
 	private readonly viewsPath?: string;
 	private readonly options?: {[key: string]: any};
 
+	private readonly swaggerComponents? : object;
+	private readonly swaggerSecurity? : object;
 	private readonly swaggerDocsPath? : string;
 	private readonly swaggerTitle?: string;
 	private readonly swaggerDescription?: string;
@@ -69,6 +71,8 @@ class Server {
 			this.swaggerDescription = properties.swagger?.description || 'API Documentation';
 			this.swaggerApiVersion = properties.swagger?.version ||'1.0.0';
 			this.swaggerRoute = properties.swagger?.route || '/api-docs';
+			this.swaggerComponents = properties.swagger?.components;
+			this.swaggerSecurity = properties.swagger?.security;
 		}
 		this.initialized = false;
 	}
@@ -229,9 +233,9 @@ class Server {
 		if(!this.swaggerDocsPath)
 			return this;
 
-		const swaggerOptions = {
+		const swaggerOptions : OAS3Options = {
 			swaggerDefinition: {
-				openapi: '3.0.0',
+				openapi: '3.0.1',
 				info: {
 					title: this.swaggerTitle!,
 					version: this.swaggerApiVersion!,
@@ -244,7 +248,12 @@ class Server {
 			apis: [this.swaggerDocsPath],
 		};
 
-		const swaggerDocs = swaggerJsDoc(swaggerOptions);
+		if(this.swaggerComponents)
+			swaggerOptions.swaggerDefinition!.components = this.swaggerComponents;
+		if(this.swaggerSecurity)
+			swaggerOptions.swaggerDefinition!.security = this.swaggerSecurity as SecurityRequirement[];
+
+		const swaggerDocs = swaggerJsDoc(swaggerOptions as Options);
 
 		this.instance.use(this.swaggerRoute!, swaggerUi.serve, swaggerUi.setup(swaggerDocs));
 		return this;

+ 3 - 1
lib/server/ServerProperties.ts

@@ -14,7 +14,9 @@ type ServerProperties = {
 		title?: string,
 		version?: string,
 		route?: string,
-		description?: string
+		description?: string,
+		components?: object,
+		security?: object
 	},
 	wsHandlers?: {[url: string] : WebSocketHandler},
 	options?: {[key: string] : any}

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "org.crazydoctor.expressts",
-  "version": "1.1.5",
+  "version": "1.1.6",
   "devDependencies": {
     "@types/express-session": "^1.17.7",
     "@types/node": "^20.4.9",

Some files were not shown because too many files changed in this diff