HttpMiddleware
Constructors
Signature
declare const cors: (options?: {
readonly allowedHeaders?: ReadonlyArray<string>;
readonly allowedMethods?: ReadonlyArray<string>;
readonly allowedOrigins?: ReadonlyArray<string> | Predicate.Predicate<string>;
readonly credentials?: boolean;
readonly exposedHeaders?: ReadonlyArray<string>;
readonly maxAge?: number;
}) => <E, R>(httpApp: App.Default<E, R>) => App.Default<E, R>;Example
import { HttpMiddleware, HttpRouter, HttpServerResponse } from "@effect/platform"
// Allow all origins and reflect requested headers (default behavior)
HttpRouter.empty.pipe(HttpRouter.get("/", HttpServerResponse.empty()), HttpMiddleware.cors())
// Restrict to specific origins and headers
HttpRouter.empty.pipe(
HttpRouter.get("/", HttpServerResponse.empty()),
HttpMiddleware.cors({
allowedOrigins: ["https://example.com"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
}),
)
// Dynamic origin checking with predicate
HttpRouter.empty.pipe(
HttpRouter.get("/", HttpServerResponse.empty()),
HttpMiddleware.cors({
allowedOrigins: (origin) => origin.endsWith(".example.com"),
}),
)Signature
declare const logger: <E, R>(httpApp: App.Default<E, R>) => App.Default<E, R>;Signature
declare const make: <M extends HttpMiddleware>(middleware: M) => M;searchParamsParser
Added in v1.0.0
Source
Signature
declare const searchParamsParser: <E, R>(
httpApp: App.Default<E, R>,
) => App.Default<E, Exclude<R, ServerRequest.ParsedSearchParams>>;xForwardedHeaders
Added in v1.0.0
Source
Signature
declare const xForwardedHeaders: <E, R>(httpApp: App.Default<E, R>) => App.Default<E, R>;Fiber Refs
currentTracerDisabledWhen
Added in v1.0.0
Source
Signature
declare const currentTracerDisabledWhen: FiberRef.FiberRef<
Predicate.Predicate<ServerRequest.HttpServerRequest>
>;loggerDisabled
Added in v1.0.0
Source
Signature
declare const loggerDisabled: FiberRef.FiberRef<boolean>;withLoggerDisabled
Added in v1.0.0
Source
Signature
declare const withLoggerDisabled: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;withTracerDisabledForUrls
Added in v1.0.0
Source
Signature
declare const withTracerDisabledForUrls: {
(urls: readonly Array<string>): <A, E, R>(layer: Layer<A, E, R>) => Layer<A, E, R>;
<A, E, R>(layer: Layer<A, E, R>, urls: readonly Array<string>): Layer<A, E, R>;
}withTracerDisabledWhen
Added in v1.0.0
Source
Signature
declare const withTracerDisabledWhen: {
(predicate: Predicate<HttpServerRequest>): <A, E, R>(layer: Layer<A, E, R>) => Layer<A, E, R>;
<A, E, R>(layer: Layer<A, E, R>, predicate: Predicate<HttpServerRequest>): Layer<A, E, R>;
};withTracerDisabledWhenEffect
Added in v1.0.0
Source
Signature
declare const withTracerDisabledWhenEffect: {
(predicate: Predicate<HttpServerRequest>): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>;
<A, E, R>(effect: Effect<A, E, R>, predicate: Predicate<HttpServerRequest>): Effect<A, E, R>;
};Models
HttpMiddleware interface
Added in v1.0.0
Source
Signature
interface HttpMiddleware {
<E, R>(self: Default<E, R>): Default<any, any>;
}Other
HttpMiddleware
Added in v1.0.0
Source
Tracing
SpanNameGenerator
Added in v1.0.0
Source
Signature
declare const SpanNameGenerator: any;SpanNameGenerator interface
Added in v1.0.0
Source
Signature
interface SpanNameGenerator {
readonly _: typeof _;
}withSpanNameGenerator
Added in v1.0.0
Source
Customizes the span name for the http app.
Signature
declare const withSpanNameGenerator: {
(f: (request: HttpServerRequest) => string): <A, E, R>(layer: Layer<A, E, R>) => Layer<A, E, R>;
<A, E, R>(layer: Layer<A, E, R>, f: (request: HttpServerRequest) => string): Layer<A, E, R>;
};Example
import { HttpMiddleware, HttpRouter, HttpServer, HttpServerResponse } from "@effect/platform"
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node"
import { Layer } from "effect"
import { createServer } from "http"
HttpRouter.empty.pipe(
HttpRouter.get("/", HttpServerResponse.empty()),
HttpServer.serve(),
// Customize the span names for this HttpApp
HttpMiddleware.withSpanNameGenerator((request) => `GET ${request.url}`),
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
Layer.launch,
NodeRuntime.runMain,
)
Creates a CORS (Cross-Origin Resource Sharing) middleware for HTTP applications.