Skip to main content

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.
PropertyTypeDescriptionSource
origin?string | string[] | OriginValidatorAllowed origins (string, regex, array, or function)source
methods?string[]Allowed HTTP methodssource
allowedHeaders?string[]Allowed request headerssource
exposedHeaders?string[]Headers exposed to clientsource
credentials?booleanAllow credentialssource
maxAge?numberPreflight cache duration (seconds)source

RateLimitOptions

Options accepted by rate limit.
PropertyTypeDescriptionSource
maxRequests?numberMax requests per windowsource
windowMs?numberTime window (ms)source
store?RateLimitStoreStorage backendsource
keyGenerator?(req: Request) => stringFunction to derive rate limit key from requestsource

LoggerOptions

Options accepted by logger.
PropertyTypeDescriptionSource
format?LogFormatLog format (combined, common, dev, short)source
skip?(req: Request) => booleanSkip logging for matching requestssource
log?(message: string) => voidCustom log output functionsource

TimeoutOptions

Options accepted by timeout.
PropertyTypeDescriptionSource
timeoutMs?numberTimeout in milliseconds (default: 60000)source
message?stringCustom message for timeout responsesource
exclude?string[]Paths to exclude from timeout (e.g., health checks)source

Exports

Functions

NameDescriptionSource
corsCreate CORS middleware.source
devLoggerCreate development request logging middleware.source
getTimeoutFromEnvGets timeout from environment variable REQUEST_TIMEOUT_MSsource
loggerCreate request logging middleware.source
prodLoggerCreate production request logging middleware.source
rateLimitCreate rate-limit middleware.source
timeoutCreates a middleware that enforces request timeouts.source
timeoutFromEnvCreates a timeout middleware with configuration from environmentsource

Classes

NameDescriptionSource
MemoryRateLimitStoreImplement memory rate limit store.source
MiddlewareContextContext for middleware.source
MiddlewarePipelineImplement middleware pipeline.source
RedisRateLimitStoreImplement redis rate limit store.source

Types

NameDescriptionSource
ContextContext for context.source
CorsOptionsOptions accepted by cors.source
ExecutionContextContext for execution.source
LogFormatPublic API contract for log format.source
LoggerOptionsOptions accepted by logger.source
MiddlewareFactoryPublic API contract for middleware factory.source
MiddlewareHandlerHandler for middleware.source
MiddlewarePipelineOptionsOptions accepted by middleware pipeline.source
NextPublic API contract for next.source
RateLimitOptionsOptions accepted by rate limit.source
RateLimitStorePublic API contract for rate limit store.source
RedisRateLimitOptionsOptions accepted by redis rate limit.source
TimeoutOptionsOptions accepted by timeout.source
User guides: