kaelum 1.2.0 → 1.3.0
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/README.md +158 -61
- package/cli/create.js +71 -31
- package/cli/index.js +49 -7
- package/cli/templates/api/app.js +16 -2
- package/cli/templates/api/controllers/usersController.js +58 -0
- package/cli/templates/api/middlewares/authMock.js +13 -0
- package/cli/templates/api/package.json +14 -7
- package/cli/templates/api/routes.js +31 -8
- package/cli/templates/web/app.js +15 -4
- package/cli/templates/web/middlewares/logger.js +5 -5
- package/cli/templates/web/package.json +17 -7
- package/cli/templates/web/public/style.css +42 -35
- package/cli/templates/web/routes.js +22 -17
- package/cli/templates/web/views/index.html +39 -19
- package/cli/utils.js +50 -4
- package/core/addRoute.js +156 -5
- package/core/apiRoute.js +135 -0
- package/core/errorHandler.js +139 -0
- package/core/healthCheck.js +204 -0
- package/core/redirect.js +177 -0
- package/core/setConfig.js +245 -20
- package/core/setMiddleware.js +183 -6
- package/core/start.js +111 -4
- package/createApp.js +166 -13
- package/package.json +4 -3
- package/bin/.gitkeep +0 -0
- package/cli/templates/api/controllers/userController.js +0 -13
- package/cli/templates/api/middlewares/logger.js +0 -6
- package/utils/.gitkeep +0 -0
package/createApp.js
CHANGED
|
@@ -1,21 +1,174 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// createApp.js
|
|
2
|
+
// Kaelum factory: creates an Express app with Kaelum helpers
|
|
3
|
+
// - enables JSON and URL-encoded body parsing by default
|
|
4
|
+
// - stores references to parsers and static middleware so they can be replaced by setConfig
|
|
5
|
+
// - wraps core/setConfig to support toggles via setConfig({ ... })
|
|
6
|
+
// - exposes existing core helpers (start, addRoute, setMiddleware, apiRoute)
|
|
7
|
+
// - exposes error handler helper app.useErrorHandler()
|
|
8
|
+
|
|
9
|
+
const express = require("express");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
|
|
12
|
+
const start = require("./core/start");
|
|
13
|
+
const addRoute = require("./core/addRoute");
|
|
14
|
+
const apiRoute = require("./core/apiRoute");
|
|
15
|
+
const setMiddleware = require("./core/setMiddleware");
|
|
16
|
+
const coreSetConfig = require("./core/setConfig");
|
|
17
|
+
const { errorHandler } = require("./core/errorHandler");
|
|
18
|
+
const registerHealth = require("./core/healthCheck");
|
|
19
|
+
const redirect = require("./core/redirect");
|
|
6
20
|
|
|
7
21
|
function createApp() {
|
|
8
22
|
const app = express();
|
|
9
23
|
|
|
10
|
-
|
|
11
|
-
app.
|
|
12
|
-
app.
|
|
24
|
+
// ensure locals object and initial persisted config
|
|
25
|
+
app.locals = app.locals || {};
|
|
26
|
+
app.locals.kaelumConfig = app.locals.kaelumConfig || {};
|
|
27
|
+
// persist baseline config so app.get("kaelum:config") is always available
|
|
28
|
+
app.set("kaelum:config", app.locals.kaelumConfig);
|
|
29
|
+
|
|
30
|
+
// --- Default static middleware (store reference so setConfig can replace it) ---
|
|
31
|
+
const defaultStatic = express.static(path.join(process.cwd(), "public"));
|
|
32
|
+
app.locals._kaelum_static = defaultStatic;
|
|
33
|
+
app.use(defaultStatic);
|
|
34
|
+
|
|
35
|
+
// --- Body parsers (enabled by default) ---
|
|
36
|
+
const jsonParser = express.json();
|
|
37
|
+
const urlencodedParser = express.urlencoded({ extended: true });
|
|
38
|
+
|
|
39
|
+
// Keep references so we can remove them later if requested
|
|
40
|
+
app.locals._kaelum_bodyparsers = [jsonParser, urlencodedParser];
|
|
41
|
+
|
|
42
|
+
// Apply them by default
|
|
43
|
+
app.use(jsonParser);
|
|
44
|
+
app.use(urlencodedParser);
|
|
45
|
+
|
|
46
|
+
// --- wrapper for core.setConfig ---
|
|
47
|
+
app.setConfig = function (options = {}) {
|
|
48
|
+
// call core setConfig if available (it should persist merged config)
|
|
49
|
+
try {
|
|
50
|
+
const merged = coreSetConfig(app, options);
|
|
51
|
+
// ensure merged config is persisted locally as well
|
|
52
|
+
if (merged && typeof merged === "object") {
|
|
53
|
+
app.locals.kaelumConfig = merged;
|
|
54
|
+
app.set("kaelum:config", merged);
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
// fallback merge and persist locally
|
|
58
|
+
const prev = app.locals.kaelumConfig || {};
|
|
59
|
+
app.locals.kaelumConfig = Object.assign({}, prev, options);
|
|
60
|
+
app.set("kaelum:config", app.locals.kaelumConfig);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// read merged config
|
|
64
|
+
const cfg = app.get("kaelum:config") || app.locals.kaelumConfig || {};
|
|
65
|
+
|
|
66
|
+
// Body parser toggle handled by core.setConfig (which manipulates app._router.stack)
|
|
67
|
+
return cfg;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// convenience getter
|
|
71
|
+
app.getKaelumConfig = function () {
|
|
72
|
+
return app.get("kaelum:config") || app.locals.kaelumConfig || {};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// convenience wrapper to set static folder directly
|
|
76
|
+
app.static = function (dir) {
|
|
77
|
+
if (!dir) {
|
|
78
|
+
// if no dir passed, act as getter
|
|
79
|
+
return app.getKaelumConfig().static || null;
|
|
80
|
+
}
|
|
81
|
+
return app.setConfig({ static: dir });
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// convenience method to remove static middleware
|
|
85
|
+
app.removeStatic = function () {
|
|
86
|
+
return app.setConfig({ static: false });
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// ---------------------------
|
|
90
|
+
// middleware utility helpers
|
|
91
|
+
// ---------------------------
|
|
92
|
+
|
|
93
|
+
// remove middleware by matching the function reference from the express internal stack
|
|
94
|
+
function removeMiddlewareByFn(appInstance, fn) {
|
|
95
|
+
if (
|
|
96
|
+
!appInstance ||
|
|
97
|
+
!appInstance._router ||
|
|
98
|
+
!Array.isArray(appInstance._router.stack)
|
|
99
|
+
)
|
|
100
|
+
return;
|
|
101
|
+
appInstance._router.stack = appInstance._router.stack.filter(
|
|
102
|
+
(layer) => layer.handle !== fn
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---------------------------
|
|
107
|
+
// bind existing core helpers to the app
|
|
108
|
+
// ---------------------------
|
|
109
|
+
if (typeof start === "function") {
|
|
110
|
+
app.start = function (port, cb) {
|
|
111
|
+
return start(app, port, cb);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof addRoute === "function") {
|
|
116
|
+
app.addRoute = function (routePath, handlers) {
|
|
117
|
+
return addRoute(app, routePath, handlers);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (typeof apiRoute === "function") {
|
|
122
|
+
app.apiRoute = function (resource, handlers) {
|
|
123
|
+
return apiRoute(app, resource, handlers);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof setMiddleware === "function") {
|
|
128
|
+
app.setMiddleware = function (middlewareOrPath, maybeMiddleware) {
|
|
129
|
+
// forward call to core/setMiddleware - keep signature flexible
|
|
130
|
+
if (arguments.length === 2) {
|
|
131
|
+
return setMiddleware(app, middlewareOrPath, maybeMiddleware);
|
|
132
|
+
}
|
|
133
|
+
return setMiddleware(app, middlewareOrPath);
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (typeof registerHealth === "function") {
|
|
138
|
+
app.healthCheck = function (routePath = "/health") {
|
|
139
|
+
registerHealth(app, routePath);
|
|
140
|
+
return app;
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (typeof redirect === "function") {
|
|
145
|
+
app.redirect = function (from, to, status = 302) {
|
|
146
|
+
return redirect(app, from, to, status);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---------------------------
|
|
151
|
+
// Error handler exposure
|
|
152
|
+
// ---------------------------
|
|
153
|
+
// Expose a convenience method to register Kaelum's generic error handler.
|
|
154
|
+
// Accepts same options as errorHandler factory: { exposeStack: boolean }
|
|
155
|
+
app.useErrorHandler = function (options = {}) {
|
|
156
|
+
// If an error handler was previously registered by Kaelum, remove it first.
|
|
157
|
+
if (app.locals && app.locals._kaelum_errorhandler) {
|
|
158
|
+
removeMiddlewareByFn(app, app.locals._kaelum_errorhandler);
|
|
159
|
+
app.locals._kaelum_errorhandler = null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const mw = errorHandler(options);
|
|
163
|
+
// store reference and install as final middleware
|
|
164
|
+
if (!app.locals) app.locals = {};
|
|
165
|
+
app.locals._kaelum_errorhandler = mw;
|
|
166
|
+
app.use(mw);
|
|
167
|
+
return app;
|
|
168
|
+
};
|
|
13
169
|
|
|
14
|
-
//
|
|
15
|
-
app.
|
|
16
|
-
app.addRoute = (path, handlers) => addRoute(app, path, handlers);
|
|
17
|
-
app.setMiddleware = (middleware) => setMiddleware(app, middleware);
|
|
18
|
-
app.setConfig = (config) => setConfig(app, config);
|
|
170
|
+
// alias for convenience
|
|
171
|
+
app.errorHandler = app.useErrorHandler;
|
|
19
172
|
|
|
20
173
|
return app;
|
|
21
174
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kaelum",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A minimalist Node.js framework for building web pages and APIs with simplicity and speed.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"exports": {
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"express": "^4.18.2",
|
|
31
31
|
"fs-extra": "^11.3.0",
|
|
32
32
|
"helmet": "^7.2.0",
|
|
33
|
-
"inquirer": "^12.6.0"
|
|
33
|
+
"inquirer": "^12.6.0",
|
|
34
|
+
"morgan": "^1.10.1"
|
|
34
35
|
}
|
|
35
|
-
}
|
|
36
|
+
}
|
package/bin/.gitkeep
DELETED
|
File without changes
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
function getUsers(req, res) {
|
|
2
|
-
res.json([{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
function createUser(req, res) {
|
|
6
|
-
const newUser = req.body;
|
|
7
|
-
res.status(201).json({ message: "User created", user: newUser });
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
getUsers,
|
|
12
|
-
createUser
|
|
13
|
-
};
|
package/utils/.gitkeep
DELETED
|
File without changes
|