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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末即供,一起剝皮案震驚了整個濱河市陶贼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌遗遵,老刑警劉巖车要,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件翼岁,死亡現(xiàn)場離奇詭異琅坡,居然都是意外死亡残家,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門陪捷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诺擅,“玉大人,你說我怎么就攤上這事凌盯∨胗瘢” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵县忌,是天一觀的道長症杏。 經(jīng)常有香客問我厉颤,道長凡简,這世上最難降的妖魔是什么秤涩? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任黎烈,我火速辦了婚禮匀谣,結果婚禮上,老公的妹妹穿的比我還像新娘必怜。我一直安慰自己后频,他們只是感情好卑惜,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布露久。 她就那樣靜靜地躺著,像睡著了一般毫痕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上消请,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音缭嫡,去河邊找鬼同衣。 笑死缸逃,一個胖子當著我的面吹牛针饥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播需频,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼昭殉!你這毒婦竟也來了饲化?” 一聲冷哼從身側響起莽鸭,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎巢块,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡越走,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年棚品,在試婚紗的時候發(fā)現(xiàn)自己被綠了靠欢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡铜跑,死狀恐怖门怪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情锅纺,我是刑警寧澤掷空,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站囤锉,受9級特大地震影響坦弟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜官地,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一酿傍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧区丑,春花似錦拧粪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宴杀,卻和暖如春癣朗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背旺罢。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工旷余, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人扁达。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓正卧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親跪解。 傳聞我的和親對象是個殘疾皇子炉旷,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,325評論 0 10
  • 漸修就是慢慢修煉,耐心踏實地每天積累一點點叉讥,相信時間的力量窘行,篤行“一切皆靠積累”。我們都渴望變得更好图仓,但是我們沒有...
    汐米閱讀 873評論 0 0
  • 夜色漸濃 你是朦朧月色里那抹未明的身影 時遠時近 時暖時冷 帶給我猶豫不定的憂傷罐盔! 前進 是距離 后退 是放棄 難...
    凡塵花語閱讀 462評論 0 0
  • 晚上11點回家,發(fā)現(xiàn)農(nóng)夫山泉桶裝水自動售賣機救崔、放在了地下停車場的樓梯口位置.可把我樂壞了惶看、我立即買了兩大桶捏顺。 體驗...
    京心達張新波閱讀 191評論 1 0
  • 有錢掙不轉賣,他是不是傻纬黎?-現(xiàn)狀偏好 一個人花40萬買了一輛二手車草丧,市場上有人出50萬,他卻不肯賣莹桅,為何? 現(xiàn)狀偏...
    宗介山城分舵閱讀 334評論 0 0