NodeClusterSocket.layerDispatcherK8sconstrepos/effect/packages/platform-node/src/NodeClusterSocket.ts:140Layer.Layer<NodeHttpClient.Dispatcher, never, never>Provides an Undici dispatcher for Kubernetes API calls, using the service account CA certificate when it is available and falling back to the default dispatcher otherwise.
export const const layerDispatcherK8s: Layer.Layer<NodeHttpClient.Dispatcher>const layerDispatcherK8s: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<Dispatcher>, 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 an Undici dispatcher for Kubernetes API calls, using the service
account CA certificate when it is available and falling back to the default
dispatcher otherwise.
layerDispatcherK8s: 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 NodeHttpClientNodeHttpClient.class Dispatcherclass Dispatcher {
Service: Service;
key: Identifier;
}
Service tag for the Undici Dispatcher used by the Undici-backed HTTP
client.
Dispatcher> = 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 NodeHttpClientNodeHttpClient.class Dispatcherclass Dispatcher {
key: Identifier;
of: (this: void, self: Undici.Dispatcher) => Undici.Dispatcher;
context: (self: Undici.Dispatcher) => Context<NodeHttpClient.Dispatcher>;
use: (f: (service: Undici.Dispatcher) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, NodeHttpClient.Dispatcher | R>;
useSync: (f: (service: Undici.Dispatcher) => A) => Effect.Effect<A, never, NodeHttpClient.Dispatcher>;
Identifier: Identifier;
Service: Shape;
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 the Undici Dispatcher used by the Undici-backed HTTP
client.
Dispatcher)(
import EffectEffect.const gen: {
<Eff extends Effect<any, any, any>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
<Self, Eff extends Effect<any, any, any>, AEff>(
options: { readonly self: Self },
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer E, infer _R>
]
? E
: never,
[Eff] extends [never]
? never
: [Eff] extends [
Effect<infer _A, infer _E, infer R>
]
? R
: never
>
}
Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to use
Use when you want to write effectful code that looks and behaves like
synchronous code, while still handling asynchronous tasks, errors, and complex
control flow such as loops and conditions.
Generator functions work similarly to async/await but keep errors,
requirements, and interruption in the Effect type. You can yield* values
from effects and return the final result at the end.
Example (Sequencing effects with generators)
import { Data, Effect } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
const addServiceCharge = (amount: number) => amount + 1
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
export const program = Effect.gen(function*() {
const transactionAmount = yield* fetchTransactionAmount
const discountRate = yield* fetchDiscountRate
const discountedAmount = yield* applyDiscount(
transactionAmount,
discountRate
)
const finalAmount = addServiceCharge(discountedAmount)
return `Final amount to charge: ${finalAmount}`
})
gen(function*() {
const const fs: FileSystem.FileSystemconst fs: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
}
fs = yield* import FileSystemFileSystem.const FileSystem: Context.Service<
FileSystem,
FileSystem
>
const FileSystem: {
key: string;
Service: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
};
of: (this: void, self: FileSystem.FileSystem) => FileSystem.FileSystem;
context: (self: FileSystem.FileSystem) => Context<FileSystem.FileSystem>;
use: (f: (service: FileSystem.FileSystem) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, FileSystem.FileSystem | R>;
useSync: (f: (service: FileSystem.FileSystem) => A) => Effect.Effect<A, never, FileSystem.FileSystem>;
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;
}
Core interface for file system operations in Effect.
Details
The FileSystem interface provides a comprehensive set of file and directory operations
that work cross-platform. All operations return Effect values that can be composed,
transformed, and executed safely with proper error handling.
Example (Accessing file system operations)
import { Console, Effect, FileSystem } from "effect"
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
// Basic file operations
const exists = yield* fs.exists("./config.json")
if (!exists) {
yield* fs.writeFileString("./config.json", "{\"env\": \"development\"}")
}
// Directory operations
yield* fs.makeDirectory("./logs", { recursive: true })
// File information
const stats = yield* fs.stat("./config.json")
yield* Console.log(`File size: ${stats.size} bytes`)
// Streaming operations
const content = yield* fs.readFileString("./config.json")
yield* Console.log("Config:", content)
})
Service tag for platform file-system operations.
When to use
Use to access or provide operations for files, directories, permissions,
streams, and sinks through the Effect context.
Details
This key is used to provide and access the FileSystem service in the Effect context.
Example (Accessing and providing FileSystem)
import { Effect, FileSystem } from "effect"
// Access the FileSystem service
const program = Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const exists = yield* fs.exists("./data.txt")
if (exists) {
const content = yield* fs.readFileString("./data.txt")
yield* Effect.log("File content:", content)
}
})
// Provide a custom FileSystem implementation
declare const platformImpl: Omit<
FileSystem.FileSystem,
"exists" | "readFileString" | "stream" | "sink" | "writeFileString"
>
const customFs = FileSystem.make(platformImpl)
const withCustomFs = Effect.provideService(
program,
FileSystem.FileSystem,
customFs
)
FileSystem
const const caCertOption: Option<string>caCertOption = yield* const fs: FileSystem.FileSystemconst fs: {
access: (path: string, options?: { readonly ok?: boolean | undefined; readonly readable?: boolean | undefined; readonly writable?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copy: (fromPath: string, toPath: string, options?: { readonly overwrite?: boolean | undefined; readonly preserveTimestamps?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
copyFile: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
chmod: (path: string, mode: number) => Effect.Effect<void, PlatformError>;
chown: (path: string, uid: number, gid: number) => Effect.Effect<void, PlatformError>;
glob: (pattern: string, options?: { readonly root?: string | undefined; readonly exclude?: ReadonlyArray<string> | undefined }) => Effect.Effect<Array<string>, PlatformError>;
exists: (path: string) => Effect.Effect<boolean, PlatformError>;
link: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
makeDirectory: (path: string, options?: { readonly recursive?: boolean | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
makeTempDirectory: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempDirectoryScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
makeTempFile: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError>;
makeTempFileScoped: (options?: { readonly directory?: string | undefined; readonly prefix?: string | undefined; readonly suffix?: string | undefined }) => Effect.Effect<string, PlatformError, Scope>;
open: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<File, PlatformError, Scope>;
readDirectory: (path: string, options?: { readonly recursive?: boolean | undefined }) => Effect.Effect<Array<string>, PlatformError>;
readFile: (path: string) => Effect.Effect<Uint8Array, PlatformError>;
readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>;
readLink: (path: string) => Effect.Effect<string, PlatformError>;
realPath: (path: string) => Effect.Effect<string, PlatformError>;
remove: (path: string, options?: { readonly recursive?: boolean | undefined; readonly force?: boolean | undefined }) => Effect.Effect<void, PlatformError>;
rename: (oldPath: string, newPath: string) => Effect.Effect<void, PlatformError>;
sink: (path: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Sink.Sink<void, Uint8Array, never, PlatformError>;
stat: (path: string) => Effect.Effect<File.Info, PlatformError>;
stream: (path: string, options?: { readonly bytesToRead?: SizeInput | undefined; readonly chunkSize?: SizeInput | undefined; readonly offset?: SizeInput | undefined }) => Stream.Stream<Uint8Array, PlatformError>;
symlink: (fromPath: string, toPath: string) => Effect.Effect<void, PlatformError>;
truncate: (path: string, length?: SizeInput) => Effect.Effect<void, PlatformError>;
utimes: (path: string, atime: Date | number, mtime: Date | number) => Effect.Effect<void, PlatformError>;
watch: (path: string) => Stream.Stream<WatchEvent, PlatformError>;
writeFile: (path: string, data: Uint8Array, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
writeFileString: (path: string, data: string, options?: { readonly flag?: OpenFlag | undefined; readonly mode?: number | undefined }) => Effect.Effect<void, PlatformError>;
}
fs.FileSystem.readFileString: (path: string, encoding?: string) => Effect.Effect<string, PlatformError>Read the contents of a file.
readFileString("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt").Pipeable.pipe<Effect.Effect<string, PlatformError, never>, Effect.Effect<Option<string>, never, never>>(this: Effect.Effect<string, PlatformError, never>, ab: (_: Effect.Effect<string, PlatformError, never>) => Effect.Effect<Option<string>, never, never>): Effect.Effect<Option<string>, never, never> (+21 overloads)pipe(
import EffectEffect.const option: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Option<A>, never, R>
Converts success to Option.some and failure to Option.none.
When to use
Use when you only care whether an effect succeeds and want recoverable
failures represented as Option.none.
Details
Success values become Option.some, recoverable failures become
Option.none, and defects still fail the effect.
Gotchas
option only captures typed, recoverable failures as Option.none.
Defects and interruptions are not captured inside the Option and still
fail the effect.
option also discards typed failure values. Use result if the failure
value matters.
Example (Capturing success or failure as Option)
import { Console, Effect, Option } from "effect"
const program = Effect.gen(function*() {
const someValue = yield* Effect.option(Effect.succeed(1))
const noneValue = yield* Effect.option(Effect.fail("missing"))
yield* Console.log(Option.isSome(someValue))
yield* Console.log(Option.isNone(noneValue))
})
Effect.runPromise(program)
// true
// true
option
)
if (const caCertOption: Option<string>caCertOption._tag: "None" | "Some"_tag === "Some") {
return yield* import EffectEffect.const acquireRelease: <A, E, R, R2>(
acquire: Effect<A, E, R>,
release: (
a: A,
exit: Exit.Exit<unknown, unknown>
) => Effect<unknown, never, R2>,
options?: { readonly interruptible?: boolean }
) => Effect<A, E, R | R2 | Scope>
Constructs a scoped resource from an acquisition effect and a release
finalizer.
When to use
Use to acquire a scoped resource with an explicit release finalizer.
Details
If acquisition succeeds, the release finalizer is added to the current scope
and is guaranteed to run when that scope closes. The finalizer receives the
Exit value used to close the scope.
By default, acquisition is protected by an uninterruptible region. Pass
{ interruptible: true } to allow the acquisition effect to be interrupted.
Example (Acquiring and releasing a resource)
import { Console, Effect, Exit } from "effect"
// Simulate a resource that needs cleanup
interface FileHandle {
readonly path: string
readonly content: string
}
// Acquire a file handle
const acquire = Effect.gen(function*() {
yield* Console.log("Opening file")
return { path: "/tmp/file.txt", content: "file content" }
})
// Release the file handle
const release = (handle: FileHandle, exit: Exit.Exit<unknown, unknown>) =>
Console.log(
`Closing file ${handle.path} with exit: ${
Exit.isSuccess(exit) ? "success" : "failure"
}`
)
// Create a scoped resource
const resource = Effect.acquireRelease(acquire, release)
// Use the resource within a scope
const program = Effect.scoped(
Effect.gen(function*() {
const handle = yield* resource
yield* Console.log(`Using file: ${handle.path}`)
return handle.content
})
)
acquireRelease(
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() =>
new import UndiciUndici.Agent({
connect: {
ca: string
}
connect: {
ca: stringca: const caCertOption: Some<string>const caCertOption: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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;
}
caCertOption.Some<string>.value: stringvalue
}
})
),
(agent: anyagent) => import EffectEffect.const promise: <A>(
evaluate: (
signal: AbortSignal
) => PromiseLike<A>
) => Effect<A>
Creates an Effect that represents an asynchronous computation guaranteed to
succeed.
When to use
Use to convert a Promise into an Effect when the async operation is
guaranteed to succeed and will not reject.
Details
An optional AbortSignal can be provided to allow for interruption of the
wrapped Promise API.
Gotchas
The Promise must not reject. If it rejects, the rejection is treated as a
defect, not as a typed failure. Use tryPromise when rejection is expected.
Interruption aborts the provided AbortSignal, but the underlying
asynchronous operation only stops if it observes that signal.
Example (Wrapping a non-rejecting Promise)
import { Effect } from "effect"
const delay = (message: string) =>
Effect.promise<string>(
() =>
new Promise((resolve) => {
setTimeout(() => {
resolve(message)
}, 2000)
})
)
// ┌─── Effect<string, never, never>
// ▼
const program = delay("Async operation completed successfully!")
promise(() => agent: anyagent.destroy())
)
}
return yield* import NodeHttpClientNodeHttpClient.const makeDispatcher: Effect.Effect<
Undici.Dispatcher,
never,
Scope.Scope
>
const makeDispatcher: {
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;
}
Acquires a new Undici Agent dispatcher and destroys it when the enclosing
scope is finalized.
makeDispatcher
})
).Pipeable.pipe<Layer.Layer<NodeHttpClient.Dispatcher, never, FileSystem.FileSystem>, Layer.Layer<NodeHttpClient.Dispatcher, never, never>>(this: Layer.Layer<NodeHttpClient.Dispatcher, never, FileSystem.FileSystem>, ab: (_: Layer.Layer<NodeHttpClient.Dispatcher, never, FileSystem.FileSystem>) => Layer.Layer<NodeHttpClient.Dispatcher, never, never>): Layer.Layer<NodeHttpClient.Dispatcher, never, never> (+21 overloads)pipe(
import LayerLayer.const provide: {
<RIn, E, ROut>(that: Layer<ROut, E, RIn>): <
RIn2,
E2,
ROut2
>(
self: Layer<ROut2, E2, RIn2>
) => Layer<
ROut2,
E | E2,
RIn | Exclude<RIn2, ROut>
>
<Layers extends [Any, ...Array<Any>]>(
that: Layers
): <A, E, R>(
self: Layer<A, E, R>
) => Layer<
A,
E | Error<Layers[number]>,
| Services<Layers[number]>
| Exclude<R, Success<Layers[number]>>
>
<RIn2, E2, ROut2, RIn, E, ROut>(
self: Layer<ROut2, E2, RIn2>,
that: Layer<ROut, E, RIn>
): Layer<
ROut2,
E | E2,
RIn | Exclude<RIn2, ROut>
>
<A, E, R, Layers extends [Any, ...Array<Any>]>(
self: Layer<A, E, R>,
that: Layers
): Layer<
A,
E | Error<Layers[number]>,
| Services<Layers[number]>
| Exclude<R, Success<Layers[number]>>
>
}
Feeds the output services of the dependency layer into the requirements of
this layer, returning a layer that only provides the services from this layer.
When to use
Use when you need to hide an implementation dependency layer from callers.
Details
In serviceLayer.pipe(Layer.provide(dependencyLayer)), the dependency layer is
built first and is used to satisfy the requirements of serviceLayer.
Example (Providing layer dependencies)
import { Context, Effect, Layer } from "effect"
class Database extends Context.Service<Database, {
readonly query: (sql: string) => Effect.Effect<string>
}>()("Database") {}
class UserService extends Context.Service<UserService, {
readonly getUser: (id: string) => Effect.Effect<{
id: string
name: string
}>
}>()("UserService") {}
class Logger extends Context.Service<Logger, {
readonly log: (msg: string) => Effect.Effect<void>
}>()("Logger") {}
// Create dependency layers
const databaseLayer = Layer.succeed(Database, {
query: Effect.fn("Database.query")((sql: string) => Effect.succeed(`DB: ${sql}`))
})
const loggerLayer = Layer.succeed(Logger, {
log: Effect.fn("Logger.log")((msg: string) => Effect.sync(() => console.log(`[LOG] ${msg}`)))
})
// UserService depends on Database and Logger
const userServiceLayer = Layer.effect(UserService, Effect.gen(function*() {
const database = yield* Database
const logger = yield* Logger
return {
getUser: Effect.fn("UserService.getUser")(function*(id: string) {
yield* logger.log(`Looking up user ${id}`)
const result = yield* database.query(
`SELECT * FROM users WHERE id = ${id}`
)
return { id, name: result }
})
}
}))
// Provide dependencies to UserService layer
const userServiceWithDependencies = userServiceLayer.pipe(
Layer.provide(Layer.mergeAll(databaseLayer, loggerLayer))
)
// Now UserService layer has no dependencies
const program = Effect.gen(function*() {
const userService = yield* UserService
return yield* userService.getUser("123")
}).pipe(
Effect.provide(userServiceWithDependencies)
)
provide(import NodeFileSystemNodeFileSystem.const layer: Layer.Layer<FileSystem>const layer: {
build: (memoMap: MemoMap, scope: Scope.Scope) => Effect<Context.Context<FileSystem>, 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 FileSystem service backed by Node filesystem APIs.
layer)
)