Skip to content

Commit 6229e81

Browse files
committed
feat: add koa router
1 parent 72211b8 commit 6229e81

File tree

7 files changed

+64
-18
lines changed

7 files changed

+64
-18
lines changed

src/core/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import {instancePoll} from '../ioc'
22
import {
33
META_INJECT,
4-
META_PROVIDE
5-
} from '../constants'
6-
import {
4+
META_PROVIDE,
75
META_CONTROLLER
86
} from '../constants'
7+
import { RuntimeOptions } from '../interface'
98
import KoaRuntime from '../runtime/koa'
109

11-
export const createApplication = () => {
10+
export const createApplication = (options: RuntimeOptions = {}) => {
1211
const controllers: Set<any> = new Set();
1312
const controllerPoll: Map<any, any> = instancePoll.getAll(META_INJECT)
1413
for(let [Controller, tag] of controllerPoll) {
@@ -21,12 +20,13 @@ export const createApplication = () => {
2120
}
2221

2322
}
24-
const app = KoaRuntime(controllers)
23+
const app = KoaRuntime(controllers, options)
2524
return app
2625
}
2726

2827
export const Controller = (path: string = '/') => {
2928
return function<T extends { new (...args: any[]): {} }>(constructor: T) {
29+
console.log(constructor)
3030
Reflect.defineMetadata(META_CONTROLLER, path, constructor)
3131
return constructor
3232
}

src/decorator/httpRouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
META_ROUTER,
33
} from '../constants'
4-
4+
import { RouteAop } from '../interface'
55
// create http request method decorator
66
export const Get = createHTTPMethodDecorator('get')
77
export const Post = createHTTPMethodDecorator('post')

src/interface/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface RuntimeOptions {
2+
plugins?: Array<Function>
3+
}
4+
5+
export interface InstanceMeta {
6+
key: symbol
7+
value: (new () => {})
8+
}
9+
10+
export interface RouteAop {
11+
before?: Array<Function>;
12+
after?: Array<Function>
13+
}

src/ioc/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ import {
33
META_PROVIDE,
44
META_INJECT,
55
} from '../constants'
6-
7-
interface InstanceMeta {
8-
key: symbol;
9-
value: (new () => {})
10-
}
6+
import {InstanceMeta} from '../interface'
117

128
class InstancePoll {
139
private providePoll: Map<any, any> = new Map();
1410
private injectPoll: Map<any, any> = new Map();
1511
add(type: Symbol, instanceMeta: InstanceMeta) {
1612
if(type === META_PROVIDE) {
17-
console.log(instanceMeta)
1813
this.providePoll.set(instanceMeta.key, instanceMeta.value)
1914
}
2015
if(type === META_INJECT) {
@@ -60,7 +55,7 @@ export const Inject = createIocDecorator(META_INJECT)
6055

6156

6257
function createIocDecorator(type) {
63-
return (tag) => target => {
58+
return tag => target => {
6459
instancePoll.add(type, {
6560
key:tag,
6661
value: target

src/runtime/koa/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
import * as Koa from 'koa'
2+
import * as Router from 'koa-router'
3+
import { RuntimeOptions } from '../../interface'
4+
import {
5+
META_CONTROLLER,
6+
META_ROUTER
7+
} from '../../constants'
28

3-
function KoaRuntime(controllers: Set<any>) {
9+
function KoaRuntime(controllers: Set<any>, options:RuntimeOptions) {
410
const app = new Koa()
5-
console.log(controllers)
11+
const router = new Router()
12+
const plugins: Array<Function> = options.plugins || []
13+
if(plugins.length) {
14+
for(let plugin of plugins){
15+
app.use(plugin)
16+
}
17+
}
18+
for(let controller of controllers) {
19+
console.log(controller)
20+
const rootPath = Reflect.getMetadata(META_CONTROLLER,controller.constructor)
21+
Object.getOwnPropertyNames(Object.getPrototypeOf(controller))
22+
.filter(name => name !== 'constructor')
23+
.forEach(name => {
24+
const metaRoute = Reflect.getMetadata(META_ROUTER,controller[name])
25+
if(metaRoute){
26+
router[metaRoute.method](`${rootPath}${metaRoute.path}`, async (ctx, next) => {
27+
await controller[name](ctx, next)
28+
},async(ctx,next) => {
29+
console.log(123)
30+
})
31+
}
32+
})
33+
}
34+
app.use(router.routes()).use(router.allowedMethods())
635
return app
736
}
837

test/app.ts

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

4-
const app = createApplication()
4+
async function test(ctx, next: Function): Promise<any> {
5+
console.log('golbal')
6+
await next()
7+
return;
8+
}
9+
10+
11+
const app = createApplication({
12+
plugins: [test]
13+
})
514

615

716
app.listen(8080, () => {

test/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export class UserController {
1616
this._user = user;
1717
}
1818
@Get('/')
19-
async getUser(ctx: any, next: Promise<any>): Promise<any> {
20-
console.log(this)
19+
async getUser(ctx: any, next: Function): Promise<any> {
2120
const result = this._user.getUser(1);
2221
ctx.body = {
2322
code: 200,
2423
data: result
2524
};
25+
await next()
2626
}
2727
}

0 commit comments

Comments
 (0)