Skip to main content
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

  1. Handler Chain: Processes handlers in order until one matches
  2. Request Logging: Automatically logs request start/end with status codes
  3. Error Handling: Catches all errors and returns 500 responses
  4. Request ID: Generates a unique 6-character ID for each request
  5. OpenTelemetry: Creates a span named http for 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:
Provide the required services before running:

Error Handling

Default Behavior

Errors are logged and return a 500 response:

Custom Error Handler

Use the onError option to hook into error events:
The error is still logged and returns 500 - onError is for side effects only.

Debug Mode

Enable detailed logging for development:
Logs handler execution and results for each request:

Request Lifecycle

Complete Example

src/server.ts

Logging

All requests are automatically logged with:
  • Start: Request started with pathname
  • End: Request completed with status {code} (info for 2xx/3xx, warn for others)
  • Request ID: Unique 6-character ID attached to all logs
Example output:
See Logger for customizing log output.