-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
register-controllers.ts
58 lines (47 loc) · 1.94 KB
/
register-controllers.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
import { getMetadataArgsStorage, ObjectKeyAny } from '../mod.ts';
import { container } from '../injection/index.ts';
import { MetaRoute } from '../models/meta-route.ts';
export function registerControllers(
controllers: any[] = [],
classes: ObjectKeyAny[] = [],
addToRoute: (route: MetaRoute) => void,
logging: boolean = true,
) {
// TODO: add two route Map (with route params / exact match)
// example: new Map(); key = route, value = object
controllers.forEach((controller) => {
const actions = getMetadataArgsStorage().actions.filter((action) => action.target === controller.target);
const params = getMetadataArgsStorage().params.filter((param) => param.target === controller.target);
// TODO: if obj not in classes
// resolve from DI
const obj: ObjectKeyAny = container.resolve(controller.target);
classes.push(obj);
if (logging) {
console.log(`register Controller: `, controller.target.name || controller.target.constructor.name);
}
let areaRoute = ``;
if (controller.area.baseRoute) {
areaRoute = controller.area.baseRoute;
}
actions.forEach((action) => {
let fullRoute: string = `${areaRoute}${controller.route}`;
const regexpRoute: RegExp = action.route instanceof RegExp ? action.route : undefined;
if (!regexpRoute) {
fullRoute += action.route;
}
const metaRoute: MetaRoute = {
baseRoute: areaRoute,
route: fullRoute,
regexpRoute,
target: obj,
action: action.method,
method: action.type,
params: params.filter((param) => param.method === action.method),
};
if (logging) {
console.log(`register route: `, metaRoute.route);
}
addToRoute(metaRoute);
});
});
}