middleware-flow
Advanced tools
Comparing version 0.3.1 to 0.4.0
69
index.js
@@ -12,19 +12,21 @@ var createCount = require('callback-count'); | ||
return function (req, res, next) { | ||
if (conditional.type === 'middleware') { | ||
conditional.if(req, res, function (err) { | ||
async(err, !err); | ||
}); | ||
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); | ||
} | ||
} | ||
else if (conditional.type === 'async') { | ||
conditional.if(req, res, async); | ||
catch (err) { | ||
async(err, !err, true); | ||
} | ||
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) { | ||
@@ -38,17 +40,20 @@ if (result) { | ||
} | ||
function async (err, result) { | ||
if (err || !result) { | ||
if (exists(err)) { | ||
req.lastError = err; | ||
function async (err, result, uncaught) { | ||
if (err) { | ||
if (uncaught || conditional.type !== 'middleware' || !conditional.else || !conditional.else[0]) { | ||
next(err); | ||
} | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
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 { | ||
// TODO | ||
// if (err && conditional.then[0] && conditional.then[0].length === 4) { | ||
// console.log('fjdksl;afjksdla;fsda'); | ||
// conditional.then[0] = conditional.then[0].bind(null, err); | ||
// } | ||
else if (result) { | ||
flow.series.apply(null, conditional.then)(req, res, next); | ||
} | ||
else { | ||
flow.series.apply(null, conditional.else)(req, res, next); | ||
} | ||
} | ||
@@ -89,12 +94,2 @@ }; | ||
return exec; | ||
} | ||
function exists (v) { | ||
return v !== null && v !== undefined; | ||
} | ||
function lengthOf (i) { | ||
return function (foo) { | ||
return foo.length === i; | ||
}; | ||
} | ||
} |
@@ -17,4 +17,8 @@ var findIndex = require('101/find-index'); | ||
var middlewares = args.slice(); // copy | ||
step(middlewares.shift()); | ||
// if (arguments.length === 4) { // force being used as error middleware | ||
// errorStep(req); // req is err - [err, req, res, next] | ||
// } | ||
// else { | ||
step(middlewares.shift()); | ||
// } | ||
function step (mw) { | ||
@@ -21,0 +25,0 @@ if (mw) { |
{ | ||
"name": "middleware-flow", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Middleware control flow library: series, parallel, or, and", | ||
@@ -10,4 +10,4 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "lab -l test", | ||
"test-watch": "nodemon --exec lab -l test" | ||
"test": "lab -l -c test", | ||
"test-watch": "nodemon --exec npm test" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -64,3 +64,3 @@ # middleware-flow [![Build Status](https://travis-ci.org/tjmehta/middleware-flow.png?branch=master)](https://travis-ci.org/tjmehta/middleware-flow) | ||
syncIf(nameQueryExists) // accepts a sync function that returns a boolean | ||
.then(one, two, three) // true -> then, false -> else | ||
.then(one, two, three) // true -> then, error -> skips all next(err) | ||
.else(error) | ||
@@ -86,3 +86,3 @@ ); | ||
asyncIf(bodyFileExists) // expects boolean as the result argument | ||
.then(one, two, three) // true -> then, false -> else, error -> else | ||
.then(one, two, three) // true -> then, false -> else, error -> skips all next(err) | ||
.else(other) | ||
@@ -107,3 +107,4 @@ ); | ||
.then(one, two, three) // no error -> then, error -> else | ||
.else(other) | ||
.else(other) // if other is an error middleware it will recieve | ||
// the error else the error will be ignored | ||
); | ||
@@ -110,0 +111,0 @@ function userIsModerator (req, res, next) { |
27822
18
913
119