1.0.3 • Published 4 years ago

agora-koa-decorator v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

agora-koa-decorator

agora-koa-decorator

安装

yarn add agora-koa-decorator

使用

基础用法

// index.ts 入口
import Koa from 'koa'
import { bootstrap, router } from 'agora-koa-decorator'

const app = new Koa()

bootstrap(path.resolve(__dirname, 'your controller dir path'))

router.prefix('/api') // if you want to add router baseURL
app.use(router.routes())

// xx.controller.ts
import { Controller, Route, HttpMethod } from 'agora-koa-decorator'

@Controller('/xx')
class XX {
  // visit by /xx/yy post
  @Route('/yy', HttpMethod.POST)
  async postYY(ctx: Context) {
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  // visit by /xx/zz get
  @Route('/zz', HttpMethod.GET)
  async getZZ(ctx: Context) {
    ctx.body = {
      code: 0,
      msg: 'success',
      data: {
        name: 'zz'
      },
    }
  }
}

中间件

// xx.controller.ts
import { Controller, Route, HttpMethod } from 'agora-koa-decorator'
import youMiddleware from 'xxxx'

@Controller('/xx', youMiddleware) // 类的子路由也会先走这个中间件
class XX {
  @Route('/yy', HttpMethod.POST)
  async postYY(ctx: Context) {
    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  @Route('/zz', HttpMethod.GET, youMiddleware) // 针对某一个子路由使用中间件
  async getZZ(ctx: Context) {
    ctx.body = {
      code: 0,
      msg: 'success',
      data: {
        name: 'zz'
      },
    }
  }
}
1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago