-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
200 lines (174 loc) · 5.46 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const extend = require("node.extend");
const yaml = require("js-yaml");
const fs = require("fs");
const expressReverse = require("express-reverse");
const path = require("path");
import { Application } from "express";
export namespace KmpTools {
interface OptionsKmpRoutes {
routingFile: string;
controllerPath: string[];
helperName: string;
}
export class Controller {
getFolderPaths(dir: string, srcDir:string = "src", controlerDir: string ="Controller", folderPaths: string[] = []) {
const items = fs.readdirSync(dir);
items.forEach((item: string) => {
const itemPath = path.join(dir, item);
if (fs.statSync(itemPath).isDirectory()) {
const containsControllers = fs.existsSync(
path.join(itemPath, controlerDir)
);
if (containsControllers) {
folderPaths.push(path.relative(srcDir, itemPath) + "/");
}
if (!containsControllers) {
this.getFolderPaths(itemPath, srcDir, controlerDir, folderPaths);
}
}
});
return folderPaths;
}
}
/**
* KmpTools manage routes
*/
export class Routes {
private controllers: { [key: string]: any } = {};
private pathObjectTab: string[] | null = null;
/**
* Constructor that initialize all routes when this class is instanced
* @param app Express.Application
* @param options OptionsKmpRoutes
*/
constructor(app: Application, options: OptionsKmpRoutes) {
options = extend(
true,
{
routingFile: path.join(__dirname, "/../../app/config/routes.yml"),
controllerPath: [path.join(__dirname, "/../../app/controllers")],
helperName: "url",
},
options || {}
);
expressReverse(app, {
helperName: options.helperName,
});
this.initRoutes(app, options);
}
/**
* Initialize all routes of application
* @param app Express.Application
* @param options OptionsKmpRoutes
* @private
*/
private initRoutes = (app: Application, options: OptionsKmpRoutes) => {
options.controllerPath.forEach((dir) => {
fs.readdirSync(dir).forEach((file: string) => {
const ext = path.extname(file) as ".js" | ".ts";
if (file.indexOf(ext) !== -1) {
const name = file.substr(0, file.indexOf("."));
const controllerCurrent = require(path.join(dir, file));
this.pathObjectTab = [];
const pathObjectString = this.depthOf(
controllerCurrent,
this.pathObjectTab
).join(".");
this.controllers[pathObjectString + "." + name] = controllerCurrent;
}
});
});
// Load YAML
this.parseYML(options.routingFile, app, null);
};
/**
* Load Yaml config file and build all routes of application
* @param file string
* @param app Express.Application
* @param prefix string|null
* @private
*/
private parseYML = (
file: string,
app: Application,
prefix: string | null
) => {
prefix = prefix || "";
let routesYaml = yaml.safeLoad(fs.readFileSync(file, "utf8"));
for (const key in routesYaml) {
const obj = routesYaml[key];
if (obj.resource) {
this.parseYML(
path.dirname(file) + "/" + obj.resource,
app,
obj.prefix
);
continue;
}
const split = obj.controller.split(":");
const bundle = split.slice(0, -1).join(".") + "Controller";
const action = split.slice(-1)[0] + "Action";
if (!obj.methods) {
throw new Error(
"No methods defined for controller " + obj.controller
);
}
obj.methods.forEach((method: string) => {
const controllerClass: any = bundle
.split(".")
.reduce((o: any, i: string) => {
if (o !== undefined) return o[i];
}, this.controllers[bundle]);
const container = app.get('container');
let actionFunction;
if(container){
let regex = /\.(\w+)Controller$/;
let bundleService = bundle.replace(regex, '.Controller.$1Controller');
actionFunction = container.get(bundleService)[action];
} else {
actionFunction = new controllerClass()[action];
}
if (!actionFunction) {
throw new Error("No action found for " + obj.controller);
}
const httpMethod = method.toLowerCase() as
| "get"
| "post"
| "delete"
| "put"
| "patch"
| "options"
| "connect"
app[httpMethod](
key + "_" + method,
//@ts-ignore
prefix + obj.pattern,
actionFunction
);
});
}
};
/**
* Build the complete class name in string
* @param object { [key: string]: any; } | any
* @param pathObject string[]
* @return string[]
* @private
*/
private depthOf(
object: { [key: string]: any } | any,
pathObject: string[]
): string[] {
for (let key in object) {
if (!object.hasOwnProperty(key)) {
continue;
}
if (typeof object[key] === "object") {
pathObject.push(key);
this.depthOf(object[key], pathObject);
}
}
return pathObject;
}
}
}