koa-decorate

koa-decorate

[图片上传失败...(image-1d1d6d-1539576237942)]
[图片上传失败...(image-c7d738-1539576237942)]
[图片上传失败...(image-122dd3-1539576237942)]
[图片上传失败...(image-1cad6-1539576237942)]

Provides decorators for router middleware of koa.

Install

[图片上传失败...(image-7145f3-1539576237942)]

Config

Koa-decorate is based on the decorator provided by es7,
but nodejs does not support the decorator temporarily.
so we need to use typescript to develop our application,
we can run our typescript directly through ts-node without offline compiling.

npm install --save-dev ts-node nodemon

nodemon.json

{
  "restartable": "rs",
  "ignore": [
    ".git",
    "node_modules"
  ],
  "verbose": true,
  "execMap": {
    "ts": "ts-node"
  },
  "watch": [
    "controller",
    "app.ts"
  ],
  "ext": "ts"
}

API Reference

<a name="module_koa-router--Decorator"></a>

Decorator

Kind: Exported class
<a name="module_koa-router--Decorator_new"></a>

new Decorator([opts])

Create a new decorated router.

Param Type Description
[opts] <code>Object</code>
[opts.router] <code>Object</code> koa-router instance
[opts.controllers] <code>Object</code> route controller classes

<a name="module_koa-decorate--Decorator+routes"></a>

Decorator.routes ⇒ <code>function</code>

Returns router middleware which dispatches a route matching the request.

Kind: instance property of <code>Decorator</code>

Example
Basic usage:

// app.ts
import Koa from 'koa';
import Router from 'koa-router';
import Decorator from 'koa-decorate';

import Controller from './controller'; // Route controller classes

const routes = new Decorator({
  router: new Router(),
  controllers: Controller
}).routes();

app.use(routes);

<a name="module_koa-decorate--Http_method"></a>

http-method ⇒ <code>@Get|@Post|@Put|@Delete|@All</code>

Create @Verb methods to match against HTTP methods, where Verb is one of the HTTP verbs
such as @Get or @Post etc.

Additionaly, @All can be used to match against all methods.

Example

// CatController.ts
import { Path, Get, Post } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  @Get
  @Path('/info')
  getCatInfo () {
    return {
      id: 1,
      name: 'Lina Weiss',
      type: 'Norwegian Forest Cat'
    }
  }

  @Post
  @Path('/info/')
  CreateCat () {
    return {
      status: 200,
      data: {
        id: 2
      },
      message: 'Created successfully...'
    }
  }

}

export { CatController };

<a name="module_koa-decorate--Path"></a>

path ⇒ <code>@Path</code>

Match URL patterns to callback functions or controller actions using @Path,
when authFunc returns true, controller can execute logical actions, otherwise access denied.

Param Type Description
path <code>String</code>
[authFunc] <code>Function => Boolean</code> route callback

Example

// CatController.ts
import { Path, Get } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  @Get
  @Path('/info/:id', (ctx) => Number(ctx.params.id) === 1)
  getCatInfo () {
    return {
      id: 1,
      name: 'Lina Weiss',
      type: 'Norwegian Forest Cat'
    }
  }
}

export { CatController };

<a name="module_koa-decorate--Parameter"></a>

parameter ⇒ <code>@Param|@Query|@Body|@Ctx|@Next</code>

Create @Parameter decorators, where Parameter is one of the parameter-names such
as @Param, @Query, @Body etc.

Param Type Description
name <code>String</code>

Example

// CatController.ts
import { Path, Get, Post, Param, Query, Body, Ctx } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  @Get
  @Path('/info/:type')
  getCatInfo (
      @Param('type') type: string,
      @Query('info') info: string) {
    return { type, info }
  }

  @Post
  @Path('/info/:type')
  CreateCat (
      @Param('type') type: string,
      @Body('requestBody') requestBody: any) {
    return {
      status: 200,
      data: Object.assign(requestBody, { type }),
      message: 'Created successfully...'
    }
  }

}

export { CatController };

<a name="module_koa-decorate--Hook"></a>

hook ⇒ <code>@Before|@After</code>

When the routing match is correct, the hookFunc is used to deal with
the transactions @Before and @After processing.

Param Type Description
[hookFunc] <code>Function</code> callback hook

Example

// CatController.ts
import { Path, Get, Param, Query, Before, After } from 'koa-decorate';

@Path('/api/cat')
class CatController {

  @Get
  @Path('/info/:type')
  @Before((ctx, next) => {
    // ...
  })
  @After((ctx, next) => {
    // ...
  })
  getCatInfo (
      @Param('type') type: string,
      @Query('info') info: string) {
    return { type, info }
  }

}

export { CatController };

<a name="module_koa-router--Controller"></a>

Controller

Kind: The dictionary of route controller classes

Example

// Controller/index.ts
import { CatController } from './cat';

export default {
  Cat: CatController
};

Example

koa-app

Licences

MIT

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,449评论 0 10
  • 渐修就是慢慢修炼,耐心踏实地每天积累一点点,相信时间的力量,笃行“一切皆靠积累”。我们都渴望变得更好,但是我们没有...
    汐米阅读 936评论 0 0
  • 夜色渐浓 你是朦胧月色里那抹未明的身影 时远时近 时暖时冷 带给我犹豫不定的忧伤! 前进 是距离 后退 是放弃 难...
    凡尘花语阅读 477评论 0 0
  • 晚上11点回家,发现农夫山泉桶装水自动售卖机、放在了地下停车场的楼梯口位置.可把我乐坏了、我立即买了两大桶。 体验...
    京心达张新波阅读 194评论 1 0
  • 有钱挣不转卖,他是不是傻?-现状偏好 一个人花40万买了一辆二手车,市场上有人出50万,他却不肯卖,为何? 现状偏...
    宗介山城分舵阅读 347评论 0 0