resultar 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/{chunk-L47MDOUR.js → chunk-ZFWL27DT.js} +108 -14
- package/dist/chunk-ZFWL27DT.js.map +1 -0
- package/dist/error.d.cts +1 -1
- package/dist/error.d.ts +1 -1
- package/dist/index-ph1sfvDU.d.cts +393 -0
- package/dist/index-ph1sfvDU.d.ts +393 -0
- package/dist/index.cjs +107 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -1
- package/dist/result-async.cjs +100 -11
- package/dist/result-async.cjs.map +1 -1
- package/dist/result-async.d.cts +1 -1
- package/dist/result-async.d.ts +1 -1
- package/dist/result-async.js +1 -1
- package/dist/result.cjs +107 -16
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +1 -1
- package/dist/result.d.ts +1 -1
- package/dist/result.js +3 -1
- package/dist/utils.cjs +417 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +14 -0
- package/dist/utils.js.map +1 -0
- package/package.json +12 -5
- package/dist/chunk-L47MDOUR.js.map +0 -1
- package/dist/index-BsQaA54t.d.cts +0 -205
- package/dist/index-BsQaA54t.d.ts +0 -205
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
2
|
-
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
3
|
-
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
4
|
-
private readonly innerPromise;
|
|
5
|
-
constructor(res: Promise<Result<T, E>>);
|
|
6
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
7
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
8
|
-
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
9
|
-
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
10
|
-
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<R, R | E>;
|
|
11
|
-
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<R, R | E>;
|
|
12
|
-
andThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
13
|
-
match<X>(ok: (t: T) => X, _err: (e: E) => X): Promise<X>;
|
|
14
|
-
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
15
|
-
/**
|
|
16
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
17
|
-
*/
|
|
18
|
-
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
19
|
-
}
|
|
20
|
-
declare function fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
21
|
-
declare function fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
22
|
-
declare function okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
23
|
-
declare function errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
24
|
-
declare function fromThrowableAsync<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
25
|
-
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
26
|
-
|
|
27
|
-
declare class Result<T, E> {
|
|
28
|
-
private readonly state;
|
|
29
|
-
/**
|
|
30
|
-
* Wraps a function with a try catch, creating a new function with the same
|
|
31
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
32
|
-
*
|
|
33
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
34
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
35
|
-
*/
|
|
36
|
-
static fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
37
|
-
static ok<T, E = never>(value: T): Result<T, E>;
|
|
38
|
-
static err<T = never, E = unknown>(error: E): Result<T, E>;
|
|
39
|
-
readonly value: T;
|
|
40
|
-
readonly error: E;
|
|
41
|
-
private constructor();
|
|
42
|
-
/**
|
|
43
|
-
* Used to check if a `Result` is an `OK`
|
|
44
|
-
*
|
|
45
|
-
* @returns `true` if the result is an `OK` variant of Result
|
|
46
|
-
*/
|
|
47
|
-
isOk(): boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Used to check if a `Result` is an `Err`
|
|
50
|
-
*
|
|
51
|
-
* @returns `true` if the result is an `Err` variant of Result
|
|
52
|
-
*/
|
|
53
|
-
isErr(): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Maps a `Result<T, E>` to `Result<U, E>`
|
|
56
|
-
* by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
57
|
-
* untouched.
|
|
58
|
-
*
|
|
59
|
-
* @param f The function to apply an `OK` value
|
|
60
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
61
|
-
*/
|
|
62
|
-
map<X, Y>(f: (t: T) => Exclude<X, Promise<any>>): Result<X, E | Y>;
|
|
63
|
-
/**
|
|
64
|
-
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
|
|
65
|
-
* contained `Err` value, leaving an `Ok` value untouched.
|
|
66
|
-
*
|
|
67
|
-
* This function can be used to pass through a successful result while
|
|
68
|
-
* handling an error.
|
|
69
|
-
*
|
|
70
|
-
* @param f a function to apply to the error `Err` value
|
|
71
|
-
*/
|
|
72
|
-
mapErr<X>(fn: (t: Exclude<E, Promise<any>>) => Exclude<X, Promise<any>>): Result<T, X>;
|
|
73
|
-
/**
|
|
74
|
-
* Similar to `map` Except you must return a new `Result`.
|
|
75
|
-
*
|
|
76
|
-
* This is useful for when you need to do a subsequent computation using the
|
|
77
|
-
* inner `T` value, but that computation might fail.
|
|
78
|
-
* Additionally, `andThen` is really useful as a tool to flatten a
|
|
79
|
-
* `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
|
|
80
|
-
*
|
|
81
|
-
* @param f The function to apply to the current value
|
|
82
|
-
*/
|
|
83
|
-
andThen<X, Y>(f: (t: T) => Exclude<Result<X, Y>, Promise<any>>): Result<X, E | Y>;
|
|
84
|
-
/**
|
|
85
|
-
* Takes an `Err` value and maps it to a `Result<T, SomeNewType>`.
|
|
86
|
-
*
|
|
87
|
-
* This is useful for error recovery.
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* @param f A function to apply to an `Err` value, leaving `Ok` values
|
|
91
|
-
* untouched.
|
|
92
|
-
*/
|
|
93
|
-
orElse<Y>(f: (t: E) => Result<T, Y>): Result<T, Y>;
|
|
94
|
-
/**
|
|
95
|
-
* Similar to `map` Except you must return a new `Result`.
|
|
96
|
-
*
|
|
97
|
-
* This is useful for when you need to do a subsequent async computation using
|
|
98
|
-
* the inner `T` value, but that computation might fail. Must return a ResultAsync
|
|
99
|
-
*
|
|
100
|
-
* @param f The function that returns a `ResultAsync` to apply to the current
|
|
101
|
-
* value
|
|
102
|
-
*/
|
|
103
|
-
asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
104
|
-
/**
|
|
105
|
-
* Maps a `Result<T, E>` to `ResultAsync<U, E>`
|
|
106
|
-
* by applying an async function to a contained `Ok` value, leaving an `Err`
|
|
107
|
-
* value untouched.
|
|
108
|
-
*
|
|
109
|
-
* @param f An async function to apply an `OK` value
|
|
110
|
-
*/
|
|
111
|
-
asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
|
|
112
|
-
/**
|
|
113
|
-
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
114
|
-
*
|
|
115
|
-
* @param v the default value to return if there is an `Err`
|
|
116
|
-
*/
|
|
117
|
-
unwrapOr<A>(defaultValue: A): T | A;
|
|
118
|
-
/**
|
|
119
|
-
*
|
|
120
|
-
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
121
|
-
* execute the function that matches the `Result` variant.
|
|
122
|
-
*
|
|
123
|
-
* Match callbacks do not necessitate to return a `Result`, however you can
|
|
124
|
-
* return a `Result` if you want to.
|
|
125
|
-
*
|
|
126
|
-
* `match` is like chaining `map` and `mapErr`, with the distinction that
|
|
127
|
-
* with `match` both functions must have the same return type.
|
|
128
|
-
*
|
|
129
|
-
* @param ok
|
|
130
|
-
* @param err
|
|
131
|
-
*/
|
|
132
|
-
match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
|
|
133
|
-
/**
|
|
134
|
-
* Performs a side effect for the `Ok` variant of `Result`.
|
|
135
|
-
*
|
|
136
|
-
* @param f The function to apply an `OK` value
|
|
137
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
138
|
-
*/
|
|
139
|
-
tap(f: (t: T) => void): Result<T, E>;
|
|
140
|
-
/**
|
|
141
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
142
|
-
*
|
|
143
|
-
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
144
|
-
*
|
|
145
|
-
* @param config
|
|
146
|
-
*/
|
|
147
|
-
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
148
|
-
/**
|
|
149
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
150
|
-
*
|
|
151
|
-
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
152
|
-
* otherwise it throws a custom object.
|
|
153
|
-
*
|
|
154
|
-
* @param config
|
|
155
|
-
*/
|
|
156
|
-
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
157
|
-
safeUnwrap(): Generator<Result<never, E>, T>;
|
|
158
|
-
}
|
|
159
|
-
declare const ok: <T, E = never>(value: T) => Result<T, E>;
|
|
160
|
-
declare const err: <T = never, E = unknown>(error: E) => Result<T, E>;
|
|
161
|
-
declare const fromThrowable: <Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: ((e: any) => E) | undefined) => (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
162
|
-
/**
|
|
163
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
164
|
-
* whichever comes first.
|
|
165
|
-
*
|
|
166
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
167
|
-
* Rust's ? operator.
|
|
168
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
169
|
-
*
|
|
170
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
171
|
-
* Rust's `result?` expression.
|
|
172
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
173
|
-
*/
|
|
174
|
-
declare function safeTry<T, E>(body: () => Generator<Result<never, E>, Result<T, E>>): Result<T, E>;
|
|
175
|
-
/**
|
|
176
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
177
|
-
* whichever comes first.
|
|
178
|
-
*
|
|
179
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
180
|
-
* Rust's ? operator.
|
|
181
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
182
|
-
*
|
|
183
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` and
|
|
184
|
-
* `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression.
|
|
185
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
186
|
-
*/
|
|
187
|
-
declare function safeTry<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): Promise<Result<T, E>>;
|
|
188
|
-
|
|
189
|
-
interface ErrorConfig {
|
|
190
|
-
withStackTrace: boolean;
|
|
191
|
-
}
|
|
192
|
-
interface ResultarError<T, E> {
|
|
193
|
-
data: {
|
|
194
|
-
type: string;
|
|
195
|
-
value: T | undefined;
|
|
196
|
-
} | {
|
|
197
|
-
type: string;
|
|
198
|
-
value: E | undefined;
|
|
199
|
-
};
|
|
200
|
-
message: string;
|
|
201
|
-
stack: string | undefined;
|
|
202
|
-
}
|
|
203
|
-
declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
|
|
204
|
-
|
|
205
|
-
export { type ErrorConfig as E, ResultAsync as R, fromPromise as a, fromThrowableAsync as b, Result as c, ok as d, errAsync as e, fromSafePromise as f, err as g, fromThrowable as h, safeTry as i, createResultarError as j, okAsync as o, safeTryAsync as s };
|
package/dist/index-BsQaA54t.d.ts
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
2
|
-
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
3
|
-
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
4
|
-
private readonly innerPromise;
|
|
5
|
-
constructor(res: Promise<Result<T, E>>);
|
|
6
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
7
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
8
|
-
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
9
|
-
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
10
|
-
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<R, R | E>;
|
|
11
|
-
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<R, R | E>;
|
|
12
|
-
andThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
13
|
-
match<X>(ok: (t: T) => X, _err: (e: E) => X): Promise<X>;
|
|
14
|
-
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
15
|
-
/**
|
|
16
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
17
|
-
*/
|
|
18
|
-
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
19
|
-
}
|
|
20
|
-
declare function fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
21
|
-
declare function fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
22
|
-
declare function okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
23
|
-
declare function errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
24
|
-
declare function fromThrowableAsync<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
25
|
-
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
26
|
-
|
|
27
|
-
declare class Result<T, E> {
|
|
28
|
-
private readonly state;
|
|
29
|
-
/**
|
|
30
|
-
* Wraps a function with a try catch, creating a new function with the same
|
|
31
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
32
|
-
*
|
|
33
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
34
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
35
|
-
*/
|
|
36
|
-
static fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
37
|
-
static ok<T, E = never>(value: T): Result<T, E>;
|
|
38
|
-
static err<T = never, E = unknown>(error: E): Result<T, E>;
|
|
39
|
-
readonly value: T;
|
|
40
|
-
readonly error: E;
|
|
41
|
-
private constructor();
|
|
42
|
-
/**
|
|
43
|
-
* Used to check if a `Result` is an `OK`
|
|
44
|
-
*
|
|
45
|
-
* @returns `true` if the result is an `OK` variant of Result
|
|
46
|
-
*/
|
|
47
|
-
isOk(): boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Used to check if a `Result` is an `Err`
|
|
50
|
-
*
|
|
51
|
-
* @returns `true` if the result is an `Err` variant of Result
|
|
52
|
-
*/
|
|
53
|
-
isErr(): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Maps a `Result<T, E>` to `Result<U, E>`
|
|
56
|
-
* by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
57
|
-
* untouched.
|
|
58
|
-
*
|
|
59
|
-
* @param f The function to apply an `OK` value
|
|
60
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
61
|
-
*/
|
|
62
|
-
map<X, Y>(f: (t: T) => Exclude<X, Promise<any>>): Result<X, E | Y>;
|
|
63
|
-
/**
|
|
64
|
-
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
|
|
65
|
-
* contained `Err` value, leaving an `Ok` value untouched.
|
|
66
|
-
*
|
|
67
|
-
* This function can be used to pass through a successful result while
|
|
68
|
-
* handling an error.
|
|
69
|
-
*
|
|
70
|
-
* @param f a function to apply to the error `Err` value
|
|
71
|
-
*/
|
|
72
|
-
mapErr<X>(fn: (t: Exclude<E, Promise<any>>) => Exclude<X, Promise<any>>): Result<T, X>;
|
|
73
|
-
/**
|
|
74
|
-
* Similar to `map` Except you must return a new `Result`.
|
|
75
|
-
*
|
|
76
|
-
* This is useful for when you need to do a subsequent computation using the
|
|
77
|
-
* inner `T` value, but that computation might fail.
|
|
78
|
-
* Additionally, `andThen` is really useful as a tool to flatten a
|
|
79
|
-
* `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
|
|
80
|
-
*
|
|
81
|
-
* @param f The function to apply to the current value
|
|
82
|
-
*/
|
|
83
|
-
andThen<X, Y>(f: (t: T) => Exclude<Result<X, Y>, Promise<any>>): Result<X, E | Y>;
|
|
84
|
-
/**
|
|
85
|
-
* Takes an `Err` value and maps it to a `Result<T, SomeNewType>`.
|
|
86
|
-
*
|
|
87
|
-
* This is useful for error recovery.
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* @param f A function to apply to an `Err` value, leaving `Ok` values
|
|
91
|
-
* untouched.
|
|
92
|
-
*/
|
|
93
|
-
orElse<Y>(f: (t: E) => Result<T, Y>): Result<T, Y>;
|
|
94
|
-
/**
|
|
95
|
-
* Similar to `map` Except you must return a new `Result`.
|
|
96
|
-
*
|
|
97
|
-
* This is useful for when you need to do a subsequent async computation using
|
|
98
|
-
* the inner `T` value, but that computation might fail. Must return a ResultAsync
|
|
99
|
-
*
|
|
100
|
-
* @param f The function that returns a `ResultAsync` to apply to the current
|
|
101
|
-
* value
|
|
102
|
-
*/
|
|
103
|
-
asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
104
|
-
/**
|
|
105
|
-
* Maps a `Result<T, E>` to `ResultAsync<U, E>`
|
|
106
|
-
* by applying an async function to a contained `Ok` value, leaving an `Err`
|
|
107
|
-
* value untouched.
|
|
108
|
-
*
|
|
109
|
-
* @param f An async function to apply an `OK` value
|
|
110
|
-
*/
|
|
111
|
-
asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
|
|
112
|
-
/**
|
|
113
|
-
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
114
|
-
*
|
|
115
|
-
* @param v the default value to return if there is an `Err`
|
|
116
|
-
*/
|
|
117
|
-
unwrapOr<A>(defaultValue: A): T | A;
|
|
118
|
-
/**
|
|
119
|
-
*
|
|
120
|
-
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
121
|
-
* execute the function that matches the `Result` variant.
|
|
122
|
-
*
|
|
123
|
-
* Match callbacks do not necessitate to return a `Result`, however you can
|
|
124
|
-
* return a `Result` if you want to.
|
|
125
|
-
*
|
|
126
|
-
* `match` is like chaining `map` and `mapErr`, with the distinction that
|
|
127
|
-
* with `match` both functions must have the same return type.
|
|
128
|
-
*
|
|
129
|
-
* @param ok
|
|
130
|
-
* @param err
|
|
131
|
-
*/
|
|
132
|
-
match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
|
|
133
|
-
/**
|
|
134
|
-
* Performs a side effect for the `Ok` variant of `Result`.
|
|
135
|
-
*
|
|
136
|
-
* @param f The function to apply an `OK` value
|
|
137
|
-
* @returns the result of applying `f` or an `Err` untouched
|
|
138
|
-
*/
|
|
139
|
-
tap(f: (t: T) => void): Result<T, E>;
|
|
140
|
-
/**
|
|
141
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
142
|
-
*
|
|
143
|
-
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
144
|
-
*
|
|
145
|
-
* @param config
|
|
146
|
-
*/
|
|
147
|
-
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
148
|
-
/**
|
|
149
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
150
|
-
*
|
|
151
|
-
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
152
|
-
* otherwise it throws a custom object.
|
|
153
|
-
*
|
|
154
|
-
* @param config
|
|
155
|
-
*/
|
|
156
|
-
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
157
|
-
safeUnwrap(): Generator<Result<never, E>, T>;
|
|
158
|
-
}
|
|
159
|
-
declare const ok: <T, E = never>(value: T) => Result<T, E>;
|
|
160
|
-
declare const err: <T = never, E = unknown>(error: E) => Result<T, E>;
|
|
161
|
-
declare const fromThrowable: <Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: ((e: any) => E) | undefined) => (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
162
|
-
/**
|
|
163
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
164
|
-
* whichever comes first.
|
|
165
|
-
*
|
|
166
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
167
|
-
* Rust's ? operator.
|
|
168
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
169
|
-
*
|
|
170
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
171
|
-
* Rust's `result?` expression.
|
|
172
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
173
|
-
*/
|
|
174
|
-
declare function safeTry<T, E>(body: () => Generator<Result<never, E>, Result<T, E>>): Result<T, E>;
|
|
175
|
-
/**
|
|
176
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
177
|
-
* whichever comes first.
|
|
178
|
-
*
|
|
179
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
180
|
-
* Rust's ? operator.
|
|
181
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
182
|
-
*
|
|
183
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` and
|
|
184
|
-
* `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression.
|
|
185
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
186
|
-
*/
|
|
187
|
-
declare function safeTry<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): Promise<Result<T, E>>;
|
|
188
|
-
|
|
189
|
-
interface ErrorConfig {
|
|
190
|
-
withStackTrace: boolean;
|
|
191
|
-
}
|
|
192
|
-
interface ResultarError<T, E> {
|
|
193
|
-
data: {
|
|
194
|
-
type: string;
|
|
195
|
-
value: T | undefined;
|
|
196
|
-
} | {
|
|
197
|
-
type: string;
|
|
198
|
-
value: E | undefined;
|
|
199
|
-
};
|
|
200
|
-
message: string;
|
|
201
|
-
stack: string | undefined;
|
|
202
|
-
}
|
|
203
|
-
declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
|
|
204
|
-
|
|
205
|
-
export { type ErrorConfig as E, ResultAsync as R, fromPromise as a, fromThrowableAsync as b, Result as c, ok as d, errAsync as e, fromSafePromise as f, err as g, fromThrowable as h, safeTry as i, createResultarError as j, okAsync as o, safeTryAsync as s };
|