Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Add in middleware support for swagger-node-express #153

Closed
wants to merge 2 commits into from

Conversation

wonderlic-chrisk
Copy link

It is quite common (and built into express) to have a piece of middleware act upon a request. Swagger doesn't have this capability, but it would be relatively straightforward to add (and quite useful). So, I added in a middleware construct:

For starters, in the Swagger constructor there is a new property:

this.middleware = [];

There is also a new helper method to add middleware:

Swagger.prototype.addMiddleware = function(m) {
this.middleware.push(m);
}

And finally, the code that actually calls out to the middleware BEFORE calling into the action of method:

var success = true;
if (self.middleware.length) {
_.forEach(self.middleware, function(m) {
var ret = m(req, res, spec, self.allModels);
if (ret) {
var message = ret.message || 'An unexpected error has occurred';
var code = ret.code || 500;
res.send(JSON.stringify({
'message': message,
'code': code
}), code);
success = false;
return success;
}
});
}

if (success) {
callback(req, res, next);
}

As the middleware could literally be anything (additional validation of the request, additional error handling, adding object / properties to the req / res, etc.), I wanted to pass as much information to them as possible. As such, I chose the req and res of the request, followed by the spec of the method (so that someone could turn off a particular piece of middleware with an additional spec property, the middleware can access the spec to see the parameters or description or whatever, etc.), followed lastly by any models that are defined inside swagger.

This is similar, but implemented differently, to #87. This is all handled inside swagger, whereas #87 handles an array of actions.

@bjrmatos
Copy link

bjrmatos commented Aug 7, 2014

👍

@jsdevel
Copy link
Contributor

jsdevel commented Aug 8, 2014

#132

@wonderlic-chrisk
Copy link
Author

@jsdevel, both #87 and #132 change the action to allow arrays of "actions". If, for example, you were wiring up additional error handling, that would need to be wired up as part of every action of every method (or, ideally, calling a separate method that did that, but the theory remains the same). This treats swagger a bit more like express (as in app.use([middleware repo / method])) which, to me, results in less code written on behalf of the developer. Honestly, however, either would work to allow the additional "middleware" functionality in swagger-node-express that it seems like the community wants.

It does seem like you guys were preparing to pull #132 into the code base. If so, I will remove this request once that is pulled in. While both pull requests could work in unison if it was so desired, it is probably not ideal as they are essentially giving the same functionality. If there is anything that I am missing that you guys would like to evaluate both on an even playing field, please let me know.

@ericrini
Copy link

+1 this is a huge value add for your tool

https://www.npmjs.com/package/swagger-validation

@fehguy
Copy link
Contributor

fehguy commented Jun 3, 2015

Closing as #207 will completely replace the implementation in this project.

@fehguy fehguy closed this Jun 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants