jcc-express-mvc 1.1.7 → 1.1.8
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/package.json +1 -1
- package/src/Router/Router.js +7 -2
- package/src/utils/index.js +33 -18
package/package.json
CHANGED
package/src/Router/Router.js
CHANGED
|
@@ -22,12 +22,13 @@ class Router {
|
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Sets middleware functions for the router.
|
|
25
|
-
* @param {Array} middlewares - Array of middleware functions.
|
|
25
|
+
* @param {Array || function} middlewares - Array of middleware functions.
|
|
26
26
|
* @returns {Router} - Returns the router instance.
|
|
27
27
|
*/
|
|
28
28
|
middleware(middlewares = [] || func) {
|
|
29
29
|
this.middlewares = middlewares;
|
|
30
|
-
|
|
30
|
+
// Sets group middleware using Util instance
|
|
31
|
+
this.util.setMiddleware(middlewares);
|
|
31
32
|
return this;
|
|
32
33
|
}
|
|
33
34
|
|
|
@@ -85,6 +86,10 @@ class Router {
|
|
|
85
86
|
this.util.routeHelper("delete", path, callback);
|
|
86
87
|
return this;
|
|
87
88
|
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Placeholder method for handling all HTTP methods.
|
|
92
|
+
*/
|
|
88
93
|
all() {}
|
|
89
94
|
}
|
|
90
95
|
|
package/src/utils/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
const { asyncHandler } = require("../helpers");
|
|
2
2
|
class Util {
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for handling route configuration.
|
|
5
|
+
* @param {string} basePath - The base path for routes.
|
|
6
|
+
* @param {object} app - The Express application object.
|
|
7
|
+
* @param {string} prefix - The route prefix.
|
|
8
|
+
* @param {function|array} middlewares - Route middlewares.
|
|
9
|
+
* @param {object} controller - The controller object.
|
|
10
|
+
*/
|
|
4
11
|
constructor(basePath, app, prefix, middlewares, controller) {
|
|
5
12
|
this.basePath = basePath;
|
|
6
13
|
this.app = app;
|
|
@@ -13,7 +20,7 @@ class Util {
|
|
|
13
20
|
* Sets the middlewares for the route group.
|
|
14
21
|
* @param {function|array} middlewares - Route middlewares.
|
|
15
22
|
*/
|
|
16
|
-
|
|
23
|
+
setMiddleware(middlewares) {
|
|
17
24
|
this.middlewares = middlewares;
|
|
18
25
|
}
|
|
19
26
|
|
|
@@ -35,7 +42,6 @@ class Util {
|
|
|
35
42
|
setGroupController(controller) {
|
|
36
43
|
this.controller = controller;
|
|
37
44
|
}
|
|
38
|
-
|
|
39
45
|
/**
|
|
40
46
|
* Checks if the controller is a class and callback is a string.
|
|
41
47
|
* @param {string} callback - The callback name.
|
|
@@ -82,22 +88,31 @@ class Util {
|
|
|
82
88
|
async routeHelper(httpMethod, path, callback) {
|
|
83
89
|
try {
|
|
84
90
|
if (this.classController(callback)) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.middlewares,
|
|
88
|
-
asyncHandler(this.controller[callback]),
|
|
89
|
-
])
|
|
90
|
-
: this.app[httpMethod](
|
|
91
|
-
this.resolvePath(path),
|
|
92
|
-
asyncHandler(this.controller[callback])
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
return this.isMiddleware()
|
|
96
|
-
? this.app[httpMethod](this.resolvePath(path), [
|
|
91
|
+
if (this.isMiddleware()) {
|
|
92
|
+
this.app[httpMethod](this.resolvePath(path), [
|
|
97
93
|
this.middlewares,
|
|
98
|
-
asyncHandler(callback),
|
|
99
|
-
])
|
|
100
|
-
|
|
94
|
+
asyncHandler(this.controller[callback]),
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
return (this.middlewares = null);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return this.app[httpMethod](
|
|
101
|
+
this.resolvePath(path),
|
|
102
|
+
asyncHandler(this.controller[callback])
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (this.isMiddleware()) {
|
|
106
|
+
this.app[httpMethod](this.resolvePath(path), [
|
|
107
|
+
this.middlewares,
|
|
108
|
+
asyncHandler(callback),
|
|
109
|
+
]);
|
|
110
|
+
return (this.middlewares = null);
|
|
111
|
+
}
|
|
112
|
+
return this.app[httpMethod](
|
|
113
|
+
this.resolvePath(path),
|
|
114
|
+
asyncHandler(callback)
|
|
115
|
+
);
|
|
101
116
|
} catch (error) {
|
|
102
117
|
throw new Error(error?.message);
|
|
103
118
|
}
|