middleware-flow
Advanced tools
Comparing version 0.1.2 to 0.2.0
70
index.js
@@ -65,4 +65,72 @@ var createCount = require('callback-count'); | ||
next(); | ||
}, | ||
_execConditional: function (conditional) { | ||
return function (req, res, next) { | ||
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); | ||
} | ||
else { | ||
throw new Error('unknown conditional type'); | ||
} | ||
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) { | ||
if (err || !result) { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
} | ||
} | ||
}; | ||
}, | ||
conditional: function (type, test) { | ||
var conditional = { | ||
type: type, | ||
if: test | ||
}; | ||
return thenAndElse(this._execConditional(conditional), conditional); | ||
}, | ||
mwIf: function (mw) { | ||
return this.conditional('middleware', mw); | ||
}, | ||
syncIf: function (mw) { | ||
return this.conditional('sync', mw); | ||
}, | ||
asyncIf: function (mw) { | ||
return this.conditional('async', mw); | ||
}, | ||
if: function (val) { | ||
return this.conditional('value', val); | ||
} | ||
}; | ||
flow.and = flow.series; | ||
flow.and = flow.series; | ||
function thenAndElse (exec, conditional) { | ||
exec.then = function (/* middlewares */) { | ||
conditional.then = Array.prototype.slice.call(arguments); | ||
return exec; | ||
}; | ||
exec.else = function (/* middlewares */) { | ||
conditional.else = Array.prototype.slice.call(arguments); | ||
return exec; | ||
}; | ||
return exec; | ||
} |
{ | ||
"name": "middleware-flow", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Middleware control flow library: series, parallel, or, and", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
13057
9
408
1