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
|
import * as routes from './routes/index.js';
import * as url from 'node:url';
import express from 'express';
let dumpHttpEndpoints = false;
function logPrefixed(...args) {
console.log('%', ...args);
}
function logHttpHeader() {
if (!dumpHttpEndpoints) return;
logPrefixed('@baseUrl=http://localhost:3000');
logPrefixed('@username=admin');
logPrefixed('@password=admin');
logPrefixed('@email=admin@example.com');
logPrefixed('@userType=admin');
logPrefixed('@accessToken=someValueXyzXyz');
logPrefixed('');
logPrefixed('');
}
function logHttpEntry(method, route, routeMethod) {
console.log(
'Registering',
method.toUpperCase(),
route + ':',
routeMethod.description || 'No description provided'
);
if (!dumpHttpEndpoints) return;
if (routeMethod.description) logPrefixed('#', routeMethod.description);
logPrefixed(method, '{{baseUrl}}' + route, 'HTTP/1.1');
if (routeMethod.exampleHeaders) {
for (var key of Object.keys(routeMethod.exampleHeaders)) {
logPrefixed(`${key}: ${routeMethod.exampleHeaders[key]}`);
}
}
if (routeMethod.exampleBody) {
logPrefixed('Content-Type: application/json');
logPrefixed('');
logPrefixed(
JSON.stringify(routeMethod.exampleBody, null, 4).replaceAll(
'\n',
'\n% '
)
);
logPrefixed('');
}
logPrefixed('');
logPrefixed('###');
}
export function registerRoutes(app) {
logHttpHeader();
let routeCount = 0;
Object.keys(routes).forEach(routeName => {
/**
* @type {RouteDescription}
*/
const route = routes[routeName];
if (route === undefined) return;
Object.keys(route.methods).forEach(routeMethodName => {
/**
* @type {RouteMethod}
*/
const routeMethod = route.methods[routeMethodName];
if (routeMethod === undefined) return;
logHttpEntry(
routeMethodName.toUpperCase(),
route.path,
routeMethod
);
app[routeMethodName](route.path, [
...(routeMethod.middlewares || []),
routeMethod.method
]);
routeCount++;
});
});
console.log(`Registered ${routeCount} routes.`);
}
// Check if the script is run directly, to create the routes.html file.
if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
const app = express();
dumpHttpEndpoints = true;
registerRoutes(app);
process.exit(1);
}
}
|