Skip to content

Commit ae04257

Browse files
authored
Merge pull request #2 from jkyberneees/ft/requests-params-update
supporting req.params override feature
2 parents a25f23a + e03e936 commit ae04257

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,19 @@ In the example, a `GET /tasks/:id` request will only execute the middleware if t
126126
- Accept-Version=2.0.0
127127
- Accept-Version=2.x
128128
- Accept-Version=2.0.x
129+
130+
#### Updatings requests params object
131+
Optionally, you can override the `req.params` object with the parameters of the matching route defined on your configs:
132+
```js
133+
middleware.iff({ endpoints: [
134+
{
135+
methods: ['GET'],
136+
url: '/tasks/:id',
137+
version: '2.0.0',
138+
updateParams: true // enabling this config will result in req.params = {id: ...}
139+
}
140+
]})
141+
```
142+
> This feature can be really useful for business specific middlewares using the `iff` matching type.
143+
144+
```

index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
const op = () => true
1+
const op = updateParams => (req, res, params) => {
2+
if (updateParams) {
3+
req.params = params
4+
}
5+
6+
return true
7+
}
28

39
module.exports = function (routerOpts = {}, routerFactory) {
410
routerOpts.defaultRoute = () => false
@@ -13,13 +19,15 @@ module.exports = function (routerOpts = {}, routerFactory) {
1319
const opts = typeof options === 'function' ? { custom: options } : (Array.isArray(options) ? { endpoints: options } : options)
1420
if (opts.endpoints && opts.endpoints.length) {
1521
// setup matching router
16-
opts.endpoints.map(endpoint => typeof endpoint === 'string' ? { methods: ['GET'], url: endpoint } : endpoint).forEach(({ methods, url, version }) => {
17-
if (version) {
18-
router.on(methods, url, { version }, op)
19-
} else {
20-
router.on(methods, url, op)
21-
}
22-
})
22+
opts.endpoints
23+
.map(endpoint => typeof endpoint === 'string' ? { methods: ['GET'], url: endpoint } : endpoint)
24+
.forEach(({ methods, url, version, updateParams = false }) => {
25+
if (version) {
26+
router.on(methods, url, { version }, op(updateParams))
27+
} else {
28+
router.on(methods, url, op(updateParams))
29+
}
30+
})
2331
}
2432

2533
const result = function (req, res, next) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "middleware-if-unless",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Invokes connect-like middleware if / unless routing criteria matches. Inspired on express-unless module.",
55
"main": "index.js",
66
"scripts": {

tests.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const iffUnless = require('./index')()
55

66
const middleware = function (req, res, next) {
77
res.body = 'hit'
8+
res.setHeader('id', req.params ? String(req.params.id) : '')
89

910
return next()
1011
}
@@ -28,7 +29,8 @@ describe('middleware-if-unless', () => {
2829
{
2930
methods: ['GET'],
3031
url: '/pets/:id',
31-
version: '3.0.0'
32+
version: '3.0.0',
33+
updateParams: true
3234
}
3335
])
3436
.unless([
@@ -106,6 +108,15 @@ describe('middleware-if-unless', () => {
106108
expect(response.text).to.equal('hit')
107109
})
108110
})
111+
112+
it('should get middleware route params on GET /pets/:id using accept-version 3.x', async () => {
113+
await request(server)
114+
.get('/pets/0')
115+
.set('accept-version', '3.x')
116+
.then((response) => {
117+
expect(response.headers.id).to.equal('0')
118+
})
119+
})
109120
})
110121

111122
it('should successfully terminate the service', async () => {

0 commit comments

Comments
 (0)