nextrush 1.5.0 β 2.0.1
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 +1203 -261
- package/dist/index.d.mts +3044 -0
- package/dist/index.d.ts +2556 -2383
- package/dist/index.js +7781 -13675
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7853 -13720
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -55
package/README.md
CHANGED
|
@@ -1,414 +1,1356 @@
|
|
|
1
|
-
# π¦ NextRush - Learning Project
|
|
2
|
-
|
|
3
1
|
<div align="center">
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
[](https://www.npmjs.com/package/nextrush)
|
|
7
|
-
[](https://www.typescriptlang.org/)
|
|
8
|
-
[](https://opensource.org/licenses/MIT)
|
|
3
|
+
# π NextRush v2
|
|
9
4
|
|
|
10
|
-
|
|
5
|
+
**Modern, Type-Safe Web Framework for Node.js**
|
|
11
6
|
|
|
12
|
-
|
|
7
|
+
_Koa-style Context β’ Express-inspired API β’ Enterprise Features Built-in_
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/nextrush)
|
|
10
|
+
[](https://nodejs.org/)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
12
|
+
[](LICENSE)
|
|
13
|
+
[](https://github.com/0xTanzim/nextrush)
|
|
14
|
+
[](https://github.com/0xTanzim/nextrush)
|
|
13
15
|
|
|
14
16
|
```typescript
|
|
15
17
|
import { createApp } from 'nextrush';
|
|
16
18
|
|
|
17
19
|
const app = createApp();
|
|
18
20
|
|
|
19
|
-
app.get('/',
|
|
20
|
-
|
|
21
|
+
app.get('/hello', async ctx => {
|
|
22
|
+
ctx.json({ message: 'Welcome to NextRush v2! π' });
|
|
21
23
|
});
|
|
22
24
|
|
|
23
25
|
app.listen(3000);
|
|
24
|
-
//
|
|
26
|
+
// Server running at http://localhost:3000
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
[**π Documentation**](./docs) β’ [**π Quick Start**](#-quick-start) β’ [**π‘ Why NextRush?**](#-why-nextrush-v2)
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
**π¨ For Production**: Use [Express.js](https://expressjs.com/), [Fastify](https://fastify.dev/), or [Koa](https://koajs.com/) instead.
|
|
31
|
+
</div>
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
---
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
## π‘ Philosophy
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
**NextRush v2 combines the best of three worlds**: Koa's elegant async context pattern, Express's intuitive helper methods, and Fastify's performance optimizationsβall with **enterprise-grade features built directly into the core**.
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
| ---- | ------------ | ------------ | --------------- | --------------- |
|
|
41
|
-
| π₯ | **Fastify** | **9,491** | Low concurrency | **5.3ms** |
|
|
42
|
-
| π₯ | **Express** | **7,146** | All loads | **14.0ms** |
|
|
43
|
-
| π₯ | **NextRush** | **5,784** | Medium loads | **34.6ms** |
|
|
39
|
+
Unlike other frameworks that require dozens of plugins for production use, NextRush v2 includes security middleware, validation, templating, WebSocket support, and advanced logging out of the box. Choose your preferred API style: convenience methods, Express-like helpers, or Fastify-style configuration.
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
**Built for teams that value type safety, developer experience, and production readiness without the plugin fatigue.**
|
|
46
42
|
|
|
47
|
-
|
|
43
|
+
---
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
| ------------ | --------- | ------- | ------------- |
|
|
51
|
-
| **Fastify** | **9,491** | 5.3ms | π **Winner** |
|
|
52
|
-
| **Express** | **6,934** | 7.2ms | π₯ Good |
|
|
53
|
-
| **NextRush** | **5,612** | 8.9ms | π₯ Decent |
|
|
45
|
+
## π― Zero Dependencies Philosophy
|
|
54
46
|
|
|
55
|
-
|
|
47
|
+
**NextRush v2 is built with ZERO runtime dependencies.** Every featureβfrom routing to WebSocket supportβis implemented using only Node.js core modules.
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
| ------------ | --------- | ------- | ------------- |
|
|
59
|
-
| **Express** | **7,146** | 14.0ms | π **Winner** |
|
|
60
|
-
| **Fastify** | **9,154** | 10.9ms | π₯ Close |
|
|
61
|
-
| **NextRush** | **5,669** | 17.6ms | π₯ Stable |
|
|
49
|
+
### Why Zero Dependencies?
|
|
62
50
|
|
|
63
|
-
|
|
51
|
+
- **π Security** - No third-party vulnerabilities in your supply chain
|
|
52
|
+
- **β‘ Faster Installs** - No dependency resolution, instant setup
|
|
53
|
+
- **π‘οΈ Stability** - No breaking changes from external packages
|
|
54
|
+
- **π Full Control** - Every line of code is maintainable and auditable
|
|
55
|
+
- **π Performance** - Optimized for Node.js internals, no abstraction overhead
|
|
64
56
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
57
|
+
```bash
|
|
58
|
+
# Check it yourself! Only devDependencies, ZERO runtime deps
|
|
59
|
+
npm ls --production --depth=0
|
|
60
|
+
# Result: (empty)
|
|
61
|
+
```
|
|
70
62
|
|
|
71
|
-
|
|
63
|
+
**Every framework claims to be "lightweight." NextRush v2 proves it.**
|
|
72
64
|
|
|
73
|
-
| Framework
|
|
74
|
-
|
|
|
75
|
-
| **
|
|
76
|
-
|
|
|
77
|
-
|
|
|
65
|
+
| Framework | Runtime Dependencies |
|
|
66
|
+
| --------------- | ------------------------------------ |
|
|
67
|
+
| **NextRush v2** | **0** β |
|
|
68
|
+
| Express | 34+ packages |
|
|
69
|
+
| Koa | 0 (but needs plugins for everything) |
|
|
70
|
+
| Fastify | 20+ packages |
|
|
71
|
+
| Hapi | 50+ packages |
|
|
78
72
|
|
|
79
|
-
|
|
73
|
+
> **Built-in features without the dependency hell.** Security, validation, WebSocket, templates, logging, CQRSβall implemented with pure Node.js.
|
|
80
74
|
|
|
81
|
-
|
|
75
|
+
---
|
|
82
76
|
|
|
83
|
-
|
|
84
|
-
- **Excellent latency** across all loads
|
|
85
|
-
- **9.5K RPS peak** - industry-leading performance
|
|
77
|
+
## π― Why NextRush v2?
|
|
86
78
|
|
|
87
|
-
|
|
79
|
+
NextRush v2 represents the evolution of Node.js web frameworks, synthesizing proven patterns from Express, Koa, and Fastify into a unified, type-safe framework with enterprise features integrated at the core.
|
|
88
80
|
|
|
89
|
-
|
|
90
|
-
- **Handles high loads well** (500+ connections)
|
|
91
|
-
- **Proven stability** under stress
|
|
81
|
+
### π Three API Styles, One Framework
|
|
92
82
|
|
|
93
|
-
|
|
83
|
+
| **Convenience API** | **Enhanced API** | **Configuration API** |
|
|
84
|
+
| ------------------- | ---------------- | --------------------- |
|
|
85
|
+
| `ctx.json()` | `ctx.res.json()` | `{ handler, schema }` |
|
|
86
|
+
| Clean & simple | Express-inspired | Fastify-style |
|
|
87
|
+
| **Recommended** | Familiar pattern | Advanced use cases |
|
|
94
88
|
|
|
95
|
-
|
|
96
|
-
- **Competitive performance** for a learning project
|
|
97
|
-
- **Room for optimization** - performance gap shows learning opportunities
|
|
89
|
+
### β‘ Performance That Scales
|
|
98
90
|
|
|
99
|
-
|
|
91
|
+
- **Zero Dependencies** - Pure Node.js core, no third-party overhead
|
|
92
|
+
- **13,261 RPS Average** - Competitive with established frameworks
|
|
93
|
+
- **Optimized Router** - O(1) static path lookup, pre-compiled routes
|
|
94
|
+
- **Memory Efficient** - ~60KB baseline, smallest footprint in class
|
|
95
|
+
- **Smart Caching** - Template caching, efficient buffer management
|
|
96
|
+
- **No Supply Chain Tax** - Every feature optimized for Node.js internals
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
- **Zero memory leaks** detected across all test scenarios
|
|
103
|
-
- **Performance gap mainly due to** architecture complexity vs. mature optimization
|
|
104
|
-
- **Learning-focused development** prioritized features over raw speed
|
|
105
|
-
- **Plugin architecture overhead** impacts performance but provides flexibility
|
|
98
|
+
### π‘οΈ Enterprise Features Built-In
|
|
106
99
|
|
|
107
|
-
|
|
100
|
+
Unlike other frameworks requiring extensive plugin ecosystems, NextRush v2 includes production-ready features in the core:
|
|
108
101
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
- **Duration**: 60-second timeout per test
|
|
112
|
-
- **Hardware**: Intel i5-8300H, 14GB RAM, Ubuntu 25.04
|
|
113
|
-
- **Node.js**: v24.4.1 (latest LTS)
|
|
114
|
-
- **Methodology**: Multiple runs, averaged results
|
|
102
|
+
```typescript
|
|
103
|
+
const app = createApp();
|
|
115
104
|
|
|
116
|
-
|
|
105
|
+
// Security middleware (CORS, Helmet, Rate Limiting)
|
|
106
|
+
app.use(app.cors({ origin: ['https://example.com'] }));
|
|
107
|
+
app.use(app.helmet({ xssFilter: true }));
|
|
108
|
+
app.use(app.rateLimiter({ max: 100, windowMs: 15 * 60 * 1000 }));
|
|
117
109
|
|
|
118
|
-
|
|
110
|
+
// Smart body parser with auto-detection
|
|
111
|
+
app.use(app.smartBodyParser({ maxSize: 10 * 1024 * 1024 }));
|
|
119
112
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
| **Express Compatible** | β
| β
| β | Drop-in replacement |
|
|
124
|
-
| **TypeScript First** | β
| β οΈ | β
| Built-in vs. @types packages |
|
|
125
|
-
| **Built-in Body Parser** | β
| β | β
| express requires body-parser |
|
|
126
|
-
| **Built-in File Uploads** | β
| β | β | express requires multer |
|
|
127
|
-
| **Built-in WebSocket** | β
| β | β | Others require socket.io |
|
|
128
|
-
| **Built-in Templates** | β
| β | β | Others require view engines |
|
|
129
|
-
| **Built-in Security** | β
| β | β | Others require helmet + more |
|
|
130
|
-
| **Built-in Rate Limiting** | β
| β | β | express-rate-limit needed |
|
|
131
|
-
| **Built-in Authentication** | β
| β | β | passport + sessions needed |
|
|
132
|
-
| **API Documentation** | β
| β | β | swagger packages needed |
|
|
133
|
-
| **Performance Ranking** | 3rd | 2nd | 1st | Speed vs. Features trade-off |
|
|
113
|
+
// Advanced validation & sanitization
|
|
114
|
+
const userData = validateRequest(ctx, userSchema);
|
|
115
|
+
const cleanData = sanitizeInput(userData);
|
|
134
116
|
|
|
135
|
-
|
|
117
|
+
// Custom error classes with contextual information
|
|
118
|
+
throw new NotFoundError('User not found', {
|
|
119
|
+
userId: ctx.params.id,
|
|
120
|
+
suggestion: 'Verify the user ID is correct',
|
|
121
|
+
});
|
|
136
122
|
|
|
137
|
-
|
|
123
|
+
// Event-driven architecture (Simple + CQRS patterns)
|
|
124
|
+
await app.events.emit('user.registered', { userId: user.id });
|
|
125
|
+
await app.eventSystem.dispatch({ type: 'CreateUser', data: {...} });
|
|
138
126
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
- **SQL Injection Prevention** - Query sanitization and parameterization
|
|
142
|
-
- **CORS Management** - Flexible cross-origin resource sharing
|
|
143
|
-
- **Rate Limiting** - Configurable request throttling per IP/user
|
|
127
|
+
// Template engine with automatic XSS protection
|
|
128
|
+
await ctx.render('profile.html', { user, isAdmin });
|
|
144
129
|
|
|
145
|
-
|
|
130
|
+
// WebSocket support with room management
|
|
131
|
+
wsApp.ws('/chat', socket => {
|
|
132
|
+
socket.join('room1');
|
|
133
|
+
socket.onMessage(data => wsApp.wsBroadcast(data, 'room1'));
|
|
134
|
+
});
|
|
146
135
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
- **Download Management** - Secure file downloads with access control
|
|
150
|
-
- **Image Processing** - Basic resize and format conversion
|
|
136
|
+
// Structured logging with multiple transports
|
|
137
|
+
ctx.logger.info('User action', { userId, action });
|
|
151
138
|
|
|
152
|
-
|
|
139
|
+
// Dependency injection container
|
|
140
|
+
const service = container.resolve('UserService');
|
|
141
|
+
```
|
|
153
142
|
|
|
154
|
-
|
|
155
|
-
- **Server-Sent Events** - Real-time data streaming to clients
|
|
156
|
-
- **Long Polling** - Fallback for real-time communication
|
|
143
|
+
### π‘οΈ Type Safety First
|
|
157
144
|
|
|
158
|
-
|
|
145
|
+
```typescript
|
|
146
|
+
// Full TypeScript integration with IntelliSense support
|
|
147
|
+
import type { Context, Middleware, RouteHandler } from 'nextrush';
|
|
159
148
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
149
|
+
app.get('/users/:id', async (ctx: Context) => {
|
|
150
|
+
const userId: string = ctx.params.id; // Type-safe route parameters
|
|
151
|
+
const userData: User = ctx.body; // Type-safe request body
|
|
152
|
+
ctx.json({ user: userData }); // Type-safe response
|
|
153
|
+
});
|
|
164
154
|
|
|
165
|
-
|
|
155
|
+
// Type-safe middleware composition
|
|
156
|
+
const authMiddleware: Middleware = async (ctx, next) => {
|
|
157
|
+
ctx.state.user = await validateToken(ctx.headers.authorization);
|
|
158
|
+
await next();
|
|
159
|
+
};
|
|
160
|
+
```
|
|
166
161
|
|
|
167
|
-
|
|
162
|
+
### π Choose Your API Style
|
|
168
163
|
|
|
169
164
|
```typescript
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
165
|
+
// Convenience Methods (Recommended)
|
|
166
|
+
app.get('/users', async ctx => {
|
|
167
|
+
ctx.json(await getUsers()); // Clean and concise
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Enhanced API (Express-inspired helpers)
|
|
171
|
+
app.get('/users', async ctx => {
|
|
172
|
+
ctx.res.json(await getUsers()); // Familiar Express-like methods
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Configuration API (Fastify-style)
|
|
176
|
+
app.get('/users', {
|
|
177
|
+
handler: async ctx => ctx.json(await getUsers()),
|
|
178
|
+
schema: { response: { 200: UserSchema } },
|
|
179
|
+
options: { tags: ['users'] },
|
|
175
180
|
});
|
|
176
181
|
```
|
|
177
182
|
|
|
178
|
-
|
|
183
|
+
### π Core Features Comparison
|
|
184
|
+
|
|
185
|
+
| Feature | NextRush v2 | Express | Fastify | Koa |
|
|
186
|
+
| ----------------------------- | --------------- | --------------- | --------------- | ----------------- |
|
|
187
|
+
| **Security Middleware** | Built-in | Plugin required | Plugin required | Plugin required |
|
|
188
|
+
| **Validation & Sanitization** | Built-in | Plugin required | Built-in | Plugin required |
|
|
189
|
+
| **Template Engine** | Built-in | Plugin required | Plugin required | Plugin required |
|
|
190
|
+
| **WebSocket Support** | Built-in | Plugin required | Plugin required | Plugin required |
|
|
191
|
+
| **Event System (CQRS)** | Dual system | Plugin required | Plugin required | Plugin required |
|
|
192
|
+
| **Advanced Logging** | Plugin included | Plugin required | Plugin required | Plugin required |
|
|
193
|
+
| **Error Classes** | 8 custom types | Basic | Basic | Basic |
|
|
194
|
+
| **Dependency Injection** | Built-in | Plugin required | Plugin required | Plugin required |
|
|
195
|
+
| **Smart Body Parser** | Auto-detection | Plugin required | Built-in | Plugin required |
|
|
196
|
+
| **Static File Serving** | Plugin included | Built-in | Plugin required | Plugin required |
|
|
197
|
+
| **TypeScript Support** | First-class | Community | First-class | Community |
|
|
198
|
+
| **Dependencies** | **0** β | 34+ packages | 20+ packages | 0 (needs plugins) |
|
|
199
|
+
| **Performance (RPS)** | ~13,261 | ~11,030 | ~22,717 | ~17,547 |
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## β¨ Comprehensive Feature Set
|
|
204
|
+
|
|
205
|
+
<table>
|
|
206
|
+
<tr>
|
|
207
|
+
<td width="50%">
|
|
208
|
+
|
|
209
|
+
### Modern Development
|
|
210
|
+
|
|
211
|
+
- **TypeScript First** - Full type safety with IntelliSense
|
|
212
|
+
- **Zero Dependencies** - Pure Node.js, minimal footprint
|
|
213
|
+
- **ESM & CJS** - Universal module compatibility
|
|
214
|
+
- **Plugin System** - Extensible architecture
|
|
215
|
+
- **Smart Body Parser** - Auto-detection & zero-copy operations
|
|
216
|
+
- **Template Engine** - Built-in HTML rendering with auto-escaping
|
|
217
|
+
|
|
218
|
+
</td>
|
|
219
|
+
<td width="50%">
|
|
220
|
+
|
|
221
|
+
### Production Ready
|
|
222
|
+
|
|
223
|
+
- **Error Handling** - Custom error classes with context
|
|
224
|
+
- **Advanced Logging** - Structured logs with multiple transports
|
|
225
|
+
- **Security** - CORS, Helmet, Rate Limiting included
|
|
226
|
+
- **Validation** - Schema validation & input sanitization
|
|
227
|
+
- **Testing** - Test-friendly design, 1461 passing tests
|
|
228
|
+
- **Static Files** - High-performance file serving
|
|
229
|
+
- **Compression** - Gzip & Brotli support
|
|
230
|
+
|
|
231
|
+
</td>
|
|
232
|
+
</tr>
|
|
233
|
+
<tr>
|
|
234
|
+
<td width="50%">
|
|
235
|
+
|
|
236
|
+
### Real-time & Events
|
|
237
|
+
|
|
238
|
+
- **WebSocket Support** - RFC 6455 compliant implementation
|
|
239
|
+
- **Room Management** - Built-in room system for grouping
|
|
240
|
+
- **Broadcasting** - Efficient message delivery patterns
|
|
241
|
+
- **Event System** - Simple events + CQRS/Event Sourcing
|
|
242
|
+
- **Connection Pooling** - Scalable connection management
|
|
243
|
+
- **Heartbeat** - Automatic connection health monitoring
|
|
244
|
+
- **Pub/Sub** - Event-driven architecture support
|
|
245
|
+
|
|
246
|
+
</td>
|
|
247
|
+
<td width="50%">
|
|
248
|
+
|
|
249
|
+
### Developer Experience
|
|
179
250
|
|
|
180
|
-
- **
|
|
181
|
-
- **
|
|
182
|
-
- **
|
|
183
|
-
- **
|
|
251
|
+
- **Enhanced Errors** - Contextual debugging information
|
|
252
|
+
- **Request/Response Enhancers** - Express-inspired helpers
|
|
253
|
+
- **Path Utilities** - URL & path manipulation tools
|
|
254
|
+
- **Cookie Utilities** - Type-safe cookie management
|
|
255
|
+
- **Dependency Injection** - Built-in DI container
|
|
256
|
+
- **Dev Warnings** - Common mistake detection
|
|
257
|
+
- **Performance Monitoring** - Built-in metrics collection
|
|
258
|
+
|
|
259
|
+
</td>
|
|
260
|
+
</tr>
|
|
261
|
+
</table>
|
|
184
262
|
|
|
185
263
|
## π **Quick Start**
|
|
186
264
|
|
|
187
|
-
###
|
|
265
|
+
### Prerequisites
|
|
266
|
+
|
|
267
|
+
- **Node.js**: >= 20.0.0
|
|
268
|
+
- **pnpm**: >= 10.0.0 (recommended) or npm/yarn/bun
|
|
269
|
+
|
|
270
|
+
### Installation
|
|
188
271
|
|
|
189
272
|
```bash
|
|
190
|
-
#
|
|
273
|
+
# pnpm (recommended)
|
|
274
|
+
pnpm add nextrush
|
|
275
|
+
|
|
276
|
+
# npm
|
|
191
277
|
npm install nextrush
|
|
192
278
|
|
|
193
|
-
#
|
|
279
|
+
# yarn
|
|
194
280
|
yarn add nextrush
|
|
195
281
|
|
|
196
|
-
#
|
|
197
|
-
|
|
282
|
+
# bun
|
|
283
|
+
bun add nextrush
|
|
198
284
|
```
|
|
199
285
|
|
|
200
|
-
###
|
|
286
|
+
### Hello World
|
|
201
287
|
|
|
202
288
|
```typescript
|
|
203
|
-
import { createApp
|
|
289
|
+
import { createApp } from 'nextrush';
|
|
204
290
|
|
|
205
|
-
// Create app with full features (development)
|
|
206
291
|
const app = createApp();
|
|
207
292
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
293
|
+
app.get('/', async ctx => {
|
|
294
|
+
ctx.json({
|
|
295
|
+
message: 'Hello NextRush v2! π',
|
|
296
|
+
timestamp: new Date().toISOString(),
|
|
297
|
+
});
|
|
213
298
|
});
|
|
214
299
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
300
|
+
app.listen(3000, () => {
|
|
301
|
+
console.log('π Server running on http://localhost:3000');
|
|
302
|
+
});
|
|
303
|
+
```
|
|
219
304
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
305
|
+
### 30-Second API
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { createApp } from 'nextrush';
|
|
309
|
+
|
|
310
|
+
const app = createApp({
|
|
311
|
+
cors: true, // Enable CORS
|
|
312
|
+
debug: true, // Development mode
|
|
225
313
|
});
|
|
226
314
|
|
|
227
|
-
//
|
|
228
|
-
app.
|
|
229
|
-
|
|
230
|
-
|
|
315
|
+
// Middleware
|
|
316
|
+
app.use(async (ctx, next) => {
|
|
317
|
+
console.log(`π ${ctx.method} ${ctx.path}`);
|
|
318
|
+
await next();
|
|
231
319
|
});
|
|
232
320
|
|
|
233
|
-
|
|
234
|
-
|
|
321
|
+
// Routes with three different styles
|
|
322
|
+
app.get('/simple', async ctx => {
|
|
323
|
+
ctx.json({ style: 'convenience' });
|
|
235
324
|
});
|
|
325
|
+
|
|
326
|
+
app.get('/express', async ctx => {
|
|
327
|
+
ctx.res.json({ style: 'express-like' });
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
app.post('/fastify', {
|
|
331
|
+
handler: async ctx => ctx.json({ style: 'fastify-style' }),
|
|
332
|
+
schema: {
|
|
333
|
+
body: {
|
|
334
|
+
type: 'object',
|
|
335
|
+
properties: { name: { type: 'string' } },
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
app.listen(3000);
|
|
236
341
|
```
|
|
237
342
|
|
|
238
|
-
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## π Multiple API Styles
|
|
239
346
|
|
|
240
|
-
|
|
347
|
+
NextRush v2 is **Koa-style at its core** with **three distinct API styles**βchoose the approach that best fits your team's preferences:
|
|
348
|
+
|
|
349
|
+
### Convenience Methods _(Recommended)_
|
|
241
350
|
|
|
242
351
|
```typescript
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
352
|
+
app.get('/users/:id', async ctx => {
|
|
353
|
+
const user = await findUser(ctx.params.id);
|
|
354
|
+
|
|
355
|
+
if (!user) {
|
|
356
|
+
ctx.json({ error: 'User not found' }, 404);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
ctx.json(user); // Clean and concise
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
app.post('/users', async ctx => {
|
|
364
|
+
const user = await createUser(ctx.body);
|
|
365
|
+
ctx.json(user, 201); // Status code as second parameter
|
|
366
|
+
});
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Enhanced API _(Express-inspired)_
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
app.get('/users/:id', async ctx => {
|
|
373
|
+
const user = await findUser(ctx.params.id);
|
|
374
|
+
|
|
375
|
+
if (!user) {
|
|
376
|
+
ctx.res.status(404).json({ error: 'User not found' });
|
|
377
|
+
return;
|
|
256
378
|
}
|
|
379
|
+
|
|
380
|
+
ctx.res.json(user); // Familiar Express-style methods
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
app.post('/users', async ctx => {
|
|
384
|
+
const user = await createUser(ctx.body);
|
|
385
|
+
ctx.res.status(201).json(user); // Chainable method calls
|
|
257
386
|
});
|
|
258
387
|
```
|
|
259
388
|
|
|
260
|
-
|
|
389
|
+
### Configuration API _(Fastify-style)_
|
|
261
390
|
|
|
262
391
|
```typescript
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
392
|
+
app.get('/users/:id', {
|
|
393
|
+
handler: async ctx => {
|
|
394
|
+
const user = await findUser(ctx.params.id);
|
|
395
|
+
ctx.json(user || { error: 'Not found' }, user ? 200 : 404);
|
|
396
|
+
},
|
|
397
|
+
schema: {
|
|
398
|
+
params: {
|
|
399
|
+
type: 'object',
|
|
400
|
+
properties: { id: { type: 'string', pattern: '^[0-9]+$' } },
|
|
401
|
+
},
|
|
402
|
+
response: {
|
|
403
|
+
200: { type: 'object', properties: { id: { type: 'string' } } },
|
|
404
|
+
404: { type: 'object', properties: { error: { type: 'string' } } },
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
options: {
|
|
408
|
+
name: 'getUser',
|
|
409
|
+
description: 'Retrieve user by ID',
|
|
410
|
+
tags: ['users'],
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
---
|
|
416
|
+
|
|
417
|
+
## π Real-time WebSocket Support
|
|
418
|
+
|
|
419
|
+
Production-ready WebSocket server with zero dependencies and full TypeScript support:
|
|
420
|
+
|
|
421
|
+
```typescript
|
|
422
|
+
import { createApp, WebSocketPlugin, withWebSocket } from 'nextrush';
|
|
423
|
+
import type { WSConnection } from 'nextrush';
|
|
424
|
+
|
|
425
|
+
const app = createApp();
|
|
426
|
+
|
|
427
|
+
// Install WebSocket plugin
|
|
428
|
+
const wsPlugin = new WebSocketPlugin({
|
|
429
|
+
path: '/ws',
|
|
430
|
+
heartbeatMs: 30000,
|
|
431
|
+
maxConnections: 1000,
|
|
432
|
+
verifyClient: async req => {
|
|
433
|
+
// Optional: Custom authentication
|
|
434
|
+
const token = new URL(req.url || '', 'http://localhost').searchParams.get(
|
|
435
|
+
'token'
|
|
436
|
+
);
|
|
437
|
+
return !!token;
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
wsPlugin.install(app);
|
|
441
|
+
|
|
442
|
+
// β
Get typed WebSocket application (Perfect IntelliSense!)
|
|
443
|
+
const wsApp = withWebSocket(app);
|
|
444
|
+
|
|
445
|
+
// Simple echo server with full type safety
|
|
446
|
+
wsApp.ws('/echo', (socket: WSConnection) => {
|
|
447
|
+
socket.send('Welcome!');
|
|
266
448
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
449
|
+
socket.onMessage((data: string | Buffer) => {
|
|
450
|
+
socket.send(`Echo: ${data}`);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
// Room-based chat system
|
|
455
|
+
wsApp.ws('/chat', (socket: WSConnection) => {
|
|
456
|
+
const room = 'general';
|
|
457
|
+
|
|
458
|
+
socket.join(room);
|
|
459
|
+
socket.send(`Joined room: ${room}`);
|
|
460
|
+
|
|
461
|
+
socket.onMessage((data: string | Buffer) => {
|
|
462
|
+
// Broadcast to all users in room using app-level method
|
|
463
|
+
wsApp.wsBroadcast(data.toString(), room);
|
|
270
464
|
});
|
|
271
465
|
|
|
272
|
-
|
|
273
|
-
console.log(
|
|
466
|
+
socket.onClose((code, reason) => {
|
|
467
|
+
console.log(`Client disconnected: ${code} - ${reason}`);
|
|
274
468
|
});
|
|
275
469
|
});
|
|
470
|
+
|
|
471
|
+
app.listen(3000);
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
**Client Usage:**
|
|
475
|
+
|
|
476
|
+
```javascript
|
|
477
|
+
// Browser WebSocket client
|
|
478
|
+
const ws = new WebSocket('ws://localhost:3000/chat');
|
|
479
|
+
ws.onopen = () => {
|
|
480
|
+
console.log('Connected!');
|
|
481
|
+
ws.send('Hello everyone!');
|
|
482
|
+
};
|
|
483
|
+
ws.onmessage = event => console.log('Received:', event.data);
|
|
484
|
+
ws.onerror = error => console.error('Error:', error);
|
|
485
|
+
ws.onclose = () => console.log('Disconnected');
|
|
486
|
+
|
|
487
|
+
// With authentication token
|
|
488
|
+
const wsAuth = new WebSocket('ws://localhost:3000/echo?token=your-jwt-token');
|
|
276
489
|
```
|
|
277
490
|
|
|
278
|
-
|
|
491
|
+
---
|
|
492
|
+
|
|
493
|
+
## π‘οΈ Built-in Security Middleware
|
|
279
494
|
|
|
280
495
|
```typescript
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
496
|
+
import { createApp } from 'nextrush';
|
|
497
|
+
|
|
498
|
+
const app = createApp();
|
|
499
|
+
|
|
500
|
+
// CORS - Cross-Origin Resource Sharing
|
|
501
|
+
app.use(
|
|
502
|
+
app.cors({
|
|
503
|
+
origin: ['https://example.com'],
|
|
504
|
+
credentials: true,
|
|
505
|
+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
|
506
|
+
})
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
// Helmet - Security headers
|
|
510
|
+
app.use(
|
|
511
|
+
app.helmet({
|
|
512
|
+
contentSecurityPolicy: true,
|
|
513
|
+
xssFilter: true,
|
|
514
|
+
noSniff: true,
|
|
515
|
+
frameguard: { action: 'deny' },
|
|
516
|
+
})
|
|
517
|
+
);
|
|
518
|
+
|
|
519
|
+
// Rate Limiting - DDoS protection
|
|
520
|
+
app.use(
|
|
521
|
+
app.rateLimiter({
|
|
522
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
523
|
+
max: 100, // 100 requests per window
|
|
524
|
+
message: 'Too many requests',
|
|
525
|
+
})
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
// Request ID - Request tracing
|
|
529
|
+
app.use(app.requestId());
|
|
530
|
+
|
|
531
|
+
// Response Timer - Performance monitoring
|
|
532
|
+
app.use(app.timer());
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
**Included Security Features:**
|
|
287
536
|
|
|
288
|
-
|
|
289
|
-
|
|
537
|
+
- **CORS** - Flexible origin control and credential handling
|
|
538
|
+
- **Helmet** - 15+ security headers preconfigured
|
|
539
|
+
- **Rate Limiting** - IP-based request throttling
|
|
540
|
+
- **XSS Protection** - Automatic escaping in template engine
|
|
541
|
+
- **Request Tracing** - Unique identifiers for request tracking
|
|
542
|
+
- **Response Timing** - Performance measurement headers
|
|
543
|
+
|
|
544
|
+
---
|
|
545
|
+
|
|
546
|
+
## π **Comprehensive Validation System**
|
|
547
|
+
|
|
548
|
+
## π Comprehensive Validation System
|
|
549
|
+
|
|
550
|
+
```typescript
|
|
551
|
+
import { validateRequest, sanitizeInput, ValidationError } from 'nextrush';
|
|
552
|
+
|
|
553
|
+
// Define validation schema
|
|
554
|
+
const userSchema = {
|
|
555
|
+
name: {
|
|
556
|
+
required: true,
|
|
557
|
+
type: 'string',
|
|
558
|
+
minLength: 2,
|
|
559
|
+
maxLength: 50,
|
|
560
|
+
},
|
|
561
|
+
email: {
|
|
562
|
+
required: true,
|
|
563
|
+
type: 'email',
|
|
564
|
+
},
|
|
565
|
+
age: {
|
|
566
|
+
type: 'number',
|
|
567
|
+
min: 18,
|
|
568
|
+
max: 120,
|
|
569
|
+
},
|
|
570
|
+
role: {
|
|
571
|
+
enum: ['user', 'admin', 'moderator'],
|
|
572
|
+
},
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
app.post('/users', async ctx => {
|
|
576
|
+
try {
|
|
577
|
+
// Validate request against schema
|
|
578
|
+
const userData = validateRequest(ctx, userSchema);
|
|
579
|
+
|
|
580
|
+
// Sanitize user input
|
|
581
|
+
const cleanData = sanitizeInput(userData);
|
|
582
|
+
|
|
583
|
+
const user = await createUser(cleanData);
|
|
584
|
+
ctx.json({ user }, 201);
|
|
585
|
+
} catch (error) {
|
|
586
|
+
if (error instanceof ValidationError) {
|
|
587
|
+
ctx.json(
|
|
588
|
+
{
|
|
589
|
+
error: 'Validation failed',
|
|
590
|
+
details: error.errors,
|
|
591
|
+
},
|
|
592
|
+
400
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
290
596
|
});
|
|
291
597
|
```
|
|
292
598
|
|
|
293
|
-
|
|
599
|
+
**Validation Capabilities:**
|
|
600
|
+
|
|
601
|
+
- **Type Checking** - string, number, boolean, email, URL, and more
|
|
602
|
+
- **Length Validation** - minLength, maxLength constraints
|
|
603
|
+
- **Range Validation** - min, max for numeric values
|
|
604
|
+
- **Pattern Matching** - Regular expression validation
|
|
605
|
+
- **Enum Validation** - Restricted value sets
|
|
606
|
+
- **Custom Validation** - Extensible validation functions
|
|
607
|
+
- **XSS Prevention** - Automatic input sanitization
|
|
608
|
+
|
|
609
|
+
π **[Complete Validation Guide β](./docs/api/validation-utilities.md)**
|
|
610
|
+
|
|
611
|
+
**Validation Features:**
|
|
612
|
+
|
|
613
|
+
- **Type Checking** - string, number, boolean, email, URL, etc.
|
|
614
|
+
- **Length Validation** - minLength, maxLength
|
|
615
|
+
- **Range Validation** - min, max for numbers
|
|
616
|
+
- **Pattern Matching** - Regex validation
|
|
617
|
+
- **Enum Validation** - Allowed values
|
|
618
|
+
- **Custom Validation** - Your own validation functions
|
|
619
|
+
- **XSS Prevention** - Auto-sanitization
|
|
620
|
+
|
|
621
|
+
π **[Complete Validation Guide β](./docs/api/validation-utilities.md)**
|
|
622
|
+
|
|
623
|
+
---
|
|
624
|
+
|
|
625
|
+
## π¨ Template Engine
|
|
294
626
|
|
|
295
627
|
```typescript
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
628
|
+
import { createApp, TemplatePlugin } from 'nextrush';
|
|
629
|
+
|
|
630
|
+
const app = createApp();
|
|
631
|
+
|
|
632
|
+
// Setup template engine
|
|
633
|
+
const templatePlugin = new TemplatePlugin({
|
|
634
|
+
viewsDir: './views',
|
|
635
|
+
cache: true,
|
|
636
|
+
helpers: {
|
|
637
|
+
formatDate: date => new Date(date).toLocaleDateString(),
|
|
638
|
+
currency: value => `$${Number(value).toFixed(2)}`,
|
|
639
|
+
},
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
templatePlugin.install(app);
|
|
643
|
+
|
|
644
|
+
// Use templates
|
|
645
|
+
app.get('/users/:id', async ctx => {
|
|
646
|
+
const user = await getUser(ctx.params.id);
|
|
647
|
+
|
|
648
|
+
await ctx.render('user-profile.html', {
|
|
649
|
+
title: 'User Profile',
|
|
650
|
+
user,
|
|
651
|
+
isAdmin: user.role === 'admin',
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
```
|
|
655
|
+
|
|
656
|
+
**views/user-profile.html:**
|
|
657
|
+
|
|
658
|
+
```html
|
|
659
|
+
<!DOCTYPE html>
|
|
660
|
+
<html>
|
|
661
|
+
<head>
|
|
662
|
+
<title>{{title}}</title>
|
|
663
|
+
</head>
|
|
664
|
+
<body>
|
|
665
|
+
<h1>{{user.name}}</h1>
|
|
666
|
+
<p>Email: {{user.email}}</p>
|
|
667
|
+
<p>Joined: {{user.createdAt | formatDate}}</p>
|
|
668
|
+
|
|
669
|
+
{{#if isAdmin}}
|
|
670
|
+
<button>Admin Panel</button>
|
|
671
|
+
{{/if}}
|
|
672
|
+
|
|
673
|
+
<!-- Loop through items -->
|
|
674
|
+
{{#each user.posts}}
|
|
675
|
+
<article>
|
|
676
|
+
<h2>{{this.title}}</h2>
|
|
677
|
+
<p>{{this.excerpt}}</p>
|
|
678
|
+
</article>
|
|
679
|
+
{{/each}}
|
|
680
|
+
</body>
|
|
681
|
+
</html>
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
**Template Features:**
|
|
685
|
+
|
|
686
|
+
- **Auto-escaping** - XSS protection by default
|
|
687
|
+
- **Control Structures** - if/else, loops, with blocks
|
|
688
|
+
- **Helpers** - Built-in & custom transformation functions
|
|
689
|
+
- **Partials** - Reusable template components
|
|
690
|
+
- **Layouts** - Consistent page structure
|
|
691
|
+
- **Caching** - Fast production performance
|
|
692
|
+
|
|
693
|
+
π **[Template Engine Documentation β](./docs/api/template-plugin.md)**
|
|
694
|
+
|
|
695
|
+
---
|
|
696
|
+
|
|
697
|
+
## π¨ Advanced Error Handling
|
|
698
|
+
|
|
699
|
+
```typescript
|
|
700
|
+
import {
|
|
701
|
+
HttpError,
|
|
702
|
+
BadRequestError,
|
|
703
|
+
UnauthorizedError,
|
|
704
|
+
ForbiddenError,
|
|
705
|
+
NotFoundError,
|
|
706
|
+
ConflictError,
|
|
707
|
+
UnprocessableEntityError,
|
|
708
|
+
TooManyRequestsError,
|
|
709
|
+
InternalServerError,
|
|
710
|
+
} from 'nextrush';
|
|
711
|
+
|
|
712
|
+
// Custom error classes with proper status codes
|
|
713
|
+
app.get('/users/:id', async ctx => {
|
|
714
|
+
const user = await db.user.findById(ctx.params.id);
|
|
715
|
+
|
|
716
|
+
if (!user) {
|
|
717
|
+
throw new NotFoundError('User not found', {
|
|
718
|
+
userId: ctx.params.id,
|
|
719
|
+
suggestion: 'Check the user ID',
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (ctx.state.user.id !== user.id) {
|
|
724
|
+
throw new ForbiddenError("Cannot access other user's data", {
|
|
725
|
+
requestedUserId: ctx.params.id,
|
|
726
|
+
currentUserId: ctx.state.user.id,
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
ctx.json({ user });
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
// Global error handler
|
|
734
|
+
app.use(async (ctx, next) => {
|
|
735
|
+
try {
|
|
736
|
+
await next();
|
|
737
|
+
} catch (error) {
|
|
738
|
+
if (error instanceof HttpError) {
|
|
739
|
+
ctx.json(
|
|
740
|
+
{
|
|
741
|
+
error: error.message,
|
|
742
|
+
code: error.code,
|
|
743
|
+
details: error.details,
|
|
744
|
+
},
|
|
745
|
+
error.statusCode
|
|
746
|
+
);
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// Unexpected errors
|
|
751
|
+
ctx.json({ error: 'Internal server error' }, 500);
|
|
305
752
|
}
|
|
306
753
|
});
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
**Error Classes:**
|
|
757
|
+
|
|
758
|
+
- **BadRequestError (400)** - Invalid request data
|
|
759
|
+
- **UnauthorizedError (401)** - Authentication required
|
|
760
|
+
- **ForbiddenError (403)** - Insufficient permissions
|
|
761
|
+
- **NotFoundError (404)** - Resource not found
|
|
762
|
+
- **ConflictError (409)** - Resource conflicts
|
|
763
|
+
- **UnprocessableEntityError (422)** - Business rule violations
|
|
764
|
+
- **TooManyRequestsError (429)** - Rate limit exceeded
|
|
765
|
+
- **InternalServerError (500)** - Server errors
|
|
766
|
+
|
|
767
|
+
π **[Complete Error Handling Guide β](./docs/api/errors.md)**
|
|
307
768
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
769
|
+
---
|
|
770
|
+
|
|
771
|
+
## πͺ Event-Driven Architecture
|
|
772
|
+
|
|
773
|
+
NextRush v2 includes dual event systems: Simple Events (Express-style) + Advanced Event System (CQRS/Event Sourcing).
|
|
774
|
+
|
|
775
|
+
### Simple Events API _(Express-style)_
|
|
776
|
+
|
|
777
|
+
```typescript
|
|
778
|
+
// Emit events
|
|
779
|
+
await app.events.emit('user.registered', {
|
|
780
|
+
userId: user.id,
|
|
781
|
+
email: user.email,
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
// Listen to events
|
|
785
|
+
app.events.on('user.registered', async data => {
|
|
786
|
+
await sendWelcomeEmail(data.email);
|
|
787
|
+
await createDefaultProfile(data.userId);
|
|
788
|
+
await trackAnalytics('User Registered', data);
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
// One-time handlers
|
|
792
|
+
app.events.once('app.ready', async () => {
|
|
793
|
+
await warmupCache();
|
|
794
|
+
await initializeServices();
|
|
795
|
+
});
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
### Advanced Event System _(CQRS/Event Sourcing)_
|
|
799
|
+
|
|
800
|
+
```typescript
|
|
801
|
+
// Define commands
|
|
802
|
+
interface CreateUserCommand {
|
|
803
|
+
type: 'CreateUser';
|
|
804
|
+
data: { name: string; email: string };
|
|
805
|
+
metadata: { id: string; timestamp: Date; correlationId: string };
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// Register command handlers
|
|
809
|
+
app.eventSystem.registerCommandHandler(
|
|
810
|
+
'CreateUser',
|
|
811
|
+
async (command: CreateUserCommand) => {
|
|
812
|
+
const user = await db.user.create(command.data);
|
|
813
|
+
|
|
814
|
+
// Emit domain event
|
|
815
|
+
await app.eventSystem.emit({
|
|
816
|
+
type: 'user.created',
|
|
817
|
+
data: { userId: user.id, email: user.email },
|
|
818
|
+
timestamp: new Date(),
|
|
819
|
+
metadata: {
|
|
820
|
+
aggregateId: user.id,
|
|
821
|
+
version: 1,
|
|
822
|
+
},
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
return user;
|
|
826
|
+
}
|
|
827
|
+
);
|
|
828
|
+
|
|
829
|
+
// Subscribe to domain events
|
|
830
|
+
app.eventSystem.subscribe('user.created', async event => {
|
|
831
|
+
await analytics.track('User Created', {
|
|
832
|
+
userId: event.data.userId,
|
|
833
|
+
correlationId: event.metadata.correlationId,
|
|
313
834
|
});
|
|
314
835
|
});
|
|
836
|
+
|
|
837
|
+
// Execute commands with CQRS
|
|
838
|
+
app.post('/users', async ctx => {
|
|
839
|
+
const command: CreateUserCommand = {
|
|
840
|
+
type: 'CreateUser',
|
|
841
|
+
data: ctx.body,
|
|
842
|
+
metadata: {
|
|
843
|
+
id: crypto.randomUUID(),
|
|
844
|
+
timestamp: new Date(),
|
|
845
|
+
correlationId: ctx.id,
|
|
846
|
+
},
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
const user = await app.eventSystem.dispatch(command);
|
|
850
|
+
ctx.json({ user }, 201);
|
|
851
|
+
});
|
|
315
852
|
```
|
|
316
853
|
|
|
317
|
-
|
|
854
|
+
**Event System Features:**
|
|
318
855
|
|
|
319
|
-
|
|
856
|
+
- **Simple Events** - Express-style emit/on/once patterns
|
|
857
|
+
- **CQRS** - Command Query Responsibility Segregation
|
|
858
|
+
- **Event Sourcing** - Domain event tracking and replay
|
|
859
|
+
- **Saga Patterns** - Complex workflow orchestration
|
|
860
|
+
- **Pub/Sub** - Event broadcasting capabilities
|
|
861
|
+
- **Correlation IDs** - Request tracing across services
|
|
862
|
+
- **Event Store** - Event history and replay functionality
|
|
320
863
|
|
|
321
|
-
|
|
322
|
-
- const express = require('express');
|
|
323
|
-
- const bodyParser = require('body-parser');
|
|
324
|
-
- const cors = require('cors');
|
|
325
|
-
- const helmet = require('helmet');
|
|
326
|
-
- const multer = require('multer');
|
|
864
|
+
π **[Complete Event System Guide β](./docs/api/events.md)**
|
|
327
865
|
|
|
328
|
-
|
|
866
|
+
---
|
|
867
|
+
|
|
868
|
+
## π Advanced Logging Plugin
|
|
869
|
+
|
|
870
|
+
```typescript
|
|
871
|
+
import { LoggerPlugin } from 'nextrush';
|
|
872
|
+
|
|
873
|
+
// Configure advanced logging
|
|
874
|
+
const loggerPlugin = new LoggerPlugin({
|
|
875
|
+
level: 'info',
|
|
876
|
+
format: 'json',
|
|
877
|
+
transports: [
|
|
878
|
+
{
|
|
879
|
+
type: 'console',
|
|
880
|
+
colorize: true,
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
type: 'file',
|
|
884
|
+
filename: 'logs/app.log',
|
|
885
|
+
maxSize: 10485760, // 10MB
|
|
886
|
+
maxFiles: 5,
|
|
887
|
+
},
|
|
888
|
+
{
|
|
889
|
+
type: 'file',
|
|
890
|
+
filename: 'logs/errors.log',
|
|
891
|
+
level: 'error',
|
|
892
|
+
maxSize: 10485760,
|
|
893
|
+
},
|
|
894
|
+
],
|
|
895
|
+
includeTimestamp: true,
|
|
896
|
+
includeMetadata: true,
|
|
897
|
+
});
|
|
329
898
|
|
|
330
|
-
|
|
331
|
-
- app.use(bodyParser.json());
|
|
332
|
-
- app.use(cors());
|
|
333
|
-
- app.use(helmet());
|
|
334
|
-
- const upload = multer({ dest: 'uploads/' });
|
|
899
|
+
loggerPlugin.install(app);
|
|
335
900
|
|
|
336
|
-
|
|
901
|
+
// Use in routes
|
|
902
|
+
app.get('/api/data', async ctx => {
|
|
903
|
+
ctx.logger.info('Fetching data', {
|
|
904
|
+
userId: ctx.state.user?.id,
|
|
905
|
+
endpoint: '/api/data',
|
|
906
|
+
});
|
|
337
907
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
908
|
+
try {
|
|
909
|
+
const data = await fetchData();
|
|
910
|
+
ctx.logger.debug('Data fetched successfully', { count: data.length });
|
|
911
|
+
ctx.json({ data });
|
|
912
|
+
} catch (error) {
|
|
913
|
+
ctx.logger.error('Failed to fetch data', {
|
|
914
|
+
error: error.message,
|
|
915
|
+
stack: error.stack,
|
|
916
|
+
});
|
|
917
|
+
throw error;
|
|
918
|
+
}
|
|
341
919
|
});
|
|
920
|
+
```
|
|
921
|
+
|
|
922
|
+
**Logger Features:**
|
|
923
|
+
|
|
924
|
+
- **Multiple Transports** - Console, file, and custom transports
|
|
925
|
+
- **Log Levels** - debug, info, warn, error, fatal
|
|
926
|
+
- **Structured Logging** - JSON format with metadata
|
|
927
|
+
- **File Rotation** - Size-based and time-based rotation
|
|
928
|
+
- **Colorized Output** - Enhanced development experience
|
|
929
|
+
- **Performance Metrics** - Request timing and throughput tracking
|
|
930
|
+
- **Custom Formatters** - Extensible log formatting
|
|
931
|
+
|
|
932
|
+
π **[Logger Plugin Documentation β](./docs/api/logger-plugin.md)**
|
|
933
|
+
|
|
934
|
+
---
|
|
935
|
+
|
|
936
|
+
## ποΈ **Advanced Features**
|
|
937
|
+
|
|
938
|
+
### Modular Router System
|
|
939
|
+
|
|
940
|
+
```typescript
|
|
941
|
+
import { createApp, createRouter } from 'nextrush';
|
|
942
|
+
|
|
943
|
+
const app = createApp();
|
|
944
|
+
|
|
945
|
+
// Create sub-routers
|
|
946
|
+
const userRouter = createRouter();
|
|
947
|
+
const adminRouter = createRouter();
|
|
948
|
+
|
|
949
|
+
// User routes
|
|
950
|
+
userRouter.get('/profile', async ctx => ctx.json({ user: 'profile' }));
|
|
951
|
+
userRouter.post('/login', async ctx => ctx.json({ message: 'Logged in' }));
|
|
952
|
+
|
|
953
|
+
// Admin routes
|
|
954
|
+
adminRouter.get('/dashboard', async ctx => ctx.json({ admin: 'dashboard' }));
|
|
955
|
+
adminRouter.get('/users', async ctx => ctx.json({ admin: 'users list' }));
|
|
956
|
+
|
|
957
|
+
// Mount routers
|
|
958
|
+
app.use('/users', userRouter);
|
|
959
|
+
app.use('/admin', adminRouter);
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
### Custom Middleware Development
|
|
963
|
+
|
|
964
|
+
```typescript
|
|
965
|
+
import type { Middleware, Context } from 'nextrush';
|
|
966
|
+
|
|
967
|
+
// Simple middleware
|
|
968
|
+
const logger: Middleware = async (ctx, next) => {
|
|
969
|
+
const start = Date.now();
|
|
970
|
+
console.log(`π ${ctx.method} ${ctx.path}`);
|
|
971
|
+
|
|
972
|
+
await next();
|
|
973
|
+
|
|
974
|
+
const duration = Date.now() - start;
|
|
975
|
+
console.log(`β
${ctx.status} ${duration}ms`);
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
// Authentication middleware with proper error handling
|
|
979
|
+
const requireAuth: Middleware = async (ctx, next) => {
|
|
980
|
+
const token = ctx.headers.authorization?.replace('Bearer ', '');
|
|
981
|
+
|
|
982
|
+
if (!token) {
|
|
983
|
+
throw new UnauthorizedError('Authentication token required', {
|
|
984
|
+
hint: 'Include Authorization: Bearer <token> header',
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
try {
|
|
989
|
+
ctx.state.user = await validateToken(token);
|
|
990
|
+
await next();
|
|
991
|
+
} catch (error) {
|
|
992
|
+
throw new UnauthorizedError('Invalid authentication token', {
|
|
993
|
+
reason: error.message,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
// Role-based authorization
|
|
999
|
+
const requireRole = (role: string): Middleware => {
|
|
1000
|
+
return async (ctx, next) => {
|
|
1001
|
+
if (!ctx.state.user) {
|
|
1002
|
+
throw new UnauthorizedError('Authentication required');
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
if (!ctx.state.user.roles.includes(role)) {
|
|
1006
|
+
throw new ForbiddenError(`${role} role required`, {
|
|
1007
|
+
userRoles: ctx.state.user.roles,
|
|
1008
|
+
requiredRole: role,
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
await next();
|
|
1013
|
+
};
|
|
1014
|
+
};
|
|
1015
|
+
|
|
1016
|
+
// Rate limiting middleware
|
|
1017
|
+
const rateLimiter: Middleware = async (ctx, next) => {
|
|
1018
|
+
const key = ctx.ip;
|
|
1019
|
+
const limit = 100;
|
|
1020
|
+
const windowMs = 15 * 60 * 1000;
|
|
1021
|
+
|
|
1022
|
+
const requests = await getRequestCount(key, windowMs);
|
|
342
1023
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
1024
|
+
if (requests >= limit) {
|
|
1025
|
+
throw new TooManyRequestsError('Rate limit exceeded', {
|
|
1026
|
+
limit,
|
|
1027
|
+
retryAfter: Math.ceil(windowMs / 1000),
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
await incrementRequestCount(key, windowMs);
|
|
1032
|
+
await next();
|
|
1033
|
+
};
|
|
1034
|
+
|
|
1035
|
+
// Use middleware
|
|
1036
|
+
app.use(logger);
|
|
1037
|
+
app.use('/api', requireAuth);
|
|
1038
|
+
app.use('/admin', requireRole('admin'));
|
|
1039
|
+
```
|
|
1040
|
+
|
|
1041
|
+
π **[Middleware Development Guide β](./docs/guides/middleware-development.md)**
|
|
1042
|
+
|
|
1043
|
+
### Enhanced Response Methods
|
|
1044
|
+
|
|
1045
|
+
```typescript
|
|
1046
|
+
// JSON response with status code
|
|
1047
|
+
app.get('/api/users', async ctx => {
|
|
1048
|
+
const users = await getUsers();
|
|
1049
|
+
ctx.json({ users }, 200);
|
|
347
1050
|
});
|
|
1051
|
+
|
|
1052
|
+
// HTML response
|
|
1053
|
+
app.get('/page', async ctx => {
|
|
1054
|
+
ctx.html('<h1>Welcome to NextRush</h1>');
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
// File download with options
|
|
1058
|
+
app.get('/download', async ctx => {
|
|
1059
|
+
ctx.file('./document.pdf', {
|
|
1060
|
+
filename: 'report.pdf',
|
|
1061
|
+
contentType: 'application/pdf',
|
|
1062
|
+
});
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
// Redirect with status code
|
|
1066
|
+
app.get('/old-url', async ctx => {
|
|
1067
|
+
ctx.redirect('/new-url', 301); // Permanent redirect
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
// CSV export
|
|
1071
|
+
app.get('/export/users', async ctx => {
|
|
1072
|
+
const users = await getUsers();
|
|
1073
|
+
const csv = users.map(u => `${u.name},${u.email}`).join('\n');
|
|
1074
|
+
ctx.csv(`name,email\n${csv}`, 'users.csv');
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
// Stream response
|
|
1078
|
+
app.get('/stream', async ctx => {
|
|
1079
|
+
const stream = fs.createReadStream('./large-file.txt');
|
|
1080
|
+
ctx.stream(stream, 'text/plain');
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
// Set custom headers
|
|
1084
|
+
app.get('/api/data', async ctx => {
|
|
1085
|
+
ctx.set('X-Custom-Header', 'value');
|
|
1086
|
+
ctx.set('Cache-Control', 'max-age=3600');
|
|
1087
|
+
ctx.json({ data: [] });
|
|
1088
|
+
});
|
|
1089
|
+
|
|
1090
|
+
// Cookie management
|
|
1091
|
+
app.get('/set-cookie', async ctx => {
|
|
1092
|
+
ctx.setCookie('session', 'abc123', {
|
|
1093
|
+
httpOnly: true,
|
|
1094
|
+
secure: true,
|
|
1095
|
+
maxAge: 3600000,
|
|
1096
|
+
sameSite: 'strict',
|
|
1097
|
+
});
|
|
1098
|
+
ctx.json({ message: 'Cookie set' });
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
app.get('/get-cookie', async ctx => {
|
|
1102
|
+
const session = ctx.getCookie('session');
|
|
1103
|
+
ctx.json({ session });
|
|
1104
|
+
});
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1107
|
+
π **[Enhanced Request/Response API β](./docs/api/enhancers.md)**
|
|
1108
|
+
|
|
1109
|
+
## π Complete API Reference
|
|
1110
|
+
|
|
1111
|
+
### Context Object
|
|
1112
|
+
|
|
1113
|
+
```typescript
|
|
1114
|
+
// ===================== Request Properties =====================
|
|
1115
|
+
ctx.req // Native HTTP request object
|
|
1116
|
+
ctx.res // Enhanced response object
|
|
1117
|
+
ctx.body // Parsed request body (JSON, form, etc.)
|
|
1118
|
+
ctx.method // HTTP method (GET, POST, PUT, DELETE, etc.)
|
|
1119
|
+
ctx.path // Request path (/users/123)
|
|
1120
|
+
ctx.url // Full request URL
|
|
1121
|
+
ctx.query // Query parameters (?page=1 β { page: '1' })
|
|
1122
|
+
ctx.headers // Request headers (lowercase keys)
|
|
1123
|
+
ctx.params // Route parameters (/users/:id β { id: '123' })
|
|
1124
|
+
ctx.cookies // Parsed cookies
|
|
1125
|
+
ctx.ip // Client IP address
|
|
1126
|
+
ctx.secure // Is HTTPS?
|
|
1127
|
+
ctx.protocol // Request protocol (http/https)
|
|
1128
|
+
ctx.hostname // Request hostname
|
|
1129
|
+
ctx.origin // Request origin (protocol + host)
|
|
1130
|
+
ctx.state // Custom request-scoped state
|
|
1131
|
+
|
|
1132
|
+
// ===================== Response Methods =====================
|
|
1133
|
+
// Convenience API (Recommended)
|
|
1134
|
+
ctx.json(data, status?) // Send JSON response
|
|
1135
|
+
ctx.html(html, status?) // Send HTML response
|
|
1136
|
+
ctx.text(text, status?) // Send plain text
|
|
1137
|
+
ctx.csv(data, filename?) // Send CSV file
|
|
1138
|
+
ctx.file(path, options?) // Send file download
|
|
1139
|
+
ctx.stream(stream, contentType?) // Send stream response
|
|
1140
|
+
ctx.redirect(url, status?) // Redirect to URL
|
|
1141
|
+
|
|
1142
|
+
// Express-like API (Enhanced)
|
|
1143
|
+
ctx.res.json(data, status?) // Send JSON
|
|
1144
|
+
ctx.res.status(code) // Set status code (chainable)
|
|
1145
|
+
ctx.res.send(data) // Auto-detect content type
|
|
1146
|
+
ctx.res.sendFile(path) // Send file
|
|
1147
|
+
ctx.res.render(template, data) // Render template
|
|
1148
|
+
|
|
1149
|
+
// ===================== Header & Cookie Methods =====================
|
|
1150
|
+
ctx.get(headerName) // Get request header
|
|
1151
|
+
ctx.set(headerName, value) // Set response header
|
|
1152
|
+
ctx.setCookie(name, value, opts) // Set cookie
|
|
1153
|
+
ctx.getCookie(name) // Get cookie value
|
|
1154
|
+
ctx.clearCookie(name) // Delete cookie
|
|
1155
|
+
|
|
1156
|
+
// ===================== Utility Properties =====================
|
|
1157
|
+
ctx.id // Unique request ID
|
|
1158
|
+
ctx.logger // Request-scoped logger
|
|
1159
|
+
ctx.services // DI container services
|
|
1160
|
+
ctx.app // Application instance
|
|
348
1161
|
```
|
|
349
1162
|
|
|
350
|
-
###
|
|
1163
|
+
### Application Methods
|
|
351
1164
|
|
|
352
1165
|
```typescript
|
|
353
|
-
//
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
1166
|
+
// ===================== HTTP Methods =====================
|
|
1167
|
+
app.get(path, ...handlers) // GET route
|
|
1168
|
+
app.post(path, ...handlers) // POST route
|
|
1169
|
+
app.put(path, ...handlers) // PUT route
|
|
1170
|
+
app.delete(path, ...handlers) // DELETE route
|
|
1171
|
+
app.patch(path, ...handlers) // PATCH route
|
|
1172
|
+
app.head(path, ...handlers) // HEAD route
|
|
1173
|
+
app.options(path, ...handlers) // OPTIONS route
|
|
1174
|
+
app.all(path, ...handlers) // All HTTP methods
|
|
1175
|
+
|
|
1176
|
+
// ===================== Middleware =====================
|
|
1177
|
+
app.use(...middleware) // Global middleware
|
|
1178
|
+
app.use(path, ...middleware) // Path-specific middleware
|
|
1179
|
+
|
|
1180
|
+
// ===================== Built-in Middleware =====================
|
|
1181
|
+
app.cors(options) // CORS middleware
|
|
1182
|
+
app.helmet(options) // Security headers
|
|
1183
|
+
app.rateLimiter(options) // Rate limiting
|
|
1184
|
+
app.compression(options) // Gzip/Brotli compression
|
|
1185
|
+
app.requestId(options) // Request ID tracking
|
|
1186
|
+
app.timer() // Response time header
|
|
1187
|
+
app.smartBodyParser(options) // Auto body parsing
|
|
1188
|
+
|
|
1189
|
+
// ===================== Event System =====================
|
|
1190
|
+
app.events.emit(event, data) // Emit simple event
|
|
1191
|
+
app.events.on(event, handler) // Listen to event
|
|
1192
|
+
app.events.once(event, handler) // One-time listener
|
|
1193
|
+
app.events.off(event, handler?) // Remove listener
|
|
1194
|
+
|
|
1195
|
+
app.eventSystem.dispatch(op) // CQRS dispatch
|
|
1196
|
+
app.eventSystem.executeCommand(c) // Execute command
|
|
1197
|
+
app.eventSystem.executeQuery(q) // Execute query
|
|
1198
|
+
app.eventSystem.subscribe(e, h) // Subscribe to domain event
|
|
1199
|
+
|
|
1200
|
+
// ===================== Server Control =====================
|
|
1201
|
+
app.listen(port, callback?) // Start HTTP server
|
|
1202
|
+
app.close(callback?) // Stop server gracefully
|
|
1203
|
+
```
|
|
1204
|
+
|
|
1205
|
+
### Router Methods
|
|
1206
|
+
|
|
1207
|
+
```typescript
|
|
1208
|
+
// Create router
|
|
1209
|
+
const router = createRouter('/prefix');
|
|
1210
|
+
|
|
1211
|
+
// HTTP methods
|
|
1212
|
+
router.get(path, handler);
|
|
1213
|
+
router.post(path, handler);
|
|
1214
|
+
router.put(path, handler);
|
|
1215
|
+
router.delete(path, handler);
|
|
1216
|
+
router.patch(path, handler);
|
|
1217
|
+
|
|
1218
|
+
// Middleware
|
|
1219
|
+
router.use(middleware);
|
|
1220
|
+
|
|
1221
|
+
// Mount router
|
|
1222
|
+
app.use('/api', router);
|
|
1223
|
+
```
|
|
1224
|
+
|
|
1225
|
+
---
|
|
1226
|
+
|
|
1227
|
+
## π§ͺ Testing
|
|
1228
|
+
|
|
1229
|
+
```typescript
|
|
1230
|
+
import { createApp } from 'nextrush';
|
|
1231
|
+
|
|
1232
|
+
describe('NextRush Application', () => {
|
|
1233
|
+
it('should handle GET requests', async () => {
|
|
1234
|
+
const app = createApp();
|
|
1235
|
+
|
|
1236
|
+
app.get('/test', async ctx => {
|
|
1237
|
+
ctx.json({ message: 'OK' });
|
|
1238
|
+
});
|
|
1239
|
+
|
|
1240
|
+
// Your test implementation
|
|
1241
|
+
});
|
|
361
1242
|
});
|
|
1243
|
+
```
|
|
1244
|
+
|
|
1245
|
+
---
|
|
1246
|
+
|
|
1247
|
+
## π₯ Enhanced Body Parser
|
|
1248
|
+
|
|
1249
|
+
Enterprise-grade body parsing with zero-copy operations:
|
|
362
1250
|
|
|
363
|
-
|
|
364
|
-
//
|
|
1251
|
+
```typescript
|
|
1252
|
+
// Auto-parsing (recommended)
|
|
1253
|
+
app.use(app.smartBodyParser());
|
|
1254
|
+
|
|
1255
|
+
// Advanced configuration
|
|
1256
|
+
app.use(
|
|
1257
|
+
app.smartBodyParser({
|
|
1258
|
+
maxSize: 10 * 1024 * 1024, // 10MB
|
|
1259
|
+
enableStreaming: true,
|
|
1260
|
+
autoDetectContentType: true,
|
|
1261
|
+
enableMetrics: true,
|
|
1262
|
+
})
|
|
1263
|
+
);
|
|
1264
|
+
|
|
1265
|
+
// Supports JSON, form-data, text, multipart
|
|
1266
|
+
app.post('/api/data', ctx => {
|
|
1267
|
+
const data = ctx.body; // Already parsed!
|
|
1268
|
+
ctx.json({ received: data });
|
|
1269
|
+
});
|
|
365
1270
|
```
|
|
366
1271
|
|
|
367
|
-
|
|
1272
|
+
**Features:** Zero-copy buffers β’ Auto-detection β’ Streaming β’ Memory-pooled β’ Cross-platform
|
|
1273
|
+
|
|
1274
|
+
π **[Full Documentation β](./docs/api/built-in-middleware.md)**
|
|
1275
|
+
|
|
1276
|
+
## π Documentation
|
|
1277
|
+
|
|
1278
|
+
Comprehensive guides, API references, and architecture deep-dives:
|
|
1279
|
+
|
|
1280
|
+
### Getting Started
|
|
368
1281
|
|
|
369
|
-
- **[
|
|
370
|
-
- **[
|
|
371
|
-
- **[
|
|
372
|
-
- **[π Benchmarks](./professional-benchmarks/)** - Complete performance testing suite
|
|
1282
|
+
- **[Getting Started Guide](./docs/guides/getting-started.md)** - Your first NextRush v2 app in 5 minutes
|
|
1283
|
+
- **[Migration from Express](./docs/guides/migration-guide.md)** - Migrate from Express.js
|
|
1284
|
+
- **[Migration from Fastify](./docs/guides/migration-guide.md)** - Migrate from Fastify
|
|
373
1285
|
|
|
374
|
-
|
|
1286
|
+
### Essential Guides
|
|
375
1287
|
|
|
376
|
-
|
|
1288
|
+
- **[Routing Guide](./docs/api/routing.md)** - All three routing styles explained (Convenience, Enhanced, Fastify-style)
|
|
1289
|
+
- **[Middleware Development](./docs/guides/middleware-development.md)** - Create custom middleware
|
|
1290
|
+
- **[Plugin Development](./docs/guides/plugin-development.md)** - Build powerful plugins
|
|
1291
|
+
- **[Error Handling Guide](./docs/guides/error-handling.md)** - Robust error management patterns
|
|
1292
|
+
- **[Testing Guide](./docs/guides/testing-guide.md)** - Unit, integration, and E2E testing
|
|
1293
|
+
- **[Production Deployment](./docs/guides/production-deployment.md)** - Deploy to production
|
|
377
1294
|
|
|
378
|
-
|
|
379
|
-
- Plugin systems and extensible architectures
|
|
380
|
-
- TypeScript advanced patterns and techniques
|
|
381
|
-
- Performance testing and optimization
|
|
382
|
-
- NPM package development and publishing
|
|
1295
|
+
### Core API Reference
|
|
383
1296
|
|
|
384
|
-
|
|
1297
|
+
- **[Application API](./docs/api/application.md)** - Core application methods and lifecycle
|
|
1298
|
+
- **[Context API](./docs/api/context.md)** - Request/response context (Koa-style)
|
|
1299
|
+
- **[Routing API](./docs/api/routing.md)** - Router and route handlers
|
|
1300
|
+
- **[Middleware API](./docs/api/middleware.md)** - Middleware system and patterns
|
|
1301
|
+
- **[Built-in Middleware](./docs/api/built-in-middleware.md)** - CORS, Helmet, Rate Limiting, Body Parser
|
|
1302
|
+
- **[Events API](./docs/api/events.md)** - Simple Events + Advanced Event System (CQRS)
|
|
1303
|
+
- **[Enhanced Request/Response](./docs/api/enhancers.md)** - Express-like helper methods
|
|
385
1304
|
|
|
386
|
-
|
|
387
|
-
- **Simple solutions** often outperform complex optimizations
|
|
388
|
-
- **Plugin systems** must be designed from day 1
|
|
389
|
-
- **Testing** should come before features, not after
|
|
1305
|
+
### Advanced Features
|
|
390
1306
|
|
|
391
|
-
|
|
1307
|
+
- **[Error Handling API](./docs/api/errors.md)** - Custom error classes and error middleware
|
|
1308
|
+
- **[Validation Utilities](./docs/api/validation-utilities.md)** - Schema validation and data sanitization
|
|
1309
|
+
- **[Configuration & Validation](./docs/api/configuration.md)** - Application configuration
|
|
1310
|
+
- **[Path Utilities](./docs/api/path-utilities.md)** - URL and path manipulation helpers
|
|
1311
|
+
- **[Developer Experience](./docs/api/developer-experience.md)** - Enhanced error messages and debugging
|
|
392
1312
|
|
|
393
|
-
|
|
1313
|
+
### Plugin APIs
|
|
394
1314
|
|
|
395
|
-
-
|
|
396
|
-
-
|
|
397
|
-
-
|
|
398
|
-
-
|
|
1315
|
+
- **[Logger Plugin](./docs/api/logger-plugin.md)** - Advanced structured logging with transports
|
|
1316
|
+
- **[WebSocket Plugin](./docs/api/websocket-plugin.md)** - Real-time WebSocket communication
|
|
1317
|
+
- **[Static Files Plugin](./docs/api/static-files-plugin.md)** - High-performance static file serving
|
|
1318
|
+
- **[Template Plugin](./docs/api/template-plugin.md)** - HTML template rendering engine
|
|
399
1319
|
|
|
400
|
-
|
|
1320
|
+
### Architecture Deep-Dives
|
|
401
1321
|
|
|
402
|
-
|
|
1322
|
+
- **[Plugin System](./docs/architecture/plugin-system.md)** - Plugin architecture and lifecycle
|
|
1323
|
+
- **[Dependency Injection](./docs/architecture/dependency-injection.md)** - DI container and service resolution
|
|
1324
|
+
- **[Orchestration System](./docs/architecture/orchestration-system.md)** - Application orchestration patterns
|
|
1325
|
+
- **[Optimized Router](./docs/architecture/optimized-router-deep-dive.md)** - Router internals and performance
|
|
1326
|
+
|
|
1327
|
+
### Performance & Benchmarks
|
|
1328
|
+
|
|
1329
|
+
- **Benchmarks** - Performance comparisons with Express, Koa, and Fastify
|
|
1330
|
+
- **Performance Guide** - Optimization techniques and best practices
|
|
1331
|
+
- **Memory Management** - Efficient memory usage patterns
|
|
1332
|
+
|
|
1333
|
+
## π€ Contributing
|
|
1334
|
+
|
|
1335
|
+
Contributions are welcome! Please see our [Contributing Guide](CONTRIBUTING.md) for details on how to get started.
|
|
1336
|
+
|
|
1337
|
+
## π License
|
|
1338
|
+
|
|
1339
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
1340
|
+
|
|
1341
|
+
## π Acknowledgments
|
|
1342
|
+
|
|
1343
|
+
- **Express.js** - Intuitive API design patterns
|
|
1344
|
+
- **Koa** - Powerful async middleware architecture
|
|
1345
|
+
- **Fastify** - Object-based route configuration approach
|
|
1346
|
+
- **TypeScript** - Type safety and enhanced developer experience
|
|
403
1347
|
|
|
404
1348
|
---
|
|
405
1349
|
|
|
406
1350
|
<div align="center">
|
|
407
1351
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
_Built with β€οΈ for education and shared for learning_
|
|
1352
|
+
**Built with precision by [Tanzim Hossain](https://github.com/0xTanzim)**
|
|
411
1353
|
|
|
412
|
-
[
|
|
1354
|
+
[Report Bug](https://github.com/0xTanzim/nextrush/issues) β’ [Request Feature](https://github.com/0xTanzim/nextrush/issues) β’ [Discussions](https://github.com/0xTanzim/nextrush/discussions)
|
|
413
1355
|
|
|
414
1356
|
</div>
|