Import
import { authRateLimit, cors, logger, MiddlewarePipeline, rateLimit, timeout,} from "veryfront/middleware";Examples
Single middleware
import { cors } from "veryfront/middleware"; const corsMiddleware = cors({ origin: "https://example.com" });Pipeline composition
import { cors, logger, MiddlewarePipeline, rateLimit, 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 once per request after each execute()/handle() response body closes, is canceled, or errors. Bodyless, locked, or already-read responses and handler/middleware exceptions clean up before the call resolves.
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: <code>Promise<Response></code>
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: <code>Promise<Response></code>
middlewarePipeline.teardown()
Drain and discard all registered teardown callbacks. Unlike the per-request cleanup run by execute() / handle(), this clears callbacks so they never run again.
Returns: <code>Promise<void></code>
middlewarePipeline.getMiddleware()
List registered middleware with metadata.
Returns: <code>Array<{ name?: string; order?: number }></code>
Type Reference
CorsOptions
Options accepted by cors.
RateLimitOptions
Options accepted by rate limit.
MemoryRateLimitStoreOptions
Options accepted by the in-memory rate limit store.
LoggerOptions
Options accepted by logger.
TimeoutOptions
Options accepted by timeout.