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

Allow swagger 'action' functions to access next() from Express. #84

Merged
merged 1 commit into from Dec 10, 2013
Merged

Allow swagger 'action' functions to access next() from Express. #84

merged 1 commit into from Dec 10, 2013

Conversation

tcbeutler
Copy link
Contributor

To use Express middleware that does post-processing, route verbs need to declared using the midddleware syntax with the third parameter callback. An example use for this might be final formatting of the response before send is called.

Action would then have access to something like this:

'action': function (req, res, next) {
  res.body = getResource();
  next();
}

When configuring the express middleware stack, post-processors can be defined.

app.use(app.router);
app.use(function (req, res, next) {
  console.log('Post-processing done here.');
  res.send(formatResponse(res));
});

Also, passing errors that come from asynchronous code becomes easy with next(error):
This only works if the error handler is defined in Express middleware and Swagger's error handler is set to null.

'action': function (req, res, next) {
  var promise = getAsyncResource(req, res);
  promise.then(function(data) {
    ....
  });
  //Calling next with an error goes right to Express error handler.
  promise.fail(function(data) {
    next(data);
  });
}

Important

This change has no negative side effects, as omitting the third parameter allows for swagger to work exactly as it did before.

'action': function (req, res) {
  res.send(getResource());
}

fehguy added a commit that referenced this pull request Dec 10, 2013
Allow swagger 'action' functions to access next() from Express.
@fehguy fehguy merged commit 1531284 into swagger-api:develop Dec 10, 2013
@fehguy
Copy link
Contributor

fehguy commented Dec 10, 2013

thanks!

@fehguy
Copy link
Contributor

fehguy commented Dec 10, 2013

@paulhill i'm about to merge to master. Any other input on this branch?

@fehguy
Copy link
Contributor

fehguy commented Feb 4, 2014

see version 2.0.1

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

2 participants