Code Monkey home page Code Monkey logo

express-routes-versioning's Introduction

Express routes versioning

Build Status Coverage Status npm version

Simple node.js module provides versioning for expressjs routes/api.

Install

npm install express-routes-versioning

Usage

Follows semver versioning format. Supports '^, ~' symbols for matching version numbers.

    var app = require('express')();
    var routesVersioning = require('express-routes-versioning')();
    app.listen(3000);

    app.get('/test', routesVersioning({
       "1.0.0": respondV1,
       "~2.2.1": respondV2
    }));

    // curl -s -H 'accept-version: 1.0.0' localhost:3000/test
    // version 1.0.0 or 1.0 or 1 !
    function respondV1(req, res, next) {
       res.status(200).send('ok v1');
    }

    //curl -s -H 'accept-version: 2.2.0' localhost:3000/test
    //Anything from 2.2.0 to 2.2.9
    function respondV2(req, res, next) {
       res.status(200).send('ok v2');
    }

Supporting '^,~' on server might appear as an anti-pattern considering how npm versioning works, where client controls the version. Here server controls the version (or it may not), and client fully trust the server. Typically the client and server belong to the same organization in these cases.

API

routesVersioning(Options, NoMatchFoundCallback)

Options - object, containing version in semver format (supports ^,~ symbols) as key and function callback (connect middleware format) to invoke when the request matches the version as value. Note: Versions are expected to be mutually exclusive, as order of execution of the version couldn't be determined.

NoMatchFoundCallback (optional)- called if request version doesn't match the version provided in the options. If this callback is not provided latest version callback is called.

How version is determined for each request ?

Default behaviour is to use accept-version headers from the client.

This can be overridden by using a middleware and providing version in req.version property.

How versions are matched ?

semver versioning format is used to match version if versions are provided in semver format, supports ^,~ symbols on the server, else direct mapping is used (for versions like 1, 1.1)

Examples

Examples are available here

Test

npm test

express-routes-versioning's People

Contributors

lirantal avatar prasanna-sr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

express-routes-versioning's Issues

Usage with Routers

I am trying to set this up to use different express Routers based on the version. I am running into problems with it though. Is this possible to do? If so, could you provide a quick example?

import routesVersioning from 'express-routes-versioning';
import defaultHandlers from '../defaultHandlers';

// API Version Imports
import v1Router from './v1';

// API Versioning Handler
export default function () {
  return routesVersioning()({
    "^1.0.0": v1Router()
  }, defaultHandlers.versionNotFound);
}

UPDATE: Was missing the function call on v1Router. Code updated to reflect proper changes for my situtation.

How to set default value?

Hi, I am trying to use your versioning. Does this component support default value? Like accept-version not set, go to default function?

Discussion about semver

This is not an issue but an attempt to have a discussion about semver usage.

This project is supporting simple part of semver. It would be easier to use the semver official lib (https://github.com/npm/node-semver) in order to reduce project's code and also offers much powerful version matching (hyphen ranges, x-ranges and the full range grammar)

Problem i can see is the "latest version callback is called" feature. It seems the callback used is the one with the higher number. But it may be more complex with advanced selector. For example, what is the latest in those cases : "1.2.3 - 1.3.4" and "~1" ?

Also, the NoMatchFoundCallback can be defined as a semver ("*") instead of a separate option, which will be cleaner from an API point of view.

Also, if version is missing from the request http header (or from req.version), we should fallback on the wildcard matching in order to be easier to determine.

What do you think ?

Types for typescript

Hello,

I am currently in the process of adding types for this package over at the DefinetlyTyped repo. Just figured I would give a heads up. That way anybody looking for types can do so via npm install @types/express-routes-versioning

The PR to add the types is here: DefinitelyTyped/DefinitelyTyped#30419

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.