This npm package provides an ExpressJS middleware that sets the request object with a version property by parsing a request HTTP header.
If you wish to employ your own logic in some middleware/configuration and set the request version programmaticaly and not by parsing a specific HTTP header:
const versionRequest = require('express-version-request')
app.use(versionRequest.setVersion('1.2.3'))Then in later middlewares you will be able to access req.version property and it's value set to 1.2.3.
By default, the library will parse the version out of the X-Api-Version HTTP header:
const versionRequest = require('express-version-request')
app.use(versionRequest.setVersionByHeader())If you wish to advise the library which HTTP header to parse to extract the version:
const versionRequest = require('express-version-request')
app.use(versionRequest.setVersionByHeader('My-HTTP-Header-Name'))By default, the library will parse the version out of the api-version query parameter:
const versionRequest = require('express-version-request')
app.use(versionRequest.setVersionByQueryParam())If you wish to advise the library which query parameter to parse to extract the version:
const versionRequest = require('express-version-request')
app.use(versionRequest.setVersionByQueryParam('myQueryParam'))The second parameter of setVersionByQueryParam is an options object.
removeQueryParam
Delete version HTTP query parameter after setting the request object with a version property.
By default it is set to false.
const versionRequest = require('express-version-request')
const options = {removeQueryParam: true}
app.use(versionRequest.setVersionByQueryParam('myQueryParam', options))If you define a middleware after versionRequest then you can verify that the version is indeed set:
app.use((req, res, next) => {
console.log(req.version)
next()
})yarn add express-version-requestyarn testProject linting:
yarn lintyarn test:coverageThe project uses the commitizen tool for standardizing changelog style commit messages so you should follow it as so:
git add . # add files to staging
yarn commit # use the wizard for the commit messageLiran Tal [email protected]