Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable versioning on demand #172

Merged
merged 4 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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.trees = {}
this.routes = []
}
Expand Down Expand Up @@ -111,6 +111,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
})