resultar 1.1.2 → 1.1.4
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 +60 -6
- package/dist/{chunk-FBVQYZXO.js → chunk-3NDPPYVV.js} +1 -1
- package/dist/chunk-3NDPPYVV.js.map +1 -0
- package/dist/{chunk-UB367EZR.js → chunk-SGURZSJC.js} +139 -59
- package/dist/chunk-SGURZSJC.js.map +1 -0
- package/dist/error.cjs.map +1 -1
- package/dist/error.d.cts +1 -1
- package/dist/error.d.ts +1 -1
- package/dist/error.js +1 -1
- package/dist/{index-OONdhGH0.d.cts → index-CKLj0kyj.d.cts} +113 -80
- package/dist/{index-OONdhGH0.d.ts → index-CKLj0kyj.d.ts} +113 -80
- package/dist/index.cjs +266 -186
- 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 +2 -2
- package/dist/result-async.cjs +119 -39
- 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 +2 -2
- package/dist/result.cjs +133 -53
- 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 +2 -2
- package/dist/utils.cjs +97 -17
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -2
- package/package.json +23 -77
- package/dist/chunk-FBVQYZXO.js.map +0 -1
- package/dist/chunk-UB367EZR.js.map +0 -1
|
@@ -1,78 +1,3 @@
|
|
|
1
|
-
type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
2
|
-
type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
|
|
3
|
-
type UnwrapAsync<T> = IsLiteralArray<T> extends 1 ? Writable<T> extends [infer H, ...infer Rest] ? H extends PromiseLike<infer HI> ? HI extends Result<unknown, unknown> ? [Dedup<HI>, ...UnwrapAsync<Rest>] : never : never : [] : T extends Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
|
|
4
|
-
type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>> : never : T extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
5
|
-
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
6
|
-
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
7
|
-
declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
8
|
-
private readonly innerPromise;
|
|
9
|
-
constructor(res: Promise<Result<T, E>>);
|
|
10
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
11
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
12
|
-
}
|
|
13
|
-
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
14
|
-
static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
15
|
-
static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
16
|
-
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
17
|
-
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
18
|
-
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
19
|
-
static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
20
|
-
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
21
|
-
static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
22
|
-
/**
|
|
23
|
-
* Wraps a async function with a try catch, creating a new function with the same
|
|
24
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
25
|
-
*
|
|
26
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
27
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
28
|
-
* @returns a new function that returns a `ResultAsync`
|
|
29
|
-
*/
|
|
30
|
-
static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
31
|
-
private readonly innerPromise;
|
|
32
|
-
constructor(res: Promise<Result<T, E>>);
|
|
33
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
34
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
35
|
-
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
36
|
-
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
37
|
-
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
38
|
-
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
39
|
-
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
40
|
-
if(fCondition: (t: T) => boolean): {
|
|
41
|
-
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
|
|
42
|
-
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
46
|
-
orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
|
|
47
|
-
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
|
|
48
|
-
match<X>(ok: (t: T) => X, _err: (e: E) => X): Promise<X>;
|
|
49
|
-
unwrapOr<A>(t: A): Promise<T | A>;
|
|
50
|
-
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
51
|
-
finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
|
|
52
|
-
/**
|
|
53
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
54
|
-
*/
|
|
55
|
-
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
56
|
-
}
|
|
57
|
-
declare const okAsync: typeof ResultAsync.okAsync;
|
|
58
|
-
declare const errAsync: typeof ResultAsync.errAsync;
|
|
59
|
-
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
60
|
-
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
61
|
-
declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
|
|
62
|
-
/**
|
|
63
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
64
|
-
* whichever comes first.
|
|
65
|
-
*
|
|
66
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
67
|
-
* Rust's ? operator.
|
|
68
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
69
|
-
*
|
|
70
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
71
|
-
* Rust's `result?` expression.
|
|
72
|
-
* @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
|
|
73
|
-
*/
|
|
74
|
-
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
75
|
-
|
|
76
1
|
type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
|
|
77
2
|
type MemberListOf<T> = ((T extends unknown ? (t: T) => T : never) extends infer U ? (U extends unknown ? (u: U) => unknown : never) extends (v: infer V) => unknown ? V : never : never) extends (_: unknown) => infer W ? [...MemberListOf<Exclude<T, W>>, W] : [];
|
|
78
3
|
type ExtractOkTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
|
|
@@ -180,6 +105,102 @@ declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: Reado
|
|
|
180
105
|
*/
|
|
181
106
|
declare const combineResultListWithAllErrors: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E[]>;
|
|
182
107
|
|
|
108
|
+
type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
109
|
+
type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
|
|
110
|
+
type UnwrapAsync<T> = IsLiteralArray<T> extends 1 ? Writable<T> extends [infer H, ...infer Rest] ? H extends PromiseLike<infer HI> ? HI extends Result<unknown, unknown> ? [Dedup<HI>, ...UnwrapAsync<Rest>] : never : never : [] : T extends Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
|
|
111
|
+
type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>> : never : T extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
112
|
+
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
113
|
+
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
114
|
+
declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
115
|
+
private readonly innerPromise;
|
|
116
|
+
constructor(res: Promise<Result<T, E>>);
|
|
117
|
+
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
118
|
+
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
119
|
+
}
|
|
120
|
+
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
121
|
+
static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
122
|
+
static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
123
|
+
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
124
|
+
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
125
|
+
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
126
|
+
static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
127
|
+
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
128
|
+
static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Wraps a async function with a try catch, creating a new function with the same
|
|
131
|
+
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
132
|
+
*
|
|
133
|
+
* @param fn function to wrap with ok on success or err on failure
|
|
134
|
+
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
135
|
+
* @returns a new function that returns a `ResultAsync`
|
|
136
|
+
*/
|
|
137
|
+
static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
138
|
+
private readonly innerPromise;
|
|
139
|
+
constructor(res: Promise<Result<T, E>>);
|
|
140
|
+
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
141
|
+
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
142
|
+
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
143
|
+
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
144
|
+
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
145
|
+
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
146
|
+
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
147
|
+
if(fCondition: (t: T) => boolean): {
|
|
148
|
+
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
|
|
149
|
+
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
153
|
+
orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
|
|
154
|
+
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
|
|
155
|
+
match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
|
|
156
|
+
unwrapOr<A>(t: A): Promise<T | A>;
|
|
157
|
+
/**
|
|
158
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
159
|
+
* This function can be used for control flow based on result values.
|
|
160
|
+
* @param f The function to call if the result is `Ok`
|
|
161
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
162
|
+
*/
|
|
163
|
+
log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
164
|
+
/**
|
|
165
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
166
|
+
* This function can be used for control flow based on result values.
|
|
167
|
+
* @param f The function to call if the result is `Ok`
|
|
168
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
169
|
+
*/
|
|
170
|
+
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
171
|
+
/**
|
|
172
|
+
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
173
|
+
* This function can be used for control flow based on result values.
|
|
174
|
+
* @param f The function to call if the result is `Err`
|
|
175
|
+
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
176
|
+
* @example
|
|
177
|
+
*/
|
|
178
|
+
tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
179
|
+
finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
|
|
180
|
+
/**
|
|
181
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
182
|
+
*/
|
|
183
|
+
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
184
|
+
}
|
|
185
|
+
declare const okAsync: typeof ResultAsync.okAsync;
|
|
186
|
+
declare const errAsync: typeof ResultAsync.errAsync;
|
|
187
|
+
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
188
|
+
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
189
|
+
declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
|
|
190
|
+
/**
|
|
191
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
192
|
+
* whichever comes first.
|
|
193
|
+
*
|
|
194
|
+
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
195
|
+
* Rust's ? operator.
|
|
196
|
+
* See `/tests/safeTry.test.ts` for examples.
|
|
197
|
+
*
|
|
198
|
+
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
199
|
+
* Rust's `result?` expression.
|
|
200
|
+
* @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
|
|
201
|
+
*/
|
|
202
|
+
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
203
|
+
|
|
183
204
|
/**
|
|
184
205
|
* A Result interface that is used to define the methods that are available on a
|
|
185
206
|
* `Result` object.
|
|
@@ -302,7 +323,6 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
302
323
|
*
|
|
303
324
|
* This is useful for error recovery.
|
|
304
325
|
*
|
|
305
|
-
*
|
|
306
326
|
* @param f A function to apply to an `Err` value, leaving `Ok` values
|
|
307
327
|
* untouched.
|
|
308
328
|
*/
|
|
@@ -332,7 +352,6 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
332
352
|
*/
|
|
333
353
|
unwrapOr<A>(defaultValue: A): T | A;
|
|
334
354
|
/**
|
|
335
|
-
*
|
|
336
355
|
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
337
356
|
* execute the function that matches the `Result` variant.
|
|
338
357
|
*
|
|
@@ -346,13 +365,27 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
346
365
|
* @param err
|
|
347
366
|
*/
|
|
348
367
|
match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
|
|
368
|
+
/**
|
|
369
|
+
* Performs a side effect no matter what the `Result` variant is.
|
|
370
|
+
*
|
|
371
|
+
* @param fn The function to apply an `OK` value
|
|
372
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
373
|
+
*/
|
|
374
|
+
log(fn: (t?: T, e?: E) => void): this;
|
|
349
375
|
/**
|
|
350
376
|
* Performs a side effect for the `Ok` variant of `Result`.
|
|
351
377
|
*
|
|
352
|
-
* @param
|
|
378
|
+
* @param fn The function to apply an `OK` value
|
|
353
379
|
* @returns the result of applying `f` or an `Err` untouched
|
|
354
380
|
*/
|
|
355
|
-
tap(
|
|
381
|
+
tap(fn: (t: T) => void): this;
|
|
382
|
+
/**
|
|
383
|
+
* Performs a side effect for the `Err` variant of `Result`.
|
|
384
|
+
*
|
|
385
|
+
* @param fn The function to apply an `Err` value
|
|
386
|
+
* @returns the result of applying `f` or an `Ok` untouched
|
|
387
|
+
*/
|
|
388
|
+
tapError(fn: (e: E) => void): this;
|
|
356
389
|
/**
|
|
357
390
|
* This method is used to clean up and release any resources that the `Result`
|
|
358
391
|
* @param f The function that will be called to clean up the resources
|
|
@@ -440,4 +473,4 @@ interface ResultarError<T, E> {
|
|
|
440
473
|
}
|
|
441
474
|
declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
|
|
442
475
|
|
|
443
|
-
export { ok as A, err as B, type
|
|
476
|
+
export { ok as A, err as B, type Combine as C, type Dedup as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type ExtractOkAsyncTypes as a, type ExtractErrTypes as b, type ExtractErrAsyncTypes as c, type InferErrTypes as d, type InferAsyncOkTypes as e, type InferAsyncErrTypes as f, type IsLiteralArray as g, type EmptyArrayToNever as h, type MembersToUnion as i, type CombineResults as j, type CombineResultsWithAllErrorsArray as k, combineResultAsyncList as l, combineResultList as m, combineResultAsyncListWithAllErrors as n, combineResultListWithAllErrors as o, type CombineResultAsyncs as p, type CombineResultsWithAllErrorsArrayAsync as q, DisposableResultAsync as r, okAsync as s, errAsync as t, fromPromise as u, fromSafePromise as v, fromThrowableAsync as w, safeTryAsync as x, Result as y, DisposableResult as z };
|
|
@@ -1,78 +1,3 @@
|
|
|
1
|
-
type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
2
|
-
type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
|
|
3
|
-
type UnwrapAsync<T> = IsLiteralArray<T> extends 1 ? Writable<T> extends [infer H, ...infer Rest] ? H extends PromiseLike<infer HI> ? HI extends Result<unknown, unknown> ? [Dedup<HI>, ...UnwrapAsync<Rest>] : never : never : [] : T extends Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
|
|
4
|
-
type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>> : never : T extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
5
|
-
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
6
|
-
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
7
|
-
declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
8
|
-
private readonly innerPromise;
|
|
9
|
-
constructor(res: Promise<Result<T, E>>);
|
|
10
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
11
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
12
|
-
}
|
|
13
|
-
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
14
|
-
static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
15
|
-
static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
16
|
-
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
17
|
-
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
18
|
-
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
19
|
-
static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
20
|
-
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
21
|
-
static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
22
|
-
/**
|
|
23
|
-
* Wraps a async function with a try catch, creating a new function with the same
|
|
24
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
25
|
-
*
|
|
26
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
27
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
28
|
-
* @returns a new function that returns a `ResultAsync`
|
|
29
|
-
*/
|
|
30
|
-
static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
31
|
-
private readonly innerPromise;
|
|
32
|
-
constructor(res: Promise<Result<T, E>>);
|
|
33
|
-
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
34
|
-
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
35
|
-
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
36
|
-
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
37
|
-
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
38
|
-
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
39
|
-
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
40
|
-
if(fCondition: (t: T) => boolean): {
|
|
41
|
-
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
|
|
42
|
-
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
46
|
-
orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
|
|
47
|
-
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
|
|
48
|
-
match<X>(ok: (t: T) => X, _err: (e: E) => X): Promise<X>;
|
|
49
|
-
unwrapOr<A>(t: A): Promise<T | A>;
|
|
50
|
-
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
51
|
-
finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
|
|
52
|
-
/**
|
|
53
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
54
|
-
*/
|
|
55
|
-
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
56
|
-
}
|
|
57
|
-
declare const okAsync: typeof ResultAsync.okAsync;
|
|
58
|
-
declare const errAsync: typeof ResultAsync.errAsync;
|
|
59
|
-
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
60
|
-
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
61
|
-
declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
|
|
62
|
-
/**
|
|
63
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
64
|
-
* whichever comes first.
|
|
65
|
-
*
|
|
66
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
67
|
-
* Rust's ? operator.
|
|
68
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
69
|
-
*
|
|
70
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
71
|
-
* Rust's `result?` expression.
|
|
72
|
-
* @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
|
|
73
|
-
*/
|
|
74
|
-
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
75
|
-
|
|
76
1
|
type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
|
|
77
2
|
type MemberListOf<T> = ((T extends unknown ? (t: T) => T : never) extends infer U ? (U extends unknown ? (u: U) => unknown : never) extends (v: infer V) => unknown ? V : never : never) extends (_: unknown) => infer W ? [...MemberListOf<Exclude<T, W>>, W] : [];
|
|
78
3
|
type ExtractOkTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
|
|
@@ -180,6 +105,102 @@ declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: Reado
|
|
|
180
105
|
*/
|
|
181
106
|
declare const combineResultListWithAllErrors: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E[]>;
|
|
182
107
|
|
|
108
|
+
type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
109
|
+
type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
|
|
110
|
+
type UnwrapAsync<T> = IsLiteralArray<T> extends 1 ? Writable<T> extends [infer H, ...infer Rest] ? H extends PromiseLike<infer HI> ? HI extends Result<unknown, unknown> ? [Dedup<HI>, ...UnwrapAsync<Rest>] : never : never : [] : T extends Array<infer A> ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Array<Result<L, R>> : never : never : never;
|
|
111
|
+
type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>> : never : T extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, MembersToUnion<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
112
|
+
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1 ? Combine<T, Depth> extends [infer Oks, infer Errs] ? ResultAsync<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never : Writable<T> extends Array<infer I> ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<EmptyArrayToNever<Array<Oks[number]>>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, EmptyArrayToNever<Array<Errs[number]>>> : ResultAsync<Oks, Errs> : never : never;
|
|
113
|
+
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
114
|
+
declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
115
|
+
private readonly innerPromise;
|
|
116
|
+
constructor(res: Promise<Result<T, E>>);
|
|
117
|
+
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
118
|
+
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
119
|
+
}
|
|
120
|
+
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
121
|
+
static okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
122
|
+
static errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
123
|
+
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
124
|
+
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
125
|
+
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
126
|
+
static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
127
|
+
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
128
|
+
static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Wraps a async function with a try catch, creating a new function with the same
|
|
131
|
+
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
132
|
+
*
|
|
133
|
+
* @param fn function to wrap with ok on success or err on failure
|
|
134
|
+
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
135
|
+
* @returns a new function that returns a `ResultAsync`
|
|
136
|
+
*/
|
|
137
|
+
static fromThrowable<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
138
|
+
private readonly innerPromise;
|
|
139
|
+
constructor(res: Promise<Result<T, E>>);
|
|
140
|
+
then<A, B>(// eslint-disable-line unicorn/no-thenable
|
|
141
|
+
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
142
|
+
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
143
|
+
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
144
|
+
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
145
|
+
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
146
|
+
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
147
|
+
if(fCondition: (t: T) => boolean): {
|
|
148
|
+
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
|
|
149
|
+
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
153
|
+
orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
|
|
154
|
+
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
|
|
155
|
+
match<X>(ok: (t: T) => X, err: (e: E) => X): Promise<X>;
|
|
156
|
+
unwrapOr<A>(t: A): Promise<T | A>;
|
|
157
|
+
/**
|
|
158
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
159
|
+
* This function can be used for control flow based on result values.
|
|
160
|
+
* @param f The function to call if the result is `Ok`
|
|
161
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
162
|
+
*/
|
|
163
|
+
log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
164
|
+
/**
|
|
165
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
166
|
+
* This function can be used for control flow based on result values.
|
|
167
|
+
* @param f The function to call if the result is `Ok`
|
|
168
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
169
|
+
*/
|
|
170
|
+
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
171
|
+
/**
|
|
172
|
+
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
173
|
+
* This function can be used for control flow based on result values.
|
|
174
|
+
* @param f The function to call if the result is `Err`
|
|
175
|
+
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
176
|
+
* @example
|
|
177
|
+
*/
|
|
178
|
+
tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
179
|
+
finally(f: (value: T, error: E) => void): DisposableResultAsync<T, E>;
|
|
180
|
+
/**
|
|
181
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
182
|
+
*/
|
|
183
|
+
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
184
|
+
}
|
|
185
|
+
declare const okAsync: typeof ResultAsync.okAsync;
|
|
186
|
+
declare const errAsync: typeof ResultAsync.errAsync;
|
|
187
|
+
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
188
|
+
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
189
|
+
declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
|
|
190
|
+
/**
|
|
191
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
192
|
+
* whichever comes first.
|
|
193
|
+
*
|
|
194
|
+
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
195
|
+
* Rust's ? operator.
|
|
196
|
+
* See `/tests/safeTry.test.ts` for examples.
|
|
197
|
+
*
|
|
198
|
+
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
199
|
+
* Rust's `result?` expression.
|
|
200
|
+
* @returns The first occurence of either an yielded Err or a returned `ResultAsync`.
|
|
201
|
+
*/
|
|
202
|
+
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
203
|
+
|
|
183
204
|
/**
|
|
184
205
|
* A Result interface that is used to define the methods that are available on a
|
|
185
206
|
* `Result` object.
|
|
@@ -302,7 +323,6 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
302
323
|
*
|
|
303
324
|
* This is useful for error recovery.
|
|
304
325
|
*
|
|
305
|
-
*
|
|
306
326
|
* @param f A function to apply to an `Err` value, leaving `Ok` values
|
|
307
327
|
* untouched.
|
|
308
328
|
*/
|
|
@@ -332,7 +352,6 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
332
352
|
*/
|
|
333
353
|
unwrapOr<A>(defaultValue: A): T | A;
|
|
334
354
|
/**
|
|
335
|
-
*
|
|
336
355
|
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
337
356
|
* execute the function that matches the `Result` variant.
|
|
338
357
|
*
|
|
@@ -346,13 +365,27 @@ declare class Result<T, E> implements Resultable<T, E> {
|
|
|
346
365
|
* @param err
|
|
347
366
|
*/
|
|
348
367
|
match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
|
|
368
|
+
/**
|
|
369
|
+
* Performs a side effect no matter what the `Result` variant is.
|
|
370
|
+
*
|
|
371
|
+
* @param fn The function to apply an `OK` value
|
|
372
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
373
|
+
*/
|
|
374
|
+
log(fn: (t?: T, e?: E) => void): this;
|
|
349
375
|
/**
|
|
350
376
|
* Performs a side effect for the `Ok` variant of `Result`.
|
|
351
377
|
*
|
|
352
|
-
* @param
|
|
378
|
+
* @param fn The function to apply an `OK` value
|
|
353
379
|
* @returns the result of applying `f` or an `Err` untouched
|
|
354
380
|
*/
|
|
355
|
-
tap(
|
|
381
|
+
tap(fn: (t: T) => void): this;
|
|
382
|
+
/**
|
|
383
|
+
* Performs a side effect for the `Err` variant of `Result`.
|
|
384
|
+
*
|
|
385
|
+
* @param fn The function to apply an `Err` value
|
|
386
|
+
* @returns the result of applying `f` or an `Ok` untouched
|
|
387
|
+
*/
|
|
388
|
+
tapError(fn: (e: E) => void): this;
|
|
356
389
|
/**
|
|
357
390
|
* This method is used to clean up and release any resources that the `Result`
|
|
358
391
|
* @param f The function that will be called to clean up the resources
|
|
@@ -440,4 +473,4 @@ interface ResultarError<T, E> {
|
|
|
440
473
|
}
|
|
441
474
|
declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
|
|
442
475
|
|
|
443
|
-
export { ok as A, err as B, type
|
|
476
|
+
export { ok as A, err as B, type Combine as C, type Dedup as D, type ExtractOkTypes as E, fromThrowable as F, safeTry as G, type ErrorConfig as H, type InferOkTypes as I, createResultarError as J, type MemberListOf as M, ResultAsync as R, type Transpose as T, type ExtractOkAsyncTypes as a, type ExtractErrTypes as b, type ExtractErrAsyncTypes as c, type InferErrTypes as d, type InferAsyncOkTypes as e, type InferAsyncErrTypes as f, type IsLiteralArray as g, type EmptyArrayToNever as h, type MembersToUnion as i, type CombineResults as j, type CombineResultsWithAllErrorsArray as k, combineResultAsyncList as l, combineResultList as m, combineResultAsyncListWithAllErrors as n, combineResultListWithAllErrors as o, type CombineResultAsyncs as p, type CombineResultsWithAllErrorsArrayAsync as q, DisposableResultAsync as r, okAsync as s, errAsync as t, fromPromise as u, fromSafePromise as v, fromThrowableAsync as w, safeTryAsync as x, Result as y, DisposableResult as z };
|