All Projects → Lansoweb → koa-router-version

Lansoweb / koa-router-version

Licence: MIT License
Semantic Versioning routing for Koa

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to koa-router-version

Agile-Server
A simple, fast, complete Node.js server solution, based on KOA. 简单快速的 、性能强劲的、功能齐全的 node 服务器解决方案合集,基于 KOA。
Stars: ✭ 24 (+26.32%)
Mutual labels:  koa, koa2
koa2-swagger-ui
Swagger UI as Koa v2 middleware
Stars: ✭ 95 (+400%)
Mutual labels:  koa, koa2
shopify-node-react-app
Shopify paid app in Node.js & React.js that connects to a store and lets merchants select products to automatically discount them in the Shopify admin interface.
Stars: ✭ 29 (+52.63%)
Mutual labels:  koa, koa-router
nodejs-koa-blog
基于 Node.js Koa2 实战开发的一套完整的博客项目网站
Stars: ✭ 1,611 (+8378.95%)
Mutual labels:  koa, koa2
bugu-web
BuguLink backend project (Koa2 + MySQL + Redis).
Stars: ✭ 36 (+89.47%)
Mutual labels:  koa2, koa-router
koahub-cli
KoaHub CLI -- KoaHub.js的开发工具,自动babel编译 ES6/7(Generator Function, Class, Async & Await)并且文件修改后自动重启。
Stars: ✭ 16 (-15.79%)
Mutual labels:  koa, koa2
node-typescript-starter
REST API using Node with typescript, KOA framework. TypeORM for SQL. Middlewares JWT (auth), CORS, Winston Logger, Error, Response
Stars: ✭ 19 (+0%)
Mutual labels:  koa, koa2
Strapi Sdk Javascript
🔌 Official JavaScript SDK for APIs built with Strapi.
Stars: ✭ 247 (+1200%)
Mutual labels:  koa, koa2
inversify-koa-utils
inversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify)
Stars: ✭ 27 (+42.11%)
Mutual labels:  koa, koa2
express-to-koa
Use express middlewares in Koa2, the one that really works.
Stars: ✭ 18 (-5.26%)
Mutual labels:  koa, koa2
amala
Lightweight, Typescript REST library powered by Koa 2+. Use classes and decorators to define your API. Inject arguments from ctx. Supports API versioning. Future support for OpenAPI3. Works with koa-router.
Stars: ✭ 41 (+115.79%)
Mutual labels:  koa2, koa-router
polix
🚀 Node.js Web Framework
Stars: ✭ 32 (+68.42%)
Mutual labels:  koa, koa2
koa-xml-body
koa middleware to parse xml request body
Stars: ✭ 36 (+89.47%)
Mutual labels:  koa, koa2
react-isomorphic-bundle
React Redux Universal (isomorphic) bundle
Stars: ✭ 53 (+178.95%)
Mutual labels:  koa, koa-router
koa-mongoDB
😊😊Koa and mongoose build services
Stars: ✭ 24 (+26.32%)
Mutual labels:  koa, koa2
koa2-proxy
基于koa@next的代理工具,支持http和https,并且可以当做本地服务器使用
Stars: ✭ 42 (+121.05%)
Mutual labels:  koa, koa2
Egg Core
A core Pluggable framework based on koa.
Stars: ✭ 194 (+921.05%)
Mutual labels:  koa, koa2
Koa Webpack Middleware
webpack dev&hot middleware for koa2
Stars: ✭ 215 (+1031.58%)
Mutual labels:  koa, koa2
koa-simple-ratelimit
Simple rate limiter for Koa.js v2 web framework
Stars: ✭ 17 (-10.53%)
Mutual labels:  koa, koa2
restria
Entria's REST API boilerplate
Stars: ✭ 25 (+31.58%)
Mutual labels:  koa, koa2

koa-router-version

Build Status NPM Downloads Coverage Status

Semantic Versioning routing for Koa

Allows you to use Semantic Versioning routes.

Requisites:

Installation

npm install --save koa-router-version

# with yarn:
yarn add koa-router-version

Usage

A basic usage:

const Koa = require('koa');
const Router = require('koa-router');
const api = require('koa-router-version');

const list = require('./list');
const detail = require('./detail');
const detail2 = require('./detail2');

const app = new Koa();
const router = new Router();

// Defines a version 1.0.0 for todo list
router.get('todo.list', '/todo', api.version({'1.0.0': list}));

// Defines 2 versions (order is NOT important)
router.get('todo.detail', '/todo/:id', api.version({
    '1.1.0': detail,
    '2.1.3': detail2
}))

// You can specify a route param as version. Will accept both /todo/321 and /v1/todo/321 
router.get('todo.detail', '/:version(v\\d)?/todo/:id', api.version({
    '1.1.0': detail,
    '2.1.3': detail2
}))

app.use(router.routes());

app.listen(3000);

For client usage:

# Latest version
$ curl -i http://localhost:3000/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 2.1.3
<more headers>

{"todo": {}}

# Specifying a version
$ curl -i -H "Accept-Version: ^1.0" http://localhost:3000/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 1.1.0
<more headers>

{"todo": {}}

# Specifying a version via path
$ curl -i http://localhost:3000/v1/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 1.1.0
<more headers>

{"todo": {}}

$ curl -i -H "Accept-Version: ~2" http://localhost:3000/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 2.1.3
<more headers>

{"todo": {}}

# Path has higher priority than header
$ curl -i -H "Accept-Version: ~2" http://localhost:3000/v1/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 1.1.0
<more headers>

{"todo": {}}

# Unknown version
$ curl -H "Accept-Version: ^3.0" http://localhost:3000/todo/1
HTTP/1.1 400 Bad Request
<more headers>

Version ^3 is not supported

Version state variable

You can access the used version through the ctx.state:

router.get('todo.list', '/todo', api.version({'1.0.0': ctx => {
  ctx.body = ctx.state.apiVersion;
}));

Options

requestHeader

The default header is Accept-Version, but you can change:

router.get('todo.list', '/todo', api.version(
  {'1.0.0': list},
  { requestHeader: 'X-Request-Version' }
));

responseHeader

The default header is X-Api-Version, but you can change:

router.get('todo.list', '/todo', api.version(
  {'1.0.0': list},
  { responseHeader: 'X-Version' }
));

routeParam

This module expects the path as v followed by a single number:

  • v1 is equivalent to ^1
  • v2 is equivalent to ^2
  • v11 is equivalent to ^11

The default param is version, but you can change it:

router.get('todo.list', '/:myversion(v\\d)/todo', api.version(
  {'1.0.0': list},
  { routeParam: 'myversion' }
));

In order to make version optional and use the latest, use the ? regex at the end:

router.get('todo.list', '/:myversion(v\\d)?/todo', api.version(
  {'1.0.0': list},
  { routeParam: 'myversion' }
));

fallbackLatest

When the requested version is not found, the default response is an error, but you can choose to use the latest version:

router.get('todo.list', '/todo', api.version(
  {'1.0.0': list},
  { fallbackLatest: true }
));
$ curl -i -H "Accept-Version: ^3.1.9" http://localhost:3000/todo/1
HTTP/1.1 200 Accepted
X-Api-Version: 2.1.3
<more headers>

{"todo": {}}
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].