/express-versioned-route

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

Primary LanguageJavaScript

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

  • Versioning the same route
  • Loading time validation
  • Custom middlewares for each version
  • Built-in soft deprecation policy
  • Case insensitive name matching
  • Minimal overhead ( less than 0.15ms/request )
  • Dynamic routing based on client headers
  • Client can choose specific version using accept-version header

installation

  • Update your .npmrc file: echo "@swvl:registry=https://npm.pkg.github.com" >> .npmrc
  • Then install the package: npm install @swvl/express-versioned-route

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

  • device-os & build-number
  • accept-version

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