diff --git a/src/api/routes.js b/src/api/routes.js
index b91e7ba..1853b57 100644
--- a/src/api/routes.js
+++ b/src/api/routes.js
@@ -2,34 +2,60 @@ 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('');
+ logPrefixed('');
+}
+
function logHttpEntry(method, route, routeMethod) {
- if (routeMethod.description) console.log('%', '#', routeMethod.description);
+ 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');
- console.log('%', method, '{{baseUrl}}' + route, 'HTTP/1.1');
if (routeMethod.exampleHeaders) {
for (var key of Object.keys(routeMethod.exampleHeaders)) {
- console.log('%', `${key}: ${routeMethod.exampleHeaders[key]}`);
+ logPrefixed(`${key}: ${routeMethod.exampleHeaders[key]}`);
}
}
+
if (routeMethod.exampleBody) {
- console.log('%', 'Content-Type: application/json');
- console.log('% ');
- console.log(
- '%',
+ logPrefixed('Content-Type: application/json');
+ logPrefixed('');
+ logPrefixed(
JSON.stringify(routeMethod.exampleBody, null, 4).replaceAll(
'\n',
'\n% '
)
);
- console.log('% ');
+ logPrefixed('');
}
- console.log('% ');
- console.log('%', '###');
+
+ logPrefixed('');
+ logPrefixed('###');
}
export function registerRoutes(app) {
- // http file header:
- console.log('%', '');
+ logHttpHeader();
let routeCount = 0;
Object.keys(routes).forEach(routeName => {
@@ -46,11 +72,6 @@ export function registerRoutes(app) {
const routeMethod = route.methods[routeMethodName];
if (routeMethod === undefined) return;
- console.log(
- 'Registering',
- routeMethodName.toUpperCase(),
- route.path
- );
logHttpEntry(
routeMethodName.toUpperCase(),
route.path,
@@ -67,10 +88,12 @@ export function registerRoutes(app) {
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);
}
|