0.4.1 • Published 3 years ago

@koa-lite/controller v0.4.1

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

@koa-lite/controller

npm package

install

$ yarn add @koa-lite/controller

use

// controller.ts
import { Prefix, Tag, Get, Summary, Query } from '@koa-lite/controller'
@Prefix('/ucenter')
export default class User {
  @Tag('User')
  @Get('/user') // or request.get('/user'), request('get', '/user')
  @Summary('User list')
  @Query(Joi.object({
    name: Joi.string().required().description('name'),
  })) // check.query()
  async sms(ctx: BaseContext) {
    ctx.body = {}
  }
}

// router.ts
import { Controller, InjectHandle } from '@koa-lite/controller'
import User from './controller.ts'

// 包装函数
InjectHandle(({ fn }) => fn)

const router = new Controller({
  prefix: '/v1',
  docs: {
    title: '接口文档 - 客户端',
    version: 'v1',
    description: '业务接口文档 - by Hotchcms',
    securities: [{
      type: 'headers',
      key: 'Authorization', // 自动注入token等信息
      value: function (data) {
        var ret = (data instanceof Object) && data.ret
        return /^Bearer\s/.test(ret) ? ret : ''
      },
    }, {
      type: 'headers',
      key: 'x-device-id',
      value: '10001',
    }],
  }})
  .controllers([ User ])
  .get('/', ctx => { ctx.body = 'API V1' })

// app.ts
app.use(router.routes())

api

  • Prefix()
  • Get()
  • Post()
  • Put()
  • Delete()
  • Body
  • Query
  • Params
  • Headers
  • Middle([])
  • Summary()
  • Tag()
0.4.1

3 years ago

0.4.0

3 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.3

4 years ago

0.1.0

4 years ago