(source: Readable, headers: IncomingHttpHeaders): Effect.Effect<
Multipart.Persisted,
Multipart.MultipartError,
Scope.Scope | FileSystem.FileSystem | Path.Path
>Parses multipart data from a Node readable request body and persists file
parts using the current FileSystem, Path, and Scope services.
export const const persisted: (
source: Readable,
headers: IncomingHttpHeaders
) => Effect.Effect<
Multipart.Persisted,
Multipart.MultipartError,
Scope.Scope | FileSystem.FileSystem | Path.Path
>
Parses multipart data from a Node readable request body and persists file
parts using the current FileSystem, Path, and Scope services.
persisted = (
source: Readablesource: import ReadableReadable,
headers: IncomingHttpHeadersheaders: import IncomingHttpHeadersIncomingHttpHeaders
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<
import MultipartMultipart.Persisted,
import MultipartMultipart.class MultipartErrorclass MultipartError {
message: string;
name: 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;
reason: Multipart.MultipartErrorReason;
}
Error raised while parsing, streaming, or persisting multipart form data.
Details
The reason field contains the concrete MultipartErrorReason.
MultipartError,
import ScopeScope.Scope | import FileSystemFileSystem.FileSystem | import PathPath.Path
> =>
import MultipartMultipart.const toPersisted: (
stream: Stream.Stream<
Multipart.Part,
Multipart.MultipartError
>,
writeFile?: (
path: string,
file: Multipart.File
) => Effect.Effect<
void,
Multipart.MultipartError,
FileSystem.FileSystem
>
) => Effect.Effect<
Multipart.Persisted,
Multipart.MultipartError,
FileSystem.FileSystem | Path.Path | Scope.Scope
>
Persists a stream of multipart parts into a record.
Details
Text fields are collected as strings, and file parts are written to files in a
scoped temporary directory.
Gotchas
Persisted file paths remain valid for the lifetime of the scope.
toPersisted(const stream: (
source: Readable,
headers: IncomingHttpHeaders
) => Stream.Stream<
Multipart.Part,
Multipart.MultipartError
>
Parses multipart data from a Node readable request body and headers into a
stream of Multipart.Part values, converting parser failures to
MultipartError.
stream(source: Readablesource, headers: IncomingHttpHeadersheaders), (path: stringpath, file: Multipart.File(parameter) file: {
_tag: "File";
key: string;
name: string;
contentType: string;
content: Stream.Stream<Uint8Array, MultipartError>;
contentEffect: Effect.Effect<Uint8Array, MultipartError>;
toString: () => string;
toJSON: () => unknown;
}
file) =>
import EffectEffect.const tryPromise: <
A,
E = Cause.UnknownError
>(
options:
| {
readonly try: (
signal: AbortSignal
) => PromiseLike<A>
readonly catch: (error: unknown) => E
}
| ((signal: AbortSignal) => PromiseLike<A>)
) => Effect<A, E>
Creates an Effect from an asynchronous computation that may throw or
reject, mapping failures into the error channel.
When to use
Use when you need to perform asynchronous operations that might fail, such
as fetching data from an API, and want thrown exceptions or rejected promises
captured as Effect errors.
Details
The promise thunk is evaluated when the effect runs. If it returns a promise
that resolves, the resolved value becomes the success value. If the thunk
throws before returning a promise, or if the returned promise rejects, the
thrown or rejected value is mapped into the error channel.
Passing the thunk directly maps failures to
Cause.UnknownError
.
Passing { try, catch } uses catch to map failures to an error of type
E.
The thunk receives an AbortSignal that is aborted if the effect is
interrupted. The underlying asynchronous operation only stops if it observes
that signal.
Gotchas
If catch throws while mapping the error, that thrown value is treated as a
defect. Return the error value you want in the error channel instead of
throwing it.
Example (Wrapping a fetch request that may fail)
import { Effect } from "effect"
const getTodo = (id: number) =>
// Will catch any errors and propagate them as UnknownError
Effect.tryPromise(() =>
fetch(`https://jsonplaceholder.typicode.com/todos/${id}`)
)
// ┌─── Effect<Response, UnknownError, never>
// ▼
const program = getTodo(1)
Example (Mapping Promise rejections to a tagged error)
import { Data, Effect } from "effect"
class TodoFetchError extends Data.TaggedError("TodoFetchError")<{ readonly cause: unknown }> {}
const getTodo = (id: number) =>
Effect.tryPromise({
try: () => fetch(`https://jsonplaceholder.typicode.com/todos/${id}`),
// remap the error
catch: (cause) => new TodoFetchError({ cause })
})
// ┌─── Effect<Response, TodoFetchError, never>
// ▼
const program = getTodo(1)
tryPromise({
try: (
signal: AbortSignal
) => PromiseLike<void>
try: (signal: AbortSignalsignal) => import NodeStreamPNodeStreamP.pipeline((file: Multipart.File(parameter) file: {
_tag: "File";
key: string;
name: string;
contentType: string;
content: Stream.Stream<Uint8Array, MultipartError>;
contentEffect: Effect.Effect<Uint8Array, MultipartError>;
toString: () => string;
toJSON: () => unknown;
}
file as class FileImplclass FileImpl {
_tag: 'File';
key: string;
name: string;
contentType: string;
content: Stream.Stream<Uint8Array, Multipart.MultipartError>;
contentEffect: Effect.Effect<Uint8Array, Multipart.MultipartError>;
file: MP.FileStream;
toJSON: () => unknown;
toString: () => string;
}
FileImpl).FileImpl.file: MP.FileStreamfile, import NFSNFS.createWriteStream(path: stringpath), { signal: AbortSignalsignal }),
catch: (
error: unknown
) => Multipart.MultipartError
catch: (cause: unknowncause) => import MultipartMultipart.class MultipartErrorclass MultipartError {
fromReason: (reason: Multipart.MultipartErrorReason['_tag'], cause?: unknown) => Multipart.MultipartError;
}
Error raised while parsing, streaming, or persisting multipart form data.
Details
The reason field contains the concrete MultipartErrorReason.
MultipartError.MultipartError.fromReason(reason: Multipart.MultipartErrorReason["_tag"], cause?: unknown): Multipart.MultipartErrorCreates a multipart error from a reason tag and optional cause.
fromReason("InternalError", cause: unknowncause)
}))