dat-middleware
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -6,2 +6,8 @@ var error = require('./error'); | ||
var pick = require('101/pick'); | ||
var pluck = require('101/pluck'); | ||
var notEquals = function (compare) { | ||
return function (val) { | ||
return val !== compare; | ||
}; | ||
}; | ||
var returnTrue = function () { return true; }; | ||
@@ -327,57 +333,25 @@ var notUndefined = function (v) { | ||
* @method function | ||
* @param {...key} string - keys checked to be true. | ||
* @param {function} ...middlewares - middlewares to execute if the exec passes | ||
*/ | ||
RequestData.prototype.if = function (/* keys */) { | ||
if (this._keys.length) { | ||
throw new Error('"if" should not be used with any other methods, invoke it with keys'); | ||
RequestData.prototype.then = function () { | ||
console.log(this.steps); | ||
if (this.steps.map(pluck('type')).every(notEquals('validation'))) { | ||
throw new Error('then should be used after some validation steps have been specified'); | ||
} | ||
var self = this; | ||
var conditional; | ||
this._keys = Array.prototype.slice.call(arguments); | ||
if (this._keys[0] && this._keys[0].or) { // or | ||
this._keys = this._keys[0].or; | ||
conditional = flow.syncIf(keypathsTrue('some')); | ||
} | ||
else { | ||
conditional = flow.syncIf(keypathsTrue('every')); | ||
} | ||
return conditional; | ||
function keypathsTrue (method) { | ||
return function (req) { | ||
return self._keys | ||
.map(self.keypath.bind(self)) | ||
[method](keypather.get.bind(keypather, req)); | ||
}; | ||
} | ||
var mwIf = flow.mwIf(this.exec.bind(this)); | ||
return mwIf.then.apply(mwIf, arguments); | ||
}; | ||
/** | ||
* @method function | ||
* @param {...key} string - keys checked to exist. | ||
* @param {function} ...middlewares - middlewares to execute if the exec passes | ||
*/ | ||
RequestData.prototype.ifExists = function (/* keys */) { | ||
if (this._keys.length) { | ||
throw new Error('"exists" should not be used with any other methods, invoke it with keys'); | ||
RequestData.prototype.else = function () { | ||
console.log(this.steps); | ||
if (this.steps.map(pluck('type')).every(notEquals('validation'))) { | ||
throw new Error('then should be used after some validation steps have been specified'); | ||
} | ||
var self = this; | ||
var conditional; | ||
this._keys = Array.prototype.slice.call(arguments); | ||
if (this._keys[0] && this._keys[0].or) { // or | ||
this._keys = this._keys[0].or; | ||
conditional = flow.syncIf(keypathsExist('some')); | ||
} | ||
else { | ||
conditional = flow.syncIf(keypathsExist('every')); | ||
} | ||
return conditional; | ||
function keypathsExist (method) { | ||
return function (req) { | ||
return self._keys | ||
.map(self.keypath.bind(self)) | ||
.map(keypather.get.bind(keypather, req)) | ||
[method](utils.exists); | ||
}; | ||
} | ||
var mwIf = flow.mwIf(this.exec.bind(this)); | ||
return mwIf.else.apply(mwIf, arguments); | ||
}; | ||
/*************** INTERNALS ***************/ | ||
@@ -384,0 +358,0 @@ |
{ | ||
"name": "dat-middleware", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"test": "mocha test", | ||
"testwatch": "nodemon --delay 1 -w lib -w test -w configs -x npm test" | ||
"test-watch": "nodemon --delay 1 -w lib -w test -w configs -x npm test" | ||
}, | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -233,7 +233,4 @@ # dat-middleware [![Build Status](https://travis-ci.org/tjmehta/dat-middleware.png?branch=master)](https://travis-ci.org/tjmehta/dat-middleware) | ||
## if(keys...).then(middlewares...).else(middlewares...) | ||
## [validation chain].then(middlewares...).else(middlewares) | ||
if the values of the key's specified are all truthy it will run the 'then middlewares' | ||
else if will run the 'else middlewares' | ||
```js | ||
@@ -243,58 +240,13 @@ var mw = require('dat-middleware'); | ||
// body of {key1:<truthy>} runs mw1 and mw2 | ||
// body of {key1:<falsy>} runs mw3 | ||
app.use(mw.body().if('key1') | ||
.then(mw1, mw2) | ||
.else(mw3)); | ||
// requires that req.body.key1 and req.body.key2 exist and are 24 characters | ||
app.use(mw.body('key1, key2').require() | ||
.then(mw1) // executes mw1 if key1 and key2 exist | ||
.else(mw2) // executes mw1 if key1 and key2 if they dont | ||
// if mw2 accept an error it will recieve the validation error, else it will be ignored | ||
); | ||
``` | ||
## using if with or | ||
```js | ||
var mw = require('dat-middleware'); | ||
var app = require('express')(); | ||
// body of {key1:<truthy>} runs mw1 and mw2 | ||
// body of {key2:<truthy>} runs mw1 and mw2 | ||
// body of {key1:<truthy>, key2:<truthy>} runs mw1 and mw2 | ||
// body of {key1:<falsy>} runs mw3 | ||
// body of {key1:<falsy>, key2:<falsy>} runs mw3 | ||
app.use(mw.body().if({ or: ['key1', 'key2'] }) | ||
.then(mw1, mw2) | ||
.else(mw3)); | ||
``` | ||
## ifExists(keys...).then(middlewares...).else(middlewares...) | ||
if the values of the key's specified all exist it will run the 'then middlewares' | ||
else if will run the 'else middlewares' | ||
```js | ||
var mw = require('dat-middleware'); | ||
var app = require('express')(); | ||
// body of {key1:true} runs mw1 and mw2 | ||
// body of {key1:null} runs mw3 | ||
app.use(mw.body().ifExists('key1') | ||
.then(mw1, mw2) | ||
.else(mw3)); | ||
``` | ||
## using ifExists with or | ||
```js | ||
var mw = require('dat-middleware'); | ||
var app = require('express')(); | ||
// body of {key1:'val'} runs mw1 and mw2 | ||
// body of {key2:true} runs mw1 and mw2 | ||
// body of {key1:'val', key2:'val'} runs mw1 and mw2 | ||
// body of {key1:undefined} runs mw3 | ||
// body of {key1:null, key2:null} runs mw3 | ||
app.use(mw.body().ifExists({ or: ['key1', 'key2'] }) | ||
.then(mw1, mw2) | ||
.else(mw3)); | ||
``` | ||
# Chaining: chained methods will run in order | ||
note: conditionals do not chain with validations and transformations | ||
note: conditionals do not chain before validations and transformations | ||
@@ -301,0 +253,0 @@ ```js |
@@ -20,88 +20,34 @@ var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
describe('conditionals', function () { | ||
describe('ifPass', function () { | ||
describe('mw.body().if(key).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('if', 'key1', { key1: true})); | ||
describe('else', conditionalFail('if', 'key2', { key1: true})); | ||
}); | ||
describe('mw.body().if(keys...).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('if', ['key1', 'key2'], { key1: true, key2: true })); | ||
describe('else', conditionalFail('if', ['key2', 'key3'], { key1: true, key2: true })); | ||
}); | ||
describe('mw.body().if({ or: keys... }).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('if', {or:['key1', 'key2']}, { key1: true })); | ||
describe('else', conditionalFail('if', {or:['key2', 'key3']}, { key1: true })); | ||
}); | ||
describe('mw.body().ifExists(key).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('ifExists', 'key1', { key1: true})); | ||
describe('else', conditionalFail('ifExists', 'key2', { key1: true})); | ||
}); | ||
describe('mw.body().ifExists(keys...).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('ifExists', ['key1', 'key2'], { key1: true, key2: true })); | ||
describe('else', conditionalFail('ifExists', ['key2', 'key3'], { key1: true, key2: true })); | ||
}); | ||
describe('mw.body().ifExists({ or: keys... }).then(mw..).else(mw...)', function () { | ||
describe('if then', conditionalPass('ifExists', {or:['key1', 'key2']}, { key1: true })); | ||
describe('else', conditionalFail('ifExists', {or:['key2', 'key3']}, { key1: true })); | ||
}); | ||
}); | ||
}); | ||
function conditionalPass (method, key, body) { | ||
return function () { | ||
describe('then', function () { | ||
beforeEach(function () { | ||
var mwBody = mw.body(); | ||
var conditional = Array.isArray(key) ? | ||
mwBody[method].apply(mwBody, key): | ||
mwBody[method].call(mwBody, key); | ||
this.app = createAppWithMiddleware( | ||
conditional | ||
.then( | ||
appendReqBody('key', '1'), | ||
appendReqBody('key', '2'), | ||
appendReqBody('key', '3') | ||
) | ||
.else(nextError(err)) | ||
mw.body('key').require() | ||
.then(appendReqBody('key', '1')) | ||
.else(appendReqBody('key', '2')) | ||
); | ||
}); | ||
it('should run middlewares in then if it passes', function (done) { | ||
it('should execute "then-middlewares" the validation passes', function (done) { | ||
request(this.app) | ||
.post('/body') | ||
.send(body) | ||
.expect(200) | ||
.expect(function (res) { | ||
res.body.key.should.equal('123'); | ||
}) | ||
.send({ key: 'val' }) | ||
.expect({ key: 'val1' }) | ||
.end(done); | ||
}); | ||
}; | ||
} | ||
function conditionalFail (method, key, body) { | ||
return function () { | ||
before(function () { | ||
var mwBody = mw.body(); | ||
var conditional = Array.isArray(key) ? | ||
mwBody[method].apply(mwBody, key): | ||
mwBody[method].call(mwBody, key); | ||
}); | ||
describe('else', function () { | ||
beforeEach(function () { | ||
this.app = createAppWithMiddleware( | ||
conditional | ||
.then( | ||
appendReqBody('key', '1'), | ||
appendReqBody('key', '2'), | ||
appendReqBody('key', '3') | ||
) | ||
.else(nextError(err)) | ||
mw.body('nonexistant').require() | ||
.then(appendReqBody('key', '1')) | ||
.else(appendReqBody('key', '2')) | ||
); | ||
}); | ||
it('should run middlewares in then if it passes', function (done) { | ||
it('should execute "else-middlewares" the validation passes', function (done) { | ||
request(this.app) | ||
.post('/body') | ||
.send(body) | ||
.expect(500) | ||
.expect(function (res) { | ||
res.body.message.should.equal('boom'); | ||
}) | ||
.send({ key: 'val' }) | ||
.expect({ key: 'val2' }) | ||
.end(done); | ||
}); | ||
}; | ||
} | ||
}); | ||
}); |
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
53347
1400
268