Lightweight api versioning tool for express
npm install version-router-express
npm install --save version-router-express
`
import:
`typescript
import {VersionRouter, VersionedRoute} from 'version-router-express';
`
This module generates Express middleware that enables efficient management of api routes versions.
It enables the use of middleware arrays for each version of the route, and flexible version resolving strategies.
Usage
*
$3
VersionedRoute assumes the existence of a 'version' property on the request object.
This property can be generated in any way along the route's path.
VersionedRoute has a static utility method that can be mounted to extract the version from a header and assign it to req.version.
`typescript
app.use(VersionedRouter.ExtractVersionFromHeader('App-version'));
`
Any custom header can be used, as well as any other strategy for assigning a value to request.version
$3
Version matching is done according to semver standards.
`typescript
const routeVersions = [
new VersionedRoute({
version: '1.0.0',
default: false,
middleware: [
(req: Request, res: Response, next: NextFunction) => {
console.log('route 2 function 1')
next()
},
(req: Request, res: Response, next: NextFunction) => {
console.log('route 2 function 2')
res.send({route: '1'})
},
]
}),
new VersionedRoute({
version: '>=1.2.0 <2.0.0',
default: false,
middleware: [
(req: Request, res: Response, next: NextFunction) => {
console.log('route 2 function 1')
next()
},
(req: Request, res: Response, next: NextFunction) => {
console.log('route 2 function 2')
res.send({route: '2'})
},
]
}),
new VersionedRoute({
version: '2.0.0',
default: true,
middleware: [
(req: Request, res: Response, next: NextFunction) => {
console.log('route 3 function 1')
next()
},
(req: Request, res: Response, next: NextFunction) => {
console.log('route 3 function 2')
res.send({route: '3'})
},
]
})
]
`
Request version is checked against the versioned route 'version' property according to the semver standard.
If no matches were found, it will resolve to the route defined as default.
Mount VersionRouter
`typescript
app.use('/testRoute', new VersionRouter(routeVersions).routeRequestByVersion)
`
The router can be configured with a custom error handler for the version matching stage
`typescript
app.use('/testRoute', new VersionRouter(
routeVersions,
{
errorHandler: (req, res, next) => {
// TODO do some error handling stuff
next(new Error('some message'))
}
}).routeRequestByVersion)
`
Api
*
Class: VersionRouter
Creates a router instance to route requests according to the value of req.version
`typescript
export declare class VersionRouter
`
$3
| Constructor | Description |
| --- | --- |
| constructor(routes, options) | Constructs a new instance of the VersionRouter class |
$3
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| errorHandler | |(req: RequestWithVersion, res: Response, next: NextFunction) => void | Error handler function for the version matching stage |
| routes | |Array<VersionedRoute> | Array of VersionedRoute defining the routing options and versions |
$3
| Method | Modifiers | Description |
| --- | --- | --- |
| ExtractVersionFromHeaders(versionHeader) | static | Middleware for extracting the version from the said request header and assign it to req.version Mount it once BEFORE all versioned routes |
| routeRequestByVersion()| | Middleware to mount on the route path e.g. app.use('/api/users', UsersRoutes.routeRequestByVersion()) |
Class: VersionedRoute
Creates a versioned route object that contains middleware and version data
Signature:
`typescript
export declare class VersionedRoute implements VersionedMiddleware
``
VersionedRoute class |
VersionRouterwill resolve to the default route in when version property is undefined or no matching version was found |
Routerinstance with the route's middleware mounted |