express-versioned-route

Simple express.js 4.x extension, to add support for versioned routes.

View on GitHub

Express-Versioned-Route

Simple express.js 4.x extension, to add support for versioned routes.

Commitizen friendly semantic-release codecov Maintainability Rating Security Rating Quality Gate Status

Technical Debt Vulnerabilities Bugs Code Smells Duplicated Lines (%) Lines of Code

Highlights

installation

Usage

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

Try it live on RunKit

Options

Global options can be passed as 1st parameter for versionsDef function, or configured as part of versionsDef specs

Example

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