All Projects → lirantal → Express Version Route

lirantal / Express Version Route

Licence: mit
A Node.js express middleware that implements API versioning for route controllers

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Express Version Route

Requent
A GraphQL like interface to map a request to eloquent query with data transformation for Laravel.
Stars: ✭ 78 (-6.02%)
Mutual labels:  api
Easyimages
此版本不再维护,已出新版:速度更快,压缩更小:
Stars: ✭ 80 (-3.61%)
Mutual labels:  api
Drf Autodocs
Ultimately automated DRF documentation rendering(UNMAINTAINED)
Stars: ✭ 82 (-1.2%)
Mutual labels:  api
Httpie Oauth
OAuth plugin for HTTPie
Stars: ✭ 78 (-6.02%)
Mutual labels:  api
Aztro
The Astrology API 💫 Get daily horoscope!
Stars: ✭ 78 (-6.02%)
Mutual labels:  api
Hydrogen
🎈 Hydrogen. Voted (by me) the world's lightest static-site generator built with TypeScript ❤ It uses 🔥 lit-html inspired templating for super duper performant template generation.
Stars: ✭ 80 (-3.61%)
Mutual labels:  api
Tortilla
Wrapping web APIs made easy.
Stars: ✭ 1,215 (+1363.86%)
Mutual labels:  api
Google Searchconsole
A wrapper for the Google Search Console API.
Stars: ✭ 83 (+0%)
Mutual labels:  api
Mygene.info
MyGene.info: A BioThings API for gene annotations
Stars: ✭ 79 (-4.82%)
Mutual labels:  api
Openapi Viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 82 (-1.2%)
Mutual labels:  api
Apis
This Repository contains link to many Open or Closed Source APIs which I've made
Stars: ✭ 79 (-4.82%)
Mutual labels:  api
Purescript Graphql
End to End typesafe GraphQL with PureScript
Stars: ✭ 79 (-4.82%)
Mutual labels:  api
Zhihu Oauth
尝试解析出知乎官方未开放的 OAuth2 接口,并提供优雅的使用方式,作为 zhihu-py3 项目的替代者,目前还在实验阶段
Stars: ✭ 1,237 (+1390.36%)
Mutual labels:  api
Health Checks Api
Standardize the way services and applications expose their status in a distributed application
Stars: ✭ 78 (-6.02%)
Mutual labels:  api
Laravel Blog
Laravel 8.0 blog application with Vue.js, Homestead, Horizon, Telescope and Pusher
Stars: ✭ 1,248 (+1403.61%)
Mutual labels:  api
Waves Api
Waves API library for Node.js and browser
Stars: ✭ 78 (-6.02%)
Mutual labels:  api
Gotenberg Php Client
PHP client for the Gotenberg API
Stars: ✭ 80 (-3.61%)
Mutual labels:  api
Bucket api
API for the bucket list
Stars: ✭ 83 (+0%)
Mutual labels:  api
Showdoc
ShowDoc is a tool greatly applicable for an IT team to share documents online一个非常适合IT团队的在线API文档、技术文档工具
Stars: ✭ 10,099 (+12067.47%)
Mutual labels:  api
Android
Android projects with reusable components which will be useful in your applications.
Stars: ✭ 81 (-2.41%)
Mutual labels:  api

express-version-route

view on npm view on npm Build Codecov npm module downloads Known Vulnerabilities Security Responsible Disclosure

express-version-route

This npm package provides an ExpressJS middleware to load route controllers based on api versions.

Implementing API Versioning in 15 lines of code:

now any request would be handled with the appropriate route handler in accordance to request.version.

Usage

Create a map where the key is the version of the supported controller, and the value is a regular ExpressJS route function signature.

const versionRouter = require('express-version-route')

const routesMap = new Map()
routesMap.set('1.0', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you version 1.0'})
})

Then, on the route which you wish to version, call the route function of this module with the map you created:

router.get('/test', versionRouter.route(routesMap))

