@erwininteractive/mvc 0.8.1 → 0.8.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/README.md +276 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,15 +4,21 @@ A lightweight, full-featured MVC framework for Node.js 20+ built with TypeScript
|
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Express** - Fast,
|
|
8
|
-
- **EJS
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
7
|
+
- **Express** - Fast, unopinionated web framework for routing and middleware
|
|
8
|
+
- **EJS Templating** - Server-side templating with embedded JavaScript
|
|
9
|
+
- **Prisma ORM** - Type-safe database client (optional, PostgreSQL/MySQL/SQLite support)
|
|
10
|
+
- **Redis Sessions** - Scalable session management with connect-redis
|
|
11
11
|
- **JWT Authentication** - Secure token-based auth with bcrypt password hashing
|
|
12
|
+
- **Session-Based Auth** - Traditional session management with Express-Session
|
|
12
13
|
- **WebAuthn (Passkeys)** - Passwordless authentication with security keys (YubiKey, Touch ID, Face ID)
|
|
13
14
|
- **Tailwind CSS** - Modern, utility-first CSS framework (optional, included with `--with-tailwind`)
|
|
14
15
|
- **Zod Validation** - Type-safe form validation with TypeScript-first schemas
|
|
15
|
-
- **CLI Tools** - Scaffold apps with `--with-tailwind`, generate models/controllers/
|
|
16
|
+
- **CLI Tools** - Scaffold apps with `--with-tailwind`, generate models/controllers/resources
|
|
17
|
+
- **Flash Messages** - Session-based success/error messages for forms
|
|
18
|
+
- **Cookie Parser** - Built-in cookie parsing for JWT and session support
|
|
19
|
+
- **Helmet Security** - HTTP header security middleware
|
|
20
|
+
- **CORS Support** - Cross-origin resource sharing middleware
|
|
21
|
+
- **GitHub Actions CI** - Automated testing with optional CI setup
|
|
16
22
|
|
|
17
23
|
## Quick Start
|
|
18
24
|
|
|
@@ -24,4 +30,268 @@ npm run dev
|
|
|
24
30
|
|
|
25
31
|
Visit http://localhost:3000 - your app is running!
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
## CLI Commands
|
|
34
|
+
|
|
35
|
+
### Initialize a new app
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Basic app
|
|
39
|
+
npx @erwininteractive/mvc init myapp
|
|
40
|
+
|
|
41
|
+
# With Tailwind CSS
|
|
42
|
+
npx @erwininteractive/mvc init myapp --with-tailwind
|
|
43
|
+
|
|
44
|
+
# With database support (Prisma)
|
|
45
|
+
npx @erwininteractive/mvc init myapp --with-database
|
|
46
|
+
|
|
47
|
+
# With database + Tailwind
|
|
48
|
+
npx @erwininteractive/mvc init myapp --with-database --with-tailwind
|
|
49
|
+
|
|
50
|
+
# With GitHub Actions CI
|
|
51
|
+
npx @erwininteractive/mvc init myapp --with-ci
|
|
52
|
+
|
|
53
|
+
# Skip npm install (install manually later)
|
|
54
|
+
npx @erwininteractive/mvc init myapp --skip-install
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Generate models
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Generate a Prisma model
|
|
61
|
+
npx erwinmvc generate model User
|
|
62
|
+
npx erwinmvc g model User
|
|
63
|
+
|
|
64
|
+
# Generate without running migration
|
|
65
|
+
npx erwinmvc generate model User --skip-migrate
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Generate controllers
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# Generate a CRUD controller
|
|
72
|
+
npx erwinmvc generate controller User
|
|
73
|
+
npx erwinmvc g controller User
|
|
74
|
+
|
|
75
|
+
# Generate without views
|
|
76
|
+
npx erwinmvc generate controller User --no-views
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Generate resources (model + controller + views)
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Complete resource with all features
|
|
83
|
+
npx erwinmvc generate resource Post
|
|
84
|
+
npx erwinmvc g resource Post
|
|
85
|
+
|
|
86
|
+
# Skip specific parts
|
|
87
|
+
npx erwinmvc generate resource Post --skip-model
|
|
88
|
+
npx erwinmvc generate resource Post --skip-views
|
|
89
|
+
npx erwinmvc generate resource Post --skip-controller
|
|
90
|
+
|
|
91
|
+
# Skip database migration
|
|
92
|
+
npx erwinmvc generate resource Post --skip-migrate
|
|
93
|
+
|
|
94
|
+
# API-only resource (no views, JSON responses)
|
|
95
|
+
npx erwinmvc generate resource Post --api-only
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Authentication commands
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Generate complete authentication system (login/register)
|
|
102
|
+
npx erwinmvc make:auth
|
|
103
|
+
npx erwinmvc ma
|
|
104
|
+
|
|
105
|
+
# Skip User model (use existing)
|
|
106
|
+
npx erwinmvc make:auth --without-model
|
|
107
|
+
|
|
108
|
+
# Only JWT (no sessions)
|
|
109
|
+
npx erwinmvc make:auth --jwt-only
|
|
110
|
+
|
|
111
|
+
# Only sessions (no JWT)
|
|
112
|
+
npx erwinmvc make:auth --session-only
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### WebAuthn (Passkeys)
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Generate WebAuthn authentication (security key login)
|
|
119
|
+
npx erwinmvc webauthn
|
|
120
|
+
npx erwinmvc w
|
|
121
|
+
|
|
122
|
+
# Skip database migration
|
|
123
|
+
npx erwinmvc webauthn --skip-migrate
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### List routes
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
# Show all defined routes
|
|
130
|
+
npx erwinmvc list:routes
|
|
131
|
+
npx erwinmvc lr
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Project Structure
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
myapp/
|
|
138
|
+
├── src/
|
|
139
|
+
│ ├── controllers/ # MVC controllers
|
|
140
|
+
│ ├── views/ # EJS templates
|
|
141
|
+
│ └── server.ts # App entry point
|
|
142
|
+
├── public/ # Static assets
|
|
143
|
+
│ └── dist/ # Compiled CSS (Tailwind)
|
|
144
|
+
├── prisma/ # Database (optional)
|
|
145
|
+
│ └── schema.prisma # Prisma schema
|
|
146
|
+
├── .github/ # CI workflows (optional)
|
|
147
|
+
├── package.json
|
|
148
|
+
├── tsconfig.json
|
|
149
|
+
├── tailwind.config.ts # Tailwind config (optional)
|
|
150
|
+
└── postcss.config.cjs # PostCSS config (optional)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Database Setup
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
# Setup Prisma database
|
|
157
|
+
npx erwinmvc init myapp --with-database
|
|
158
|
+
cd myapp
|
|
159
|
+
|
|
160
|
+
# Edit .env with your DATABASE_URL
|
|
161
|
+
# DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
|
|
162
|
+
|
|
163
|
+
# Run migrations
|
|
164
|
+
npx prisma migrate dev --name init
|
|
165
|
+
|
|
166
|
+
# Generate Prisma client (auto-run by CLI)
|
|
167
|
+
npx prisma generate
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Tailwind CSS Setup
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npx erwinmvc init myapp --with-tailwind
|
|
174
|
+
cd myapp
|
|
175
|
+
|
|
176
|
+
# Configure content paths in tailwind.config.ts
|
|
177
|
+
# Edit src/assets/tailwind.css for custom styles
|
|
178
|
+
|
|
179
|
+
# Build CSS
|
|
180
|
+
npx tailwindcss -i ./src/assets/tailwind.css -o ./public/dist/tailwind.css --watch
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Authentication
|
|
184
|
+
|
|
185
|
+
### Session + JWT Auth
|
|
186
|
+
|
|
187
|
+
The `make:auth` command generates:
|
|
188
|
+
|
|
189
|
+
- User model with password hashing
|
|
190
|
+
- Register/login forms with Zod validation
|
|
191
|
+
- Session-based authentication
|
|
192
|
+
- JWT token generation for API access
|
|
193
|
+
- Password verification with bcrypt
|
|
194
|
+
- Flash messages for errors/success
|
|
195
|
+
|
|
196
|
+
### WebAuthn (Passkeys)
|
|
197
|
+
|
|
198
|
+
The `webauthn` command generates:
|
|
199
|
+
|
|
200
|
+
- Passkey registration flow
|
|
201
|
+
- Passkey authentication flow
|
|
202
|
+
- User credential storage in database
|
|
203
|
+
- Security key support (YubiKey, Touch ID, Face ID)
|
|
204
|
+
|
|
205
|
+
## Validation
|
|
206
|
+
|
|
207
|
+
Use Zod schemas for type-safe form validation:
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { z } from "zod";
|
|
211
|
+
import { validate } from "@erwininteractive/mvc";
|
|
212
|
+
|
|
213
|
+
const userSchema = z.object({
|
|
214
|
+
username: z.string().min(3),
|
|
215
|
+
email: z.string().email(),
|
|
216
|
+
password: z.string().min(8)
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
app.post("/users", validate(userSchema), async (req, res) => {
|
|
220
|
+
const user = req.validatedBody; // Type-safe validated data
|
|
221
|
+
// ...
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## API Reference
|
|
226
|
+
|
|
227
|
+
### Core Functions
|
|
228
|
+
|
|
229
|
+
- `createMvcApp(options)` - Create Express app with views, static files
|
|
230
|
+
- `startServer(app)` - Start HTTP server on port 3000
|
|
231
|
+
- `hashPassword(plain)` - Hash password with bcrypt
|
|
232
|
+
- `verifyPassword(plain, hash)` - Verify password
|
|
233
|
+
- `signToken(payload, expiresIn)` - Sign JWT token
|
|
234
|
+
- `verifyToken(token)` - Verify and decode JWT
|
|
235
|
+
- `authenticate` - Express middleware for JWT authentication
|
|
236
|
+
- `validate(schema, strategy)` - Zod validation middleware
|
|
237
|
+
|
|
238
|
+
### WebAuthn Functions
|
|
239
|
+
|
|
240
|
+
- `startRegistration(req, res)` - Begin WebAuthn registration
|
|
241
|
+
- `completeRegistration(req, res)` - Complete WebAuthn registration
|
|
242
|
+
- `startAuthentication(req, res)` - Begin WebAuthn login
|
|
243
|
+
- `completeAuthentication(req, res)` - Complete WebAuthn login
|
|
244
|
+
- `getRPConfig()` - Get relying party configuration
|
|
245
|
+
|
|
246
|
+
### Validation Helpers
|
|
247
|
+
|
|
248
|
+
- `getFieldErrors(error)` - Extract field errors from Zod error
|
|
249
|
+
- `getOldInput(req)` - Get previously submitted form data
|
|
250
|
+
- `getErrors(req)` - Get flash error messages
|
|
251
|
+
- `hasFieldError(field, errors)` - Check if field has errors
|
|
252
|
+
- `getFieldError(field, errors)` - Get error message for field
|
|
253
|
+
|
|
254
|
+
## Environment Variables
|
|
255
|
+
|
|
256
|
+
Required for full functionality:
|
|
257
|
+
|
|
258
|
+
```env
|
|
259
|
+
# Required
|
|
260
|
+
JWT_SECRET=your-secret-key-here
|
|
261
|
+
|
|
262
|
+
# Database (optional)
|
|
263
|
+
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
|
|
264
|
+
|
|
265
|
+
# WebAuthn (optional, defaults to localhost)
|
|
266
|
+
WEBAUTHN_RP_ID=localhost
|
|
267
|
+
WEBAUTHN_RP_NAME=MyApp
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Testing
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
# Run all tests
|
|
274
|
+
npm test
|
|
275
|
+
|
|
276
|
+
# Run specific test file
|
|
277
|
+
npm test -- auth
|
|
278
|
+
npm test -- cli
|
|
279
|
+
npm test -- generators
|
|
280
|
+
|
|
281
|
+
# With coverage
|
|
282
|
+
npm run test -- --coverage
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Production Build
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
# Build TypeScript and CLI
|
|
289
|
+
npm run build
|
|
290
|
+
|
|
291
|
+
# The app is ready for production deployment
|
|
292
|
+
# Server runs on port 3000
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
MIT © [Erwin Interactive](https://github.com/erwininteractive)
|
package/package.json
CHANGED