koa-ts-core 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -2
- package/dist/index.cjs.js +37049 -1
- package/dist/index.esm.js +37038 -1
- package/dist/init/address.d.ts +1 -1
- package/dist/middleware/request_params_middleware.d.ts +7 -0
- package/dist/types/meta_data.d.ts +16 -0
- package/dist/types/route.d.ts +1 -0
- package/dist/utils/path.d.ts +3 -3
- package/package.json +7 -2
- package/dist/utils/port.d.ts +0 -13
package/README.md
CHANGED
|
@@ -66,14 +66,24 @@ import { Router, AuthRouter } from "koa-ts-core";
|
|
|
66
66
|
import { Context } from "koa";
|
|
67
67
|
|
|
68
68
|
class User {
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
|
|
70
|
+
@Router("get", "/index/:userId")
|
|
71
71
|
async userInfo(ctx: Context) {
|
|
72
72
|
ctx.body = {
|
|
73
73
|
success: true,
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
// 可选路由参数
|
|
78
|
+
@Router("get", "/user_list{/:user_name}")
|
|
79
|
+
async userList(ctx: Context) {
|
|
80
|
+
const userName = ctx.params.user_name;
|
|
81
|
+
ctx.body = {
|
|
82
|
+
success: true,
|
|
83
|
+
userName
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
77
87
|
@AuthRouter("post")
|
|
78
88
|
// 注册权限路由
|
|
79
89
|
async setUserInfo(ctx: Context) {
|
|
@@ -86,6 +96,12 @@ class User {
|
|
|
86
96
|
export default User;
|
|
87
97
|
```
|
|
88
98
|
|
|
99
|
+
#### 装饰器实现
|
|
100
|
+
|
|
101
|
+
`@Router`和`@AuthRouter`装饰器本质是基于[koa-router](https://github.com/koajs/router)进行封装
|
|
102
|
+
|
|
103
|
+
ps:路由参数参考[path-to-regexp](https://github.com/pillarjs/path-to-regexp)
|
|
104
|
+
|
|
89
105
|
### 权限校验
|
|
90
106
|
|
|
91
107
|
每一个控制器的路由注册方法有一个唯一对应的参数校验器,例如:目录`src/controller/api/v1/user.ts`中`userInfo`方法对应的校验器为`src/validate/api/v1/user.ts`中的静态方法`userInfo`
|