Skip to content

SoprachevAK/express-api-version

Repository files navigation

express-api-version

npm package Build Status Downloads Code Coverage

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'))

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published