@seip/blue-bird 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env_example +26 -0
- package/AGENTS.md +199 -0
- package/README.md +79 -0
- package/backend/index.js +13 -0
- package/backend/routes/api.js +31 -0
- package/backend/routes/frontend.js +41 -0
- package/backend/routes/seo.js +39 -0
- package/core/app.js +325 -0
- package/core/auth.js +83 -0
- package/core/cache.js +45 -0
- package/core/cli/component.js +42 -0
- package/core/cli/init.js +118 -0
- package/core/cli/react.js +435 -0
- package/core/cli/route.js +43 -0
- package/core/cli/swagger.js +40 -0
- package/core/config.js +47 -0
- package/core/debug.js +249 -0
- package/core/logger.js +100 -0
- package/core/middleware.js +27 -0
- package/core/router.js +333 -0
- package/core/seo.js +100 -0
- package/core/swagger.js +25 -0
- package/core/template.js +462 -0
- package/core/upload.js +76 -0
- package/core/validate.js +380 -0
- package/frontend/index.html +27 -0
- package/frontend/landing.html +70 -0
- package/frontend/public/favicon.ico +0 -0
- package/frontend/resources/css/tailwind.css +18 -0
- package/frontend/resources/js/App.jsx +70 -0
- package/frontend/resources/js/Main.jsx +19 -0
- package/frontend/resources/js/blue-bird/components/Button.jsx +67 -0
- package/frontend/resources/js/blue-bird/components/Card.jsx +18 -0
- package/frontend/resources/js/blue-bird/components/DataTable.jsx +126 -0
- package/frontend/resources/js/blue-bird/components/Input.jsx +21 -0
- package/frontend/resources/js/blue-bird/components/Label.jsx +12 -0
- package/frontend/resources/js/blue-bird/components/LanguageButton.jsx +23 -0
- package/frontend/resources/js/blue-bird/components/Link.jsx +16 -0
- package/frontend/resources/js/blue-bird/components/Modal.jsx +27 -0
- package/frontend/resources/js/blue-bird/components/Skeleton.jsx +45 -0
- package/frontend/resources/js/blue-bird/components/Translate.jsx +12 -0
- package/frontend/resources/js/blue-bird/components/Typography.jsx +69 -0
- package/frontend/resources/js/blue-bird/contexts/LanguageContext.jsx +41 -0
- package/frontend/resources/js/blue-bird/contexts/SPAContext.jsx +237 -0
- package/frontend/resources/js/blue-bird/contexts/SnackbarContext.jsx +38 -0
- package/frontend/resources/js/blue-bird/contexts/ThemeContext.jsx +49 -0
- package/frontend/resources/js/blue-bird/locales/en.json +48 -0
- package/frontend/resources/js/blue-bird/locales/es.json +48 -0
- package/frontend/resources/js/components/Header.jsx +56 -0
- package/frontend/resources/js/pages/About.jsx +32 -0
- package/frontend/resources/js/pages/Home.jsx +82 -0
- package/package.json +58 -0
- package/vite.config.js +23 -0
package/core/debug.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import Router from "./router.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Advanced Debug module for Blue Bird.
|
|
5
|
+
* Provides metrics history, route statistics and live monitoring.
|
|
6
|
+
*/
|
|
7
|
+
class Debug {
|
|
8
|
+
|
|
9
|
+
constructor() {
|
|
10
|
+
this.router = new Router("/debug");
|
|
11
|
+
this.limit = 50;
|
|
12
|
+
this.initStore();
|
|
13
|
+
this.registerRoutes();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
initStore() {
|
|
17
|
+
if (!global.__bluebird_debug_store__) {
|
|
18
|
+
global.__bluebird_debug_store__ = {
|
|
19
|
+
requests: [],
|
|
20
|
+
routes: {},
|
|
21
|
+
errors4xx: 0,
|
|
22
|
+
errors5xx: 0
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static shouldTrack(req) {
|
|
28
|
+
const url = req.originalUrl || "";
|
|
29
|
+
|
|
30
|
+
if (url.startsWith("/debug")) return false;
|
|
31
|
+
|
|
32
|
+
if (/\.(json|css|js|map|png|jpg|jpeg|gif|svg|ico|webp)$/i.test(url)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (req.method === "OPTIONS") return false;
|
|
37
|
+
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static middlewareMetrics(app) {
|
|
42
|
+
app.use((req, res, next) => {
|
|
43
|
+
|
|
44
|
+
if (!Debug.shouldTrack(req)) return next();
|
|
45
|
+
|
|
46
|
+
const start = process.hrtime();
|
|
47
|
+
|
|
48
|
+
res.on("finish", () => {
|
|
49
|
+
|
|
50
|
+
const diff = process.hrtime(start);
|
|
51
|
+
const responseTime = diff[0] * 1e3 + diff[1] / 1e6;
|
|
52
|
+
|
|
53
|
+
const memory = process.memoryUsage();
|
|
54
|
+
const ramUsedMB = memory.rss / 1024 / 1024;
|
|
55
|
+
|
|
56
|
+
const cpuUsage = process.cpuUsage();
|
|
57
|
+
const cpuUsedMS = (cpuUsage.user + cpuUsage.system) / 1000;
|
|
58
|
+
|
|
59
|
+
const record = {
|
|
60
|
+
method: req.method,
|
|
61
|
+
url: req.originalUrl,
|
|
62
|
+
status: res.statusCode,
|
|
63
|
+
responseTime: Number(responseTime.toFixed(2)),
|
|
64
|
+
ramUsedMB: Number(ramUsedMB.toFixed(2)),
|
|
65
|
+
cpuUsedMS: Number(cpuUsedMS.toFixed(2)),
|
|
66
|
+
date: new Date().toISOString()
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const store = global.__bluebird_debug_store__;
|
|
70
|
+
|
|
71
|
+
store.requests.unshift(record);
|
|
72
|
+
if (store.requests.length > 50) store.requests.pop();
|
|
73
|
+
|
|
74
|
+
const routeKey = `${req.method} ${req.route?.path || req.path}`;
|
|
75
|
+
|
|
76
|
+
if (!store.routes[routeKey]) {
|
|
77
|
+
store.routes[routeKey] = {
|
|
78
|
+
count: 0,
|
|
79
|
+
totalTime: 0
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
store.routes[routeKey].count += 1;
|
|
84
|
+
store.routes[routeKey].totalTime += record.responseTime;
|
|
85
|
+
|
|
86
|
+
if (record.status >= 400 && record.status < 500) store.errors4xx++;
|
|
87
|
+
if (record.status >= 500) store.errors5xx++;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
next();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
registerRoutes() {
|
|
95
|
+
|
|
96
|
+
this.router.get("/", (req, res) => {
|
|
97
|
+
|
|
98
|
+
const store = global.__bluebird_debug_store__;
|
|
99
|
+
|
|
100
|
+
if (req.query.fetch === "true") {
|
|
101
|
+
return res.json(store);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (req.query.reset === "true") {
|
|
105
|
+
global.__bluebird_debug_store__ = {
|
|
106
|
+
requests: [],
|
|
107
|
+
routes: {},
|
|
108
|
+
errors4xx: 0,
|
|
109
|
+
errors5xx: 0
|
|
110
|
+
};
|
|
111
|
+
return res.json({ ok: true });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
res.send(`
|
|
115
|
+
<!DOCTYPE html>
|
|
116
|
+
<html>
|
|
117
|
+
<head>
|
|
118
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
119
|
+
<title>Blue Bird Debug</title>
|
|
120
|
+
</head>
|
|
121
|
+
<body class="bg-gray-100 text-gray-800 p-10">
|
|
122
|
+
|
|
123
|
+
<div class="max-w-7xl mx-auto">
|
|
124
|
+
|
|
125
|
+
<div class="flex justify-between items-center mb-8">
|
|
126
|
+
<h1 class="text-3xl font-bold text-blue-600">Blue Bird Debug Panel</h1>
|
|
127
|
+
<button onclick="resetData()" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg shadow">
|
|
128
|
+
Reset
|
|
129
|
+
</button>
|
|
130
|
+
</div>
|
|
131
|
+
|
|
132
|
+
<div class="grid grid-cols-3 gap-6 mb-8">
|
|
133
|
+
|
|
134
|
+
<div class="bg-white p-6 rounded-xl shadow">
|
|
135
|
+
<h3 class="text-gray-500 text-sm">Total Requests</h3>
|
|
136
|
+
<p id="totalReq" class="text-2xl font-bold">0</p>
|
|
137
|
+
</div>
|
|
138
|
+
|
|
139
|
+
<div class="bg-yellow-100 p-6 rounded-xl shadow">
|
|
140
|
+
<h3 class="text-yellow-600 text-sm">4xx Errors</h3>
|
|
141
|
+
<p id="err4" class="text-2xl font-bold text-yellow-700">0</p>
|
|
142
|
+
</div>
|
|
143
|
+
|
|
144
|
+
<div class="bg-red-100 p-6 rounded-xl shadow">
|
|
145
|
+
<h3 class="text-red-600 text-sm">5xx Errors</h3>
|
|
146
|
+
<p id="err5" class="text-2xl font-bold text-red-700">0</p>
|
|
147
|
+
</div>
|
|
148
|
+
|
|
149
|
+
</div>
|
|
150
|
+
|
|
151
|
+
<div class="grid grid-cols-2 gap-10">
|
|
152
|
+
|
|
153
|
+
<div class="bg-white p-6 rounded-xl shadow">
|
|
154
|
+
<h2 class="text-lg font-semibold mb-4">Route Stats</h2>
|
|
155
|
+
<table class="table-fixed w-full text-sm">
|
|
156
|
+
<thead>
|
|
157
|
+
<tr class="border-b">
|
|
158
|
+
<th class="text-left w-1/2 py-2">Route</th>
|
|
159
|
+
<th class="text-left w-1/4">Hits</th>
|
|
160
|
+
<th class="text-left w-1/4">Avg Time</th>
|
|
161
|
+
</tr>
|
|
162
|
+
</thead>
|
|
163
|
+
<tbody id="routesBody"></tbody>
|
|
164
|
+
</table>
|
|
165
|
+
</div>
|
|
166
|
+
|
|
167
|
+
<div class="bg-white p-6 rounded-xl shadow">
|
|
168
|
+
<h2 class="text-lg font-semibold mb-4">Last Requests</h2>
|
|
169
|
+
<table class="table-fixed w-full text-sm">
|
|
170
|
+
<thead>
|
|
171
|
+
<tr class="border-b">
|
|
172
|
+
<th class="text-left w-1/2 py-2">URL</th>
|
|
173
|
+
<th class="text-left w-1/6">Method</th>
|
|
174
|
+
<th class="text-left w-1/6">Status</th>
|
|
175
|
+
<th class="text-left w-1/6">Time</th>
|
|
176
|
+
</tr>
|
|
177
|
+
</thead>
|
|
178
|
+
<tbody id="historyBody"></tbody>
|
|
179
|
+
</table>
|
|
180
|
+
</div>
|
|
181
|
+
|
|
182
|
+
</div>
|
|
183
|
+
|
|
184
|
+
</div>
|
|
185
|
+
|
|
186
|
+
<script>
|
|
187
|
+
async function loadData() {
|
|
188
|
+
const res = await fetch('/debug?fetch=true');
|
|
189
|
+
const data = await res.json();
|
|
190
|
+
|
|
191
|
+
document.getElementById("totalReq").innerText = data.requests.length;
|
|
192
|
+
document.getElementById("err4").innerText = data.errors4xx;
|
|
193
|
+
document.getElementById("err5").innerText = data.errors5xx;
|
|
194
|
+
|
|
195
|
+
const routesBody = document.getElementById("routesBody");
|
|
196
|
+
const historyBody = document.getElementById("historyBody");
|
|
197
|
+
|
|
198
|
+
routesBody.innerHTML = "";
|
|
199
|
+
historyBody.innerHTML = "";
|
|
200
|
+
|
|
201
|
+
Object.entries(data.routes).forEach(([key, value]) => {
|
|
202
|
+
const avg = (value.totalTime / value.count).toFixed(2);
|
|
203
|
+
let color = "";
|
|
204
|
+
if (avg > 500) color = "text-red-600 font-semibold";
|
|
205
|
+
else if (avg > 200) color = "text-yellow-600";
|
|
206
|
+
|
|
207
|
+
routesBody.innerHTML += \`
|
|
208
|
+
<tr class="border-b">
|
|
209
|
+
<td class="py-1 truncate">\${key}</td>
|
|
210
|
+
<td>\${value.count}</td>
|
|
211
|
+
<td class="\${color}">\${avg} ms</td>
|
|
212
|
+
</tr>\`;
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
data.requests.forEach(r => {
|
|
216
|
+
historyBody.innerHTML += \`
|
|
217
|
+
<tr class="border-b text-xs">
|
|
218
|
+
<td class="truncate">\${r.url}</td>
|
|
219
|
+
<td>\${r.method}</td>
|
|
220
|
+
<td>\${r.status}</td>
|
|
221
|
+
<td>\${r.responseTime} ms</td>
|
|
222
|
+
</tr>\`;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function resetData() {
|
|
227
|
+
await fetch('/debug?reset=true');
|
|
228
|
+
loadData();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
loadData();
|
|
232
|
+
setInterval(loadData, 3000);
|
|
233
|
+
</script>
|
|
234
|
+
|
|
235
|
+
</body>
|
|
236
|
+
</html>
|
|
237
|
+
`);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
getRouter() {
|
|
242
|
+
return {
|
|
243
|
+
path: this.router.getPath(),
|
|
244
|
+
router: this.router.getRouter()
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export default Debug;
|
package/core/logger.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import Config from "./config.js"
|
|
4
|
+
|
|
5
|
+
const __dirname = Config.dirname()
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Logger class for managing application logs by creating dated folders and log files.
|
|
9
|
+
*/
|
|
10
|
+
class Logger {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the Logger instance and ensures the logs directory exists.
|
|
14
|
+
*/
|
|
15
|
+
constructor() {
|
|
16
|
+
this.folder = path.join(__dirname, "logs");
|
|
17
|
+
this._currentDay = null;
|
|
18
|
+
this._currentDayFolder = null;
|
|
19
|
+
if (!fs.existsSync(this.folder)) {
|
|
20
|
+
fs.mkdirSync(this.folder, { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Ensures and returns the path to the log folder for the current day.
|
|
26
|
+
* Caches the folder path for the current day to avoid repeated fs checks.
|
|
27
|
+
* @returns {string} The absolute path to the current day's log folder.
|
|
28
|
+
*/
|
|
29
|
+
nowFolder() {
|
|
30
|
+
const today = this.now();
|
|
31
|
+
|
|
32
|
+
if (this._currentDay === today && this._currentDayFolder) {
|
|
33
|
+
return this._currentDayFolder;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const folder = path.join(this.folder, today);
|
|
37
|
+
|
|
38
|
+
if (!fs.existsSync(folder)) {
|
|
39
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this._currentDay = today;
|
|
43
|
+
this._currentDayFolder = folder;
|
|
44
|
+
return folder;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Gets the current date formatted as YYYY-MM-DD.
|
|
49
|
+
* @returns {string} The formatted date string.
|
|
50
|
+
*/
|
|
51
|
+
now() {
|
|
52
|
+
return new Date().toISOString().split("T")[0];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Appends an informational message to the info.log file (non-blocking).
|
|
57
|
+
* @param {string} message - The message to log.
|
|
58
|
+
*/
|
|
59
|
+
info(message) {
|
|
60
|
+
const logFile = path.join(this.nowFolder(), 'info.log');
|
|
61
|
+
fs.appendFile(logFile, `${message}\n`, (err) => {
|
|
62
|
+
if (err) console.error('Logger write error:', err.message);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Appends an error message to the error.log file (non-blocking).
|
|
68
|
+
* @param {string} message - The error message to log.
|
|
69
|
+
*/
|
|
70
|
+
error(message) {
|
|
71
|
+
const logFile = path.join(this.nowFolder(), 'error.log');
|
|
72
|
+
fs.appendFile(logFile, `${message}\n`, (err) => {
|
|
73
|
+
if (err) console.error('Logger write error:', err.message);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Appends a warning message to the warn.log file (non-blocking).
|
|
79
|
+
* @param {string} message - The warning message to log.
|
|
80
|
+
*/
|
|
81
|
+
warning(message) {
|
|
82
|
+
const logFile = path.join(this.nowFolder(), 'warn.log');
|
|
83
|
+
fs.appendFile(logFile, `${message}\n`, (err) => {
|
|
84
|
+
if (err) console.error('Logger write error:', err.message);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Appends a debug message to the debug.log file (non-blocking).
|
|
90
|
+
* @param {string} message - The debug message to log.
|
|
91
|
+
*/
|
|
92
|
+
debug(message) {
|
|
93
|
+
const logFile = path.join(this.nowFolder(), 'debug.log');
|
|
94
|
+
fs.appendFile(logFile, `${message}\n`, (err) => {
|
|
95
|
+
if (err) console.error('Logger write error:', err.message);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default Logger;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Auth from "./auth.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Common middlewares for the Blue Bird framework.
|
|
5
|
+
*/
|
|
6
|
+
const Middleware = {
|
|
7
|
+
/**
|
|
8
|
+
* Authentication protection middleware.
|
|
9
|
+
* @type {Function}
|
|
10
|
+
*/
|
|
11
|
+
auth: Auth.protect(),
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Web authentication protection middleware (redirects to home if fails).
|
|
15
|
+
* @type {Function}
|
|
16
|
+
*/
|
|
17
|
+
webAuth: Auth.protect({ redirect: "/" }),
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Logging middleware (can be extended).
|
|
21
|
+
*/
|
|
22
|
+
logger: (req, res, next) => {
|
|
23
|
+
next();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default Middleware;
|
package/core/router.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import Config from "./config.js";
|
|
3
|
+
import Template from "./template.js";
|
|
4
|
+
import SEO from "./seo.js";
|
|
5
|
+
|
|
6
|
+
const __dirname = Config.dirname();
|
|
7
|
+
const props = Config.props();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Router wrapper class for handling Express routing logic.
|
|
11
|
+
*/
|
|
12
|
+
class Router {
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new Router instance.
|
|
15
|
+
* @param {string} [path="/"] - The base path for this router.
|
|
16
|
+
* @example
|
|
17
|
+
* const router = new Router("/api")
|
|
18
|
+
* router.get("/", (req, res) => {
|
|
19
|
+
* res.json({ message: "Hello World!" })
|
|
20
|
+
* })
|
|
21
|
+
*/
|
|
22
|
+
constructor(path = "/") {
|
|
23
|
+
this.router = express.Router();
|
|
24
|
+
this.path = path;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Registers a middleware on this router.
|
|
29
|
+
* @param {...Function} middleware - Middleware functions.
|
|
30
|
+
* @example
|
|
31
|
+
* router.use(Auth.protect());
|
|
32
|
+
* router.use(App.helmet());
|
|
33
|
+
*/
|
|
34
|
+
use(...middleware) {
|
|
35
|
+
this.router.use(...middleware);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Registers a GET route handler.
|
|
40
|
+
* @param {string} path - The relative path for the GET route.
|
|
41
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
42
|
+
* @example
|
|
43
|
+
* router.get("/users", (req, res) => {
|
|
44
|
+
* const users = [
|
|
45
|
+
* {
|
|
46
|
+
* name: "John Doe",
|
|
47
|
+
* email: "john.doe@example.com",
|
|
48
|
+
* },
|
|
49
|
+
* {
|
|
50
|
+
* name: "Jane Doe2",
|
|
51
|
+
* email: "jane.doe2@example.com",
|
|
52
|
+
* },
|
|
53
|
+
* ]
|
|
54
|
+
* res.json(users)
|
|
55
|
+
* })
|
|
56
|
+
*/
|
|
57
|
+
get(path, ...callback) {
|
|
58
|
+
if (path === "/*" || path === "*") {
|
|
59
|
+
path = /.*/;
|
|
60
|
+
}
|
|
61
|
+
this.router.get(path, callback);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Registers a POST route handler.
|
|
66
|
+
* @param {string} path - The relative path for the POST route.
|
|
67
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
68
|
+
* @example
|
|
69
|
+
* router.post("/users", (req, res) => {
|
|
70
|
+
* return res.json({ message: "User created successfully" })
|
|
71
|
+
* })
|
|
72
|
+
*/
|
|
73
|
+
post(path, ...callback) {
|
|
74
|
+
if (path === "/*" || path === "*") {
|
|
75
|
+
path = /.*/;
|
|
76
|
+
}
|
|
77
|
+
this.router.post(path, callback);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Registers a PUT route handler.
|
|
82
|
+
* @param {string} path - The relative path for the PUT route.
|
|
83
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
84
|
+
* @example
|
|
85
|
+
* router.put("/users", (req, res) => {
|
|
86
|
+
* return res.json({ message: "User updated successfully" })
|
|
87
|
+
* })
|
|
88
|
+
*/
|
|
89
|
+
put(path, ...callback) {
|
|
90
|
+
this.router.put(path, callback);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Registers a DELETE route handler.
|
|
95
|
+
* @param {string} path - The relative path for the DELETE route.
|
|
96
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
97
|
+
* @example
|
|
98
|
+
* router.delete("/users", (req, res) => {
|
|
99
|
+
* return res.json({ message: "User deleted successfully" })
|
|
100
|
+
* })
|
|
101
|
+
*/
|
|
102
|
+
delete(path, ...callback) {
|
|
103
|
+
this.router.delete(path, callback);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Registers a PATCH route handler.
|
|
108
|
+
* @param {string} path - The relative path for the PATCH route.
|
|
109
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
110
|
+
* @example
|
|
111
|
+
* router.patch("/users", (req, res) => {
|
|
112
|
+
* return res.json({ message: "User patched successfully" })
|
|
113
|
+
* })
|
|
114
|
+
*/
|
|
115
|
+
patch(path, ...callback) {
|
|
116
|
+
this.router.patch(path, callback);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Registers an OPTIONS route handler.
|
|
121
|
+
* @param {string} path - The relative path for the OPTIONS route.
|
|
122
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
123
|
+
* @example
|
|
124
|
+
* router.options("/users", (req, res) => {
|
|
125
|
+
* return res.json({ message: "User options successfully" })
|
|
126
|
+
* })
|
|
127
|
+
*/
|
|
128
|
+
options(path, ...callback) {
|
|
129
|
+
this.router.options(path, callback);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns the underlying Express router instance.
|
|
134
|
+
* @returns {import('express').Router} The Express router object.
|
|
135
|
+
*/
|
|
136
|
+
getRouter() {
|
|
137
|
+
return this.router;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Returns the base path associated with this router.
|
|
142
|
+
* @returns {string} The router path.
|
|
143
|
+
*/
|
|
144
|
+
getPath() {
|
|
145
|
+
return this.path;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Resolves meta tags for a route, supporting simple meta, inline multilingual, or external seoData.
|
|
150
|
+
*
|
|
151
|
+
* @private
|
|
152
|
+
* @param {Object} route - The route configuration.
|
|
153
|
+
* @param {string} lang - Target language.
|
|
154
|
+
* @param {Object} [seoData=null] - External SEO data object.
|
|
155
|
+
* @param {Array<string>} [languages=[]] - Available languages.
|
|
156
|
+
* @returns {Object} Resolved meta tags object.
|
|
157
|
+
*/
|
|
158
|
+
_resolveMeta(route, lang, seoData = null, languages = []) {
|
|
159
|
+
const { meta = {}, seoKey } = route;
|
|
160
|
+
|
|
161
|
+
if (seoKey && seoData && seoData[seoKey]) {
|
|
162
|
+
const seoEntry = seoData[seoKey];
|
|
163
|
+
return seoEntry[lang] || seoEntry[Object.keys(seoEntry)[0]] || {};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const hasLangKeys = languages.some(
|
|
167
|
+
(l) => meta[l] && typeof meta[l] === "object",
|
|
168
|
+
);
|
|
169
|
+
if (hasLangKeys) {
|
|
170
|
+
return meta[lang] || meta[Object.keys(meta).find((k) => meta[k])] || {};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return meta;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Registers a single SEO route with Template.renderReact.
|
|
178
|
+
*
|
|
179
|
+
* @private
|
|
180
|
+
* @param {string} routePath - Express route path.
|
|
181
|
+
* @param {string} component - React component name.
|
|
182
|
+
* @param {Object} props - Props to pass to the component.
|
|
183
|
+
* @param {Object} metaData - Resolved meta data.
|
|
184
|
+
* @param {string} lang - Language code.
|
|
185
|
+
*/
|
|
186
|
+
_registerSeoRoute(routePath, component, props, metaData, lang, options = {}) {
|
|
187
|
+
const { seoData, languages } = options;
|
|
188
|
+
|
|
189
|
+
this.get(routePath, (req, res) => {
|
|
190
|
+
let activeLang = lang;
|
|
191
|
+
|
|
192
|
+
const isDefaultRoute = !languages.some(l => req.path.startsWith(`/${l}`));
|
|
193
|
+
if (isDefaultRoute && req.cookies?.blue_bird_lang && languages?.includes(req.cookies.blue_bird_lang)) {
|
|
194
|
+
activeLang = req.cookies.blue_bird_lang;
|
|
195
|
+
}
|
|
196
|
+
if (req.query.source === "frontend" && req.query.lang && languages?.includes(req.query.lang)) {
|
|
197
|
+
activeLang = req.query.lang;
|
|
198
|
+
res.cookie("blue_bird_lang", activeLang, {
|
|
199
|
+
maxAge: 31536000000,
|
|
200
|
+
httpOnly: false,
|
|
201
|
+
path: "/",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
let activeMeta = metaData;
|
|
206
|
+
if (activeLang !== lang) {
|
|
207
|
+
activeMeta = this._resolveMeta(
|
|
208
|
+
{ meta: metaData, seoKey: options.seoKey },
|
|
209
|
+
activeLang,
|
|
210
|
+
seoData,
|
|
211
|
+
languages,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const dynamicProps = {
|
|
216
|
+
props: {
|
|
217
|
+
...props,
|
|
218
|
+
params: req.params,
|
|
219
|
+
query: req.query,
|
|
220
|
+
lang: activeLang,
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const metaTags = {
|
|
225
|
+
titleMeta: activeMeta.titleMeta || activeMeta.title || "",
|
|
226
|
+
descriptionMeta: activeMeta.descriptionMeta || activeMeta.description || "",
|
|
227
|
+
keywordsMeta: activeMeta.keywordsMeta || activeMeta.keywords || "",
|
|
228
|
+
ogImage: activeMeta.ogImage || "",
|
|
229
|
+
ogType: activeMeta.ogType || "website",
|
|
230
|
+
twitterCard: activeMeta.twitterCard || "summary_large_image",
|
|
231
|
+
langMeta: activeLang,
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
return Template.renderReact(res, component, dynamicProps, { metaTags });
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Registers multiple routes based on an SEO configuration array.
|
|
240
|
+
* Supports simple meta, inline multilingual meta, and external seoData files.
|
|
241
|
+
* Automatically generates language-prefixed routes and registers sitemap.xml and robots.txt.
|
|
242
|
+
*
|
|
243
|
+
* @param {Array<Object>} routesConfig - Array of route objects.
|
|
244
|
+
* @param {Object} [options={}] - Configuration options.
|
|
245
|
+
* @param {Array<string>} [options.languages=[]] - Languages to register (e.g., ["en", "es"]).
|
|
246
|
+
* @param {string} [options.defaultLanguage="en"] - The default language for unprefixed paths.
|
|
247
|
+
* @param {Object} [options.seoData=null] - External SEO data object (like seo.php pattern).
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* // Simple (no i18n)
|
|
251
|
+
* router.seo([
|
|
252
|
+
* {
|
|
253
|
+
* path: "/",
|
|
254
|
+
* component: "Home",
|
|
255
|
+
* meta: { titleMeta: "Home", descriptionMeta: "Welcome" },
|
|
256
|
+
* props: { id: 1 }
|
|
257
|
+
* }
|
|
258
|
+
* ]);
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* // Multilingual (inline)
|
|
262
|
+
* router.seo([
|
|
263
|
+
* {
|
|
264
|
+
* path: "/",
|
|
265
|
+
* component: "Home",
|
|
266
|
+
* meta: {
|
|
267
|
+
* en: { titleMeta: "Home", descriptionMeta: "Welcome" },
|
|
268
|
+
* es: { titleMeta: "Inicio", descriptionMeta: "Bienvenido" }
|
|
269
|
+
* }
|
|
270
|
+
* }
|
|
271
|
+
* ], { languages: ["en", "es"], defaultLanguage: "en" });
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* // Multilingual (external seoData file)
|
|
275
|
+
* import seoData from "./seo.js";
|
|
276
|
+
* router.seo([
|
|
277
|
+
* { path: "/", component: "Home", seoKey: "home" }
|
|
278
|
+
* ], { languages: ["en", "es"], defaultLanguage: "en", seoData });
|
|
279
|
+
*/
|
|
280
|
+
seo(routesConfig, options = {}) {
|
|
281
|
+
const { languages = [], defaultLanguage = null, seoData = null } = options;
|
|
282
|
+
|
|
283
|
+
const defaultLanguageOption=(defaultLanguage === null) ? props.langMeta : "en";
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
SEO.registerRoutes(this.router, routesConfig, options);
|
|
287
|
+
|
|
288
|
+
routesConfig.forEach((route) => {
|
|
289
|
+
const { path: routePath, component, props = {}, seoKey } = route;
|
|
290
|
+
|
|
291
|
+
if (languages.length > 0) {
|
|
292
|
+
languages.forEach((lang) => {
|
|
293
|
+
const langMeta = this._resolveMeta(route, lang, seoData, languages);
|
|
294
|
+
const langPath = `/${lang}${routePath === "/" ? "" : routePath}`;
|
|
295
|
+
this._registerSeoRoute(
|
|
296
|
+
langPath,
|
|
297
|
+
component,
|
|
298
|
+
props,
|
|
299
|
+
langMeta,
|
|
300
|
+
lang,
|
|
301
|
+
{ seoData, languages, seoKey },
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const defaultMeta = this._resolveMeta(
|
|
306
|
+
route,
|
|
307
|
+
defaultLanguageOption,
|
|
308
|
+
seoData,
|
|
309
|
+
languages,
|
|
310
|
+
);
|
|
311
|
+
this._registerSeoRoute(
|
|
312
|
+
routePath,
|
|
313
|
+
component,
|
|
314
|
+
props,
|
|
315
|
+
defaultMeta,
|
|
316
|
+
defaultLanguageOption,
|
|
317
|
+
{ seoData, languages, seoKey },
|
|
318
|
+
);
|
|
319
|
+
} else {
|
|
320
|
+
const meta = this._resolveMeta(route, defaultLanguageOption, seoData, []);
|
|
321
|
+
this._registerSeoRoute(
|
|
322
|
+
routePath,
|
|
323
|
+
component,
|
|
324
|
+
props,
|
|
325
|
+
meta,
|
|
326
|
+
defaultLanguageOption,
|
|
327
|
+
{ seoData, languages: [], seoKey },
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
export default Router;
|