From e03e93622a295a10b240294c61b088c13e23eb55 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Wed, 9 Jan 2019 16:24:12 +0100 Subject: [PATCH] supporting req.params override feature --- README.md | 16 ++++++++++++++++ index.js | 24 ++++++++++++++++-------- package-lock.json | 2 +- package.json | 2 +- tests.js | 13 ++++++++++++- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 87b5a02..21a86f8 100644 --- a/README.md +++ b/README.md @@ -126,3 +126,19 @@ In the example, a `GET /tasks/:id` request will only execute the middleware if t - Accept-Version=2.0.0 - Accept-Version=2.x - Accept-Version=2.0.x + +#### Updatings requests params object +Optionally, you can override the `req.params` object with the parameters of the matching route defined on your configs: +```js +middleware.iff({ endpoints: [ + { + methods: ['GET'], + url: '/tasks/:id', + version: '2.0.0', + updateParams: true // enabling this config will result in req.params = {id: ...} + } +]}) +``` +> This feature can be really useful for business specific middlewares using the `iff` matching type. + +``` \ No newline at end of file diff --git a/index.js b/index.js index 6cf69b3..e1010db 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,10 @@ -const op = () => true +const op = updateParams => (req, res, params) => { + if (updateParams) { + req.params = params + } + + return true +} module.exports = function (routerOpts = {}, routerFactory) { routerOpts.defaultRoute = () => false @@ -13,13 +19,15 @@ module.exports = function (routerOpts = {}, routerFactory) { const opts = typeof options === 'function' ? { custom: options } : (Array.isArray(options) ? { endpoints: options } : options) if (opts.endpoints && opts.endpoints.length) { // setup matching router - opts.endpoints.map(endpoint => typeof endpoint === 'string' ? { methods: ['GET'], url: endpoint } : endpoint).forEach(({ methods, url, version }) => { - if (version) { - router.on(methods, url, { version }, op) - } else { - router.on(methods, url, op) - } - }) + opts.endpoints + .map(endpoint => typeof endpoint === 'string' ? { methods: ['GET'], url: endpoint } : endpoint) + .forEach(({ methods, url, version, updateParams = false }) => { + if (version) { + router.on(methods, url, { version }, op(updateParams)) + } else { + router.on(methods, url, op(updateParams)) + } + }) } const result = function (req, res, next) { diff --git a/package-lock.json b/package-lock.json index 044d40d..59aa185 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "middleware-if-unless", - "version": "1.0.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8bff00d..75e845f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "middleware-if-unless", - "version": "1.1.0", + "version": "1.2.0", "description": "Invokes connect-like middleware if / unless routing criteria matches. Inspired on express-unless module.", "main": "index.js", "scripts": { diff --git a/tests.js b/tests.js index dd85ead..e114277 100644 --- a/tests.js +++ b/tests.js @@ -5,6 +5,7 @@ const iffUnless = require('./index')() const middleware = function (req, res, next) { res.body = 'hit' + res.setHeader('id', req.params ? String(req.params.id) : '') return next() } @@ -28,7 +29,8 @@ describe('middleware-if-unless', () => { { methods: ['GET'], url: '/pets/:id', - version: '3.0.0' + version: '3.0.0', + updateParams: true } ]) .unless([ @@ -106,6 +108,15 @@ describe('middleware-if-unless', () => { expect(response.text).to.equal('hit') }) }) + + it('should get middleware route params on GET /pets/:id using accept-version 3.x', async () => { + await request(server) + .get('/pets/0') + .set('accept-version', '3.x') + .then((response) => { + expect(response.headers.id).to.equal('0') + }) + }) }) it('should successfully terminate the service', async () => {