Skip to content

Commit a25f23a

Browse files
authored
Merge pull request #1 from jkyberneees/supporting-accept-version-header
supporting routing versions
2 parents 7da14a1 + c6a144d commit a25f23a

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
5+
before_install:
6+
# package-lock.json was introduced in npm@5
7+
- '[[ $(node -v) =~ ^v9.*$ ]] || npm install -g npm@latest' # skipped when using node 9
8+
install: npm install

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ Execute middleware only if routing restrictions matches:
4141
const app = require('express')()
4242
app.use(middleware.iff([
4343
{
44-
methods: ['POST', 'DELETE', 'PUT', 'PATCH'],
45-
url: '/tasks/:id'
44+
methods: ['POST', 'DELETE', 'PUT', 'PATCH'],
45+
url: '/tasks/:id'
4646
}
4747
]))
4848

@@ -79,6 +79,10 @@ const iu = require('middleware-if-unless')(
7979
function(opts){}
8080
)
8181
```
82+
### Known compatible routers:
83+
- https://www.npmjs.com/package/find-my-way
84+
- https://www.npmjs.com/package/anumargak
85+
8286
## iff / unless
8387
Both methods share the same configuration format:
8488

@@ -105,4 +109,20 @@ middleware.unless({ endpoints: [
105109
url: '/tasks/:id/*'
106110
}
107111
]})
108-
```
112+
```
113+
#### Supporting Accept-Version header
114+
Optionally, you can also restrict your middleware execution to specific versions using the `Accept-Version` header:
115+
> The `version` value should follow the [semver](https://semver.org/) specification.
116+
```js
117+
middleware.iff({ endpoints: [
118+
{
119+
methods: ['GET'],
120+
url: '/tasks/:id',
121+
version: '2.0.0'
122+
}
123+
]})
124+
```
125+
In the example, a `GET /tasks/:id` request will only execute the middleware if the `Accept-Version` header matches `2.0.0`. For example:
126+
- Accept-Version=2.0.0
127+
- Accept-Version=2.x
128+
- Accept-Version=2.0.x

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ module.exports = function (routerOpts = {}, routerFactory) {
1313
const opts = typeof options === 'function' ? { custom: options } : (Array.isArray(options) ? { endpoints: options } : options)
1414
if (opts.endpoints && opts.endpoints.length) {
1515
// setup matching router
16-
opts.endpoints.map(endpoint => typeof endpoint === 'string' ? { methods: ['GET'], url: endpoint } : endpoint).forEach(({ methods, url }) => {
17-
router.on(methods, url, op)
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+
}
1822
})
1923
}
2024

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.0.0",
3+
"version": "1.1.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: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,23 @@ describe('middleware-if-unless', () => {
2424
.iff(req => req.url.startsWith('/pets'))
2525
.iff([
2626
'/pets/*',
27-
'/pets/:id'
28-
]).unless([
27+
'/pets/:id',
28+
{
29+
methods: ['GET'],
30+
url: '/pets/:id',
31+
version: '3.0.0'
32+
}
33+
])
34+
.unless([
2935
'/pets/groups/list',
3036
{
3137
url: '/pets/:id', methods: ['DELETE']
3238
}
33-
]).unless(req => req.url.endsWith('.js'))
39+
])
40+
.unless(req => req.url.endsWith('.js'))
41+
.unless([{
42+
url: '/pets/:id', methods: ['GET'], version: '2.x'
43+
}])
3444
)
3545

3646
server = await service.start(~~process.env.PORT)
@@ -78,6 +88,24 @@ describe('middleware-if-unless', () => {
7888
expect(response.text).to.equal('')
7989
})
8090
})
91+
92+
it('should skip middleware on GET /pets/:id using accept-version 2.x', async () => {
93+
await request(server)
94+
.get('/pets/0')
95+
.set('accept-version', '2.0.1')
96+
.then((response) => {
97+
expect(response.text).to.equal('')
98+
})
99+
})
100+
101+
it('should hit middleware on GET /pets/:id using accept-version 3.x', async () => {
102+
await request(server)
103+
.get('/pets/0')
104+
.set('accept-version', '3.x')
105+
.then((response) => {
106+
expect(response.text).to.equal('hit')
107+
})
108+
})
81109
})
82110

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

0 commit comments

Comments
 (0)