middleware-flow
Advanced tools
Comparing version 0.4.0 to 0.4.1
138
index.js
var createCount = require('callback-count'); | ||
var flow = module.exports = { | ||
series: require('./lib/series'), | ||
parallel: require('./lib/parallel'), | ||
or: require('./lib/or'), | ||
next: function (req, res, next) { | ||
next(); | ||
}, | ||
_execConditional: function (conditional) { | ||
return function (req, res, next) { | ||
try { | ||
if (conditional.type === 'middleware') { | ||
conditional.if(req, res, function (err) { | ||
async(err, !err); | ||
}); | ||
} | ||
else if (conditional.type === 'async') { | ||
conditional.if(req, res, async); | ||
} | ||
else if (conditional.type === 'sync') { | ||
sync(conditional.if(req, res)); | ||
} | ||
else { //if (conditional.type === 'value') { | ||
sync(conditional.if); | ||
} | ||
var flow = module.exports = {}; | ||
flow.series = require('./lib/series'); | ||
flow.parallel = require('./lib/parallel'); | ||
flow.or = require('./lib/or'); | ||
flow.next = function (req, res, next) { | ||
next(); | ||
}; | ||
flow._execConditional = function (conditional) { | ||
return function (req, res, next) { | ||
try { | ||
if (conditional.type === 'middleware') { | ||
conditional.if(req, res, function (err) { | ||
async(err, !err); | ||
}); | ||
} | ||
catch (err) { | ||
async(err, !err, true); | ||
else if (conditional.type === 'async') { | ||
conditional.if(req, res, async); | ||
} | ||
function sync (result) { | ||
if (result) { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
else if (conditional.type === 'sync') { | ||
sync(conditional.if(req, res)); | ||
} | ||
else { //if (conditional.type === 'value') { | ||
sync(conditional.if); | ||
} | ||
} | ||
catch (err) { | ||
async(err, !err, true); | ||
} | ||
function sync (result) { | ||
if (result) { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
function async (err, result, uncaught) { | ||
if (err) { | ||
if (uncaught || conditional.type !== 'middleware' || !conditional.else || !conditional.else[0]) { | ||
next(err); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
function async (err, result, uncaught) { | ||
if (err) { | ||
if (uncaught || conditional.type !== 'middleware' || !conditional.else || !conditional.else[0]) { | ||
next(err); | ||
else { // if (conditional.type === 'middleware') { | ||
if (conditional.else[0].length === 4) { | ||
conditional.else[0] = conditional.else[0].bind(null, err); | ||
} | ||
else { // if (conditional.type === 'middleware') { | ||
if (conditional.else[0].length === 4) { | ||
conditional.else[0] = conditional.else[0].bind(null, err); | ||
} | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
else if (result) { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
}; | ||
}, | ||
conditional: function (type, test) { | ||
var conditional = { | ||
type: type, | ||
if: test | ||
}; | ||
return thenAndElse(flow._execConditional(conditional), conditional); | ||
}, | ||
mwIf: function (mw) { | ||
return flow.conditional('middleware', mw); | ||
}, | ||
syncIf: function (mw) { | ||
return flow.conditional('sync', mw); | ||
}, | ||
asyncIf: function (mw) { | ||
return flow.conditional('async', mw); | ||
}, | ||
if: function (val) { | ||
return flow.conditional('value', val); | ||
} | ||
else if (result) { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
}; | ||
}; | ||
flow.conditional = function (type, test) { | ||
var conditional = { | ||
type: type, | ||
if: test | ||
}; | ||
return thenAndElse(flow._execConditional(conditional), conditional); | ||
}; | ||
flow.mwIf = function (mw) { | ||
return flow.conditional('middleware', mw); | ||
}; | ||
flow.syncIf = function (mw) { | ||
return flow.conditional('sync', mw); | ||
}; | ||
flow.asyncIf = function (mw) { | ||
return flow.conditional('async', mw); | ||
}; | ||
flow.if = function (val) { | ||
return flow.conditional('value', val); | ||
}; | ||
flow.and = flow.series; | ||
flow.try = require('./lib/try-catch')(flow.mwIf); | ||
@@ -82,0 +82,0 @@ function thenAndElse (exec, conditional) { |
@@ -26,2 +26,5 @@ var findIndex = require('101/find-index'); | ||
try { | ||
if (mw.length === 4) { | ||
return step(middlewares.shift()); | ||
} | ||
mw(req, res, function (err) { | ||
@@ -52,3 +55,8 @@ if (err) { | ||
mw(err, req, res, function (e) { | ||
errorStep(e || err); | ||
if (e) { | ||
errorStep(e); | ||
} | ||
else { | ||
step(middlewares.shift()); | ||
} | ||
}); | ||
@@ -55,0 +63,0 @@ } |
{ | ||
"name": "middleware-flow", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Middleware control flow library: series, parallel, or, and", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -95,7 +95,6 @@ # middleware-flow [![Build Status](https://travis-ci.org/tjmehta/middleware-flow.png?branch=master)](https://travis-ci.org/tjmehta/middleware-flow) | ||
## mwIf(middleware).then(middlewares...).else(middlewares...) | ||
## mwIf(middleware).then(middlewares..).else(middlewares..) | ||
```js | ||
var mwIf = require('middleware-flow').mwIf; | ||
var or = require('middleware-flow').or; | ||
var app = require('express')(); | ||
@@ -107,3 +106,3 @@ | ||
.else(other) // if other is an error middleware it will recieve | ||
// the error else the error will be ignored | ||
// the error else the error will be ignored | ||
); | ||
@@ -120,3 +119,20 @@ function userIsModerator (req, res, next) { | ||
## try(middlewares..).catch(middlewares..) | ||
```js | ||
var flow = require('middleware-flow'); | ||
var app = require('express')(); | ||
app.use( | ||
flow.try(saveUser) // error here, just runs the catch middlewares | ||
.catch(rollback) // no error -> other, error -> rollback | ||
// if rollback is an error middleware it will recieve | ||
// the error else the error will be ignored | ||
); | ||
function saveUser (req, res, next) { | ||
db.save(req.user, next); | ||
} | ||
``` | ||
# License | ||
### MIT |
@@ -23,3 +23,9 @@ module.exports = { | ||
}; | ||
}, | ||
extendErrMessage: function (ext) { | ||
return function (err, req, res, next) { | ||
err.message = err.message+ext; | ||
next(err); | ||
}; | ||
} | ||
}; |
@@ -22,3 +22,8 @@ var res = module.exports = { | ||
}; | ||
}, | ||
sendQuery: function () { | ||
return function (req, res, next) { | ||
res.json(req.query); | ||
}; | ||
} | ||
}; |
@@ -39,2 +39,3 @@ var Lab = require('lab'); | ||
series( | ||
wrapAcceptErr(res.write('NO')), | ||
nextErr(err), | ||
@@ -41,0 +42,0 @@ res.write('1'), |
32241
22
1061
135