@fjell/express-router 4.4.56 → 4.4.57
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/util/general.js +1 -1
- package/dist/util/general.js.map +2 -2
- package/package.json +11 -11
- package/MIGRATION_v3.md +0 -255
- package/build.js +0 -4
- package/docs/docs.config.ts +0 -44
- package/docs/index.html +0 -18
- package/docs/package.json +0 -34
- package/docs/public/README.md +0 -332
- package/docs/public/examples-README.md +0 -339
- package/docs/public/fjell-icon.svg +0 -1
- package/docs/public/pano.png +0 -0
- package/docs/tsconfig.node.json +0 -6
- package/examples/README.md +0 -386
- package/examples/basic-router-example.ts +0 -391
- package/examples/full-application-example.ts +0 -768
- package/examples/nested-router-example.ts +0 -601
- package/examples/router-handlers-example.ts +0 -394
- package/examples/router-options-example.ts +0 -214
- package/vitest.config.ts +0 -39
package/examples/README.md
DELETED
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
# Fjell-Express-Router Examples
|
|
2
|
-
|
|
3
|
-
This directory contains examples demonstrating how to use fjell-express-router for building Express.js applications with automatic CRUD routing, hierarchical data management, and business logic integration with different patterns and complexity levels.
|
|
4
|
-
|
|
5
|
-
## Examples
|
|
6
|
-
|
|
7
|
-
### 1. `basic-router-example.ts` ⭐ **Start Here!**
|
|
8
|
-
**Perfect for beginners!** Demonstrates the fundamental way to use fjell-express-router for REST API development:
|
|
9
|
-
- **Basic PItemRouter usage** - Create routers for primary entities (Users, Tasks)
|
|
10
|
-
- **Automatic CRUD endpoints** - GET, POST, PUT, DELETE routes generated automatically
|
|
11
|
-
- **Mock data operations** - Simple in-memory storage with business logic
|
|
12
|
-
- **Express app integration** - Mount routers and add custom middleware
|
|
13
|
-
- **Custom business routes** - Dashboard and health check endpoints
|
|
14
|
-
|
|
15
|
-
Great for understanding the fundamentals of fjell-express-router for REST API development.
|
|
16
|
-
|
|
17
|
-
### 2. `nested-router-example.ts` 🏗️ **Hierarchical Data Management**
|
|
18
|
-
**Advanced hierarchical routing!** Demonstrates complex data structures with nested relationships:
|
|
19
|
-
- **Multiple router types**: PItemRouter for Organizations, CItemRouter for Departments and Employees
|
|
20
|
-
- **Nested route mounting**: `/organizations/:orgId/departments/:deptId/employees/:empId`
|
|
21
|
-
- **Hierarchical data models**: Organization → Department → Employee relationships
|
|
22
|
-
- **Location-based operations**: Complex queries spanning multiple organizational levels
|
|
23
|
-
- **Business analytics**: Organizational hierarchy summaries and statistics
|
|
24
|
-
|
|
25
|
-
Shows how fjell-express-router handles enterprise organizational data patterns with deep hierarchies.
|
|
26
|
-
|
|
27
|
-
### 3. `full-application-example.ts` 🚀 **Production-Ready Application**
|
|
28
|
-
**Complete enterprise application!** Demonstrates a realistic e-commerce system with advanced patterns:
|
|
29
|
-
- **Multiple interconnected entities**: Customer, Product, Order, OrderItem, Review
|
|
30
|
-
- **Advanced middleware**: Error handling, request logging, business validation
|
|
31
|
-
- **Complex business logic**: Product catalog, customer analytics, order management
|
|
32
|
-
- **Production patterns**: CORS headers, security middleware, proper error handling
|
|
33
|
-
- **Real-world scenarios**: Complete e-commerce platform with customer lifecycle management
|
|
34
|
-
|
|
35
|
-
Perfect for understanding how to build production-ready applications with fjell-express-router.
|
|
36
|
-
|
|
37
|
-
### 4. `router-handlers-example.ts` 🎯 **Router-Level Handler Precedence**
|
|
38
|
-
**Advanced router customization!** Demonstrates how to define custom handlers at the router level that take precedence over library-level handlers:
|
|
39
|
-
- **Router-level handlers**: Define custom actions, facets, allActions, and allFacets directly in router options
|
|
40
|
-
- **Handler precedence**: Router-level handlers are called first, with fallback to library handlers
|
|
41
|
-
- **Cross-system integration**: Perfect for aggregating data from multiple services or external APIs
|
|
42
|
-
- **Business logic separation**: Keep complex integrations at the router layer while maintaining library simplicity
|
|
43
|
-
- **Flexible architecture**: Supports both PItemRouter and CItemRouter with appropriate type safety
|
|
44
|
-
|
|
45
|
-
Ideal for scenarios requiring external service calls, data aggregation, or complex business logic that shouldn't be in the core library layer.
|
|
46
|
-
|
|
47
|
-
## Key Concepts Demonstrated
|
|
48
|
-
|
|
49
|
-
### Basic Router Setup (basic-router-example.ts)
|
|
50
|
-
```typescript
|
|
51
|
-
// Import fjell-express-router functionality
|
|
52
|
-
import { PItemRouter, createRegistry } from '@fjell/express-router';
|
|
53
|
-
import express from 'express';
|
|
54
|
-
|
|
55
|
-
// Create Express app and registry
|
|
56
|
-
const app = express();
|
|
57
|
-
const registry = createRegistry();
|
|
58
|
-
|
|
59
|
-
// Create router instance
|
|
60
|
-
const userRouter = new PItemRouter(userInstance, 'user');
|
|
61
|
-
|
|
62
|
-
// Mount router to create automatic REST endpoints
|
|
63
|
-
app.use('/api/users', userRouter.getRouter());
|
|
64
|
-
// This creates:
|
|
65
|
-
// GET /api/users - List all users
|
|
66
|
-
// GET /api/users/:userPk - Get specific user
|
|
67
|
-
// POST /api/users - Create new user
|
|
68
|
-
// PUT /api/users/:userPk - Update user
|
|
69
|
-
// DELETE /api/users/:userPk - Delete user
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Nested Routing (nested-router-example.ts)
|
|
73
|
-
```typescript
|
|
74
|
-
// Create hierarchical routers
|
|
75
|
-
const orgRouter = new PItemRouter(orgInstance, 'organization');
|
|
76
|
-
const deptRouter = new CItemRouter(deptInstance, 'department', orgRouter);
|
|
77
|
-
const empRouter = new CItemRouter(empInstance, 'employee', deptRouter);
|
|
78
|
-
|
|
79
|
-
// Mount nested routes
|
|
80
|
-
app.use('/api/organizations', orgRouter.getRouter());
|
|
81
|
-
app.use('/api/organizations/:organizationPk/departments', deptRouter.getRouter());
|
|
82
|
-
app.use('/api/organizations/:organizationPk/departments/:departmentPk/employees', empRouter.getRouter());
|
|
83
|
-
|
|
84
|
-
// This creates nested endpoints like:
|
|
85
|
-
// GET /api/organizations/org-1/departments/dept-1/employees
|
|
86
|
-
// POST /api/organizations/org-1/departments/dept-1/employees
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Production Application (full-application-example.ts)
|
|
90
|
-
```typescript
|
|
91
|
-
// Advanced middleware setup
|
|
92
|
-
app.use(express.json({ limit: '10mb' }));
|
|
93
|
-
app.use(requestLogger);
|
|
94
|
-
app.use(validateCustomerTier);
|
|
95
|
-
app.use(errorHandler);
|
|
96
|
-
|
|
97
|
-
// Business logic routes
|
|
98
|
-
app.get('/api/dashboard', async (req, res) => {
|
|
99
|
-
// Complex dashboard with analytics
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
app.get('/api/catalog', async (req, res) => {
|
|
103
|
-
// Product catalog with filtering and search
|
|
104
|
-
});
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Data Model Patterns
|
|
108
|
-
|
|
109
|
-
### Primary Items (PItemRouter)
|
|
110
|
-
Primary items are top-level entities that exist independently:
|
|
111
|
-
```typescript
|
|
112
|
-
interface User extends Item<'user'> {
|
|
113
|
-
id: string;
|
|
114
|
-
name: string;
|
|
115
|
-
email: string;
|
|
116
|
-
// ... other properties
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Creates routes: /api/users, /api/users/:userPk
|
|
120
|
-
const userRouter = new PItemRouter(userInstance, 'user');
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### Contained Items (CItemRouter)
|
|
124
|
-
Contained items exist within a parent context and inherit location:
|
|
125
|
-
```typescript
|
|
126
|
-
interface Department extends Item<'department', 'organization'> {
|
|
127
|
-
id: string;
|
|
128
|
-
name: string;
|
|
129
|
-
organizationId: string; // Reference to parent
|
|
130
|
-
// ... other properties
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Creates nested routes: /api/organizations/:orgPk/departments
|
|
134
|
-
const deptRouter = new CItemRouter(deptInstance, 'department', orgRouter);
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### Multi-Level Hierarchies
|
|
138
|
-
Deep nesting is supported for complex organizational structures:
|
|
139
|
-
```typescript
|
|
140
|
-
interface Employee extends Item<'employee', 'organization', 'department'> {
|
|
141
|
-
id: string;
|
|
142
|
-
name: string;
|
|
143
|
-
organizationId: string;
|
|
144
|
-
departmentId: string;
|
|
145
|
-
// ... other properties
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Creates deeply nested routes: /api/organizations/:orgPk/departments/:deptPk/employees
|
|
149
|
-
const empRouter = new CItemRouter(empInstance, 'employee', deptRouter);
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
## Running the Examples
|
|
153
|
-
|
|
154
|
-
### Prerequisites
|
|
155
|
-
```bash
|
|
156
|
-
# Install dependencies
|
|
157
|
-
npm install express @fjell/core @fjell/lib @fjell/express-router
|
|
158
|
-
|
|
159
|
-
# For running TypeScript examples
|
|
160
|
-
npm install -g tsx
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### Running Individual Examples
|
|
164
|
-
|
|
165
|
-
**Basic Router Example:**
|
|
166
|
-
```bash
|
|
167
|
-
npx tsx examples/basic-router-example.ts
|
|
168
|
-
# Server runs on http://localhost:3001
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
**Nested Router Example:**
|
|
172
|
-
```bash
|
|
173
|
-
npx tsx examples/nested-router-example.ts
|
|
174
|
-
# Server runs on http://localhost:3002
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
**Full Application Example:**
|
|
178
|
-
```bash
|
|
179
|
-
npx tsx examples/full-application-example.ts
|
|
180
|
-
# Server runs on http://localhost:3003
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
**Router Handlers Example:**
|
|
184
|
-
```bash
|
|
185
|
-
npx tsx examples/router-handlers-example.ts
|
|
186
|
-
# Server runs on http://localhost:3004
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
## Testing the Examples
|
|
190
|
-
|
|
191
|
-
### Basic Router Example (Port 3001)
|
|
192
|
-
```bash
|
|
193
|
-
# Health check
|
|
194
|
-
curl http://localhost:3001/api/health
|
|
195
|
-
|
|
196
|
-
# Dashboard summary
|
|
197
|
-
curl http://localhost:3001/api/dashboard
|
|
198
|
-
|
|
199
|
-
# List all users
|
|
200
|
-
curl http://localhost:3001/api/users
|
|
201
|
-
|
|
202
|
-
# Get specific user
|
|
203
|
-
curl http://localhost:3001/api/users/user-1
|
|
204
|
-
|
|
205
|
-
# Create new user
|
|
206
|
-
curl -X POST http://localhost:3001/api/users \
|
|
207
|
-
-H "Content-Type: application/json" \
|
|
208
|
-
-d '{"name":"New User","email":"new@example.com","role":"user"}'
|
|
209
|
-
|
|
210
|
-
# List all tasks
|
|
211
|
-
curl http://localhost:3001/api/tasks
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### Nested Router Example (Port 3002)
|
|
215
|
-
```bash
|
|
216
|
-
# Organizational hierarchy
|
|
217
|
-
curl http://localhost:3002/api/hierarchy
|
|
218
|
-
|
|
219
|
-
# Statistics summary
|
|
220
|
-
curl http://localhost:3002/api/stats
|
|
221
|
-
|
|
222
|
-
# List organizations
|
|
223
|
-
curl http://localhost:3002/api/organizations
|
|
224
|
-
|
|
225
|
-
# List departments for organization
|
|
226
|
-
curl http://localhost:3002/api/organizations/org-1/departments
|
|
227
|
-
|
|
228
|
-
# List employees for department
|
|
229
|
-
curl http://localhost:3002/api/organizations/org-1/departments/dept-1/employees
|
|
230
|
-
|
|
231
|
-
# Create new department
|
|
232
|
-
curl -X POST http://localhost:3002/api/organizations/org-1/departments \
|
|
233
|
-
-H "Content-Type: application/json" \
|
|
234
|
-
-d '{"name":"IT Department","budget":1000000,"headCount":15}'
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### Full Application Example (Port 3003)
|
|
238
|
-
```bash
|
|
239
|
-
# System health
|
|
240
|
-
curl http://localhost:3003/health
|
|
241
|
-
|
|
242
|
-
# Business dashboard
|
|
243
|
-
curl http://localhost:3003/api/dashboard
|
|
244
|
-
|
|
245
|
-
# Product catalog
|
|
246
|
-
curl http://localhost:3003/api/catalog
|
|
247
|
-
|
|
248
|
-
# Filtered catalog
|
|
249
|
-
curl "http://localhost:3003/api/catalog?category=Electronics&featured=true"
|
|
250
|
-
|
|
251
|
-
# Customer analytics
|
|
252
|
-
curl http://localhost:3003/api/customers/cust-1/analytics
|
|
253
|
-
|
|
254
|
-
# List customers
|
|
255
|
-
curl http://localhost:3003/api/customers
|
|
256
|
-
|
|
257
|
-
# Customer orders
|
|
258
|
-
curl http://localhost:3003/api/customers/cust-1/orders
|
|
259
|
-
|
|
260
|
-
# Create new customer
|
|
261
|
-
curl -X POST http://localhost:3003/api/customers \
|
|
262
|
-
-H "Content-Type: application/json" \
|
|
263
|
-
-d '{
|
|
264
|
-
"name": "New Customer",
|
|
265
|
-
"email": "customer@example.com",
|
|
266
|
-
"phone": "+1-555-0789",
|
|
267
|
-
"address": {
|
|
268
|
-
"street": "789 Pine St",
|
|
269
|
-
"city": "Seattle",
|
|
270
|
-
"state": "WA",
|
|
271
|
-
"zipCode": "98101"
|
|
272
|
-
},
|
|
273
|
-
"tier": "bronze"
|
|
274
|
-
}'
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
### Router Handlers Example (Port 3004)
|
|
278
|
-
```bash
|
|
279
|
-
# Test router-level user actions
|
|
280
|
-
curl -X POST http://localhost:3004/api/users/user-1/activate
|
|
281
|
-
curl -X POST http://localhost:3004/api/users/user-1/deactivate
|
|
282
|
-
|
|
283
|
-
# Test router-level user facets
|
|
284
|
-
curl http://localhost:3004/api/users/user-1/profile
|
|
285
|
-
curl http://localhost:3004/api/users/user-1/activity
|
|
286
|
-
|
|
287
|
-
# Test router-level all actions
|
|
288
|
-
curl -X POST http://localhost:3004/api/users/bulk-activate
|
|
289
|
-
curl -X POST http://localhost:3004/api/users/send-notifications
|
|
290
|
-
|
|
291
|
-
# Test router-level all facets
|
|
292
|
-
curl http://localhost:3004/api/users/statistics
|
|
293
|
-
curl http://localhost:3004/api/users/analytics
|
|
294
|
-
|
|
295
|
-
# Test router-level post actions (nested under users)
|
|
296
|
-
curl -X POST http://localhost:3004/api/users/user-1/posts/post-1/publish
|
|
297
|
-
curl -X POST http://localhost:3004/api/users/user-1/posts/post-1/archive
|
|
298
|
-
|
|
299
|
-
# Test router-level post facets
|
|
300
|
-
curl http://localhost:3004/api/users/user-1/posts/post-1/analytics
|
|
301
|
-
curl http://localhost:3004/api/users/user-1/posts/post-1/comments
|
|
302
|
-
|
|
303
|
-
# Test fallback to library handlers for undefined router handlers
|
|
304
|
-
curl -X POST http://localhost:3004/api/users/user-1/someOtherAction
|
|
305
|
-
curl http://localhost:3004/api/users/user-1/someOtherFacet
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
## Integration Patterns
|
|
309
|
-
|
|
310
|
-
### Mock Operations
|
|
311
|
-
All examples use mock operations that simulate real database interactions:
|
|
312
|
-
```typescript
|
|
313
|
-
const createUserOperations = () => ({
|
|
314
|
-
async all() { /* return all users */ },
|
|
315
|
-
async get(key) { /* return specific user */ },
|
|
316
|
-
async create(item) { /* create new user */ },
|
|
317
|
-
async update(key, updates) { /* update user */ },
|
|
318
|
-
async remove(key) { /* delete user */ },
|
|
319
|
-
async find(finder, params) { /* find users with criteria */ }
|
|
320
|
-
});
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
### Instance Creation
|
|
324
|
-
Fjell instances wrap operations and provide router integration:
|
|
325
|
-
```typescript
|
|
326
|
-
const mockUserInstance = {
|
|
327
|
-
operations: createUserOperations(),
|
|
328
|
-
options: {}
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
const userRouter = new PItemRouter(mockUserInstance, 'user');
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
### Error Handling
|
|
335
|
-
Production applications should include comprehensive error handling:
|
|
336
|
-
```typescript
|
|
337
|
-
const errorHandler = (err, req, res, next) => {
|
|
338
|
-
console.error('Application Error:', err.message);
|
|
339
|
-
res.status(500).json({
|
|
340
|
-
error: 'Internal Server Error',
|
|
341
|
-
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
|
|
342
|
-
});
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
app.use(errorHandler);
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
## Best Practices
|
|
349
|
-
|
|
350
|
-
### 1. **Start Simple**
|
|
351
|
-
Begin with the basic router example to understand core concepts before moving to complex hierarchies.
|
|
352
|
-
|
|
353
|
-
### 2. **Design Your Data Model**
|
|
354
|
-
Plan your Item interfaces and relationships carefully:
|
|
355
|
-
- Use PItemRouter for independent entities
|
|
356
|
-
- Use CItemRouter for entities that belong to parents
|
|
357
|
-
- Define clear location hierarchies for nested data
|
|
358
|
-
|
|
359
|
-
### 3. **Implement Operations**
|
|
360
|
-
Create comprehensive operations that handle:
|
|
361
|
-
- CRUD operations (all, get, create, update, remove)
|
|
362
|
-
- Business logic finders (find method with custom logic)
|
|
363
|
-
- Error handling and validation
|
|
364
|
-
|
|
365
|
-
### 4. **Add Business Logic**
|
|
366
|
-
Extend beyond basic CRUD with:
|
|
367
|
-
- Custom middleware for validation
|
|
368
|
-
- Business logic routes (dashboards, analytics)
|
|
369
|
-
- Proper error handling and logging
|
|
370
|
-
|
|
371
|
-
### 5. **Production Considerations**
|
|
372
|
-
For production applications:
|
|
373
|
-
- Implement proper authentication/authorization
|
|
374
|
-
- Add rate limiting and security headers
|
|
375
|
-
- Use environment-specific configurations
|
|
376
|
-
- Implement comprehensive logging and monitoring
|
|
377
|
-
|
|
378
|
-
## Next Steps
|
|
379
|
-
|
|
380
|
-
1. **Study the examples** in order of complexity
|
|
381
|
-
2. **Run each example** and test the endpoints
|
|
382
|
-
3. **Modify the data models** to match your use case
|
|
383
|
-
4. **Implement real operations** with your database/API
|
|
384
|
-
5. **Add authentication and security** for production use
|
|
385
|
-
|
|
386
|
-
For more advanced usage, see the fjell-express-router documentation and explore other fjell ecosystem packages like fjell-cache, fjell-lib, and fjell-client-api.
|