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

easy-version

v1.2.2

Published

Middleware for API Versioning using content negotiation and media types

Downloads

2

Readme

Easy-Version

Comprehensive middleware for API versioning

Middlweare for Express framework to enable API versioning. Implements versioning through content media fields (content-negotiation) to specify the version as media type for ensure backwards compatibility using request headers.

The goal of this middleware is to avoid the URI versioning, replacing by the use of common request headers.

E.g.

Accept: application/wm.my-service-name+json;version=2.0.0

Content-Type: application/wm.my-service-name+json;version=2.0.0

The recommended format for the media type is:

application/<prefix>.<service-name>+json;version=<version>
  • Its strongly suggest that specify the version value using semantic versioning (https://semver.org/).
  • The prefix value is optional, but is recommended to use the prefix to avoid conflicts with other media types. E.g. ms for Microservice, wm for web-middleware, etc.
  • The service-name value is the name of the service (API) that is being versioned.

References

Build with

  • NodeJs v16.15.1
  • NPM v8.11.0

Last Update

  • 2022-10-05 Change the RouteTo function to be the last middleware in the stack.

Get Started

For use this package, first install as dependency

npm i easy-version

Import into the express application

const {
    registerVersion,
    routeTo
} = require('easy-version');

Register middlerware for detects the requested API version

    app.use(registerVersion());

For each route, regiter the handlers per accepted version of the API

const pathVersions = {
    "1.0.0": Controller.myCustomHandler,
    "2.0.0": myCustomHandlerV2,
};
app.get('/path', routeTo(pathVersions));

Response Headers

Its recommended that the APIs must send a Vary header to enable cache and proxies to differentiate between versions (as below):

res.setHeader("Content-Type", "application/x.service-name+json;version="+req.version);
res.setHeader("Vary", "Content-Type");

This headers will be injected automatically

Documentation

registerVersion (customParser)

Function to parse the Accept header value for get the version requested and registers it to the request version attribute.

@param {function} customParser (Optional) Function to execute a custom parse logic for detects the version

routeTo (args, notVersionFound)

Function to execute the correct handler for the version requested.

@param {object} args (Required) Map with the version number and its handler

@param {function} notVersionFound (Optional) Default callback for executes if no version found

Note: This function is responsible for call the request handlers, so its necessary that the handlers are registered before this function and returns a response. No middleware can be registered after this function.

Request.version

Injects the version detected into the version attribute of the standard request object. So its available to read in all the requests.

req.version Attribute to get the received version

Author

Rafael Chavez Solis [email protected]

License

MIT License (MIT) - LICENSE file for details