https://github.com/swvl/express-versioned-route
Simple express.js 4.x extension, to add support for versioned routes.
https://github.com/swvl/express-versioned-route
Last synced: over 1 year ago
JSON representation
Simple express.js 4.x extension, to add support for versioned routes.
- Host: GitHub
- URL: https://github.com/swvl/express-versioned-route
- Owner: swvl
- Created: 2020-03-04T06:18:55.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T06:19:14.000Z (over 3 years ago)
- Last Synced: 2025-03-06T09:57:01.643Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://swvl.github.io/express-versioned-route/
- Size: 2.58 MB
- Stars: 7
- Watchers: 47
- Forks: 1
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
- awesome-egypt-opensource - swvl/express-versioned-route - Simple express.js 4.x extension, to add support for versioned routes (Projects / Web and Publishing)
README
# Express-Versioned-Route
Simple express.js 4.x extension, to add support for versioned routes.
[](http://commitizen.github.io/cz-cli/)
[](https://github.com/semantic-release/semantic-release)
[](https://codecov.io/gh/swvl/express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
[](https://sonarcloud.io/dashboard?id=swvl_express-versioned-route)
## Highlights
- Versioning the same route
- Loading time validation
- Custom middlewares for each version
- Built-in soft deprecation policy
- Case insensitive name matching
- Minimal overhead ( less than 0.15ms/request )
- Dynamic routing based on client headers
- Client can choose specific version using `accept-version` header
## installation
- Update your .npmrc file: `echo "@swvl:registry=https://npm.pkg.github.com" >> .npmrc`
- Then install the package: `npm install @swvl/express-versioned-route`
## Usage
```js
const searchVersionDef = versionsDef()({
versions: {
simpleSearch: ['2020-Q1', searchMW1, searchMW2, searchHandlerV1],
dynamicSearch: [dynamicSearchMW1, dynamicSearchMW2, searchHandlerV2, 'default'],
superSearch: [searchHandlerV3],
deepSearch: [searchHandlerV4],
},
Android: [
[400, 'simpleSearch'],
[450, 'dynamicSearch'],
[500, 'superSearch'],
],
iOS: [
[400, 'simpleSearch'],
[460, 'dynamicSearch'],
],
});
app.get('/search', mw1, mw2, searchVersionDef);
```
#### Expected user headers
* device-os & build-number
* accept-version
[](https://runkit.com/swvl/express-versioned-route)
## Options
Global options can be passed as 1st parameter for versionsDef function, or configured as part of versionsDef specs
#### Example
```js
const searchVersionDef = versionsDef(globalOptions)({
versions: {
...
}
...,
options: {
...
}
});
```
#### Supported options
prop name
description
default
onDeprecated
callback
(versionName, req)=> {}
allowClientRequestFallbackToDefaultVersion
invalid client headers will fallback to default version instead of 404
true
onClientSelection
configure how client should be selected based on req
onClientSelection: req => {
return {
name: req.headers['device-os'],
buildNumber: req.headers['build-number'],
};
}
## License
[Apache-2.0]()