Documentation Index
Fetch the complete documentation index at: https://veryfront.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
veryfront/middleware
CORS, rate limiting, logging, and timeout middleware.
Import
import {
cors,
rateLimit,
logger,
timeout,
MiddlewarePipeline,
devLogger,
} from "veryfront/middleware";
Examples
Single middleware
import { cors } from "veryfront/middleware";
const corsMiddleware = cors({ origin: "https://example.com" });
Pipeline composition
import { MiddlewarePipeline, cors, rateLimit, logger, timeout } from "veryfront/middleware";
const pipeline = new MiddlewarePipeline()
.use(cors({ origin: "https://example.com" }))
.use(rateLimit({ maxRequests: 100, windowMs: 60_000 }))
.use(logger({ format: "combined" }))
.use(timeout({ timeoutMs: 30_000 }));
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.
| Property | Type | Description | Source |
|---|
maxRequests? | number | Max requests per window | source |
windowMs? | number | Time window (ms) | source |
store? | RateLimitStore | Storage backend | source |
keyGenerator? | (req: Request) => string | Function to derive rate limit key from request | source |
LoggerOptions
Options accepted by logger.
| Property | Type | Description | Source |
|---|
format? | LogFormat | Log format (combined, common, dev, short) | source |
skip? | (req: Request) => boolean | Skip logging for matching requests | source |
log? | (message: string) => void | Custom log output function | source |
TimeoutOptions
Options accepted by timeout.
| Property | Type | Description | Source |
|---|
timeoutMs? | number | Timeout in milliseconds (default: 60000) | source |
message? | string | Custom message for timeout response | source |
exclude? | string[] | Paths to exclude from timeout (e.g., health checks) | source |
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
| Name | Description | Source |
|---|
MemoryRateLimitStore | Implement memory rate limit store. | source |
MiddlewareContext | Context for middleware. | source |
MiddlewarePipeline | Implement middleware pipeline. | source |
RedisRateLimitStore | Implement redis rate limit store. | source |
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 |
User guides: