npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

version-router-express

v0.2.12

Published

Lightweight api versioning tool for express

Downloads

26

Readme

Version Router Express

Lightweight api versioning tool for express

Build Status Coverage Status Install Size Dependency Count

Installation


npm install --save version-router-express

import:

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


Determining the desired version for the request

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.

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

Configuring versioned routes

Version matching is done according to semver standards.

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

app.use('/testRoute', new VersionRouter(routeVersions).routeRequestByVersion)

The router can be configured with a custom error handler for the version matching stage

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

export declare class VersionRouter

Constructors

| Constructor | Description | | --- | --- | | constructor(routes, options) | Constructs a new instance of the VersionRouter class |

Properties

| 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 |

Methods

| 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:

export declare class VersionedRoute implements VersionedMiddleware 

Implements: VersionedMiddleware

Constructors

| Constructor | Description | | --- | --- | | constructor(config) | Constructs a new instance of the VersionedRoute class |

Properties

| Property | Modifiers | Type | Description | | --- | --- | --- | --- | | default | | boolean | Sets the version as default.VersionRouterwill resolve to the default route in when version property is undefined or no matching version was found | | middleware | | Array<RequestHandler>| An array of request handlers to mount on the versioned route | | version | | string | Semver version number |

Methods

| Method | Modifiers | Description | | --- | --- | --- | | toRouter() | | returns a Routerinstance with the route's middleware mounted |