Skip to content

Commit e3ae582

Browse files
decorator
1 parent 7f20e59 commit e3ae582

14 files changed

+313
-0
lines changed

TS/decorator/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "decorator",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"devDependencies": {
12+
"@types/koa": "^2.0.50",
13+
"@types/koa-bodyparser": "^4.3.0",
14+
"@types/koa-router": "^7.0.42",
15+
"typescript": "^3.6.3"
16+
},
17+
"dependencies": {
18+
"koa": "^2.8.2",
19+
"koa-bodyparser": "^4.2.1",
20+
"koa-router": "^7.4.0"
21+
}
22+
}

TS/decorator/src/controllers/index.js

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/controllers/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/controllers/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Router, Get, Post, Query } from '../decorators';
2+
3+
@Router()
4+
class Index {
5+
@Get('/')
6+
index(@Query('b') b: string, @Query('a') a: string) {
7+
return 'hello world ' + a + ' ' + b;
8+
}
9+
}
10+
11+
export default Index;

TS/decorator/src/controllers/users.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/controllers/users.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/controllers/users.ts

Whitespace-only changes.

TS/decorator/src/decorators.js

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/decorators.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/decorators.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 全局变量, 用于存储对应的数据
2+
export const RouterList: any[] = [];
3+
export const ControllerList: any = [];
4+
export const ParseList: any[] = [];
5+
export const ParamList: any[] = [];
6+
7+
// router 装饰器, 仅用于注册至全局变量, 项目启动时会根据全局变量统一创建 router
8+
export function Router(basename = '') {
9+
return (constructor: any) => {
10+
RouterList.push({
11+
basename,
12+
constructor: constructor
13+
});
14+
}
15+
}
16+
17+
// method 装饰器, 主要用来装饰 get/post 等方法
18+
export function Method(type: any) {
19+
return (path: string = '') => (target: any, name: string, descriptor: any) => {
20+
ControllerList.push({
21+
target,
22+
type,
23+
path,
24+
method: name,
25+
controller: descriptor.value
26+
});
27+
}
28+
}
29+
30+
// 参数装饰器, 用来获取参数
31+
export function Param(position: string) {
32+
return (key: any) => (target: any, name: string, index: any) => {
33+
ParamList.push({
34+
target,
35+
key,
36+
position,
37+
index,
38+
method: name
39+
})
40+
}
41+
}
42+
43+
export function Parse(type: string) {
44+
return (target: any, name: any, index: any) => {
45+
ParseList.push({
46+
target,
47+
type,
48+
method: name,
49+
index
50+
});
51+
}
52+
}
53+
54+
export const Query = Param('query');
55+
export const Body = Param('body');
56+
export const Header = Param('header');
57+
export const cookie = Param('cookie');
58+
export const Get = Method('get');
59+
export const Post = Method('post');

TS/decorator/src/index.js

+58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TS/decorator/src/index.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Koa from 'koa';
2+
import Router from 'koa-router';
3+
import bodyParse from 'koa-bodyparser';
4+
import compose from 'koa-compose';
5+
import './controllers/index';
6+
import './controllers/users';
7+
import { RouterList, ControllerList, ParamList, ParseList } from './decorators';
8+
9+
10+
function bootstrap() {
11+
// 根据全局变量, 动态创建对应路由
12+
const routes: any[] = [];
13+
14+
RouterList.forEach(({ basename, constructor }) => {
15+
const router = new Router({
16+
prefix: basename
17+
});
18+
routes.push(router.routes());
19+
20+
// 挂载对应路由函数
21+
ControllerList.filter(i => i.target === constructor.prototype)
22+
.forEach((controller) => {
23+
router[controller.type](controller.path, async (ctx: any, next) => {
24+
// 整合对应参数
25+
const args: any[] = [];
26+
ParamList
27+
.filter(({ target, method }) => target === constructor.prototype && method === controller.method)
28+
.forEach(({ index, key, position }) => {
29+
switch (position) {
30+
case 'body':
31+
args[index] = ctx.request.body[key];
32+
break;
33+
case 'header':
34+
args[index] = ctx.headers[key];
35+
break;
36+
case 'cookie':
37+
args[index] = ctx.cookies.get('key');
38+
break;
39+
case 'query':
40+
args[index] = ctx.request.query[key];
41+
break;
42+
default:
43+
break;
44+
}
45+
});
46+
ctx.body = controller.controller(...args);
47+
});
48+
})
49+
50+
});
51+
52+
// 开启服务
53+
const app = new Koa();
54+
app.use(bodyParse());
55+
app.use(compose(routes));
56+
app.listen(3000, () => console.log('server listen on 127.0.0.1:3000'));
57+
}
58+
59+
bootstrap();
60+
61+

TS/decorator/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"experimentalDecorators": true,
4+
"esModuleInterop": true,
5+
"module": "commonjs",
6+
"target": "es2019",
7+
"sourceMap": true,
8+
"outDir": "dist",
9+
"baseUrl": "."
10+
},
11+
"exclude": [
12+
"node_modules"
13+
]
14+
}

0 commit comments

Comments
 (0)