Skip to content

Commit

Permalink
added middleware options for the swagger api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kalin-marinov committed Mar 14, 2020
1 parent a695245 commit cb7190e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"label": "build",
"type": "typescript",
"tsconfig": "sample\\tsconfig.json",
"tsconfig": "sample/tsconfig.json",
"problemMatcher": [
"$tsc"
]
Expand Down
2 changes: 1 addition & 1 deletion sample/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.use('/', indexRouter);
app.use('/', authRouter);

// catch 404 and forward to error handler
app.use(function (req: express.Request, res:express.Response, next: express.NextFunction) {
app.use(function (req: express.Request, res: express.Response, next: express.NextFunction) {
next(new Error('Not found'));
});

Expand Down
2 changes: 1 addition & 1 deletion sample/bin/www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ function onListening() {


process.on('uncaughtException', err => console.log(`Caught unhandled exception: ${err.message} > Stack: ${err.stack}`));
process.on('unhandledRejection', (reason, p) => console.log(`Unhandled Rejection at: Promise${p}. Reason: ${reason.message} > Stack: ${reason.stack}.`));
process.on('unhandledRejection', (reason: any, p) => console.log(`Unhandled Rejection at: Promise${p}. Reason: ${reason.message} > Stack: ${reason.stack}.`));
2 changes: 1 addition & 1 deletion sample/config/swagger.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as express from 'express';
import { fromOptions, fromObject } from 'express-swagger-generator';
import options from './swagger.json';
import options from './swagger.json';
//import swaggerObject from './swagger-def.json';

export function configureSwagger(app: express.Express) {
Expand Down
2 changes: 1 addition & 1 deletion sample/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ router.get('/api/people/:name/addresses', function (req, res, next) {
* @security Bearer
*/
router.get('/api/people/:name/addresses/:addressType', function (req, res, next) {
let addressType = req.params.addressType;
let addressType = Number(req.params.addressType) as AddressType;
res.json([{ number: 10, street: "random str", type: addressType } as IAddress]);
});

Expand Down
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as express from 'express';
import { RequestHandler } from 'express';
// import { Spec } from 'swagger-schema-official';


Expand All @@ -23,7 +24,7 @@ declare namespace expressSwaggerGenerator {

interface ISwaggerManager {
getObject(): any;
registerInExpress(app: express.Express, routes?: { url: string, docs: string }): void;
registerInExpress(app: express.Express, routes?: { url: string, docs: string }, middlewares?: RequestHandler | RequestHandler[]): void;
validate(callback: (err: any, result?: any) => void): void;
}

Expand Down
12 changes: 8 additions & 4 deletions src/lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,18 +487,22 @@ function createSwaggerObject(options) {
}

/** Registers an express midleware based on */
function expressMiddleware(app, swaggerObject, routes) {
function expressMiddleware(app, swaggerObject, routes, middlewares) {
parser.parse(swaggerObject, (err, api) => {
if (!err) swaggerObject = api;
});

let url = routes ? routes.url : '/api-docs'
let docs = routes ? routes.docs : '/api-docs.json'

app.use(docs, function (req, res) {
if (!middlewares) middlewares = [];
else if (!Array.isArray(middlewares)) middlewares = [middlewares];

app.use(docs, ...middlewares, function (req, res) {
res.json(swaggerObject);
});
app.use(url, swaggerUi({

app.use(url, ...middlewares, swaggerUi({
route: url,
docs: docs
}));
Expand All @@ -507,7 +511,7 @@ function expressMiddleware(app, swaggerObject, routes) {
function fromObject(swaggerObject) {
return {
getObject: function () { return swaggerObject },
registerInExpress: function (app, routes) { expressMiddleware(app, swaggerObject, routes); },
registerInExpress: function (app, routes, middlewares) { expressMiddleware(app, swaggerObject, routes, middlewares); },
validate: function (callback) { parser.parse(swaggerObject, callback) }
}
}
Expand Down

0 comments on commit cb7190e

Please sign in to comment.