Import
Examples
Single middleware
Pipeline composition
API
middlewarePipeline.use(middleware)
Add a middleware handler to the pipeline.
Returns: this
middlewarePipeline.useFor(pattern, )
Add a middleware handler that only runs for matching URL patterns.
Returns: this
middlewarePipeline.onTeardown(cb)
Register a cleanup callback that runs after the response is sent.
Returns: this
middlewarePipeline.compose()
Compose all registered middleware into a single handler function.
Returns: MiddlewareHandler
middlewarePipeline.execute(req, env, executionCtx, adapter)
Execute the pipeline for an incoming request.
Returns: Promise<Response>
middlewarePipeline.handle(req, handler)
Run the middleware pipeline with a final request handler. Unlike execute, which returns a 404 when no middleware responds, handle invokes the given handler as the terminal step so middleware can add headers, validate auth, etc. before the handler runs.
Returns: Promise<Response>
middlewarePipeline.teardown()
Run all registered teardown callbacks.
Returns: Promise<void>
middlewarePipeline.getMiddleware()
List registered middleware with metadata.
Returns: Array<{ name?: string; order?: number }>
Type Reference
CorsOptions
Options accepted by cors.
| Property | Type | Description | Source |
|---|---|---|---|
origin? | string | string[] | OriginValidator | Allowed origins (string, regex, array, or function) | source |
methods? | string[] | Allowed HTTP methods | source |
allowedHeaders? | string[] | Allowed request headers | source |
exposedHeaders? | string[] | Headers exposed to client | source |
credentials? | boolean | Allow credentials | source |
maxAge? | number | Preflight cache duration (seconds) | source |
RateLimitOptions
Options accepted by rate limit.
LoggerOptions
Options accepted by logger.
TimeoutOptions
Options accepted by timeout.
Exports
Functions
| Name | Description | Source |
|---|---|---|
cors | Create CORS middleware. | source |
devLogger | Create development request logging middleware. | source |
getTimeoutFromEnv | Gets timeout from environment variable REQUEST_TIMEOUT_MS | source |
logger | Create request logging middleware. | source |
prodLogger | Create production request logging middleware. | source |
rateLimit | Create rate-limit middleware. | source |
timeout | Creates a middleware that enforces request timeouts. | source |
timeoutFromEnv | Creates a timeout middleware with configuration from environment | source |
Classes
Types
| Name | Description | Source |
|---|---|---|
Context | Context for context. | source |
CorsOptions | Options accepted by cors. | source |
ExecutionContext | Context for execution. | source |
LogFormat | Public API contract for log format. | source |
LoggerOptions | Options accepted by logger. | source |
MiddlewareFactory | Public API contract for middleware factory. | source |
MiddlewareHandler | Handler for middleware. | source |
MiddlewarePipelineOptions | Options accepted by middleware pipeline. | source |
Next | Public API contract for next. | source |
RateLimitOptions | Options accepted by rate limit. | source |
RateLimitStore | Public API contract for rate limit store. | source |
RedisRateLimitOptions | Options accepted by redis rate limit. | source |
TimeoutOptions | Options accepted by timeout. | source |