1.0.1 • Published 2 years ago

koa-annotation-router v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

koa-annotation-router

拓展 @koa/router,使其支持注解风格路由写法

import { Router, controller, mapping } from 'koa-annotation-router'

controllermapping 用于装饰类和方法定义访问路径

Router 继承自 @koa/router 增加了 addController 方法用于注册被装饰的类,增加了 printRoutes 方法用于打印所有注册的路由

┌─────────┬────────┬────────────────────┬──────────────────────────────┐
│ (index) │ method │        path        │            caller            │
├─────────┼────────┼────────────────────┼──────────────────────────────┤
│    0    │ 'GET'  │ '/api/user/hello'  │ 'new UserController().hello' │
│    1    │ 'POST' │ '/api/user/update' │   'UserController.update'    │
│    2    │ 'GET'  │      '/hello'      │              ''              │
└─────────┴────────┴────────────────────┴──────────────────────────────┘

安装

注意:不需要再安装 @koa/router 或者 koa-router

yarn add koa-annotation-router

使用

参考 test/index.tstest/demo.ts

import Koa, { Context } from 'koa'
import { Router, controller, mapping } from 'koa-annotation-router'

@controller('/api/user')
class UserController {
  name: string
  constructor(name: string) {
    this.name = name
  }

  @mapping('/hello')
  public async hello(ctx: Context) {
    ctx.response.body = `hello ${this.name}`
  }

  @mapping('/update', ['post'])
  public static async update(ctx: Context) {
    ctx.response.body = {
      code: 200,
      data: null,
      msg: 'ok'
    }
  }
}

const router = new Router()
router.addController(new UserController('tom'))
router.get('/hello', async function (ctx: Context) {
  ctx.response.body = `hello`
})

const app = new Koa().use(router.routes())
app.listen(3000)