Skip to content

Commit

Permalink
Enable versioning on demand (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Nov 6, 2020
1 parent 1bcedc7 commit 0118ab6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -144,7 +144,9 @@ router.on('GET', '/example', (req, res, params, store) => {

##### Versioned routes

If needed you can provide a `version` option, which will allow you to declare multiple versions of the same route.
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.

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.

###### default
<a name="semver"></a>
Expand Down
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -50,7 +50,7 @@ function Router (opts) {
this.ignoreTrailingSlash = opts.ignoreTrailingSlash || false
this.maxParamLength = opts.maxParamLength || 100
this.allowUnsafeRegex = opts.allowUnsafeRegex || false
this.versioning = opts.versioning || acceptVersionStrategy
this.versioning = opts.versioning || acceptVersionStrategy(false)
this.tree = new Node({ versions: this.versioning.storage() })
this.routes = []
}
Expand Down Expand Up @@ -105,6 +105,9 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
})

const version = opts.version
if (version != null && this.versioning.disabled) {
this.versioning = acceptVersionStrategy(true)
}

for (var i = 0, len = path.length; i < len; i++) {
// search for parametric or wildcard routes
Expand Down
19 changes: 15 additions & 4 deletions lib/accept-version.js
Expand Up @@ -2,9 +2,20 @@

const SemVerStore = require('semver-store')

module.exports = {
storage: SemVerStore,
deriveVersion: function (req, ctx) {
return req.headers['accept-version']
function build (enabled) {
if (enabled) {
return {
storage: SemVerStore,
deriveVersion: function (req, ctx) {
return req.headers['accept-version']
}
}
}
return {
storage: SemVerStore,
deriveVersion: function (req, ctx) {},
disabled: true
}
}

module.exports = build
25 changes: 25 additions & 0 deletions test/version.default-versioning.test.js
Expand Up @@ -240,3 +240,28 @@ test('It should throw if you declare multiple times the same route', t => {
t.is(err.message, 'Method \'GET\' already declared for route \'/\' version \'1.2.3\'')
}
})

test('Versioning won\'t work if there are no versioned routes', t => {
t.plan(2)

const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('We should not be here')
}
})

findMyWay.on('GET', '/', (req, res) => {
t.pass('Yeah!')
})

findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.x' }
}, null)

findMyWay.lookup({
method: 'GET',
url: '/'
}, null)
})

0 comments on commit 0118ab6

Please sign in to comment.