5
5
META_CONTROLLER ,
6
6
META_ROUTER
7
7
} from '../../constants'
8
+ import { resolvePath } from '../../utils'
8
9
9
10
function KoaRuntime ( controllers : Set < any > , options :RuntimeOptions ) {
10
11
const app = new Koa ( )
@@ -16,17 +17,23 @@ function KoaRuntime(controllers: Set<any>, options:RuntimeOptions) {
16
17
}
17
18
}
18
19
for ( let controller of controllers ) {
19
- console . log ( controller )
20
20
const rootPath = Reflect . getMetadata ( META_CONTROLLER , controller . constructor )
21
+ if ( ! rootPath ) {
22
+ throw new Error ( 'this class is not controller' )
23
+ }
21
24
Object . getOwnPropertyNames ( Object . getPrototypeOf ( controller ) )
22
25
. filter ( name => name !== 'constructor' )
23
26
. forEach ( name => {
24
27
const metaRoute = Reflect . getMetadata ( META_ROUTER , controller [ name ] )
25
28
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
+ }
30
37
}
31
38
} )
32
39
}
0 commit comments