/
RouteService.ts
139 lines (120 loc) · 3.44 KB
/
RouteService.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
import {Constant, InjectorService, Service} from "@tsed/di";
import {$log} from "ts-log-debug";
import {colorize} from "ts-log-debug/lib/layouts/utils/colorizeUtils";
import {AfterRoutesInit} from "../../server/interfaces/AfterRoutesInit";
import {ControllerProvider} from "../class/ControllerProvider";
import {EndpointMetadata} from "../class/EndpointMetadata";
import {IControllerRoute} from "../interfaces";
/**
* `RouteService` is used to provide all routes collected by annotation `@Controller`.
*/
@Service()
export class RouteService implements AfterRoutesInit {
/**
*
*/
@Constant("logger.disableRoutesSummary", false)
disableRoutesSummary: boolean;
private readonly _routes: {route: string; provider: any}[] = [];
constructor(private injector: InjectorService) {}
/**
*
* @returns {{route: string; provider: any}[]}
*/
get routes(): {route: string; provider: any}[] {
return this._routes || [];
}
/**
*
*/
$afterRoutesInit() {
if (!this.disableRoutesSummary) {
this.injector.logger.info("Routes mounted :");
this.printRoutes();
}
}
/**
*
* @returns {number}
* @param route
*/
addRoute(route: {route: string; provider: any}) {
return this._routes.push(route);
}
/**
* Get all routes built by TsExpressDecorators and mounted on Express application.
* @returns {IControllerRoute[]}
*/
getRoutes(): IControllerRoute[] {
const routes: IControllerRoute[] = [];
this.routes.forEach((config: {route: string; provider: ControllerProvider}) => {
this.buildRoutes(routes, config.provider, config.route);
});
return routes;
}
/**
* Print all route mounted in express via Annotation.
*/
public printRoutes(): void {
const mapColor: {[key: string]: string} = {
GET: "green",
POST: "yellow",
PUT: "blue",
DELETE: "red",
PATCH: "magenta",
ALL: "cyan"
};
const routes = this.getRoutes().map(route => {
const method = route.method.toUpperCase();
route.method = {
length: method.length,
toString: () => {
return colorize(method, mapColor[method]);
}
} as any;
return route;
});
const str = $log.drawTable(routes, {
padding: 1,
header: {
method: "Method",
url: "Endpoint",
name: "Class method"
}
});
this.injector.logger.info("\n" + str.trim());
}
/**
* Return all Routes stored in ControllerProvider manager.
* @returns {IControllerRoute[]}
*/
getAll(): IControllerRoute[] {
return this.getRoutes();
}
/**
*
* @param routes
* @param ctrl
* @param endpointUrl
*/
private buildRoutes(routes: any[], ctrl: ControllerProvider, endpointUrl: string) {
ctrl.children
.map(ctrl => this.injector.getProvider(ctrl))
.forEach((provider: ControllerProvider) => this.buildRoutes(routes, provider, `${endpointUrl}${provider.path}`));
ctrl.endpoints.forEach((endpoint: EndpointMetadata) => {
const {pathsMethods, params, targetName, methodClassName} = endpoint;
pathsMethods.forEach(({path, method}) => {
if (!!method) {
routes.push({
method,
name: `${targetName}.${String(methodClassName)}()`,
url: `${endpointUrl}${path || ""}`.replace(/\/\//gi, "/"),
className: targetName,
methodClassName,
parameters: params
});
}
});
});
}
}