Skip to content

Commit b0f4cb4

Browse files
committed
docs: enhance README.md with detailed middleware documentation and examples
1 parent 8acbcb7 commit b0f4cb4

File tree

3 files changed

+1386
-56
lines changed

3 files changed

+1386
-56
lines changed

README.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A high-performance, minimalist HTTP framework for [Bun](https://bun.sh/), inspir
55
## Key Benefits
66

77
- **🚀 Bun-Native Performance**: Optimized for Bun's runtime with minimal overhead
8-
- **Zero Dependencies**: Core framework uses only essential, lightweight dependencies
8+
- **Minimal Dependencies**: Only 2 essential dependencies (`trouter`, `fast-querystring`)
99
- **🔧 TypeScript First**: Full TypeScript support with comprehensive type definitions
1010
- **🎯 Minimalist API**: Clean, intuitive API that's easy to learn and use
1111
- **🔄 Middleware Support**: Flexible middleware system with async/await support
@@ -203,6 +203,42 @@ Bun.serve({
203203
})
204204
```
205205

206+
## Middleware Support
207+
208+
0http-bun includes a comprehensive middleware system with built-in middlewares for common use cases:
209+
210+
- **[Body Parser](./lib/middleware/README.md#body-parser)** - Automatic request body parsing (JSON, form data, text)
211+
- **[CORS](./lib/middleware/README.md#cors)** - Cross-Origin Resource Sharing with flexible configuration
212+
- **[JWT Authentication](./lib/middleware/README.md#jwt-authentication)** - JSON Web Token authentication and authorization
213+
- **[Logger](./lib/middleware/README.md#logger)** - Request logging with multiple output formats
214+
- **[Rate Limiting](./lib/middleware/README.md#rate-limiting)** - Flexible rate limiting with sliding window support
215+
216+
### Quick Example
217+
218+
```javascript
219+
// Import middleware functions from the middleware module
220+
const {
221+
createCORS,
222+
createLogger,
223+
createBodyParser,
224+
createJWTAuth,
225+
createRateLimit,
226+
} = require('0http-bun/lib/middleware')
227+
228+
const {router} = http()
229+
230+
// Apply middleware stack
231+
router.use(createCORS()) // Enable CORS
232+
router.use(createLogger()) // Request logging
233+
router.use(createBodyParser()) // Parse request bodies
234+
router.use(createRateLimit({max: 100})) // Rate limiting
235+
236+
// Protected routes
237+
router.use('/api/*', createJWTAuth({secret: process.env.JWT_SECRET}))
238+
```
239+
240+
📖 **[Complete Middleware Documentation](./lib/middleware/README.md)**
241+
206242
### Error Handling
207243

208244
```typescript
@@ -245,8 +281,9 @@ router.get('/api/risky', (req: ZeroRequest) => {
245281

246282
- **Minimal overhead**: Direct use of Web APIs
247283
- **Efficient routing**: Based on the proven `trouter` library
248-
- **Fast parameter parsing**: Optimized URL parameter extraction
249-
- **Query string parsing**: Uses `fast-querystring` for performance
284+
- **Fast parameter parsing**: Optimized URL parameter extraction with caching
285+
- **Query string parsing**: Uses `fast-querystring` for optimal performance
286+
- **Memory efficient**: Route caching and object reuse to minimize allocations
250287

251288
### Benchmark Results
252289

@@ -256,18 +293,51 @@ Run benchmarks with:
256293
bun run bench
257294
```
258295

296+
_Performance characteristics will vary based on your specific use case and middleware stack._
297+
259298
## TypeScript Support
260299

261300
Full TypeScript support is included with comprehensive type definitions:
262301

263302
```typescript
303+
// Main framework types
264304
import {
265305
ZeroRequest,
266306
StepFunction,
267307
RequestHandler,
268308
IRouter,
269309
IRouterConfig,
270310
} from '0http-bun'
311+
312+
// Middleware-specific types
313+
import {
314+
LoggerOptions,
315+
JWTAuthOptions,
316+
APIKeyAuthOptions,
317+
RateLimitOptions,
318+
CORSOptions,
319+
BodyParserOptions,
320+
MemoryStore,
321+
} from '0http-bun/lib/middleware'
322+
323+
// Example typed middleware
324+
const customMiddleware: RequestHandler = (
325+
req: ZeroRequest,
326+
next: StepFunction,
327+
) => {
328+
req.ctx = req.ctx || {}
329+
req.ctx.timestamp = Date.now()
330+
return next()
331+
}
332+
333+
// Example typed route handler
334+
const typedHandler = (req: ZeroRequest): Response => {
335+
return Response.json({
336+
params: req.params,
337+
query: req.query,
338+
context: req.ctx,
339+
})
340+
}
271341
```
272342

273343
## License

0 commit comments

Comments
 (0)