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 viewEngine?;
     private readonly viewsPath?;
     private readonly viewsPath?;
     private readonly options?;
     private readonly options?;
+    private readonly swaggerComponents?;
+    private readonly swaggerSecurity?;
     private readonly swaggerDocsPath?;
     private readonly swaggerDocsPath?;
     private readonly swaggerTitle?;
     private readonly swaggerTitle?;
     private readonly swaggerDescription?;
     private readonly swaggerDescription?;

+ 8 - 2
dist/server/Server.js

@@ -55,7 +55,7 @@ const swagger_ui_express_1 = __importDefault(require("swagger-ui-express"));
 /** @sealed */
 /** @sealed */
 class Server {
 class Server {
     constructor(properties) {
     constructor(properties) {
-        var _a, _b, _c, _d, _e;
+        var _a, _b, _c, _d, _e, _f, _g;
         this.instance = (0, express_1.default)();
         this.instance = (0, express_1.default)();
         this.httpServer = http_2.default.createServer(this.instance);
         this.httpServer = http_2.default.createServer(this.instance);
         this.port = properties.port;
         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.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.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.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;
         this.initialized = false;
     }
     }
@@ -225,7 +227,7 @@ class Server {
             return this;
             return this;
         const swaggerOptions = {
         const swaggerOptions = {
             swaggerDefinition: {
             swaggerDefinition: {
-                openapi: '3.0.0',
+                openapi: '3.0.1',
                 info: {
                 info: {
                     title: this.swaggerTitle,
                     title: this.swaggerTitle,
                     version: this.swaggerApiVersion,
                     version: this.swaggerApiVersion,
@@ -237,6 +239,10 @@ class Server {
             },
             },
             apis: [this.swaggerDocsPath],
             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);
         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));
         this.instance.use(this.swaggerRoute, swagger_ui_express_1.default.serve, swagger_ui_express_1.default.setup(swaggerDocs));
         return this;
         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;
         version?: string;
         route?: string;
         route?: string;
         description?: string;
         description?: string;
+        components?: object;
+        security?: object;
     };
     };
     wsHandlers?: {
     wsHandlers?: {
         [url: string]: WebSocketHandler;
         [url: string]: WebSocketHandler;

+ 13 - 4
lib/server/Server.ts

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

+ 3 - 1
lib/server/ServerProperties.ts

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

+ 1 - 1
package.json

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

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