2.0.0 • Published 9 months ago

express-route-versioning-middleware v2.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

Express Route Versioning Middleware

ExpressRouteVersioning Test

No more versioning routes by adding parameters to the url or adding to the path name. Simply add accept-version: versionNumber to the request headers and let your middleware choose the correct function.

Why use this version

This version does not dictate your express.Request, express.Response, or NextFunction types for the RouteHandler. So if you have a middleware before/after that adjusts response.locals to a strict type this will play nicely in TS.

Supports Express 5!

Usage

Version numbers must be in ascending order

Basic express usage

import express, { Request, Response, NextFunction } from 'express';
import { routeVersionHandler } from 'express-route-versioning-middleware';

const app = express();
const router = express.Router();

app.use(router);

//curl --header "accept-version: 0.1.5" localhost:8081/doThings
//Will hit testFunc2
router.get('/doThings', routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.2.3': testFunc3
}));

Same example but with middleware for auth before route versioning.

//curl --header "accept-version: 0.1.5" localhost:8081/doThings
//Will hit testFunc2 with fancyAuthMiddleware's next()
router.get('/doThings', fancyAuthMiddleware, routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.2.3': testFunc3
}));

Versioning Logic

Run the highest version function where requester's version is >= function version

In simple terms

'minimumVersionRequired': functionToCall

Examples:

routeVersionHandler({
    '0.0.0': testFunc1,
    '0.1.0': testFunc2,
    '1.0.0': testFunc3,
    '1.2.3': testFunc4
})

testFunc1
curl --header "accept-version: 0.0.0" localhost:8081/doThings

testFunc1
curl --header "accept-version: 0.0.999" localhost:8081/doThings

testFunc3
curl --header "accept-version: 1.2.2" localhost:8081/doThings

testFunc4
curl --header "accept-version: 1.2.3" localhost:8081/doThings

Version Information

  • 1.x.x Supports Express ^4.x.x
  • 2.x.x Supports Express ^5.x.x
1.0.9

9 months ago

1.0.10

9 months ago

2.0.0

9 months ago

1.0.8

12 months ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago