createFetchHandler takes an array of handlers and returns a Bun-compatible fetch function. It processes handlers in order, logs requests, and handles errors gracefully.
Basic Usage
How It Works
- Handler Chain: Processes handlers in order until one matches
- Request Logging: Automatically logs request start/end with status codes
- Error Handling: Catches all errors and returns 500 responses
- Request ID: Generates a unique 6-character ID for each request
- OpenTelemetry: Creates a span named
httpfor each request
Handler Matching
Handlers are evaluated sequentially. The first handler that matches processes the request:If no handlers match, a
404 Not Found response is returned automatically.Type Inference
The return type infers all service requirements from your handlers:Error Handling
Default Behavior
Errors are logged and return a 500 response:Custom Error Handler
Use theonError option to hook into error events:
onError is for side effects only.
Debug Mode
Enable detailed logging for development:Request Lifecycle
Complete Example
src/server.ts
Logging
All requests are automatically logged with:- Start:
Request startedwith pathname - End:
Request completed with status {code}(info for 2xx/3xx, warn for others) - Request ID: Unique 6-character ID attached to all logs