Skip to content

Commit 7a2c07f

Browse files
committed
feat: add all route
1 parent 1a44f26 commit 7a2c07f

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

src/decorator/httpRouter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ export const Get = createHTTPMethodDecorator('get')
77
export const Post = createHTTPMethodDecorator('post')
88
export const Put = createHTTPMethodDecorator('put')
99
export const Delete = createHTTPMethodDecorator('delete')
10+
export const All = createHTTPMethodDecorator('get', 'post', 'put', 'delete')
1011

1112

12-
function createHTTPMethodDecorator(method: string) {
13+
function createHTTPMethodDecorator(...methods: Array<string>) {
1314
return (path: string, aopPlugins: RouteAop = {}) => (
1415
target: any,
1516
propertyKey: string,
@@ -30,7 +31,7 @@ function createHTTPMethodDecorator(method: string) {
3031
}
3132
}
3233
Reflect.defineMetadata(META_ROUTER,{
33-
method,
34+
methods,
3435
path,
3536
beforePlugins,
3637
afterPlugins

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'reflect-metadata'
22

33
import {Inject, Provide} from './ioc'
4-
import {Get,Post,Put,Delete} from './decorator/httpRouter'
4+
import {Get,Post,Put,Delete, All} from './decorator/httpRouter'
55
import {createApplication, Controller} from './core'
66

77
export {
@@ -12,5 +12,6 @@ export {
1212
Post,
1313
Put,
1414
Delete,
15+
All,
1516
Controller
1617
}

src/runtime/koa/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
META_CONTROLLER,
66
META_ROUTER
77
} from '../../constants'
8+
import {resolvePath} from '../../utils'
89

910
function KoaRuntime(controllers: Set<any>, options:RuntimeOptions) {
1011
const app = new Koa()
@@ -16,17 +17,23 @@ function KoaRuntime(controllers: Set<any>, options:RuntimeOptions) {
1617
}
1718
}
1819
for(let controller of controllers) {
19-
console.log(controller)
2020
const rootPath = Reflect.getMetadata(META_CONTROLLER,controller.constructor)
21+
if(!rootPath) {
22+
throw new Error('this class is not controller')
23+
}
2124
Object.getOwnPropertyNames(Object.getPrototypeOf(controller))
2225
.filter(name => name !== 'constructor')
2326
.forEach(name => {
2427
const metaRoute = Reflect.getMetadata(META_ROUTER,controller[name])
2528
if(metaRoute){
26-
const {method, path, beforePlugins, afterPlugins} = metaRoute
27-
router[method](`${rootPath}${path}`,...beforePlugins, async (ctx, next) => {
28-
await controller[name](ctx, next)
29-
}, ...afterPlugins)
29+
const {methods, path, beforePlugins, afterPlugins} = metaRoute
30+
const routePath = resolvePath(rootPath, path)
31+
for(let method of methods) {
32+
router[method](routePath,...beforePlugins, async (ctx, next) => {
33+
// TODO
34+
await controller[name](ctx, next)
35+
}, ...afterPlugins)
36+
}
3037
}
3138
})
3239
}

src/utils/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const resolvePath = (rootPath: string, path: string) => {
2+
if(rootPath === '/') {
3+
return path;
4+
}
5+
return `${rootPath}${path}`
6+
}

test/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import './loader'
22
import {createApplication} from '../src/index'
33

44
async function test(ctx, next: Function): Promise<any> {
5-
console.log('golbal')
5+
console.log(ctx)
66
await next()
77
return;
88
}

test/controller.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Post,
55
Put,
66
Delete,
7+
All,
78
Controller
89
} from '../src/index'
910
import {UserInstance} from './constants'
@@ -15,12 +16,13 @@ export class UserController {
1516
constructor(user) {
1617
this._user = user;
1718
}
18-
@Get('/', {
19+
@Get('/getUser', {
1920
before: [before1, before2, before3],
2021
after: [after1, after2, after3]
2122
})
2223
async getUser(ctx: any, next: Function): Promise<any> {
23-
console.log('middle')
24+
// console.log(ctx)
25+
// console.log('middle')
2426
const result = this._user.getUser(1);
2527
ctx.body = {
2628
code: 200,
@@ -30,6 +32,25 @@ export class UserController {
3032
}
3133
}
3234

35+
36+
@Inject(UserInstance)
37+
@Controller()
38+
export class TestController {
39+
private _user
40+
constructor(user) {
41+
this._user = user
42+
}
43+
@Get('/getTestUser')
44+
async getUser(ctx: any, next: Function): Promise<any> {
45+
const result = this._user.getUser(2);
46+
ctx.body = {
47+
code: 200,
48+
data: result
49+
};
50+
}
51+
52+
}
53+
3354
async function before1(ctx, next) {
3455
console.log(1)
3556
await next()

0 commit comments

Comments
 (0)