Skip to content

Commit ab40835

Browse files
authored
Enable versioning on demand (#172)
1 parent b9337ca commit ab40835

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ router.on('GET', '/example', (req, res, params, store) => {
144144

145145
##### Versioned routes
146146

147-
If needed you can provide a `version` option, which will allow you to declare multiple versions of the same route.
147+
If needed you can provide a `version` option, which will allow you to declare multiple versions of the same route. If you never configure a versioned route, the `'Accept-Version'` header will be ignored.
148+
149+
Remember to set a [Vary](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary) header in your responses with the value you are using for deifning the versioning (e.g.: 'Accept-Version'), to prevent cache poisoning attacks. You can also configure this as part your Proxy/CDN.
148150

149151
###### default
150152
<a name="semver"></a>

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function Router (opts) {
5151
this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
5252
this.maxParamLength = opts.maxParamLength || 100
5353
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
54-
this.versioning = opts.versioning || acceptVersionStrategy
54+
this.versioning = opts.versioning || acceptVersionStrategy(false)
5555
this.trees = {}
5656
this.routes = []
5757
}
@@ -111,6 +111,9 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
111111
})
112112

113113
const version = opts.version
114+
if (version != null && this.versioning.disabled) {
115+
this.versioning = acceptVersionStrategy(true)
116+
}
114117

115118
for (var i = 0, len = path.length; i < len; i++) {
116119
// search for parametric or wildcard routes

lib/accept-version.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
const SemVerStore = require('semver-store')
44

5-
module.exports = {
6-
storage: SemVerStore,
7-
deriveVersion: function (req, ctx) {
8-
return req.headers['accept-version']
5+
function build (enabled) {
6+
if (enabled) {
7+
return {
8+
storage: SemVerStore,
9+
deriveVersion: function (req, ctx) {
10+
return req.headers['accept-version']
11+
}
12+
}
13+
}
14+
return {
15+
storage: SemVerStore,
16+
deriveVersion: function (req, ctx) {},
17+
disabled: true
918
}
1019
}
20+
21+
module.exports = build

test/version.default-versioning.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,28 @@ test('It should throw if you declare multiple times the same route', t => {
240240
t.is(err.message, 'Method \'GET\' already declared for route \'/\' version \'1.2.3\'')
241241
}
242242
})
243+
244+
test('Versioning won\'t work if there are no versioned routes', t => {
245+
t.plan(2)
246+
247+
const findMyWay = FindMyWay({
248+
defaultRoute: (req, res) => {
249+
t.fail('We should not be here')
250+
}
251+
})
252+
253+
findMyWay.on('GET', '/', (req, res) => {
254+
t.pass('Yeah!')
255+
})
256+
257+
findMyWay.lookup({
258+
method: 'GET',
259+
url: '/',
260+
headers: { 'accept-version': '2.x' }
261+
}, null)
262+
263+
findMyWay.lookup({
264+
method: 'GET',
265+
url: '/'
266+
}, null)
267+
})

0 commit comments

Comments
 (0)