-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexlist.js
111 lines (90 loc) · 2.54 KB
/
exlist.js
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
const Table = require('cli-table3');
const methods = require('./methods');
const Route = require('./Route');
const routesList = [];
let application = null;
function url(handle, obj) {
const rte = route(handle);
const link = paramFix(rte.url, obj);
const domain = application.get('domain') || '..';
return `${domain}${link}`;
}
function paramFix(link, obj) {
if (!obj) {
return link;
}
let result = null;
while (result = link.match(/(:)\w+/g)) {
const found = result[0];
link = link.replace(found, obj[found.substr(1)]);
}
return link;
}
function route(handle) {
const length = routesList.length;
for (let i = 0; i < length; i++) {
const rte = routesList[i];
if (rte.handle === handle) {
return rte;
}
}
return routesList[0];
}
function argsFixer(args, method) {
if (args.length > 0 &&
(args[0].constructor === String && args[0].indexOf('/') === 0) ||
(args[0].url && args[0].url.indexOf('/') === 0)) {
let url = '';
let rteObj = null;
const arg0 = args[0];
if (arg0.constructor === String) {
url = arg0;
rteObj = new Route({url, handle: url, method});
} else {
url = arg0.url;
arg0.method = method;
rteObj = new Route(arg0);
}
routesList.push(rteObj);
args[0] = url;
}
return args;
}
module.exports = function (app) {
application = app;
methods.forEach((element) => {
const tempfn = app[element];
app[element] = function () {
const args = argsFixer(arguments, element);
return tempfn.apply(this, args);
};
});
return function (req, res, next) {
res.locals.getRoute = route;
res.exlist = function () {
return res.redirect(
url(...Object.keys(arguments).map((key) => arguments[key]))
);
};
res.exlist.route = res.locals.route = url;
next();
};
};
module.exports.route = route;
module.exports.list = function list() {
return routesList;
};
module.exports.cliTable = function cliTable() {
const table = new Table({
head: ['Route', 'Method', 'Handle', 'Arguments']
});
routesList.forEach((element) => {
const arr = [];
arr.push(element.url);
arr.push(element.method);
arr.push(element.handle);
arr.push(element.getParams().toString());
table.push(arr);
});
console.log(table.toString());
};