If no route matches the version requested by a client then the next middleware in the chain will be called. To set a route fallback incase no version matches set a 'default' key on the routes map, for example:

routesMap.set('default', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you, this is the default route'})
})

If maximal possible version (for example to get the latest bugfix) is necessary, then please specify useMaxVersion: true in route function, then the maximal possible version will be returned for your request. For example for 1.0 request, the version 1.0.2 will be returned:

const routesMap = new Map()
routesMap.set('1.0.0', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you version 1.0.0'})
})
routesMap.set('1.0.2', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you version 1.0.2'})
})

router.get('/test', versionRouter.route(routesMap,{useMaxVersion: true}))

Usage with TypeScript

import * as versionRouter from 'express-version-route'
import { Router, Handler } from 'express';

const router = Router();
const routesMap = new Map<string, Handler>();

routesMap.set('1.0', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you version 1.0'})
})

routesMap.set('default', (req, res, next) => {
  return res.status(200).json({'message': 'hello to you, this is the default route'})
})

router.get('/test', versionRouter.route(routesMap))

How it works

The Library

A requested version from the client must be available on the request object at req.version. You are encouraged to use this module's twin: express-version-request which is another simple ExpressJS middleware that populates req.version from the client's X-Api-Version header, Accept header or from a query string (such as 'api-version=1.0.0')

The key for the routes versions you define can be a non-semver format, for example: 1.0 or just 1. Under the hood, expression-version-route uses the semver module to check if the version found on the request object at req.version matches the route.

Client-Server flow

  1. An API client will send a request to your API endpoint with an HTTP header that specifies the requested version of the API to use:
curl --header "X-Api-Version: 1.0.0" https://www.example.com/api/users
  1. The express-version-request library will parse the X-Api-Version and sets ExpressJS's req.version property to 1.0.0.
  2. The express-version-route library, when implemented like the usage example above will match the 1.0 route version because semver will match 1.0.0 to 1.0, and then reply with the JSON payload {'message': 'hello to you version 1.0'}.

Installation

yarn add express-version-route

TypeScript Support

yarn add --dev @types/express-version-route

Note: Don't forget to add types for Express as well!

Tests

yarn test

Project linting:

yarn lint

Coverage

yarn test:coverage

Commit

The project uses the commitizen tool for standardizing changelog style commit messages so you should follow it as so:

git add .           # add files to staging
yarn commit      # use the wizard for the commit message

On API Versioning...

An API versioning is a practice that enables services to evolve their APIs with new changes, signatures and the overall API contract without interrupting API consumers and forcing them to repeatedly make changes in order to keep in pace with changes to APIs.

Several methodologies exist to version your API:

  • URL: A request specifies the version for the resource: http://api.domain.com/api/v1/schools/3/students
  • Query String: A request specifies the resource in a query string: http://api.domain.com/api/schools/3/students?api-version=1
  • Custom HTTP Header: A request to a resource http://api.domain.com/api/schools/3/students with a custom HTTP header set in the request X-Api-Version: 1
  • MIME Type content negotiation: A request to a resource http://api.domain.com/api/schools/3/students with an Accept header that specifies the requested content and its version: Accept: application/vnd.ecma.app-v2+json

There is no strict rule on which methodology to follow and each has their own pros and cons. The RESTful approach is the semantic mime-type content negotiation, but a more pragmatic solution is the URL or custom HTTP header.

Why API Versioning at all ?

Upgrading APIs with some breaking change would lead to breaking existing products, services or even your own frontend web application which is dependent on your API contract. By implementing API versioning you can ensure that changes you make to your underlying API endpoints are not affecting systems that consume them, and using a new version of an API is an opt-in on the consumer. read more...

Alternative Node.js libraries

Several npm projects exist which provide similar API versioning capabilities to ExpressJS projects, and I have even contributed Pull Requests to some of them that provide fixes or extra functionality but unfortunately they all seem to be unmaintained, or buggy.

Author

Liran Tal [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].