@erwininteractive/mvc 0.3.0 → 0.3.6

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.
@@ -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
+ }
@@ -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
+ }
@@ -111,10 +111,10 @@ function setupDatabase(targetDir) {
111
111
  if (fs_1.default.existsSync(frameworkPrismaSchema)) {
112
112
  fs_1.default.copyFileSync(frameworkPrismaSchema, path_1.default.join(prismaDir, "schema.prisma"));
113
113
  }
114
- // Install Prisma
114
+ // Install Prisma (pin to v6 to avoid breaking changes in v7)
115
115
  console.log("\nSetting up database...");
116
116
  try {
117
- (0, child_process_1.execSync)("npm install @prisma/client prisma", { cwd: targetDir, stdio: "inherit" });
117
+ (0, child_process_1.execSync)("npm install @prisma/client@^6.0.0 prisma@^6.0.0", { cwd: targetDir, stdio: "inherit" });
118
118
  (0, child_process_1.execSync)("npx prisma generate", { cwd: targetDir, stdio: "inherit" });
119
119
  }
120
120
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@erwininteractive/mvc",
3
- "version": "0.3.0",
3
+ "version": "0.3.6",
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/mvc.git"
35
+ "url": "https://github.com/erwininteractive/typescript-erwinmvc.git"
36
36
  },
37
37
  "engines": {
38
38
  "node": ">=20.0.0"
@@ -60,7 +60,7 @@
60
60
  "@types/express-session": "^1.18.0",
61
61
  "@types/jest": "^29.5.13",
62
62
  "@types/jsonwebtoken": "^9.0.7",
63
- "@types/node": "^22.7.5",
63
+ "@types/node": "^22.19.15",
64
64
  "@types/supertest": "^6.0.2",
65
65
  "esbuild": "^0.24.0",
66
66
  "jest": "^29.7.0",
@@ -1,19 +1,19 @@
1
1
  generator client {
2
- provider = "prisma-client-js"
2
+ provider = "prisma-client-js"
3
3
  }
4
4
 
5
5
  datasource db {
6
- provider = "postgresql"
7
- url = env("DATABASE_URL")
6
+ provider = "postgresql"
7
+ url = env("DATABASE_URL")
8
8
  }
9
9
 
10
10
  model User {
11
- id Int @id @default(autoincrement())
12
- email String @unique
13
- hashedPassword String
14
- role String @default("user")
15
- createdAt DateTime @default(now())
16
- updatedAt DateTime @updatedAt
11
+ id Int @id @default(autoincrement())
12
+ email String @unique
13
+ hashedPassword String
14
+ role String @default("user")
15
+ createdAt DateTime @default(now())
16
+ updatedAt DateTime @updatedAt
17
17
 
18
- @@map("users")
18
+ @@map("users")
19
19
  }