HttpMiddleware.ts
HttpMiddleware.ts overview
Section titled “HttpMiddleware.ts overview”Wraps HTTP server apps with request and response behavior.
A middleware is a function from one HTTP server app effect to another. The app
runs with the current HttpServerRequest in its context, so middleware can
inspect or rewrite the request, provide request-scoped services, attach hooks
before the response is sent, or observe the app exit. This module includes
middleware for response logging, server tracing, forwarded proxy headers,
parsed search parameters, and CORS response headers.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”Middleware that handles CORS preflight requests and adds configured CORS headers to HTTP responses.
Signature
declare const cors: (options?: { readonly allowedOrigins?: ReadonlyArray<string> | Predicate<string> | undefined readonly allowedMethods?: ReadonlyArray<string> | undefined readonly allowedHeaders?: ReadonlyArray<string> | undefined readonly exposedHeaders?: ReadonlyArray<string> | undefined readonly maxAge?: number | undefined readonly credentials?: boolean | undefined}) => <E, R>( httpApp: Effect.Effect<HttpServerResponse, E, R>) => Effect.Effect<HttpServerResponse, E, R | HttpServerRequest>Since v4.0.0
Logger
Section titled “Logger”logger
Section titled “logger”Middleware that logs sent HTTP responses with request method, request URL, and response status annotations.
Signature
declare const logger: <E, R>( httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>) => Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>Since v4.0.0
withLoggerDisabled
Section titled “withLoggerDisabled”Runs an effect with HTTP response logging disabled for the current server request.
Signature
declare const withLoggerDisabled: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | HttpServerRequest>Since v4.0.0
Proxying
Section titled “Proxying”xForwardedHeaders
Section titled “xForwardedHeaders”Middleware that trusts X-Forwarded-Host and X-Forwarded-For, updating the request host header and remote address.
Signature
declare const xForwardedHeaders: <E, R>( httpApp: Effect.Effect<Response.HttpServerResponse, E, HttpServerRequest | R>) => Effect.Effect<Response.HttpServerResponse, E, HttpServerRequest | R>Since v4.0.0
Tracer
Section titled “Tracer”SpanNameGenerator
Section titled “SpanNameGenerator”Context reference for generating server span names from HTTP server requests.
Signature
declare const SpanNameGenerator: Context.Reference<(request: HttpServerRequest) => string>Since v4.0.0
TracerDisabledWhen
Section titled “TracerDisabledWhen”Context reference for a predicate that disables server-side tracing for matching requests.
Signature
declare const TracerDisabledWhen: Context.Reference<Predicate<HttpServerRequest>>Since v4.0.0
layerTracerDisabledForUrls
Section titled “layerTracerDisabledForUrls”Creates a layer that disables server-side tracing for requests whose URL exactly matches one of the supplied URLs.
Signature
declare const layerTracerDisabledForUrls: (urls: ReadonlyArray<string>) => Layer.Layer<never>Since v4.0.0
tracer
Section titled “tracer”Middleware that creates a server trace span for each request and records request and response HTTP attributes.
Signature
declare const tracer: <E, R>( httpApp: Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>) => Effect.Effect<HttpServerResponse, E, HttpServerRequest | R>Since v4.0.0
constructors
Section titled “constructors”Defines an HttpMiddleware while preserving its precise type.
Signature
declare const make: <M extends HttpMiddleware>(middleware: M) => MSince v4.0.0
models
Section titled “models”HttpMiddleware (interface)
Section titled “HttpMiddleware (interface)”Middleware that transforms an HTTP server app effect into another HTTP server app effect.
Signature
export interface HttpMiddleware { <E, R>(self: Effect.Effect<HttpServerResponse, E, R | HttpServerRequest>): Effect.Effect<HttpServerResponse, any, any>}Since v4.0.0
search params
Section titled “search params”searchParamsParser
Section titled “searchParamsParser”Middleware that parses the current request URL’s search parameters and provides them as ParsedSearchParams.
Signature
declare const searchParamsParser: <E, R>( httpApp: Effect.Effect<HttpServerResponse, E, R>) => Effect.Effect<Response.HttpServerResponse, E, HttpServerRequest | Exclude<R, Request.ParsedSearchParams>>Since v4.0.0
HttpMiddleware (namespace)
Section titled “HttpMiddleware (namespace)”Namespace containing types associated with HttpMiddleware.
Since v4.0.0
Applied (interface)
Section titled “Applied (interface)”Callable type representing middleware already specialized to a particular transformed app type.
Signature
export interface Applied<A extends Effect.Effect<HttpServerResponse, any, any>, E, R> { (self: Effect.Effect<HttpServerResponse, E, R>): A}Since v4.0.0