Skip to content

HttpMiddleware

16 exports Added in v1.0.0 Source

Constructors

cors

Added in v1.0.0 Source

Creates a CORS (Cross-Origin Resource Sharing) middleware for HTTP applications.

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"),
  }),
)

logger

Added in v1.0.0 Source

Signature

declare const logger: <E, R>(httpApp: App.Default<E, R>) => App.Default<E, R>;

make

Added in v1.0.0 Source

Signature

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

Signature

declare const searchParamsParser: <E, R>(
  httpApp: App.Default<E, R>,
) => App.Default<E, Exclude<R, ServerRequest.ParsedSearchParams>>;

Signature

declare const xForwardedHeaders: <E, R>(httpApp: App.Default<E, R>) => App.Default<E, R>;

Fiber Refs

Signature

declare const currentTracerDisabledWhen: FiberRef.FiberRef<
  Predicate.Predicate<ServerRequest.HttpServerRequest>
>;

Signature

declare const loggerDisabled: FiberRef.FiberRef<boolean>;

Signature

declare const withLoggerDisabled: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;

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

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

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

Tracing

Signature

declare const SpanNameGenerator: any;

SpanNameGenerator interface

Added in v1.0.0 Source

Signature

interface SpanNameGenerator {
  readonly _: typeof _;
}

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