Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luckcoding/koa-controller
整合koa-router、joi,采用注解方式开发业务逻辑,实现参数验证、中间件绑定、接口文档生成等功能。
https://github.com/luckcoding/koa-controller
Last synced: 3 months ago
JSON representation
整合koa-router、joi,采用注解方式开发业务逻辑,实现参数验证、中间件绑定、接口文档生成等功能。
- Host: GitHub
- URL: https://github.com/luckcoding/koa-controller
- Owner: luckcoding
- License: mit
- Created: 2020-01-07T08:07:19.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-22T14:40:27.000Z (about 2 years ago)
- Last Synced: 2024-10-13T11:43:43.533Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 355 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @koa-lite/controller
[](https://www.npmjs.com/package/@koa-lite/controller)
## install
`$ yarn add @koa-lite/controller`
## demo
> real demo: [hotchcms](https://github.com/luckcoding/hotchcms)
* `$ npm install`
* `$ npm start`## use
```js
// 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()`