dat-middleware
Advanced tools
Comparing version 1.5.4 to 1.5.5
@@ -7,13 +7,20 @@ var isString = require('101/is-string'); | ||
return args.map(function (arg) { | ||
var kpArgs; | ||
if (isString(arg)) { | ||
var keypathVal = keypather.get(req, arg); | ||
return exists(keypathVal) ? keypathVal : arg; | ||
kpArgs = [arg]; | ||
} | ||
else if (Array.isArray(arg)) { | ||
kpArgs = arg; | ||
} | ||
else { | ||
return arg; | ||
} | ||
kpArgs = [req].concat(kpArgs); | ||
var keypathVal = keypather.get.apply(keypather, kpArgs); | ||
return exists(keypathVal) ? keypathVal : arg; | ||
}); | ||
} | ||
var endMethods = ['send', 'json', 'end']; | ||
var endMethods = ['send', 'json', 'end', 'redirect']; | ||
function middlewarizeResMethod (method) { | ||
@@ -23,3 +30,5 @@ return function (/* keypathArgs */) { | ||
return function (req, res, next) { | ||
var args = replaceKeypaths(req, keypathArgs); | ||
var args = keypathArgs[2]? // if exact=true, dont replace keypaths | ||
keypathArgs.slice(0,2): | ||
replaceKeypaths(req, keypathArgs.slice(0,2)); | ||
res[method].apply(res, args); | ||
@@ -26,0 +35,0 @@ if (!~endMethods.indexOf(method)) { |
{ | ||
"name": "dat-middleware", | ||
"version": "1.5.4", | ||
"version": "1.5.5", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -38,3 +38,3 @@ "main": "index.js", | ||
"fn-object": "^0.2.2", | ||
"keypather": "^1.1.0", | ||
"keypather": "^1.6.0", | ||
"middleware-flow": "^0.2.0", | ||
@@ -41,0 +41,0 @@ "map-utils": "~0.4.0" |
var express = require('express'); | ||
var mw = require('../../index'); | ||
module.exports = function createAppWithMiddleware (middleware) { | ||
module.exports = function createAppWithMiddleware (middleware, middleware2) { | ||
middleware2 = middleware2 || function (req, res, next) {next();}; | ||
var app = express(); | ||
@@ -11,3 +12,4 @@ app.use(express.json()); | ||
app.all('/', | ||
middleware); | ||
middleware, | ||
middleware2); | ||
app.all('/body', | ||
@@ -14,0 +16,0 @@ // inspect, |
@@ -32,2 +32,50 @@ var createAppWithMiddleware = require('./fixtures/createAppWithMiddleware'); | ||
}); | ||
describe('keypaths', function() { | ||
beforeEach(function () { | ||
this.app = createAppWithMiddleware( | ||
mw.res.json(201, 'body') | ||
); | ||
}); | ||
it('should support req keypaths', function (done) { | ||
request(this.app) | ||
.post('/') | ||
.send({ foo: 'bar' }) | ||
.expect(201, {foo:'bar'}) | ||
.end(done); | ||
}); | ||
describe('keypath functions', function() { | ||
beforeEach(function () { | ||
this.app = createAppWithMiddleware( | ||
mw.body().set('func', function (a) { | ||
return a; | ||
}), | ||
mw.res.json(201, ['body.func(%)', { result: true }]) | ||
); | ||
}); | ||
it('should support req keypaths', function (done) { | ||
request(this.app) | ||
.post('/') | ||
.send({ foo: 'bar' }) | ||
.expect(201, {result:true}) | ||
.end(done); | ||
}); | ||
describe('keypaths false', function() { | ||
beforeEach(function () { | ||
this.app = createAppWithMiddleware( | ||
mw.body().set('func', function (a) { | ||
return a; | ||
}), | ||
mw.res.json(201, ['body.func(%)', { result: true }], true) | ||
); | ||
}); | ||
it('should support req keypaths keypaths=false option', function (done) { | ||
request(this.app) | ||
.post('/') | ||
.send({ foo: 'bar' }) | ||
.expect(201, ['body.func(%)', { result: true }]) | ||
.end(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -34,0 +82,0 @@ describe('status, write, and end', function() { |
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
61180
1664
Updatedkeypather@^1.6.0