@seip/blue-bird 0.2.0 → 0.2.2
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 +11 -11
- package/LICENSE +21 -21
- package/README.md +80 -80
- package/backend/index.js +12 -12
- package/backend/routes/app.js +52 -40
- package/core/app.js +182 -182
- package/core/auth.js +69 -69
- package/core/cli/component.js +42 -42
- package/core/cli/init.js +117 -116
- package/core/cli/react.js +408 -393
- package/core/cli/route.js +42 -42
- package/core/config.js +41 -41
- package/core/logger.js +80 -80
- package/core/middleware.js +27 -27
- package/core/router.js +134 -134
- package/core/template.js +283 -220
- package/core/upload.js +76 -76
- package/core/validate.js +291 -291
- package/frontend/index.html +20 -0
- package/package.json +40 -43
package/core/cli/route.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import Config from "../config.js";
|
|
4
|
-
|
|
5
|
-
const __dirname = Config.dirname();
|
|
6
|
-
|
|
7
|
-
class RouteCLI {
|
|
8
|
-
/**
|
|
9
|
-
* Create route
|
|
10
|
-
*/
|
|
11
|
-
create() {
|
|
12
|
-
let nameRoute = process.argv[2];
|
|
13
|
-
if (!nameRoute) {
|
|
14
|
-
console.log("Please provide a route name. Usage: npm run route <route-name>");
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
nameRoute =nameRoute.charAt(0).toUpperCase() + nameRoute.slice(1);
|
|
18
|
-
const folder= path.join(__dirname, 'backend/routes');
|
|
19
|
-
if (!fs.existsSync(folder)){
|
|
20
|
-
fs.mkdirSync(folder, { recursive: true });
|
|
21
|
-
}
|
|
22
|
-
const filePath = path.join(folder, `${nameRoute}.js`);
|
|
23
|
-
if (fs.existsSync(filePath)) {
|
|
24
|
-
console.log(`Route ${nameRoute} already exists.`);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const content =`import Router from "@seip/blue-bird/core/router.js"
|
|
28
|
-
|
|
29
|
-
const router${nameRoute} = new Router("/${nameRoute.toLowerCase()}");
|
|
30
|
-
|
|
31
|
-
router${nameRoute}.get("/", (req, res) => {
|
|
32
|
-
res.json({ message: "Hello from ${nameRoute} route!" });
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
export default router${nameRoute};
|
|
36
|
-
`;
|
|
37
|
-
fs.writeFileSync(filePath, content);
|
|
38
|
-
console.log(`Route ${nameRoute} created successfully at ${filePath}`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const routeCLI = new RouteCLI();
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import Config from "../config.js";
|
|
4
|
+
|
|
5
|
+
const __dirname = Config.dirname();
|
|
6
|
+
|
|
7
|
+
class RouteCLI {
|
|
8
|
+
/**
|
|
9
|
+
* Create route
|
|
10
|
+
*/
|
|
11
|
+
create() {
|
|
12
|
+
let nameRoute = process.argv[2];
|
|
13
|
+
if (!nameRoute) {
|
|
14
|
+
console.log("Please provide a route name. Usage: npm run route <route-name>");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
nameRoute =nameRoute.charAt(0).toUpperCase() + nameRoute.slice(1);
|
|
18
|
+
const folder= path.join(__dirname, 'backend/routes');
|
|
19
|
+
if (!fs.existsSync(folder)){
|
|
20
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
21
|
+
}
|
|
22
|
+
const filePath = path.join(folder, `${nameRoute}.js`);
|
|
23
|
+
if (fs.existsSync(filePath)) {
|
|
24
|
+
console.log(`Route ${nameRoute} already exists.`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const content =`import Router from "@seip/blue-bird/core/router.js"
|
|
28
|
+
|
|
29
|
+
const router${nameRoute} = new Router("/${nameRoute.toLowerCase()}");
|
|
30
|
+
|
|
31
|
+
router${nameRoute}.get("/", (req, res) => {
|
|
32
|
+
res.json({ message: "Hello from ${nameRoute} route!" });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export default router${nameRoute};
|
|
36
|
+
`;
|
|
37
|
+
fs.writeFileSync(filePath, content);
|
|
38
|
+
console.log(`Route ${nameRoute} created successfully at ${filePath}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const routeCLI = new RouteCLI();
|
|
43
43
|
routeCLI.create()
|
package/core/config.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Configuration class to manage application-wide settings and environment variables.
|
|
5
|
-
*/
|
|
6
|
-
class Config {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Returns the base directory of the application.
|
|
10
|
-
* @returns {string} The current working directory.
|
|
11
|
-
*/
|
|
12
|
-
static dirname() {
|
|
13
|
-
return process.cwd()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Retrieves application properties from environment variables or default values.
|
|
18
|
-
* @returns {Object} The configuration properties object.
|
|
19
|
-
*/
|
|
20
|
-
static props() {
|
|
21
|
-
const config = {
|
|
22
|
-
debug: process.env.DEBUG === "true" ? true : false,
|
|
23
|
-
descriptionMeta: process.env.DESCRIPTION_META || "",
|
|
24
|
-
keywordsMeta: process.env.KEYWORDS_META || "",
|
|
25
|
-
titleMeta: process.env.TITLE_META || "",
|
|
26
|
-
authorMeta: process.env.AUTHOR_META || "",
|
|
27
|
-
description: process.env.DESCRIPTION || "",
|
|
28
|
-
title: process.env.TITLE || "",
|
|
29
|
-
version: process.env.VERSION || "1.0.0",
|
|
30
|
-
langMeta: process.env.LANGMETA || "en",
|
|
31
|
-
host: process.env.HOST || "http://localhost",
|
|
32
|
-
port: parseInt(process.env.PORT) || 3001,
|
|
33
|
-
static: {
|
|
34
|
-
path: process.env.STATIC_PATH || "frontend/public",
|
|
35
|
-
options: {}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
return config
|
|
40
|
-
}
|
|
41
|
-
}
|
|
1
|
+
import path from "path";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration class to manage application-wide settings and environment variables.
|
|
5
|
+
*/
|
|
6
|
+
class Config {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns the base directory of the application.
|
|
10
|
+
* @returns {string} The current working directory.
|
|
11
|
+
*/
|
|
12
|
+
static dirname() {
|
|
13
|
+
return process.cwd()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves application properties from environment variables or default values.
|
|
18
|
+
* @returns {Object} The configuration properties object.
|
|
19
|
+
*/
|
|
20
|
+
static props() {
|
|
21
|
+
const config = {
|
|
22
|
+
debug: process.env.DEBUG === "true" ? true : false,
|
|
23
|
+
descriptionMeta: process.env.DESCRIPTION_META || "",
|
|
24
|
+
keywordsMeta: process.env.KEYWORDS_META || "",
|
|
25
|
+
titleMeta: process.env.TITLE_META || "",
|
|
26
|
+
authorMeta: process.env.AUTHOR_META || "",
|
|
27
|
+
description: process.env.DESCRIPTION || "",
|
|
28
|
+
title: process.env.TITLE || "",
|
|
29
|
+
version: process.env.VERSION || "1.0.0",
|
|
30
|
+
langMeta: process.env.LANGMETA || "en",
|
|
31
|
+
host: process.env.HOST || "http://localhost",
|
|
32
|
+
port: parseInt(process.env.PORT) || 3001,
|
|
33
|
+
static: {
|
|
34
|
+
path: process.env.STATIC_PATH || "frontend/public",
|
|
35
|
+
options: {}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
return config
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
42
|
export default Config
|
package/core/logger.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
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
|
-
if (!fs.existsSync(this.folder)) {
|
|
18
|
-
fs.mkdirSync(this.folder, { recursive: true });
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Ensures and returns the path to the log folder for the current day.
|
|
24
|
-
* @returns {string} The absolute path to the current day's log folder.
|
|
25
|
-
*/
|
|
26
|
-
nowFolder() {
|
|
27
|
-
const folder = path.join(this.folder, this.now());
|
|
28
|
-
|
|
29
|
-
if (!fs.existsSync(folder)) {
|
|
30
|
-
fs.mkdirSync(folder, { recursive: true });
|
|
31
|
-
}
|
|
32
|
-
return folder;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Gets the current date formatted as YYYY-MM-DD.
|
|
37
|
-
* @returns {string} The formatted date string.
|
|
38
|
-
*/
|
|
39
|
-
now() {
|
|
40
|
-
return new Date().toISOString().split("T")[0];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Appends an informational message to the info.log file.
|
|
45
|
-
* @param {string} message - The message to log.
|
|
46
|
-
*/
|
|
47
|
-
info(message) {
|
|
48
|
-
const logFile = path.join(this.nowFolder(), 'info.log');
|
|
49
|
-
fs.appendFileSync(logFile, `${message}\n`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Appends an error message to the error.log file.
|
|
54
|
-
* @param {string} message - The error message to log.
|
|
55
|
-
*/
|
|
56
|
-
error(message) {
|
|
57
|
-
const logFile = path.join(this.nowFolder(), 'error.log');
|
|
58
|
-
fs.appendFileSync(logFile, `${message}\n`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Appends a warning message to the warn.log file.
|
|
63
|
-
* @param {string} message - The warning message to log.
|
|
64
|
-
*/
|
|
65
|
-
warning(message) {
|
|
66
|
-
const logFile = path.join(this.nowFolder(), 'warn.log');
|
|
67
|
-
fs.appendFileSync(logFile, `${message}\n`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Appends a debug message to the debug.log file.
|
|
72
|
-
* @param {string} message - The debug message to log.
|
|
73
|
-
*/
|
|
74
|
-
debug(message) {
|
|
75
|
-
const logFile = path.join(this.nowFolder(), 'debug.log');
|
|
76
|
-
fs.appendFileSync(logFile, `${message}\n`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export default Logger;
|
|
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
|
+
if (!fs.existsSync(this.folder)) {
|
|
18
|
+
fs.mkdirSync(this.folder, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Ensures and returns the path to the log folder for the current day.
|
|
24
|
+
* @returns {string} The absolute path to the current day's log folder.
|
|
25
|
+
*/
|
|
26
|
+
nowFolder() {
|
|
27
|
+
const folder = path.join(this.folder, this.now());
|
|
28
|
+
|
|
29
|
+
if (!fs.existsSync(folder)) {
|
|
30
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
31
|
+
}
|
|
32
|
+
return folder;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gets the current date formatted as YYYY-MM-DD.
|
|
37
|
+
* @returns {string} The formatted date string.
|
|
38
|
+
*/
|
|
39
|
+
now() {
|
|
40
|
+
return new Date().toISOString().split("T")[0];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Appends an informational message to the info.log file.
|
|
45
|
+
* @param {string} message - The message to log.
|
|
46
|
+
*/
|
|
47
|
+
info(message) {
|
|
48
|
+
const logFile = path.join(this.nowFolder(), 'info.log');
|
|
49
|
+
fs.appendFileSync(logFile, `${message}\n`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Appends an error message to the error.log file.
|
|
54
|
+
* @param {string} message - The error message to log.
|
|
55
|
+
*/
|
|
56
|
+
error(message) {
|
|
57
|
+
const logFile = path.join(this.nowFolder(), 'error.log');
|
|
58
|
+
fs.appendFileSync(logFile, `${message}\n`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Appends a warning message to the warn.log file.
|
|
63
|
+
* @param {string} message - The warning message to log.
|
|
64
|
+
*/
|
|
65
|
+
warning(message) {
|
|
66
|
+
const logFile = path.join(this.nowFolder(), 'warn.log');
|
|
67
|
+
fs.appendFileSync(logFile, `${message}\n`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Appends a debug message to the debug.log file.
|
|
72
|
+
* @param {string} message - The debug message to log.
|
|
73
|
+
*/
|
|
74
|
+
debug(message) {
|
|
75
|
+
const logFile = path.join(this.nowFolder(), 'debug.log');
|
|
76
|
+
fs.appendFileSync(logFile, `${message}\n`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export default Logger;
|
package/core/middleware.js
CHANGED
|
@@ -1,27 +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;
|
|
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
CHANGED
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import Config from "./config.js";
|
|
3
|
-
|
|
4
|
-
const __dirname = Config.dirname()
|
|
5
|
-
const props = Config.props()
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Router wrapper class for handling Express routing logic.
|
|
9
|
-
*/
|
|
10
|
-
class Router {
|
|
11
|
-
/**
|
|
12
|
-
* Creates a new Router instance.
|
|
13
|
-
* @param {string} [path="/"] - The base path for this router.
|
|
14
|
-
* @example
|
|
15
|
-
* const router = new Router("/api")
|
|
16
|
-
* router.get("/", (req, res) => {
|
|
17
|
-
* res.json({ message: "Hello World!" })
|
|
18
|
-
* })
|
|
19
|
-
*/
|
|
20
|
-
constructor(path = "/") {
|
|
21
|
-
this.router = express.Router()
|
|
22
|
-
this.path = path
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Registers a GET route handler.
|
|
27
|
-
* @param {string} path - The relative path for the GET route.
|
|
28
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
29
|
-
* @example
|
|
30
|
-
* router.get("/users", (req, res) => {
|
|
31
|
-
* const users = [
|
|
32
|
-
* {
|
|
33
|
-
* name: "John Doe",
|
|
34
|
-
* email: "john.doe@example.com",
|
|
35
|
-
* },
|
|
36
|
-
* {
|
|
37
|
-
* name: "Jane Doe2",
|
|
38
|
-
* email: "jane.doe2@example.com",
|
|
39
|
-
* },
|
|
40
|
-
* ]
|
|
41
|
-
* res.json(users)
|
|
42
|
-
* })
|
|
43
|
-
*/
|
|
44
|
-
get(path, ...callback) {
|
|
45
|
-
if (path === "/*" || path === "*") {
|
|
46
|
-
path = /.*/
|
|
47
|
-
}
|
|
48
|
-
this.router.get(path, callback)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Registers a POST route handler.
|
|
53
|
-
* @param {string} path - The relative path for the POST route.
|
|
54
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
55
|
-
* @example
|
|
56
|
-
* router.post("/users", (req, res) => {
|
|
57
|
-
* return res.json({ message: "User created successfully" })
|
|
58
|
-
* })
|
|
59
|
-
*/
|
|
60
|
-
post(path, ...callback) {
|
|
61
|
-
if (path === "/*" || path === "*") {
|
|
62
|
-
path = /.*/
|
|
63
|
-
}
|
|
64
|
-
this.router.post(path, callback)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Registers a PUT route handler.
|
|
69
|
-
* @param {string} path - The relative path for the PUT route.
|
|
70
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
71
|
-
* @example
|
|
72
|
-
* router.put("/users", (req, res) => {
|
|
73
|
-
* return res.json({ message: "User updated successfully" })
|
|
74
|
-
* })
|
|
75
|
-
*/
|
|
76
|
-
put(path, ...callback) {
|
|
77
|
-
this.router.put(path, callback)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Registers a DELETE route handler.
|
|
82
|
-
* @param {string} path - The relative path for the DELETE route.
|
|
83
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
84
|
-
* @example
|
|
85
|
-
* router.delete("/users", (req, res) => {
|
|
86
|
-
* return res.json({ message: "User deleted successfully" })
|
|
87
|
-
* })
|
|
88
|
-
*/
|
|
89
|
-
delete(path, ...callback) {
|
|
90
|
-
this.router.delete(path, callback)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Registers a PATCH route handler.
|
|
95
|
-
* @param {string} path - The relative path for the PATCH route.
|
|
96
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
97
|
-
* @example
|
|
98
|
-
* router.patch("/users", (req, res) => {
|
|
99
|
-
* return res.json({ message: "User patched successfully" })
|
|
100
|
-
* })
|
|
101
|
-
*/
|
|
102
|
-
patch(path, ...callback) {
|
|
103
|
-
this.router.patch(path, callback)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Registers an OPTIONS route handler.
|
|
108
|
-
* @param {string} path - The relative path for the OPTIONS route.
|
|
109
|
-
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
110
|
-
* @example
|
|
111
|
-
* router.options("/users", (req, res) => {
|
|
112
|
-
* return res.json({ message: "User options successfully" })
|
|
113
|
-
* })
|
|
114
|
-
*/
|
|
115
|
-
options(path, ...callback) {
|
|
116
|
-
this.router.options(path, callback)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Returns the underlying Express router instance.
|
|
121
|
-
* @returns {import('express').Router} The Express router object.
|
|
122
|
-
*/
|
|
123
|
-
getRouter() {
|
|
124
|
-
return this.router
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns the base path associated with this router.
|
|
129
|
-
* @returns {string} The router path.
|
|
130
|
-
*/
|
|
131
|
-
getPath() {
|
|
132
|
-
return this.path
|
|
133
|
-
}
|
|
134
|
-
}
|
|
1
|
+
import express from "express";
|
|
2
|
+
import Config from "./config.js";
|
|
3
|
+
|
|
4
|
+
const __dirname = Config.dirname()
|
|
5
|
+
const props = Config.props()
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Router wrapper class for handling Express routing logic.
|
|
9
|
+
*/
|
|
10
|
+
class Router {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new Router instance.
|
|
13
|
+
* @param {string} [path="/"] - The base path for this router.
|
|
14
|
+
* @example
|
|
15
|
+
* const router = new Router("/api")
|
|
16
|
+
* router.get("/", (req, res) => {
|
|
17
|
+
* res.json({ message: "Hello World!" })
|
|
18
|
+
* })
|
|
19
|
+
*/
|
|
20
|
+
constructor(path = "/") {
|
|
21
|
+
this.router = express.Router()
|
|
22
|
+
this.path = path
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Registers a GET route handler.
|
|
27
|
+
* @param {string} path - The relative path for the GET route.
|
|
28
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
29
|
+
* @example
|
|
30
|
+
* router.get("/users", (req, res) => {
|
|
31
|
+
* const users = [
|
|
32
|
+
* {
|
|
33
|
+
* name: "John Doe",
|
|
34
|
+
* email: "john.doe@example.com",
|
|
35
|
+
* },
|
|
36
|
+
* {
|
|
37
|
+
* name: "Jane Doe2",
|
|
38
|
+
* email: "jane.doe2@example.com",
|
|
39
|
+
* },
|
|
40
|
+
* ]
|
|
41
|
+
* res.json(users)
|
|
42
|
+
* })
|
|
43
|
+
*/
|
|
44
|
+
get(path, ...callback) {
|
|
45
|
+
if (path === "/*" || path === "*") {
|
|
46
|
+
path = /.*/
|
|
47
|
+
}
|
|
48
|
+
this.router.get(path, callback)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Registers a POST route handler.
|
|
53
|
+
* @param {string} path - The relative path for the POST route.
|
|
54
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
55
|
+
* @example
|
|
56
|
+
* router.post("/users", (req, res) => {
|
|
57
|
+
* return res.json({ message: "User created successfully" })
|
|
58
|
+
* })
|
|
59
|
+
*/
|
|
60
|
+
post(path, ...callback) {
|
|
61
|
+
if (path === "/*" || path === "*") {
|
|
62
|
+
path = /.*/
|
|
63
|
+
}
|
|
64
|
+
this.router.post(path, callback)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Registers a PUT route handler.
|
|
69
|
+
* @param {string} path - The relative path for the PUT route.
|
|
70
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
71
|
+
* @example
|
|
72
|
+
* router.put("/users", (req, res) => {
|
|
73
|
+
* return res.json({ message: "User updated successfully" })
|
|
74
|
+
* })
|
|
75
|
+
*/
|
|
76
|
+
put(path, ...callback) {
|
|
77
|
+
this.router.put(path, callback)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Registers a DELETE route handler.
|
|
82
|
+
* @param {string} path - The relative path for the DELETE route.
|
|
83
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
84
|
+
* @example
|
|
85
|
+
* router.delete("/users", (req, res) => {
|
|
86
|
+
* return res.json({ message: "User deleted successfully" })
|
|
87
|
+
* })
|
|
88
|
+
*/
|
|
89
|
+
delete(path, ...callback) {
|
|
90
|
+
this.router.delete(path, callback)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Registers a PATCH route handler.
|
|
95
|
+
* @param {string} path - The relative path for the PATCH route.
|
|
96
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
97
|
+
* @example
|
|
98
|
+
* router.patch("/users", (req, res) => {
|
|
99
|
+
* return res.json({ message: "User patched successfully" })
|
|
100
|
+
* })
|
|
101
|
+
*/
|
|
102
|
+
patch(path, ...callback) {
|
|
103
|
+
this.router.patch(path, callback)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Registers an OPTIONS route handler.
|
|
108
|
+
* @param {string} path - The relative path for the OPTIONS route.
|
|
109
|
+
* @param {...Function} callback - One or more handler functions (middlewares and controller).
|
|
110
|
+
* @example
|
|
111
|
+
* router.options("/users", (req, res) => {
|
|
112
|
+
* return res.json({ message: "User options successfully" })
|
|
113
|
+
* })
|
|
114
|
+
*/
|
|
115
|
+
options(path, ...callback) {
|
|
116
|
+
this.router.options(path, callback)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the underlying Express router instance.
|
|
121
|
+
* @returns {import('express').Router} The Express router object.
|
|
122
|
+
*/
|
|
123
|
+
getRouter() {
|
|
124
|
+
return this.router
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns the base path associated with this router.
|
|
129
|
+
* @returns {string} The router path.
|
|
130
|
+
*/
|
|
131
|
+
getPath() {
|
|
132
|
+
return this.path
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
135
|
export default Router;
|