Skip to content

Commit

Permalink
Merge pull request #2 from jkyberneees/ft/requests-params-update
Browse files Browse the repository at this point in the history
supporting req.params override feature
  • Loading branch information
jkyberneees authored Jan 9, 2019
2 parents a25f23a + e03e936 commit ae04257
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
13 changes: 12 additions & 1 deletion tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -28,7 +29,8 @@ describe('middleware-if-unless', () => {
{
methods: ['GET'],
url: '/pets/:id',
version: '3.0.0'
version: '3.0.0',
updateParams: true
}
])
.unless([
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit ae04257

Please sign in to comment.