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
|
@@ -0,0 +1,393 @@
|
|
|
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
|
+
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
5
|
+
static combine<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultAsyncs<T>;
|
|
6
|
+
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...Array<ResultAsync<unknown, unknown>>]>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
7
|
+
static combineWithAllErrors<T extends ReadonlyArray<ResultAsync<unknown, unknown>>>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
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
|
+
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
13
|
+
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
14
|
+
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
15
|
+
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
16
|
+
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
17
|
+
match<X>(ok: (t: T) => X, _err: (e: E) => X): Promise<X>;
|
|
18
|
+
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
19
|
+
/**
|
|
20
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
21
|
+
*/
|
|
22
|
+
safeUnwrap(): AsyncGenerator<Result<never, E>, T>;
|
|
23
|
+
}
|
|
24
|
+
declare function fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
25
|
+
declare function fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
26
|
+
declare function okAsync<T, E = never>(value: T): ResultAsync<T, E>;
|
|
27
|
+
declare function errAsync<T = never, E = unknown>(error: E): ResultAsync<T, E>;
|
|
28
|
+
declare function fromThrowableAsync<A extends readonly any[], T, E>(fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
29
|
+
declare function safeTryAsync<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
30
|
+
type CombineResultAsyncs<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
31
|
+
type CombineResultsWithAllErrorsArrayAsync<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, Array<ExtractErrAsyncTypes<T>[number]>>;
|
|
32
|
+
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;
|
|
33
|
+
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;
|
|
34
|
+
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;
|
|
35
|
+
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
36
|
+
|
|
37
|
+
type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
|
|
38
|
+
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] : [];
|
|
39
|
+
type ExtractOkTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
|
|
40
|
+
[idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never;
|
|
41
|
+
};
|
|
42
|
+
type ExtractOkAsyncTypes<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = {
|
|
43
|
+
[idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never;
|
|
44
|
+
};
|
|
45
|
+
type ExtractErrTypes<T extends ReadonlyArray<Result<unknown, unknown>>> = {
|
|
46
|
+
[idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never;
|
|
47
|
+
};
|
|
48
|
+
type ExtractErrAsyncTypes<T extends ReadonlyArray<ResultAsync<unknown, unknown>>> = {
|
|
49
|
+
[idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never;
|
|
50
|
+
};
|
|
51
|
+
type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never;
|
|
52
|
+
type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never;
|
|
53
|
+
type InferAsyncOkTypes<R> = R extends ResultAsync<infer T, unknown> ? T : never;
|
|
54
|
+
type InferAsyncErrTypes<R> = R extends ResultAsync<unknown, infer E> ? E : never;
|
|
55
|
+
type IsLiteralArray<T> = T extends {
|
|
56
|
+
length: infer L;
|
|
57
|
+
} ? L extends number ? number extends L ? 0 : 1 : 0 : 0;
|
|
58
|
+
type EmptyArrayToNever<T, NeverArrayToNever extends number = 0> = T extends [] ? never : NeverArrayToNever extends 1 ? T extends [never, ...infer Rest] ? [EmptyArrayToNever<Rest>] extends [never] ? never : T : T : T;
|
|
59
|
+
type Prev = [
|
|
60
|
+
never,
|
|
61
|
+
0,
|
|
62
|
+
1,
|
|
63
|
+
2,
|
|
64
|
+
3,
|
|
65
|
+
4,
|
|
66
|
+
5,
|
|
67
|
+
6,
|
|
68
|
+
7,
|
|
69
|
+
8,
|
|
70
|
+
9,
|
|
71
|
+
10,
|
|
72
|
+
11,
|
|
73
|
+
12,
|
|
74
|
+
13,
|
|
75
|
+
14,
|
|
76
|
+
15,
|
|
77
|
+
16,
|
|
78
|
+
17,
|
|
79
|
+
18,
|
|
80
|
+
19,
|
|
81
|
+
20,
|
|
82
|
+
21,
|
|
83
|
+
22,
|
|
84
|
+
23,
|
|
85
|
+
24,
|
|
86
|
+
25,
|
|
87
|
+
26,
|
|
88
|
+
27,
|
|
89
|
+
28,
|
|
90
|
+
29,
|
|
91
|
+
30,
|
|
92
|
+
31,
|
|
93
|
+
32,
|
|
94
|
+
33,
|
|
95
|
+
34,
|
|
96
|
+
35,
|
|
97
|
+
36,
|
|
98
|
+
37,
|
|
99
|
+
38,
|
|
100
|
+
39,
|
|
101
|
+
40,
|
|
102
|
+
41,
|
|
103
|
+
42,
|
|
104
|
+
43,
|
|
105
|
+
44,
|
|
106
|
+
45,
|
|
107
|
+
46,
|
|
108
|
+
47,
|
|
109
|
+
48,
|
|
110
|
+
49,
|
|
111
|
+
...Array<0>
|
|
112
|
+
];
|
|
113
|
+
type Transpose<A, Transposed extends unknown[][] = [], Depth extends number = 10> = A extends [infer T, ...infer Rest] ? T extends [infer L, infer R] ? Transposed extends [infer PL, infer PR] ? PL extends unknown[] ? PR extends unknown[] ? Transpose<Rest, [[...PL, L], [...PR, R]], Prev[Depth]> : never : never : Transpose<Rest, [[L], [R]], Prev[Depth]> : Transposed : Transposed;
|
|
114
|
+
type CollectResults<T, Collected extends unknown[] = [], Depth extends number = 50> = [
|
|
115
|
+
Depth
|
|
116
|
+
] extends [never] ? [] : T extends [infer H, ...infer Rest] ? H extends Result<infer L, infer R> ? CollectResults<Rest, [
|
|
117
|
+
...Collected,
|
|
118
|
+
[L, R]
|
|
119
|
+
], Prev[Depth]> : never : Collected;
|
|
120
|
+
type UnknownMembersToNever<T> = T extends [infer H, ...infer R] ? [[unknown] extends [H] ? never : H, ...UnknownMembersToNever<R>] : T;
|
|
121
|
+
type MembersToUnion<T> = T extends unknown[] ? T[number] : never;
|
|
122
|
+
type Combine<T, Depth extends number = 5> = Transpose<CollectResults<T>, [], Depth> extends [
|
|
123
|
+
infer L,
|
|
124
|
+
infer R
|
|
125
|
+
] ? [UnknownMembersToNever<L>, UnknownMembersToNever<R>] : Transpose<CollectResults<T>, [], Depth> extends [] ? [[], []] : never;
|
|
126
|
+
type Traverse<T, Depth extends number = 5> = Combine<T, Depth> extends [infer Oks, infer Errs] ? Result<EmptyArrayToNever<Oks, 1>, MembersToUnion<Errs>> : never;
|
|
127
|
+
type TraverseWithAllErrors<T, Depth extends number = 5> = Combine<T, Depth> extends [
|
|
128
|
+
infer Oks,
|
|
129
|
+
infer Errs
|
|
130
|
+
] ? Result<EmptyArrayToNever<Oks>, EmptyArrayToNever<Errs>> : never;
|
|
131
|
+
type CombineResults<T extends ReadonlyArray<Result<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? Traverse<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>;
|
|
132
|
+
type CombineResultsWithAllErrorsArray<T extends ReadonlyArray<Result<unknown, unknown>>> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrors<T> : Result<ExtractOkTypes<T>, Array<ExtractErrTypes<T>[number]>>;
|
|
133
|
+
declare const combineResultAsyncList: <T, E>(asyncResultList: ReadonlyArray<ResultAsync<T, E>>) => ResultAsync<readonly T[], E>;
|
|
134
|
+
/**
|
|
135
|
+
* Short circuits on the FIRST Err value that we find
|
|
136
|
+
*/
|
|
137
|
+
declare const combineResultList: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E>;
|
|
138
|
+
declare const combineResultAsyncListWithAllErrors: <T, E>(asyncResultList: ReadonlyArray<ResultAsync<T, E>>) => ResultAsync<readonly T[], E[]>;
|
|
139
|
+
/**
|
|
140
|
+
* Give a list of all the errors we find
|
|
141
|
+
*/
|
|
142
|
+
declare const combineResultListWithAllErrors: <T, E>(resultList: ReadonlyArray<Result<T, E>>) => Result<readonly T[], E[]>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A Result interface that is used to define the methods that are available on a
|
|
146
|
+
* `Result` object.
|
|
147
|
+
*
|
|
148
|
+
* The immutability of the `Result` object is maintained by the interface.
|
|
149
|
+
*/
|
|
150
|
+
interface Resultable<T, E> {
|
|
151
|
+
get value(): T;
|
|
152
|
+
get error(): E;
|
|
153
|
+
/**
|
|
154
|
+
* Used to check if a `Result` is an `OK`
|
|
155
|
+
*
|
|
156
|
+
* @returns `true` if the result is an `OK` variant of Result
|
|
157
|
+
*/
|
|
158
|
+
isOk(): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Used to check if a `Result` is an `Err`
|
|
161
|
+
*
|
|
162
|
+
* @returns `true` if the result is an `Err` variant of Result
|
|
163
|
+
*/
|
|
164
|
+
isErr(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
167
|
+
*
|
|
168
|
+
* @param v the default value to return if there is an `Err`
|
|
169
|
+
*/
|
|
170
|
+
unwrapOr<A>(defaultValue: A): T | A;
|
|
171
|
+
/**
|
|
172
|
+
* **This method is unsafe, and should only be used in a test environments**
|
|
173
|
+
*
|
|
174
|
+
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
175
|
+
*
|
|
176
|
+
* @param config
|
|
177
|
+
*/
|
|
178
|
+
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
179
|
+
/**
|
|
180
|
+
* **This method is unsafe, and should only be used in a test environments**
|
|
181
|
+
*
|
|
182
|
+
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
183
|
+
* otherwise it throws a custom object.
|
|
184
|
+
*
|
|
185
|
+
* @param config
|
|
186
|
+
*/
|
|
187
|
+
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
188
|
+
}
|
|
189
|
+
declare class Result<T, E> implements Resultable<T, E> {
|
|
190
|
+
private readonly state;
|
|
191
|
+
/**
|
|
192
|
+
* Wraps a function with a try catch, creating a new function with the same
|
|
193
|
+
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
194
|
+
*
|
|
195
|
+
* @param fn function to wrap with ok on success or err on failure
|
|
196
|
+
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
197
|
+
*/
|
|
198
|
+
static fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
199
|
+
static ok<T, E = never>(value: T): Result<T, E>;
|
|
200
|
+
static err<T = never, E = unknown>(error: E): Result<T, E>;
|
|
201
|
+
static combine<T extends readonly [Result<unknown, unknown>, ...Array<Result<unknown, unknown>>]>(resultList: T): CombineResults<T>;
|
|
202
|
+
static combine<T extends ReadonlyArray<Result<unknown, unknown>>>(resultList: T): CombineResults<T>;
|
|
203
|
+
static combineWithAllErrors<T extends readonly [Result<unknown, unknown>, ...Array<Result<unknown, unknown>>]>(resultList: T): CombineResultsWithAllErrorsArray<T>;
|
|
204
|
+
static combineWithAllErrors<T extends ReadonlyArray<Result<unknown, unknown>>>(resultList: T): CombineResultsWithAllErrorsArray<T>;
|
|
205
|
+
readonly value: T;
|
|
206
|
+
readonly error: E;
|
|
207
|
+
private constructor();
|
|
208
|
+
/**
|
|
209
|
+
* Used to check if a `Result` is an `OK`
|
|
210
|
+
*
|
|
211
|
+
* @returns `true` if the result is an `OK` variant of Result
|
|
212
|
+
*/
|
|
213
|
+
isOk(): boolean;
|
|
214
|
+
/**
|
|
215
|
+
* Used to check if a `Result` is an `Err`
|
|
216
|
+
*
|
|
217
|
+
* @returns `true` if the result is an `Err` variant of Result
|
|
218
|
+
*/
|
|
219
|
+
isErr(): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Maps a `Result<T, E>` to `Result<U, E>`
|
|
222
|
+
* by applying a function to a contained `Ok` value, leaving an `Err` value
|
|
223
|
+
* untouched.
|
|
224
|
+
*
|
|
225
|
+
* @param f The function to apply an `OK` value
|
|
226
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
227
|
+
*/
|
|
228
|
+
map<X, Y>(f: (t: T) => X): Result<X, E | Y>;
|
|
229
|
+
/**
|
|
230
|
+
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a
|
|
231
|
+
* contained `Err` value, leaving an `Ok` value untouched.
|
|
232
|
+
*
|
|
233
|
+
* This function can be used to pass through a successful result while
|
|
234
|
+
* handling an error.
|
|
235
|
+
*
|
|
236
|
+
* @param f a function to apply to the error `Err` value
|
|
237
|
+
*/
|
|
238
|
+
mapErr<X>(fn: (t: E) => X): Result<T, X>;
|
|
239
|
+
/**
|
|
240
|
+
* Similar to `map` Except you must return a new `Result`.
|
|
241
|
+
*
|
|
242
|
+
* This is useful for when you need to do a subsequent computation using the
|
|
243
|
+
* inner `T` value, but that computation might fail.
|
|
244
|
+
* Additionally, `andThen` is really useful as a tool to flatten a
|
|
245
|
+
* `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below).
|
|
246
|
+
*
|
|
247
|
+
* @param f The function to apply to the current value
|
|
248
|
+
*/
|
|
249
|
+
andThen<X, Y>(f: (t: T) => Result<X, Y>): Result<X, E | Y>;
|
|
250
|
+
/**
|
|
251
|
+
* Takes an `Err` value and maps it to a `Result<T, SomeNewType>`.
|
|
252
|
+
*
|
|
253
|
+
* This is useful for error recovery.
|
|
254
|
+
*
|
|
255
|
+
*
|
|
256
|
+
* @param f A function to apply to an `Err` value, leaving `Ok` values
|
|
257
|
+
* untouched.
|
|
258
|
+
*/
|
|
259
|
+
orElse<Y>(f: (t: E) => Result<T, Y>): Result<T, Y>;
|
|
260
|
+
/**
|
|
261
|
+
* Similar to `map` Except you must return a new `Result`.
|
|
262
|
+
*
|
|
263
|
+
* This is useful for when you need to do a subsequent async computation using
|
|
264
|
+
* the inner `T` value, but that computation might fail. Must return a ResultAsync
|
|
265
|
+
*
|
|
266
|
+
* @param f The function that returns a `ResultAsync` to apply to the current
|
|
267
|
+
* value
|
|
268
|
+
*/
|
|
269
|
+
asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
270
|
+
/**
|
|
271
|
+
* Maps a `Result<T, E>` to `ResultAsync<U, E>`
|
|
272
|
+
* by applying an async function to a contained `Ok` value, leaving an `Err`
|
|
273
|
+
* value untouched.
|
|
274
|
+
*
|
|
275
|
+
* @param f An async function to apply an `OK` value
|
|
276
|
+
*/
|
|
277
|
+
asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
|
|
278
|
+
/**
|
|
279
|
+
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
280
|
+
*
|
|
281
|
+
* @param v the default value to return if there is an `Err`
|
|
282
|
+
*/
|
|
283
|
+
unwrapOr<A>(defaultValue: A): T | A;
|
|
284
|
+
/**
|
|
285
|
+
*
|
|
286
|
+
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
287
|
+
* execute the function that matches the `Result` variant.
|
|
288
|
+
*
|
|
289
|
+
* Match callbacks do not necessitate to return a `Result`, however you can
|
|
290
|
+
* return a `Result` if you want to.
|
|
291
|
+
*
|
|
292
|
+
* `match` is like chaining `map` and `mapErr`, with the distinction that
|
|
293
|
+
* with `match` both functions must have the same return type.
|
|
294
|
+
*
|
|
295
|
+
* @param ok
|
|
296
|
+
* @param err
|
|
297
|
+
*/
|
|
298
|
+
match<X>(fnOk: (t: T) => X, fnErr: (e: E) => X): X;
|
|
299
|
+
/**
|
|
300
|
+
* Performs a side effect for the `Ok` variant of `Result`.
|
|
301
|
+
*
|
|
302
|
+
* @param f The function to apply an `OK` value
|
|
303
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
304
|
+
*/
|
|
305
|
+
tap(f: (t: T) => void): Result<T, E>;
|
|
306
|
+
/**
|
|
307
|
+
* This method is used to clean up and release any resources that the `Result`
|
|
308
|
+
* @param f The function that will be called to clean up the resources
|
|
309
|
+
*/
|
|
310
|
+
finally<X = T, Y = E>(f: (value: X, error: Y) => void): DisposableResult<X, Y>;
|
|
311
|
+
/**
|
|
312
|
+
* **This method is unsafe, and should only be used in a test environments**
|
|
313
|
+
*
|
|
314
|
+
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
315
|
+
*
|
|
316
|
+
* @param config
|
|
317
|
+
*/
|
|
318
|
+
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
319
|
+
/**
|
|
320
|
+
* **This method is unsafe, and should only be used in a test environments**
|
|
321
|
+
*
|
|
322
|
+
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
323
|
+
* otherwise it throws a custom object.
|
|
324
|
+
*
|
|
325
|
+
* @param config
|
|
326
|
+
*/
|
|
327
|
+
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
328
|
+
safeUnwrap(): Generator<Result<never, E>, T>;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A `Disposable` is an object that has a `dispose` method that can be used to
|
|
332
|
+
* clean up resources.
|
|
333
|
+
*/
|
|
334
|
+
declare class DisposableResult<T, E> implements Resultable<T, E>, Disposable {
|
|
335
|
+
readonly result: Resultable<T, E>;
|
|
336
|
+
private readonly finalizer;
|
|
337
|
+
constructor(result: Resultable<T, E>, finalizer: (value: T, error: E) => void);
|
|
338
|
+
get value(): T;
|
|
339
|
+
get error(): E;
|
|
340
|
+
_unsafeUnwrap(config?: ErrorConfig | undefined): T;
|
|
341
|
+
_unsafeUnwrapErr(config?: ErrorConfig | undefined): E;
|
|
342
|
+
isOk(): boolean;
|
|
343
|
+
isErr(): boolean;
|
|
344
|
+
unwrapOr<A>(defaultValue: A): T | A;
|
|
345
|
+
[Symbol.dispose](): void;
|
|
346
|
+
}
|
|
347
|
+
declare const ok: <T, E = never>(value: T) => Result<T, E>;
|
|
348
|
+
declare const err: <T = never, E = unknown>(error: E) => Result<T, E>;
|
|
349
|
+
declare const fromThrowable: <Fn extends (...args: readonly any[]) => unknown, E>(fn: Fn, errorFn?: ((e: any) => E) | undefined) => (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
350
|
+
/**
|
|
351
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
352
|
+
* whichever comes first.
|
|
353
|
+
*
|
|
354
|
+
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
355
|
+
* Rust's ? operator.
|
|
356
|
+
* See `/tests/safeTry.test.ts` for examples.
|
|
357
|
+
*
|
|
358
|
+
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
359
|
+
* Rust's `result?` expression.
|
|
360
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
361
|
+
*/
|
|
362
|
+
declare function safeTry<T, E>(body: () => Generator<Result<never, E>, Result<T, E>>): Result<T, E>;
|
|
363
|
+
/**
|
|
364
|
+
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
365
|
+
* whichever comes first.
|
|
366
|
+
*
|
|
367
|
+
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
368
|
+
* Rust's ? operator.
|
|
369
|
+
* See `/tests/safeTry.test.ts` for examples.
|
|
370
|
+
*
|
|
371
|
+
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` and
|
|
372
|
+
* `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression.
|
|
373
|
+
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
374
|
+
*/
|
|
375
|
+
declare function safeTry<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): Promise<Result<T, E>>;
|
|
376
|
+
|
|
377
|
+
interface ErrorConfig {
|
|
378
|
+
withStackTrace: boolean;
|
|
379
|
+
}
|
|
380
|
+
interface ResultarError<T, E> {
|
|
381
|
+
data: {
|
|
382
|
+
type: string;
|
|
383
|
+
value: T | undefined;
|
|
384
|
+
} | {
|
|
385
|
+
type: string;
|
|
386
|
+
value: E | undefined;
|
|
387
|
+
};
|
|
388
|
+
message: string;
|
|
389
|
+
stack: string | undefined;
|
|
390
|
+
}
|
|
391
|
+
declare const createResultarError: <T, E>(message: string, result: Result<T, E>, config?: ErrorConfig) => ResultarError<T, E>;
|
|
392
|
+
|
|
393
|
+
export { err as A, fromThrowable as B, type CombineResultAsyncs as C, type Dedup as D, type ExtractOkTypes as E, safeTry as F, type ErrorConfig as G, createResultarError as H, type InferOkTypes as I, type MemberListOf as M, ResultAsync as R, type Transpose as T, fromPromise as a, fromThrowableAsync as b, type CombineResultsWithAllErrorsArrayAsync as c, type ExtractOkAsyncTypes as d, errAsync as e, fromSafePromise as f, type ExtractErrTypes as g, type ExtractErrAsyncTypes as h, type InferErrTypes as i, type InferAsyncOkTypes as j, type InferAsyncErrTypes as k, type IsLiteralArray as l, type EmptyArrayToNever as m, type MembersToUnion as n, okAsync as o, type Combine as p, type CombineResults as q, type CombineResultsWithAllErrorsArray as r, safeTryAsync as s, combineResultAsyncList as t, combineResultList as u, combineResultAsyncListWithAllErrors as v, combineResultListWithAllErrors as w, Result as x, DisposableResult as y, ok as z };
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
DisposableResult: () => DisposableResult,
|
|
23
24
|
Result: () => Result,
|
|
24
25
|
ResultAsync: () => ResultAsync,
|
|
25
26
|
err: () => err,
|
|
@@ -35,20 +36,6 @@ __export(src_exports, {
|
|
|
35
36
|
});
|
|
36
37
|
module.exports = __toCommonJS(src_exports);
|
|
37
38
|
|
|
38
|
-
// src/error.ts
|
|
39
|
-
var defaultErrorConfig = {
|
|
40
|
-
withStackTrace: false
|
|
41
|
-
};
|
|
42
|
-
var createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
43
|
-
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
44
|
-
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
45
|
-
return {
|
|
46
|
-
data,
|
|
47
|
-
message,
|
|
48
|
-
stack: maybeStack
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
39
|
// src/result-async.ts
|
|
53
40
|
var ResultAsync = class _ResultAsync {
|
|
54
41
|
static fromSafePromise(promise) {
|
|
@@ -59,6 +46,14 @@ var ResultAsync = class _ResultAsync {
|
|
|
59
46
|
const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
|
|
60
47
|
return new _ResultAsync(newPromise);
|
|
61
48
|
}
|
|
49
|
+
static combine(asyncResultList) {
|
|
50
|
+
return combineResultAsyncList(asyncResultList);
|
|
51
|
+
}
|
|
52
|
+
static combineWithAllErrors(asyncResultList) {
|
|
53
|
+
return combineResultAsyncListWithAllErrors(
|
|
54
|
+
asyncResultList
|
|
55
|
+
);
|
|
56
|
+
}
|
|
62
57
|
innerPromise;
|
|
63
58
|
constructor(res) {
|
|
64
59
|
this.innerPromise = res;
|
|
@@ -87,6 +82,7 @@ var ResultAsync = class _ResultAsync {
|
|
|
87
82
|
}
|
|
88
83
|
andThen(f) {
|
|
89
84
|
return new _ResultAsync(
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
90
86
|
this.innerPromise.then(async (res) => {
|
|
91
87
|
if (res.isErr()) {
|
|
92
88
|
return errAsync(res.error);
|
|
@@ -157,7 +153,57 @@ function safeTryAsync(body) {
|
|
|
157
153
|
);
|
|
158
154
|
}
|
|
159
155
|
|
|
156
|
+
// src/utils.ts
|
|
157
|
+
var appendValueToEndOfList = (value) => (list) => [...list, value];
|
|
158
|
+
var combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
159
|
+
combineResultList
|
|
160
|
+
);
|
|
161
|
+
var combineResultList = (resultList) => resultList.reduce(
|
|
162
|
+
// eslint-disable-line unicorn/no-array-reduce
|
|
163
|
+
(acc, result) => acc.isOk() ? result.isErr() ? err(result.error) : acc.map(appendValueToEndOfList(result.value)) : acc,
|
|
164
|
+
ok([])
|
|
165
|
+
);
|
|
166
|
+
var combineResultAsyncListWithAllErrors = (asyncResultList) => {
|
|
167
|
+
const x = ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
168
|
+
combineResultListWithAllErrors
|
|
169
|
+
);
|
|
170
|
+
return x;
|
|
171
|
+
};
|
|
172
|
+
var combineResultListWithAllErrors = (resultList) => resultList.reduce(
|
|
173
|
+
// eslint-disable-line unicorn/no-array-reduce
|
|
174
|
+
(acc, result) => result.isErr() ? acc.isErr() ? err([...acc.error, result.error]) : err([result.error]) : acc.isErr() ? acc : ok([...acc.value, result.value]),
|
|
175
|
+
ok([])
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// src/error.ts
|
|
179
|
+
var defaultErrorConfig = {
|
|
180
|
+
withStackTrace: false
|
|
181
|
+
};
|
|
182
|
+
var createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
183
|
+
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
184
|
+
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
185
|
+
return {
|
|
186
|
+
data,
|
|
187
|
+
message,
|
|
188
|
+
stack: maybeStack
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
|
|
160
192
|
// src/result.ts
|
|
193
|
+
var Ok = class {
|
|
194
|
+
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
195
|
+
constructor(value) {
|
|
196
|
+
this.value = value;
|
|
197
|
+
}
|
|
198
|
+
ok = true;
|
|
199
|
+
};
|
|
200
|
+
var Err = class {
|
|
201
|
+
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
202
|
+
constructor(error) {
|
|
203
|
+
this.error = error;
|
|
204
|
+
}
|
|
205
|
+
ok = false;
|
|
206
|
+
};
|
|
161
207
|
var Result = class _Result {
|
|
162
208
|
constructor(state) {
|
|
163
209
|
this.state = state;
|
|
@@ -190,10 +236,16 @@ var Result = class _Result {
|
|
|
190
236
|
};
|
|
191
237
|
}
|
|
192
238
|
static ok(value) {
|
|
193
|
-
return new _Result(
|
|
239
|
+
return new _Result(new Ok(value));
|
|
194
240
|
}
|
|
195
241
|
static err(error) {
|
|
196
|
-
return new _Result(
|
|
242
|
+
return new _Result(new Err(error));
|
|
243
|
+
}
|
|
244
|
+
static combine(resultList) {
|
|
245
|
+
return combineResultList(resultList);
|
|
246
|
+
}
|
|
247
|
+
static combineWithAllErrors(resultList) {
|
|
248
|
+
return combineResultListWithAllErrors(resultList);
|
|
197
249
|
}
|
|
198
250
|
value;
|
|
199
251
|
error;
|
|
@@ -313,6 +365,14 @@ var Result = class _Result {
|
|
|
313
365
|
}
|
|
314
366
|
return err(this.error);
|
|
315
367
|
}
|
|
368
|
+
finally(f) {
|
|
369
|
+
const resultDisposable = new DisposableResult(this, f);
|
|
370
|
+
try {
|
|
371
|
+
return resultDisposable;
|
|
372
|
+
} finally {
|
|
373
|
+
resultDisposable[Symbol.dispose]();
|
|
374
|
+
}
|
|
375
|
+
}
|
|
316
376
|
/**
|
|
317
377
|
* **This method is unsafe, and should only be used in a test environments**
|
|
318
378
|
*
|
|
@@ -354,6 +414,36 @@ var Result = class _Result {
|
|
|
354
414
|
}();
|
|
355
415
|
}
|
|
356
416
|
};
|
|
417
|
+
var DisposableResult = class {
|
|
418
|
+
constructor(result, finalizer) {
|
|
419
|
+
this.result = result;
|
|
420
|
+
this.finalizer = finalizer;
|
|
421
|
+
}
|
|
422
|
+
get value() {
|
|
423
|
+
return this.result.value;
|
|
424
|
+
}
|
|
425
|
+
get error() {
|
|
426
|
+
return this.result.error;
|
|
427
|
+
}
|
|
428
|
+
_unsafeUnwrap(config) {
|
|
429
|
+
return this.result._unsafeUnwrap(config);
|
|
430
|
+
}
|
|
431
|
+
_unsafeUnwrapErr(config) {
|
|
432
|
+
return this.result._unsafeUnwrapErr(config);
|
|
433
|
+
}
|
|
434
|
+
isOk() {
|
|
435
|
+
return this.result.isOk();
|
|
436
|
+
}
|
|
437
|
+
isErr() {
|
|
438
|
+
return this.result.isErr();
|
|
439
|
+
}
|
|
440
|
+
unwrapOr(defaultValue) {
|
|
441
|
+
return this.result.unwrapOr(defaultValue);
|
|
442
|
+
}
|
|
443
|
+
[Symbol.dispose]() {
|
|
444
|
+
this.finalizer(this.result.value, this.result.error);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
357
447
|
var ok = (value) => Result.ok(value);
|
|
358
448
|
var err = (error) => Result.err(error);
|
|
359
449
|
var fromThrowable = (fn, errorFn) => Result.fromThrowable(fn, errorFn);
|
|
@@ -366,6 +456,7 @@ function safeTry(body) {
|
|
|
366
456
|
}
|
|
367
457
|
// Annotate the CommonJS export names for ESM import in node:
|
|
368
458
|
0 && (module.exports = {
|
|
459
|
+
DisposableResult,
|
|
369
460
|
Result,
|
|
370
461
|
ResultAsync,
|
|
371
462
|
err,
|