express-api-version

Express middleware for API versioning

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
express-api-version
1.0.0a year agoa year agoMinified + gzip package size for express-api-version in KB

Readme

express-api-version
!npm packagenpm-imgnpm-url !Build Statusbuild-imgbuild-url !Downloadsdownloads-imgdownloads-url !Code Coveragecodecov-imgcodecov-url
The module adds convenient versioning to your project. You can specify versions anywhere of request (path, query, headers). Use semver format. Before each handler, put a middleware with a version check, if it does not satisfies, there will be a continue to the next handler.

Install

npm install express-api-version

Usage

Add versionParser middleware and specialize the path to version relative to the Request.
import express from 'express'
import { versionParser, versionSatisfies } from 'express-api-version'

const app = express()

app.use(versionParser('query.v')) // Request.query.v

// curl -s localhost:5000/test?v=1.1.2
app.get('/test', versionSatisfies('1.0.0 - 1.2.3'), (req, res) => {
  return res.send('1')
})

// curl -s localhost:5000/test?v=0.3.0
app.get('/test', versionSatisfies('<1.0.0'), (req, res) => {
  return res.send('0')
})

app.listen(5000)

For using x-api-version header:
app.use(versionParser('headers.x-api-version'))