(
evaluate: LazyArg<Http.Server>,
options: Config.Wrap<
Net.ListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefined
}
>
): Layer.Layer<
| HttpServer.HttpServer
| NodeServices.NodeServices
| HttpPlatform.HttpPlatform
| Etag.Generator,
ServeError | Config.ConfigError
>Provides a Node HttpServer together with the Node HTTP platform, ETag,
and core Node platform services, reading the listen and shutdown options from
a Config value.
export const const layerConfig: (
evaluate: LazyArg<Http.Server>,
options: Config.Wrap<
Net.ListenOptions & {
readonly disablePreemptiveShutdown?:
| boolean
| undefined
readonly gracefulShutdownTimeout?:
| Duration.Input
| undefined
}
>
) => Layer.Layer<
| HttpServer.HttpServer
| NodeServices.NodeServices
| HttpPlatform.HttpPlatform
| Etag.Generator,
ServeError | Config.ConfigError
>
Provides a Node HttpServer together with the Node HTTP platform, ETag,
and core Node platform services, reading the listen and shutdown options from
a Config value.
layerConfig = (
evaluate: LazyArg<Http.Server>evaluate: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<import HttpHttp.type Http.Server = /*unresolved*/ anyServer>,
options: Config.Wrap<
Net.ListenOptions & {
readonly disablePreemptiveShutdown?:
| boolean
| undefined
readonly gracefulShutdownTimeout?:
| Duration.Input
| undefined
}
>
(parameter) options: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
options: import ConfigConfig.type Wrap<A> = [NonNullable<A>] extends [
infer T
]
? [IsPlainObject<T>] extends [true]
?
| Config.Config<A>
| {
readonly [K in keyof A]: Config.Wrap<
A[K]
>
}
: Config.Config<A>
: Config.Config<A>
Utility type that recursively replaces primitives with Config in a nested
structure.
When to use
Use when typing the input of
unwrap
so callers can pass either a Config
or a record of Configs.
Details
Config.Wrap<{ key: string }> becomes { key: Config<string> } | Config<{ key: string }>
Wrap<
import NetNet.type Net.ListenOptions = /*unresolved*/ anyListenOptions & {
readonly disablePreemptiveShutdown?: boolean | undefineddisablePreemptiveShutdown?: boolean | undefined
readonly gracefulShutdownTimeout?: Duration.Input | undefinedgracefulShutdownTimeout?: import DurationDuration.type Input =
| number
| bigint
| Duration.Duration
| readonly [seconds: number, nanos: number]
| `${number} nano`
| `${number} nanos`
| `${number} micro`
| `${number} micros`
| `${number} milli`
| `${number} millis`
| `${number} second`
| `${number} seconds`
| `${number} minute`
| `${number} minutes`
| `${number} hour`
| `${number} hours`
| `${number} day`
| `${number} days`
| `${number} week`
| `${number} weeks`
| "Infinity"
| "-Infinity"
| Duration.DurationObject
Valid input types that can be converted to a Duration.
When to use
Use when an API should accept any value that Effect can convert into a
Duration, including existing durations, millisecond numbers, nanosecond
bigints, high-resolution tuples, duration strings, infinity strings, or
duration objects.
Details
String inputs accept values like "10 seconds", "500 millis",
"Infinity", and "-Infinity". Finite fractional values that are
normalized to nanoseconds are rounded to the nearest nanosecond, with ties
away from zero.
Input | undefined
}
>
): import LayerLayer.interface Layer<in ROut, out E = never, out RIn = never>A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection,
especially when services have dependencies, can fail during construction, or
need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer
provides, E as the possible errors during layer construction, and RIn as
the services this layer requires as dependencies.
Layer<
import HttpServerHttpServer.class HttpServerclass HttpServer {
key: Identifier;
Service: {
serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect: Effect.Effect<HttpServerResponse, E…;
address: Address;
};
}
Service tag for an HTTP server runtime.
Details
The service can serve an HTTP response effect and exposes the address where the
server is listening.
HttpServer | import NodeServicesNodeServices.type NodeServices =
| FileSystem.FileSystem
| Path.Path
| ChildProcessSpawner
| Crypto
| Stdio
| Terminal
The union of core services provided by the Node platform layer, including
child process spawning, filesystem, path, stdio, and terminal services.
NodeServices | import HttpPlatformHttpPlatform.class HttpPlatformclass HttpPlatform {
key: Identifier;
Service: {
fileResponse: (path: string, options?: Response.Options.WithContent & { readonly bytesToRead?: FileSystem.SizeInput | undefined; readonly chunkSize?: FileSystem.SizeInput | undefined; readonly offset?: FileSystem.SizeInput | undefined }) => Effect.Effec…;
fileWebResponse: (file: Body.HttpBody.FileLike, options?: Response.Options.WithContent & { readonly bytesToRead?: FileSystem.SizeInput | undefined; readonly chunkSize?: FileSystem.SizeInput | undefined; readonly offset?: FileSystem.SizeInput | undefined })…;
};
}
Service for platform-specific HTTP response helpers, including file-backed server responses.
HttpPlatform | import EtagEtag.class Generatorclass Generator {
key: Identifier;
Service: {
fromFileInfo: (info: FileSystem.File.Info) => Effect.Effect<Etag>;
fromFileWeb: (file: Body.HttpBody.FileLike) => Effect.Effect<Etag>;
};
}
Service for generating ETags from filesystem file information or Web File-like metadata.
Generator,
class ServeErrorclass ServeError {
name: string;
message: string;
stack: string;
cause: unknown;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
_tag: Tag;
}
Error wrapping a low-level failure from the HTTP server implementation.
ServeError | import ConfigConfig.class ConfigErrorclass ConfigError {
_tag: 'ConfigError';
name: string;
cause: SourceError | Schema.SchemaError;
message: string;
toString: () => string;
}
Represents the error type produced when config loading or validation fails.
When to use
Use when you need to inspect config loading or validation failures.
Details
Wraps either:
- A
SourceError — the provider could not read data (I/O failure).
- A
SchemaError — the data was found but did not match the schema
(wrong type, out of range, missing key, etc.).
ConfigError
> =>
import LayerLayer.const mergeAll: <
Layers extends [
Layer<never, any, any>,
...Array<Layer<never, any, any>>
]
>(
...layers: Layers
) => Layer<
Success<Layers[number]>,
Error<Layers[number]>,
Services<Layers[number]>
>
Combines all the provided layers concurrently, creating a new layer with
merged input, error, and output types.
When to use
Use when you need to combine multiple independent layers.
Details
All layers are built concurrently, and their outputs are merged into a single layer.
If multiple merged layers depend on the same layer value, that dependency is
shared by default. Reuse a named layer value when you want services to share
the same resource, such as one database pool.
Example (Merging independent layers)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
class Logger extends Context.Service<Logger, {
readonly log: (msg: string) => Effect.Effect<void>
}>()("Logger") {}
const dbLayer = Layer.succeed(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed("result"))
})
const loggerLayer = Layer.succeed(Logger, {
log: Effect.fn("Logger.log")((msg: string) => Effect.sync(() => console.log(msg)))
})
const mergedLayer = Layer.mergeAll(dbLayer, loggerLayer)
mergeAll(
import LayerLayer.const effect: {
<I, S>(service: Context.Key<I, S>): <E, R>(
effect: Effect<S, E, R>
) => Layer<I, E, Exclude<R, Scope.Scope>>
<I, S, E, R>(
service: Context.Key<I, S>,
effect: Effect<Types.NoInfer<S>, E, R>
): Layer<I, E, Exclude<R, Scope.Scope>>
}
Constructs a layer from an effect that produces a single service.
When to use
Use when you need to construct a Layer-provided service with an Effect,
dependencies, or scoped resource acquisition.
Details
This allows you to create a Layer from an Effect that produces a service.
The Effect is executed in the scope of the layer, allowing for proper
resource management.
Example (Creating a layer from an effect)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
const layer = Layer.effect(Database,
Effect.sync(() => ({
query: (sql: string) => Effect.succeed(`Query: ${sql}`)
}))
)
effect(import HttpServerHttpServer.class HttpServerclass HttpServer {
key: Identifier;
Service: {
serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect: Effect.Effect<HttpServerResponse, E…;
address: Address;
};
of: (this: void, self: { readonly serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect…;
context: (self: { readonly serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect: Effect.Eff…;
use: (f: (service: { readonly serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect: Eff…;
useSync: (f: (service: { readonly serve: { <E, R>(effect: Effect.Effect<HttpServerResponse, E, R>): Effect.Effect<void, never, Exclude<R, HttpServerRequest> | Scope.Scope>; <E, R, App extends Effect.Effect<HttpServerResponse, any, any>>(effect: Eff…;
Identifier: Identifier;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Service tag for an HTTP server runtime.
Details
The service can serve an HTTP response effect and exposes the address where the
server is listening.
HttpServer)(
import EffectEffect.const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
}
Chains effects to produce new Effect instances, useful for combining
operations that depend on previous results.
When to use
Use when you need to chain multiple effects, ensuring that each
step produces a new Effect while flattening any nested effects that may
occur.
Details
flatMap lets you sequence effects so that the result of one effect can be
used in the next step. It is similar to flatMap used with arrays but works
specifically with Effect instances, allowing you to avoid deeply nested
effect structures.
Since effects are immutable, flatMap always returns a new effect instead of
changing the original one.
Example (Choosing flatMap syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => Effect.succeed(n + 1)
const flatMappedWithPipe = pipe(myEffect, Effect.flatMap(transformation))
const flatMappedWithDataFirst = Effect.flatMap(myEffect, transformation)
const flatMappedWithMethod = myEffect.pipe(Effect.flatMap(transformation))
Example (Sequencing dependent effects)
import { Data, Effect, pipe } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
// Function to apply a discount safely to a transaction amount
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
// Simulated asynchronous task to fetch a transaction amount from database
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
// Chaining the fetch and discount application using `flatMap`
const finalAmount = pipe(
fetchTransactionAmount,
Effect.flatMap((amount) => applyDiscount(amount, 5))
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 95
flatMap(import ConfigConfig.const unwrap: <T>(
wrapped: Wrap<T>
) => Config<T>
Constructs a Config<T> from a value matching Wrap<T>.
When to use
Use when accepting config from callers who may pass either a single Config or a
record of individual Configs.
Details
If the input is already a Config, it is returned as-is. Otherwise, each
key is recursively unwrapped and combined.
Example (Unwrapping a record of configs)
import { Config } from "effect"
interface Options {
key: string
}
const makeConfig = (config: Config.Wrap<Options>): Config.Config<Options> =>
Config.unwrap(config)
unwrap(options: Config.Wrap<
Net.ListenOptions & {
readonly disablePreemptiveShutdown?:
| boolean
| undefined
readonly gracefulShutdownTimeout?:
| Duration.Input
| undefined
}
>
(parameter) options: {
parse: (provider: ConfigProvider.ConfigProvider, pathPrefix?: Path) => Effect.Effect<T, ConfigError>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
options), (options: anyoptions) => const make: (
evaluate: LazyArg<Http.Server>,
options: any
) => Effect.Effect<
{
readonly serve: {
<E, R>(
effect: Effect.Effect<
HttpServerResponse,
E,
R
>
): Effect.Effect<
void,
never,
| Exclude<R, HttpServerRequest>
| Scope.Scope
>
<
E,
R,
App extends Effect.Effect<
HttpServerResponse,
any,
any
>
>(
effect: Effect.Effect<
HttpServerResponse,
E,
R
>,
middleware: Middleware.HttpMiddleware.Applied<
App,
E,
R
>
): Effect.Effect<
void,
never,
| Exclude<R, HttpServerRequest>
| Scope.Scope
>
}
readonly address: HttpServer.Address
},
ServeError,
Scope.Scope
>
Creates a scoped HttpServer from a Node http.Server, starts listening
with the supplied options, registers request and upgrade handling, and closes
the server during scope finalization with optional graceful-shutdown control.
make(evaluate: LazyArg<Http.Server>evaluate, options: anyoptions))
),
const layerHttpServices: Layer.Layer<
| NodeServices.NodeServices
| HttpPlatform.HttpPlatform
| Etag.Generator
>
const layerHttpServices: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<Generator | HttpPlatform | NodeServices>, never, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Provides the Node HTTP support services used by NodeHttpServer, including
the HTTP platform, ETag generator, and core Node platform services.
layerHttpServices
)