0.1.1 • Published 5 years ago

@sphericalelephant/express-api-version v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

express-api-version

NPM Version NPM Downloads Build Status Coverage Status

A simple API versioning middleware for express.

Usage

const {VersionManager, MiddlewareFactory} = require('@sphericalelephant/express-api-version');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');

const middlewareFactory = new MiddleWarefactory({
  // required: your custom vendor extension, resulting Content-Type application/vnd.api-test-vendor-1.0.0+json
  vndExtension: 'test-vendor',
  // required: a list of valid API versions
  supportedVersions: ['1.0.0', '1.1.1', '2.0.0', '2.2.2'],
  // optional: is used to attach the maximum satifying version to the req object, if not set '__api_version' is used
  attachVersionKey: 'customKey'
});

app.use(bodyParser.json({type: middlewareFactory.getVndExtensions()}));
app.get('/', (req, res, next) => {
  if (!req['customKey']) {
    return res.status(400).send();
  }
  // do something with the version key here
});