conditional-middleware
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "conditional-middleware", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Conditional, composable middleware for connect, express, and other soon-to-be available server modules.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
# conditional-middleware | ||
This module allows you to compose a chain of middleware based on a condition. If the condition fails, the middleware does not execute - not even as a wrapper around `next()` calls. | ||
Compose chains of middleware based on a condition. If the condition fails, the middleware does not execute - not even as a wrapper around `next()` calls. | ||
@@ -23,3 +23,3 @@ ``` | ||
In the following example, if `shouldHandleRequest` returns **false**, middleware1 and middleware2 will not execute. | ||
In the following example, if `shouldHandleRequest` returns **false**, _middleware1_ and _middleware2_ will not execute. | ||
@@ -51,5 +51,5 @@ ```js | ||
## Use a "context" for `if/else if` behavior | ||
## Use a "context" for ` if/else if ` behavior | ||
Consider a situation where you want to have multiple conditions but only want the first one that returns **true**. This type of behavior is availble by creating a "context". In the following example, as soon as one condition returns **true**, none of the other conditions are even checked. | ||
Consider a situation where you want to have multiple conditions but only want the first one that returns **true**. This type of behavior is availble by creating a "context". In the following example, as soon as one condition returns **true**, none of the other conditions are checked. | ||
@@ -134,3 +134,3 @@ ```js | ||
function shouldHandleRequest (req) { | ||
function alwaysFalse (req) { | ||
req.foobar = true; // set a custom property | ||
@@ -141,3 +141,3 @@ return false; | ||
const app = express(); | ||
app.use(conditional(shouldHandleRequest, [ | ||
app.use(conditional(alwaysFalse, [ | ||
thisWillNeverRun | ||
@@ -156,3 +156,3 @@ ])); | ||
Yup, that works too! | ||
Yup, that works too! Remember, the `condtional` function will always return [connect-friendly](https://github.com/senchalabs/connect#mount-middleware) middleware, allowing you to nest conditionals as deep as you want. | ||
@@ -173,3 +173,4 @@ ```js | ||
next(); | ||
} | ||
}, | ||
conditional(...) | ||
]), | ||
@@ -182,2 +183,7 @@ (req, res, next) => { | ||
])); | ||
``` | ||
``` | ||
## What lies ahead? | ||
- [Koa](https://github.com/koajs/koa) support - [Vote on the issue here](https://github.com/DesignByOnyx/conditional-middleware/issues/1) | ||
- A new API? - [Cast your opinions here](https://github.com/DesignByOnyx/conditional-middleware/issues/2) |
@@ -70,10 +70,13 @@ const assert = require('chai').assert; | ||
const middleware1 = (req, res, next) => { | ||
next('error') | ||
next({ message: 'error' }); | ||
}; | ||
const middleware2 = (err, req, res, next) => { | ||
assert.equal(err, 'error', 'got the error'); | ||
done(); | ||
err.message = 'very bad error'; | ||
next(err); | ||
}; | ||
const reducer = chainMiddleware({}, {}); | ||
[middleware1, middleware2].reduce(reducer, Promise.resolve()).catch(done); | ||
[middleware1, middleware2].reduce(reducer, Promise.resolve()).catch(err => { | ||
assert.equal(err.message, 'very bad error', 'got the error'); | ||
done(); | ||
}); | ||
}); | ||
@@ -89,6 +92,6 @@ }); | ||
it('works when the condition returns a boolean true', done => { | ||
const middleware = conditional(() => true, [() => { | ||
done(); | ||
const middleware = conditional(() => true, [(req, res, next) => { | ||
next(); | ||
}]); | ||
middleware({}, {}); | ||
middleware({}, {}, done); | ||
}); | ||
@@ -104,4 +107,4 @@ | ||
it('works when the condition returns a promise true', done => { | ||
const middleware = conditional(() => Promise.resolve(true), [() => { | ||
done(); | ||
const middleware = conditional(() => Promise.resolve(true), [(req, res, next) => { | ||
next(); | ||
}]); | ||
@@ -119,3 +122,3 @@ middleware({}, {}, done); | ||
it('calls the middleware in order if the condition passes', done => { | ||
const middleware = conditional(req => true, [ | ||
const middleware = conditional(() => true, [ | ||
(req, res, next) => { | ||
@@ -148,4 +151,2 @@ if (req.mw2) { | ||
it('does not run subsequent middleware chains within the same "context"', done => { | ||
@@ -152,0 +153,0 @@ const CONTEXT = 'arbitrary_context'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49709
244
182