-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-router.ts
150 lines (122 loc) · 4.81 KB
/
module-router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { IRouter, Router } from 'express';
import { Annotator, Type } from '../utils/annotations';
import { NemModule, NemModuleWithProviders } from '../metadata/module';
import { Injectable, Injector, Provider } from '@neoskop/injector';
import { ControllerRouterFactory } from './controller-router';
/** @hidden */
const debug = require('debug')('nem:factory:module');
export interface IModuleContext<T = any> {
/**
* The module type class
*/
moduleType : Type<T>;
/**
* The module metadata
*/
metadata : NemModule;
/**
* The controller factory
*/
factory: ControllerRouterFactory;
/**
* The use router
*/
router : IRouter<any>;
/**
* The module instance
*/
module? : T;
}
@Injectable()
export class ModuleRouterFactory {
constructor(protected injector : Injector) {
}
static getProviders(moduleType : Type<any>) : Provider[] {
const metadata = this.assertModuleAnnotation(moduleType);
return [
moduleType,
...(metadata.providers || []),
...(metadata.modules || []).map(declaration => {
const nemModule = this.assertNemModuleWithProviders(Array.isArray(declaration) ? declaration[1] : declaration);
return [
this.getProviders(nemModule.nemModule),
...(nemModule.providers || [])
]
}).reduce((t, c) => t.concat(c), [])
]
}
createRouterFromModule(moduleType : Type<any>, options : { router?: IRouter<any> } = {}) : IModuleContext {
debug('create router from module', moduleType.name);
const ctx = this.initContext(moduleType, options);
if(ctx.metadata.modules) {
for(const mod of ctx.metadata.modules) {
this.handleImport(mod, ctx.router);
}
}
if(ctx.metadata.middlewares) {
for(const middleware of ctx.metadata.middlewares) {
this.handleMiddleware(middleware, ctx.router);
}
}
if(ctx.metadata.router) {
for(const [ path, subRouter ] of ctx.metadata.router) {
this.handleRouter(path, subRouter, ctx.router);
}
}
if(ctx.metadata.controller) {
for(const [ path, controller ] of ctx.metadata.controller) {
this.handleRouter(path, ctx.factory.createRouterFromController(controller), ctx.router);
}
}
ctx.module = this.injector.get(moduleType);
return ctx;
}
protected initContext(moduleType : Type<any>, { router = Router() } : { router?: IRouter<any> }) : IModuleContext {
const ctx : IModuleContext = {
router,
moduleType,
metadata: ModuleRouterFactory.assertModuleAnnotation(moduleType),
factory: this.injector.get(ControllerRouterFactory)
};
return ctx;
}
protected static assertModuleAnnotation(moduleType : Type<any>) : NemModule {
const annotations = Annotator.getCtorAnnotations(moduleType);
for(const annotation of annotations) {
if(annotation instanceof NemModule) {
return annotation;
}
}
throw new Error(`Class "${moduleType.name}" has no module annotation`);
}
protected static assertNemModuleWithProviders(arg : NemModuleWithProviders|Type<any>) : NemModuleWithProviders {
if(typeof arg === 'object' && arg.hasOwnProperty('nemModule')) {
return arg as NemModuleWithProviders;
}
return {
nemModule: arg as Type<any>
}
}
protected handleImport(module : [ string|RegExp, Type<any> | NemModuleWithProviders ] |Type<any> | NemModuleWithProviders, router : IRouter<any>) {
if(Array.isArray(module)) {
const [ path, mod ] = module;
const nemModule = ModuleRouterFactory.assertNemModuleWithProviders(mod);
debug('handle import', path, nemModule.nemModule.name);
router.use(path, this.createRouterFromModule(nemModule.nemModule).router);
} else {
const { nemModule } = ModuleRouterFactory.assertNemModuleWithProviders(module);
debug('handle import', nemModule.name);
this.createRouterFromModule(nemModule, { router });
}
}
protected handleMiddleware(middleware : any|any[], router : IRouter<any>) {
if(Array.isArray(middleware)) {
router.use(...middleware);
} else {
router.use(middleware);
}
}
protected handleRouter(route : string|RegExp, subRouter : Router, router : IRouter<any>) {
router.use(route, subRouter);
}
}