Skip to content

HttpMiddleware.ts

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



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>

Source

Since v4.0.0

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>

Source

Since v4.0.0

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>

Source

Since v4.0.0

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>

Source

Since v4.0.0

Context reference for generating server span names from HTTP server requests.

Signature

declare const SpanNameGenerator: Context.Reference<(request: HttpServerRequest) => string>

Source

Since v4.0.0

Context reference for a predicate that disables server-side tracing for matching requests.

Signature

declare const TracerDisabledWhen: Context.Reference<Predicate<HttpServerRequest>>

Source

Since v4.0.0

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>

Source

Since v4.0.0

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>

Source

Since v4.0.0

Defines an HttpMiddleware while preserving its precise type.

Signature

declare const make: <M extends HttpMiddleware>(middleware: M) => M

Source

Since v4.0.0

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>
}

Source

Since v4.0.0

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>>

Source

Since v4.0.0

Namespace containing types associated with HttpMiddleware.

Source

Since v4.0.0

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
}

Source

Since v4.0.0