@erwininteractive/mvc 0.3.1 → 0.3.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/dist/cli.d.ts +1 -0
- package/dist/framework/App.d.ts +18 -1
- package/dist/framework/App.js +34 -0
- package/package.json +2 -2
package/dist/cli.d.ts
CHANGED
package/dist/framework/App.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Express } from "express";
|
|
1
|
+
import { Express, Response } from "express";
|
|
2
2
|
import { RedisClientType } from "redis";
|
|
3
3
|
import helmet from "helmet";
|
|
4
4
|
import cors from "cors";
|
|
@@ -36,3 +36,20 @@ export declare function createMvcApp(options?: MvcAppOptions): Promise<MvcApp>;
|
|
|
36
36
|
* Start the Express server.
|
|
37
37
|
*/
|
|
38
38
|
export declare function startServer(app: Express, port?: number): void;
|
|
39
|
+
/**
|
|
40
|
+
* Helper for content negotiation - respond with EJS view or JSON based on Accept header.
|
|
41
|
+
* @param res - Express response object
|
|
42
|
+
* @param viewName - Name of the EJS view to render (without extension)
|
|
43
|
+
* @param data - Data to pass to the view or send as JSON
|
|
44
|
+
*
|
|
45
|
+
* If Accept header contains "text/html", renders the view.
|
|
46
|
+
* If Accept header contains "application/json", sends JSON response.
|
|
47
|
+
*/
|
|
48
|
+
export declare function respond(res: Response, viewName: string, data: Record<string, unknown>): Response;
|
|
49
|
+
declare global {
|
|
50
|
+
namespace Express {
|
|
51
|
+
interface Response {
|
|
52
|
+
respond: typeof respond;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/dist/framework/App.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createMvcApp = createMvcApp;
|
|
7
7
|
exports.startServer = startServer;
|
|
8
|
+
exports.respond = respond;
|
|
8
9
|
const express_1 = __importDefault(require("express"));
|
|
9
10
|
const express_session_1 = __importDefault(require("express-session"));
|
|
10
11
|
const redis_1 = require("redis");
|
|
@@ -73,6 +74,20 @@ async function createMvcApp(options = {}) {
|
|
|
73
74
|
// View engine
|
|
74
75
|
app.set("view engine", "ejs");
|
|
75
76
|
app.set("views", path_1.default.resolve(viewsPath));
|
|
77
|
+
// Add respond helper to response
|
|
78
|
+
app.use((req, res, next) => {
|
|
79
|
+
res.respond = function (viewName, data) {
|
|
80
|
+
const accept = req.headers.accept;
|
|
81
|
+
if (accept?.includes("application/json")) {
|
|
82
|
+
res.json(data);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
res.render(viewName, data);
|
|
86
|
+
}
|
|
87
|
+
return res;
|
|
88
|
+
};
|
|
89
|
+
next();
|
|
90
|
+
});
|
|
76
91
|
return { app, redisClient };
|
|
77
92
|
}
|
|
78
93
|
/**
|
|
@@ -98,3 +113,22 @@ function startServer(app, port = Number(process.env.PORT) || 3000) {
|
|
|
98
113
|
console.log(`Server listening on port ${port}`);
|
|
99
114
|
});
|
|
100
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Helper for content negotiation - respond with EJS view or JSON based on Accept header.
|
|
118
|
+
* @param res - Express response object
|
|
119
|
+
* @param viewName - Name of the EJS view to render (without extension)
|
|
120
|
+
* @param data - Data to pass to the view or send as JSON
|
|
121
|
+
*
|
|
122
|
+
* If Accept header contains "text/html", renders the view.
|
|
123
|
+
* If Accept header contains "application/json", sends JSON response.
|
|
124
|
+
*/
|
|
125
|
+
function respond(res, viewName, data) {
|
|
126
|
+
const accept = res.getHeader("Accept");
|
|
127
|
+
if (accept?.includes("application/json")) {
|
|
128
|
+
res.json(data);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
res.render(viewName, data);
|
|
132
|
+
}
|
|
133
|
+
return res;
|
|
134
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@erwininteractive/mvc",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "A lightweight, full-featured MVC framework for Node.js with Express, Prisma, and EJS",
|
|
5
5
|
"main": "dist/framework/index.js",
|
|
6
6
|
"types": "dist/framework/index.d.ts",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"repository": {
|
|
34
34
|
"type": "git",
|
|
35
|
-
"url": "https://github.com/erwininteractive/
|
|
35
|
+
"url": "https://github.com/erwininteractive/typescript-erwinmvc.git"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
38
|
"node": ">=20.0.0"
|