forked from labithiotis/express-list-routes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (92 loc) · 2.78 KB
/
index.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
const path = require('path');
const defaultOptions = {
prefix: '',
spacer: 7,
logger: console.info,
};
const COLORS = {
yellow: 33,
green: 32,
blue: 34,
red: 31,
grey: 90,
magenta: 35,
clear: 39,
};
const spacer = (x) => (x > 0 ? [...new Array(x)].map(() => ' ').join('') : '');
const colorText = (color, string) => `\u001b[${color}m${string}\u001b[${COLORS.clear}m`;
function colorMethod(method) {
switch (method) {
case 'POST':
return colorText(COLORS.yellow, method);
case 'GET':
return colorText(COLORS.green, method);
case 'PUT':
return colorText(COLORS.blue, method);
case 'DELETE':
return colorText(COLORS.red, method);
case 'PATCH':
return colorText(COLORS.grey, method);
default:
return method;
}
}
function getPathFromRegex(regexp) {
return regexp.toString().replace('/^', '').replace('?(?=\\/|$)/i', '').replace(/\\\//g, '/');
}
function combineStacks(acc, stack) {
if (stack.handle.stack) {
const routerPath = getPathFromRegex(stack.regexp);
return [...acc, ...stack.handle.stack.map((stack) => ({ routerPath, ...stack }))];
}
return [...acc, stack];
}
function getStacks(app) {
// Express 3
if (app.routes) {
// convert to express 4
return Object.keys(app.routes)
.reduce((acc, method) => [...acc, ...app.routes[method]], [])
.map((route) => ({ route: { stack: [route] } }));
}
// Express 4
if (app._router && app._router.stack) {
return app._router.stack.reduce(combineStacks, []);
}
// Express 4 Router
if (app.stack) {
return app.stack.reduce(combineStacks, []);
}
// Express 5
if (app.router && app.router.stack) {
return app.router.stack.reduce(combineStacks, []);
}
return [];
}
module.exports = function expressListRoutes(app, opts) {
const stacks = getStacks(app);
const options = { ...defaultOptions, ...opts };
const routes = {};
if (stacks) {
for (const stack of stacks) {
if (stack.route) {
const routeLogged = {};
for (const route of stack.route.stack) {
const method = route.method ? route.method.toUpperCase() : null;
if (!routeLogged[method] && method) {
const stackMethod = colorMethod(method);
const stackSpace = spacer(options.spacer - method.length);
const stackPath = path.resolve(
[options.prefix, stack.routerPath, stack.route.path, route.path].filter((s) => !!s).join(''),
);
const routePath = stackPath.replaceAll(/\s*\(.*?\)\s*/g, "").replaceAll('(', '').replaceAll(')', '')
options.print && options.logger(stackMethod, stackSpace, routePath);
routeLogged[method] = true;
routes[routePath] = route.name;
}
}
}
}
}
return routes;
};