Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow async handlers #138

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"lastsemic": false,
"iterator": false,
"funcscope": false,
"esnext": false,
"esnext": true,

"newcap": false,
"noempty": false,
Expand Down
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
language: node_js
node_js:
- "0.12"
- "4"
- "6"
- "8"
branches:
only:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ module.exports = {
}
```

### Async Handlders
async/await requires at least node 7.6. Errors that occurred in async function will be automatically passed to next() callback
```javascript
module.exports = {
get: async (req, res) => { ... },
put: async (req, res) => { ... },
...
}
```

### Handler Middleware

Handlers can also specify middleware chains by providing an array of handler functions under the verb:
Expand Down
32 changes: 25 additions & 7 deletions lib/expressroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ var async = require('async'),
utils = require('swaggerize-routes/lib/utils'),
pathRegexp = require('path-to-regexp');

/**
* Wraps try catch block around async function
* Errors are sent to next handler automatically
*
* @param fn
* @return {Function}
*/
function asyncMiddleware(fn) {
return function (req, res, next) {
return Promise.resolve(fn(req, res, next)).catch(next);
};
}

/**
* Makes default accessor functions for a specific data location, e.g. query, params etc
Expand Down Expand Up @@ -125,34 +137,40 @@ function buildRoutePath(mountpath, path) {
* @param routeSpec
*/
function makeExpressRoute(router, mountpath, route, securityDefinitions) {
var path, args, before, validators;
var path, args, before, validators, handlers, i;

path = buildRoutePath(mountpath, route.path);
args = [path];
before = [];
handlers = [];

if (route.security) {
before.push(authorizeFor(route.security, securityDefinitions));
}

if (thing.isArray(route.handler)) {
if (route.handler.length > 1) {
Array.prototype.push.apply(before, route.handler.slice(0, route.handler.length - 1));
if (!thing.isArray(route.handler)) {
route.handler = [route.handler];
}

for (i = 0; i < route.handler.length; ++i) {
if (route.handler[i].constructor.name === "AsyncFunction") {
handlers.push(asyncMiddleware(route.handler[i]));
} else {
handlers.push(route.handler[i]);
}
route.handler = route.handler[route.handler.length - 1];
}

validators = [];

for (var i = 0; i < route.validators.length; ++i) {
for (i = 0; i < route.validators.length; ++i) {
validators.push(makeValidator(route.validators[i], route.consumes));
}

before = before.concat(validators);


Array.prototype.push.apply(args, before);
args.push(route.handler);
Array.prototype.push.apply(args, handlers);
router[route.method].apply(router, args);
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swaggerize-express",
"version": "4.0.6",
"version": "4.0.7",
"author": "Trevor Livingston <[email protected]>",
"contributors": [
"Trevor Livingston <[email protected]>",
Expand Down Expand Up @@ -28,7 +28,7 @@
},
"bugs": "http://github.com/krakenjs/swaggerize-express/issues",
"engines": {
"node": "<=4.x"
"node": ">=4.8.7"
},
"dependencies": {
"async": "^1.5.0",
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/handlers/pets/{id}.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ var store = require('../../lib/store');


module.exports = {
get: function (req, res) {
get: async (req, res) => {
// pretend waiting for database
await new Promise(res => setTimeout(res, 10));

res.json(store.get(req.params['id']));
},
delete: function (req, res) {
Expand Down