yml-mvc-router
Version:
A configurable, Express-compatible routing module that maps routes from YAML to controllers following the MVC pattern
129 lines (115 loc) • 3.35 kB
JavaScript
/**
* Development route inspector
*/
class Inspector {
constructor(config) {
this.config = config;
}
/**
* Handle inspector route
* @param {Object} req - Express request
* @param {Object} res - Express response
* @param {Array} routesMeta - Routes metadata
*/
handle(req, res, routesMeta) {
const html = this._generateHtml(routesMeta);
res.setHeader('Content-Type', 'text/html');
res.send(html);
}
/**
* Generate HTML for route inspector
* @param {Array} routesMeta - Routes metadata
* @returns {string} HTML string
*/
_generateHtml(routesMeta) {
const rows = routesMeta
.map(route => this._generateRow(route))
.join('');
return `
<html>
<head>
<title>yml-mvc-router Inspector</title>
<style>
body { font-family: monospace; margin: 20px; }
h1 { color: #333; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.error { background-color: #ffebee; }
.success { background-color: #e8f5e8; }
.method { font-weight: bold; padding: 2px 6px; border-radius: 3px; }
.GET { background: #4CAF50; color: white; }
.POST { background: #2196F3; color: white; }
.PUT { background: #FF9800; color: white; }
.DELETE { background: #f44336; color: white; }
.PATCH { background: #9C27B0; color: white; }
.assets { font-size: 0.8em; color: #666; }
</style>
</head>
<body>
<h1>yml-mvc-router Inspector</h1>
<p><strong>Routes file:</strong> ${this.config.routes}</p>
<p><strong>Total routes:</strong> ${routesMeta.length}</p>
<table>
<thead>
<tr>
<th>Method</th>
<th>Path</th>
<th>Controller</th>
<th>Middlewares</th>
<th>Assets</th>
<th>Status</th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>
<script>
// Auto-refresh every 5 seconds
setTimeout(() => location.reload(), 5000);
</script>
</body>
</html>
`.trim();
}
/**
* Generate table row for a route
* @param {Object} route - Route metadata
* @returns {string} HTML table row
*/
_generateRow(route) {
const statusClass = route.registered ? 'success' : 'error';
const statusText = route.registered ? 'OK' : route.error || 'Error';
const middlewares = (route.resolvedMiddlewares || [])
.join(', ') || 'none';
const assets = this._formatAssets(route.assets);
return `
<tr class="${statusClass}">
<td><span class="method ${route.method}">${route.method}</span></td>
<td>${route.path}</td>
<td>${route.controller}.${route.action}</td>
<td>${middlewares}</td>
<td class="assets">${assets}</td>
<td>${statusText}</td>
</tr>
`;
}
/**
* Format assets for display
* @param {Object} assets - Assets configuration
* @returns {string} Formatted assets string
*/
_formatAssets(assets = {}) {
const parts = [];
if (assets.css && assets.css.length > 0) {
parts.push(`CSS: ${assets.css.join(', ')}`);
}
if (assets.js && assets.js.length > 0) {
parts.push(`JS: ${assets.js.join(', ')}`);
}
return parts.join('<br>') || 'none';
}
}
module.exports = Inspector;