Rust-like Result types, easily serialized over the wire
npm install resultwireThis library provides a minimal Result type and utility functions that can be sent from server to client with the design being inspired by Rust's Result
ResultWire doesn't use classes, and instead uses plain objects. This means that it can be sent from server to client, and the helper methods can check the actual objects contents, instead of relying on instanceOf methods. Specifically, this library is designed to be used with Remix's turbo-stream. The ``Result` type and its variants (`Ok`, `Err`) can be serialized and deserialized over the network, making it suitable for use in client-server communication.
`bash`
npm install resultwire
`typescript`
import { ok, err, Result, ... } from 'resultwire'
- Types
- Ok
- Err
- Result
- Functions
- ok
- err
- isOk
- isErr
- map
- mapErr
- unwrapOr
- andThen
- orElse
- match
- fromThrowable
- fromPromise
- combine
- combineWithAllErrors
- unsafeUnwrap
Represents a successful value wrapped in an `Ok` variant.
Represents an error value wrapped in an `Err` variant.
A union type that represents either an `Ok` value or an `Err` value.
Creates a new `Ok` result.
Example:
`typescript`
const result = ok(42);
Creates a new `Err` result.
Example:
`typescript`
const result = err('Something went wrong');
Checks if a `Result` is `Ok`.
Example:
`typescript`
const result = ok(42);
const isOkResult = isOk(result); // true
Checks if a `Result` is `Err`.
Example:
`typescript`
const result = err('Something went wrong');
const isErrResult = isErr(result); // true
Maps a function over the value of an `Ok` result.
Example:
`typescript`
const result = ok(42);
const mappedResult = map(result, (value) => value * 2);
Maps a function over the error of an `Err` result.
Example:
`typescript`
const result = err('Something went wrong');
const mappedResult = mapErr(result, (error) => new Error(error));
Unwraps a `Result`, returning the value if it's `Ok`, or a default value if it's `Err`.
Example:
`typescript`
const result = ok(42);
const unwrappedResult = unwrapOr(result, 0); // 42
Chains a function that returns a `Result` to the value of an `Ok` result.
Example:
`typescript`
const result = ok(42);
const chainedResult = andThen(result, (value) => ok(value * 2));
Chains a function that returns a `Result` to the error of an `Err` result.
Example:
`typescript`
const result = err('Something went wrong');
const chainedResult = orElse(result, (error) => ok('Default value'));
Matches a `Result` against two functions, one for `Ok` and one for `Err`.
Example:
`typescript`
const result = ok(42);
const matchedResult = match(result, (value) => value * 2, (error) => 0); // 84$3
Executes a function that may throw an error and returns the result as a `Result`.
Example:
`typescript`
const result = fromThrowable(() => {
// Code that may throw an error
});
Converts a `Promise` into a `Promise`.
Example:
`typescript`
const promise = Promise.resolve(42);
const result = await fromPromise(promise);
Combines an array of `Result`s into a single `Result`.
Example:
`typescript`
const results = [ok(1), ok(2), ok(3)];
const combinedResult = combine(results);
Combines an array of `Result`s into a single `Result`, collecting all errors.
Example:
`typescript`
const results = [ok(1), err('Error 1'), ok(3), err('Error 2')];
const combinedResult = combineWithAllErrors(results);
Unwraps a `Result`, throwing an error if it's `Err`.
Example:
`typescript``
const result = ok(42);
const unwrappedResult = unsafeUnwrap(result); // 42