File tree Expand file tree Collapse file tree 7 files changed +64
-18
lines changed Expand file tree Collapse file tree 7 files changed +64
-18
lines changed Original file line number Diff line number Diff line change 1
1
import { instancePoll } from '../ioc'
2
2
import {
3
3
META_INJECT ,
4
- META_PROVIDE
5
- } from '../constants'
6
- import {
4
+ META_PROVIDE ,
7
5
META_CONTROLLER
8
6
} from '../constants'
7
+ import { RuntimeOptions } from '../interface'
9
8
import KoaRuntime from '../runtime/koa'
10
9
11
- export const createApplication = ( ) => {
10
+ export const createApplication = ( options : RuntimeOptions = { } ) => {
12
11
const controllers : Set < any > = new Set ( ) ;
13
12
const controllerPoll : Map < any , any > = instancePoll . getAll ( META_INJECT )
14
13
for ( let [ Controller , tag ] of controllerPoll ) {
@@ -21,12 +20,13 @@ export const createApplication = () => {
21
20
}
22
21
23
22
}
24
- const app = KoaRuntime ( controllers )
23
+ const app = KoaRuntime ( controllers , options )
25
24
return app
26
25
}
27
26
28
27
export const Controller = ( path : string = '/' ) => {
29
28
return function < T extends { new ( ...args : any [ ] ) : { } } > ( constructor : T ) {
29
+ console . log ( constructor )
30
30
Reflect . defineMetadata ( META_CONTROLLER , path , constructor )
31
31
return constructor
32
32
}
Original file line number Diff line number Diff line change 1
1
import {
2
2
META_ROUTER ,
3
3
} from '../constants'
4
-
4
+ import { RouteAop } from '../interface'
5
5
// create http request method decorator
6
6
export const Get = createHTTPMethodDecorator ( 'get' )
7
7
export const Post = createHTTPMethodDecorator ( 'post' )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -3,18 +3,13 @@ import {
3
3
META_PROVIDE ,
4
4
META_INJECT ,
5
5
} from '../constants'
6
-
7
- interface InstanceMeta {
8
- key : symbol ;
9
- value : ( new ( ) => { } )
10
- }
6
+ import { InstanceMeta } from '../interface'
11
7
12
8
class InstancePoll {
13
9
private providePoll : Map < any , any > = new Map ( ) ;
14
10
private injectPoll : Map < any , any > = new Map ( ) ;
15
11
add ( type : Symbol , instanceMeta : InstanceMeta ) {
16
12
if ( type === META_PROVIDE ) {
17
- console . log ( instanceMeta )
18
13
this . providePoll . set ( instanceMeta . key , instanceMeta . value )
19
14
}
20
15
if ( type === META_INJECT ) {
@@ -60,7 +55,7 @@ export const Inject = createIocDecorator(META_INJECT)
60
55
61
56
62
57
function createIocDecorator ( type ) {
63
- return ( tag ) => target => {
58
+ return tag => target => {
64
59
instancePoll . add ( type , {
65
60
key :tag ,
66
61
value : target
Original file line number Diff line number Diff line change 1
1
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'
2
8
3
- function KoaRuntime ( controllers : Set < any > ) {
9
+ function KoaRuntime ( controllers : Set < any > , options : RuntimeOptions ) {
4
10
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 ( ) )
6
35
return app
7
36
}
8
37
Original file line number Diff line number Diff line change 1
1
import './loader'
2
2
import { createApplication } from '../src/index'
3
3
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
+ } )
5
14
6
15
7
16
app . listen ( 8080 , ( ) => {
Original file line number Diff line number Diff line change @@ -16,12 +16,12 @@ export class UserController {
16
16
this . _user = user ;
17
17
}
18
18
@Get ( '/' )
19
- async getUser ( ctx : any , next : Promise < any > ) : Promise < any > {
20
- console . log ( this )
19
+ async getUser ( ctx : any , next : Function ) : Promise < any > {
21
20
const result = this . _user . getUser ( 1 ) ;
22
21
ctx . body = {
23
22
code : 200 ,
24
23
data : result
25
24
} ;
25
+ await next ( )
26
26
}
27
27
}
You can’t perform that action at this time.
0 commit comments