resultar 2.0.0 → 3.0.0-alpha.2
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 +437 -606
- package/dist/index.d.ts +622 -3
- package/dist/index.js +1007 -10
- package/dist/index.js.map +1 -1
- package/package.json +30 -60
- package/dist/no-discard.d.ts +0 -18
- package/dist/no-discard.js +0 -124
- package/dist/no-discard.js.map +0 -1
- package/dist/result-async-CdlVdXgX.js +0 -698
- package/dist/result-async-CdlVdXgX.js.map +0 -1
- package/dist/result-async-CxGBicja.d.ts +0 -439
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
//#region src/error.d.ts
|
|
2
|
-
interface ErrorConfig {
|
|
3
|
-
withStackTrace: boolean;
|
|
4
|
-
}
|
|
5
|
-
//#endregion
|
|
6
|
-
//#region src/utils.d.ts
|
|
7
|
-
type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never };
|
|
8
|
-
type ExtractOkAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never };
|
|
9
|
-
type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never };
|
|
10
|
-
type ExtractErrAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { [idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never };
|
|
11
|
-
type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never;
|
|
12
|
-
type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never;
|
|
13
|
-
type InferAsyncOkTypes<R> = R extends ResultAsync<infer T, unknown> ? T : never;
|
|
14
|
-
type InferAsyncErrTypes<R> = R extends ResultAsync<unknown, infer E> ? E : never;
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/result.d.ts
|
|
17
|
-
type TaggedValue$1 = {
|
|
18
|
-
readonly _tag: string;
|
|
19
|
-
};
|
|
20
|
-
type TaggedErrorValue$1 = Error & TaggedValue$1;
|
|
21
|
-
type TagsOf$1<E> = Extract<E, TaggedValue$1>["_tag"];
|
|
22
|
-
type ErrorTagsOf$1<E> = Extract<E, TaggedErrorValue$1>["_tag"];
|
|
23
|
-
type ErrorForTag$1<E, Tag extends string> = Extract<E, {
|
|
24
|
-
readonly _tag: Tag;
|
|
25
|
-
}>;
|
|
26
|
-
type ExcludeTag$1<E, Tag extends string> = Exclude<E, {
|
|
27
|
-
readonly _tag: Tag;
|
|
28
|
-
}>;
|
|
29
|
-
type UntaggedError$1<E> = Exclude<Extract<E, Error>, TaggedErrorValue$1>;
|
|
30
|
-
type CatchTagHandlerResult$1<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
|
|
31
|
-
type MatchTagHandlerResult$1<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
|
|
32
|
-
type CatchTagHandlers$1<E, Handlers> = { readonly [Tag in keyof Handlers]: Tag extends TagsOf$1<E> ? (error: ErrorForTag$1<E, Tag & string>) => Result<unknown, unknown> : never };
|
|
33
|
-
type MatchTagHandlers$1<E, Handlers> = { readonly [Tag in ErrorTagsOf$1<E>]: (error: ErrorForTag$1<E, Tag>) => unknown } & ([UntaggedError$1<E>] extends [never] ? {
|
|
34
|
-
readonly Error?: (error: Error) => unknown;
|
|
35
|
-
} : {
|
|
36
|
-
readonly Error: (error: UntaggedError$1<E>) => unknown;
|
|
37
|
-
}) & { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf$1<E> | "Error" ? Handlers[Tag] : never };
|
|
38
|
-
type PartialMatchTagHandlers$1<E, Handlers> = { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf$1<E> ? (error: ErrorForTag$1<E, Tag & string>) => unknown : Tag extends "Error" ? (error: Extract<E, Error>) => unknown : never };
|
|
39
|
-
type PipeFn$1<Input, Output> = (input: Input) => Output;
|
|
40
|
-
type ResultFinalizer<T, E> = (value: T | undefined, error: E | undefined) => void;
|
|
41
|
-
/**
|
|
42
|
-
* A Result interface that is used to define the methods that are available on a
|
|
43
|
-
* `Result` object.
|
|
44
|
-
*
|
|
45
|
-
* The immutability of the `Result` object is maintained by the interface.
|
|
46
|
-
*/
|
|
47
|
-
interface Resultable<T, E> {
|
|
48
|
-
get value(): T;
|
|
49
|
-
get error(): E;
|
|
50
|
-
/**
|
|
51
|
-
* Used to check if a `Result` is an `OK`
|
|
52
|
-
*
|
|
53
|
-
* @returns `true` if the result is an `OK` variant of Result
|
|
54
|
-
*/
|
|
55
|
-
isOk(): boolean;
|
|
56
|
-
/**
|
|
57
|
-
* Used to check if a `Result` is an `Err`
|
|
58
|
-
*
|
|
59
|
-
* @returns `true` if the result is an `Err` variant of Result
|
|
60
|
-
*/
|
|
61
|
-
isErr(): boolean;
|
|
62
|
-
/**
|
|
63
|
-
* Unwrap the `Ok` value, or return the default if there is an `Err`
|
|
64
|
-
*
|
|
65
|
-
* @param v the default value to return if there is an `Err`
|
|
66
|
-
*/
|
|
67
|
-
unwrapOr<A>(defaultValue: A): T | A;
|
|
68
|
-
/**
|
|
69
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
70
|
-
*
|
|
71
|
-
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object.
|
|
72
|
-
*
|
|
73
|
-
* @param config
|
|
74
|
-
*/
|
|
75
|
-
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
76
|
-
/**
|
|
77
|
-
* **This method is unsafe, and should only be used in a test environments**
|
|
78
|
-
*
|
|
79
|
-
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`,
|
|
80
|
-
* otherwise it throws a custom object.
|
|
81
|
-
*
|
|
82
|
-
* @param config
|
|
83
|
-
*/
|
|
84
|
-
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
85
|
-
}
|
|
86
|
-
interface ResultOperations<T, E> {
|
|
87
|
-
isOk(): this is OkResult<T, E>;
|
|
88
|
-
isErr(): this is ErrResult<T, E>;
|
|
89
|
-
unwrapOr<A>(defaultValue: A): T | A;
|
|
90
|
-
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
91
|
-
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
92
|
-
map<X>(f: (t: T) => X): Result<X, E>;
|
|
93
|
-
filterOrElse<U extends T, F>(predicate: (value: T) => value is U, onFalse: (value: T) => F): Result<U, E | F>;
|
|
94
|
-
filterOrElse<F>(predicate: (value: T) => boolean, onFalse: (value: T) => F): Result<T, E | F>;
|
|
95
|
-
mapErr<X>(fn: (t: E) => X): Result<T, X>;
|
|
96
|
-
andThen<X, Y>(f: (t: T) => Result<X, Y>): Result<X, E | Y>;
|
|
97
|
-
if(fCondition: (t: T) => boolean): {
|
|
98
|
-
true: <X1, Y1>(fTrue: (t: T) => Result<X1, Y1>) => {
|
|
99
|
-
false: <X2, Y2>(fFalse: (t: T) => Result<X2, Y2>) => Result<X1 | X2, Y1 | Y2 | E>;
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): Result<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
103
|
-
catchTag<const Tag extends TagsOf$1<E>, R extends Result<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag$1<E, Tag>) => R): Result<T | InferOkTypes<R>, ExcludeTag$1<E, Tag> | InferErrTypes<R>>;
|
|
104
|
-
catchTags<const Handlers extends object>(handlers: Handlers & CatchTagHandlers$1<E, Handlers>): Result<T | InferOkTypes<CatchTagHandlerResult$1<Handlers>>, ExcludeTag$1<E, keyof Handlers & string> | InferErrTypes<CatchTagHandlerResult$1<Handlers>>>;
|
|
105
|
-
asyncAndThen<X, Y>(f: (t: T) => ResultAsync<X, Y>): ResultAsync<X, E | Y>;
|
|
106
|
-
asyncMap<X>(f: (t: T) => Promise<X>): ResultAsync<X, E>;
|
|
107
|
-
match<A, B = A>(fnOk: (t: T) => A, fnErr: (e: E) => B): A | B;
|
|
108
|
-
matchTags<A, const Handlers extends object>(fnOk: (t: T) => A, handlers: Handlers & MatchTagHandlers$1<E, Handlers>): A | MatchTagHandlerResult$1<Handlers>;
|
|
109
|
-
matchTagsPartial<A, B, const Handlers extends object>(fnOk: (t: T) => A, handlers: Handlers & PartialMatchTagHandlers$1<E, Handlers>, fallback: (error: E) => B): A | MatchTagHandlerResult$1<Handlers> | B;
|
|
110
|
-
pipe<A>(ab: PipeFn$1<Result<T, E>, A>): A;
|
|
111
|
-
pipe<A, B>(ab: PipeFn$1<Result<T, E>, A>, bc: PipeFn$1<A, B>): B;
|
|
112
|
-
pipe<A, B, C>(ab: PipeFn$1<Result<T, E>, A>, bc: PipeFn$1<A, B>, cd: PipeFn$1<B, C>): C;
|
|
113
|
-
log(fn: (t?: T, e?: E) => void): this;
|
|
114
|
-
tap(fn: (t: T) => void): this;
|
|
115
|
-
tapError(fn: (e: E) => void): this;
|
|
116
|
-
toDisposable(f: ResultFinalizer<T, E>): DisposableResult<T, E>;
|
|
117
|
-
unwrapOrThrow(): T;
|
|
118
|
-
[Symbol.iterator](): Generator<Result<never, E>, T>;
|
|
119
|
-
safeUnwrap(): Generator<Result<never, E>, T>;
|
|
120
|
-
}
|
|
121
|
-
interface OkResult<T, E> extends ResultOperations<T, E> {
|
|
122
|
-
readonly value: T;
|
|
123
|
-
}
|
|
124
|
-
interface ErrResult<T, E> extends ResultOperations<T, E> {
|
|
125
|
-
readonly error: E;
|
|
126
|
-
}
|
|
127
|
-
type Result<T, E> = OkResult<T, E> | ErrResult<T, E>;
|
|
128
|
-
type StrictResult<T, E extends Error = Error> = Result<T, E>;
|
|
129
|
-
interface ResultStatic {
|
|
130
|
-
[Symbol.hasInstance](value: unknown): boolean;
|
|
131
|
-
tryCatch<T, E>(this: void, fn: () => Exclude<T, Promise<unknown>>, errorFn?: (e: unknown) => E): Result<T, E>;
|
|
132
|
-
fromThrowable<Fn extends (...args: readonly any[]) => unknown, E>(this: void, fn: Fn, errorFn?: (e: any) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
133
|
-
ok<T, E = never>(this: void, value: T): OkResult<T, E>;
|
|
134
|
-
ok<E = never>(this: void, value: void): OkResult<void, E>;
|
|
135
|
-
unit<E = never>(this: void): Result<undefined, E>;
|
|
136
|
-
err<T = never, E extends string = string>(this: void, err: E): ErrResult<T, E>;
|
|
137
|
-
err<T = never, E = unknown>(this: void, err: E): ErrResult<T, E>;
|
|
138
|
-
err<T = never>(this: void, err: void): ErrResult<T, void>;
|
|
139
|
-
combine<T extends readonly [Result<unknown, unknown>, ...Result<unknown, unknown>[]]>(this: void, resultList: T): CombineResults<T>;
|
|
140
|
-
combine<T extends readonly Result<unknown, unknown>[]>(this: void, resultList: T): CombineResults<T>;
|
|
141
|
-
combineWithAllErrors<T extends readonly [Result<unknown, unknown>, ...Result<unknown, unknown>[]]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
|
|
142
|
-
combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(this: void, resultList: T): CombineResultsWithAllErrorsArray<T>;
|
|
143
|
-
}
|
|
144
|
-
declare const Result: ResultStatic;
|
|
145
|
-
/**
|
|
146
|
-
* A `Disposable` is an object that has a `dispose` method that can be used to
|
|
147
|
-
* clean up resources.
|
|
148
|
-
*/
|
|
149
|
-
declare class DisposableResult<T, E> implements Resultable<T, E>, Disposable {
|
|
150
|
-
private disposed;
|
|
151
|
-
private readonly finalizer;
|
|
152
|
-
readonly result: Result<T, E>;
|
|
153
|
-
constructor(result: Result<T, E>, finalizer: ResultFinalizer<T, E>);
|
|
154
|
-
get value(): T;
|
|
155
|
-
get error(): E;
|
|
156
|
-
_unsafeUnwrap(config?: ErrorConfig): T;
|
|
157
|
-
_unsafeUnwrapErr(config?: ErrorConfig): E;
|
|
158
|
-
isOk(): boolean;
|
|
159
|
-
isErr(): boolean;
|
|
160
|
-
unwrapOr<A>(defaultValue: A): T | A;
|
|
161
|
-
[Symbol.dispose](): void;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
165
|
-
* whichever comes first.
|
|
166
|
-
*
|
|
167
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
168
|
-
* Rust's ? operator.
|
|
169
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
170
|
-
*
|
|
171
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()` works as
|
|
172
|
-
* Rust's `result?` expression.
|
|
173
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
174
|
-
*/
|
|
175
|
-
declare function safeTry<T, E>(body: () => Generator<Result<never, E>, Result<T, E>>): Result<T, E>;
|
|
176
|
-
declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(body: () => Generator<YieldErr, GeneratorReturnResult>): Result<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
|
|
177
|
-
/**
|
|
178
|
-
* Evaluates the given generator to a Result returned or an Err yielded from it,
|
|
179
|
-
* whichever comes first.
|
|
180
|
-
*
|
|
181
|
-
* This function, in combination with `Result.safeUnwrap()`, is intended to emulate
|
|
182
|
-
* Rust's ? operator.
|
|
183
|
-
* See `/tests/safeTry.test.ts` for examples.
|
|
184
|
-
*
|
|
185
|
-
* @param body - What is evaluated. In body, `yield* result.safeUnwrap()`,
|
|
186
|
-
* `yield* result`, and `yield* resultAsync` work as Rust's `result?` expression.
|
|
187
|
-
* @returns The first occurence of either an yielded Err or a returned Result.
|
|
188
|
-
*/
|
|
189
|
-
declare function safeTry<T, E>(body: () => AsyncGenerator<Result<never, E>, Result<T, E>>): ResultAsync<T, E>;
|
|
190
|
-
declare function safeTry<YieldErr extends Result<never, unknown>, GeneratorReturnResult extends Result<unknown, unknown>>(body: () => AsyncGenerator<YieldErr, GeneratorReturnResult>): ResultAsync<InferOkTypes<GeneratorReturnResult>, InferErrTypes<YieldErr> | InferErrTypes<GeneratorReturnResult>>;
|
|
191
|
-
declare function ok<T, E = never>(value: T): OkResult<T, E>;
|
|
192
|
-
declare function ok<E = never>(value: void): OkResult<void, E>;
|
|
193
|
-
declare function err<T = never, E extends string = string>(err: E): ErrResult<T, E>;
|
|
194
|
-
declare function err<T = never, E = unknown>(err: E): ErrResult<T, E>;
|
|
195
|
-
declare function err<T = never>(err: void): ErrResult<T, void>;
|
|
196
|
-
declare const fromThrowable: typeof Result.fromThrowable;
|
|
197
|
-
declare function unit<E = never>(): OkResult<undefined, E>;
|
|
198
|
-
declare const tryCatch: typeof Result.tryCatch;
|
|
199
|
-
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, ...0[]];
|
|
200
|
-
type CollectResults<T, Collected extends unknown[] = [], Depth extends number = 50> = [Depth] extends [never] ? [] : T extends [infer H, ...infer Rest] ? H extends Result<infer L, infer R> ? CollectResults<Rest, [...Collected, [L, R]], Prev[Depth]> : never : Collected;
|
|
201
|
-
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;
|
|
202
|
-
type Combine<T, Depth extends number = 5> = Transpose<CollectResults<T>, [], Depth> extends [infer L, infer R] ? [UnknownMembersToNever<L>, UnknownMembersToNever<R>] : Transpose<CollectResults<T>, [], Depth> extends [] ? [[], []] : never;
|
|
203
|
-
type Dedup<T> = T extends Result<infer RL, infer RR> ? Result<RL, RR> : T;
|
|
204
|
-
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] : [];
|
|
205
|
-
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;
|
|
206
|
-
type UnknownMembersToNever<T> = T extends [infer H, ...infer R] ? [[unknown] extends [H] ? never : H, ...UnknownMembersToNever<R>] : T;
|
|
207
|
-
type MembersToUnion<T> = T extends unknown[] ? T[number] : never;
|
|
208
|
-
type IsLiteralArray<T> = T extends {
|
|
209
|
-
length: infer L;
|
|
210
|
-
} ? L extends number ? number extends L ? 0 : 1 : 0 : 0;
|
|
211
|
-
type Traverse<T, Depth extends number = 5> = Combine<T, Depth> extends [infer Oks, infer Errs] ? Result<EmptyArrayToNever<Oks, 1>, MembersToUnion<Errs>> : never;
|
|
212
|
-
type TraverseWithAllErrors<T, Depth extends number = 5> = Traverse<T, Depth> extends Result<infer Oks, infer Errs> ? Result<Oks, Errs[]> : never;
|
|
213
|
-
type CombineResults<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? Traverse<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>;
|
|
214
|
-
type CombineResultsWithAllErrorsArray<T extends readonly Result<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrors<T> : Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>;
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region src/result-async.d.ts
|
|
217
|
-
type TaggedValue = {
|
|
218
|
-
readonly _tag: string;
|
|
219
|
-
};
|
|
220
|
-
type TaggedErrorValue = Error & TaggedValue;
|
|
221
|
-
type TagsOf<E> = Extract<E, TaggedValue>["_tag"];
|
|
222
|
-
type ErrorTagsOf<E> = Extract<E, TaggedErrorValue>["_tag"];
|
|
223
|
-
type ErrorForTag<E, Tag extends string> = Extract<E, {
|
|
224
|
-
readonly _tag: Tag;
|
|
225
|
-
}>;
|
|
226
|
-
type ExcludeTag<E, Tag extends string> = Exclude<E, {
|
|
227
|
-
readonly _tag: Tag;
|
|
228
|
-
}>;
|
|
229
|
-
type UntaggedError<E> = Exclude<Extract<E, Error>, TaggedErrorValue>;
|
|
230
|
-
type CatchTagHandlerResult<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
|
|
231
|
-
type MatchTagHandlerResult<Handlers> = { readonly [Key in keyof Handlers]: Handlers[Key] extends ((...args: infer _Args) => infer R) ? R : never }[keyof Handlers];
|
|
232
|
-
type CatchTagHandlers<E, Handlers> = { readonly [Tag in keyof Handlers]: Tag extends TagsOf<E> ? (error: ErrorForTag<E, Tag & string>) => Result<unknown, unknown> | ResultAsync<unknown, unknown> : never };
|
|
233
|
-
type MatchTagHandlers<E, Handlers> = { readonly [Tag in ErrorTagsOf<E>]: (error: ErrorForTag<E, Tag>) => unknown } & ([UntaggedError<E>] extends [never] ? {
|
|
234
|
-
readonly Error?: (error: Error) => unknown;
|
|
235
|
-
} : {
|
|
236
|
-
readonly Error: (error: UntaggedError<E>) => unknown;
|
|
237
|
-
}) & { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf<E> | "Error" ? Handlers[Tag] : never };
|
|
238
|
-
type PartialMatchTagHandlers<E, Handlers> = { readonly [Tag in keyof Handlers]: Tag extends ErrorTagsOf<E> ? (error: ErrorForTag<E, Tag & string>) => unknown : Tag extends "Error" ? (error: Extract<E, Error>) => unknown : never };
|
|
239
|
-
type PipeFn<Input, Output> = (input: Input) => Output;
|
|
240
|
-
type ResultAsyncFinalizer<T, E> = (value: T | undefined, error: E | undefined) => void | Promise<void>;
|
|
241
|
-
type HandlerOk<R> = InferOkTypes<R> | InferAsyncOkTypes<R>;
|
|
242
|
-
type HandlerErr<R> = InferErrTypes<R> | InferAsyncErrTypes<R>;
|
|
243
|
-
declare class DisposableResultAsync<T, E> implements PromiseLike<Result<T, E>>, AsyncDisposable {
|
|
244
|
-
private readonly innerPromise;
|
|
245
|
-
private readonly finalizer;
|
|
246
|
-
private disposed;
|
|
247
|
-
constructor(res: Promise<Result<T, E>>, finalizer: ResultAsyncFinalizer<T, E>);
|
|
248
|
-
then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
249
|
-
[Symbol.asyncDispose](): Promise<void>;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Represents an asynchronous Result type that wraps a Promise of a Result<T, E>.
|
|
253
|
-
* This class provides a way to handle asynchronous operations that may succeed with a value of type T
|
|
254
|
-
* or fail with an error of type E.
|
|
255
|
-
*
|
|
256
|
-
* @template T - The type of the success value
|
|
257
|
-
* @template E - The type of the error value
|
|
258
|
-
*
|
|
259
|
-
* @implements {PromiseLike<Result<T, E>>}
|
|
260
|
-
*
|
|
261
|
-
* @example
|
|
262
|
-
* ```typescript
|
|
263
|
-
* // Create a successful async result
|
|
264
|
-
* const okAsync = ResultAsync.okAsync(42);
|
|
265
|
-
*
|
|
266
|
-
* // Create a failed async result
|
|
267
|
-
* const errAsync = ResultAsync.errAsync(new Error("Something went wrong"));
|
|
268
|
-
*
|
|
269
|
-
* // Transform a Promise into a ResultAsync
|
|
270
|
-
* const resultFromPromise = ResultAsync.fromPromise(
|
|
271
|
-
* fetch("https://api.example.com/data"),
|
|
272
|
-
* (error) => new Error(`API call failed: ${error}`)
|
|
273
|
-
* );
|
|
274
|
-
* ```
|
|
275
|
-
*
|
|
276
|
-
* @remarks
|
|
277
|
-
* ResultAsync implements the PromiseLike interface, allowing it to be used with async/await syntax.
|
|
278
|
-
* It provides various utility methods for transforming and combining results, similar to the Result type,
|
|
279
|
-
* but operating in an asynchronous context.
|
|
280
|
-
*
|
|
281
|
-
* The class includes methods for:
|
|
282
|
-
* - Creating ResultAsync instances (okAsync, errAsync, fromPromise)
|
|
283
|
-
* - Transforming values (map, mapErr)
|
|
284
|
-
* - Chaining operations (andThen, orElse)
|
|
285
|
-
* - Error handling (tapError)
|
|
286
|
-
* - Conditional branching (if)
|
|
287
|
-
* - Combining multiple ResultAsync instances (combine, combineWithAllErrors)
|
|
288
|
-
*/
|
|
289
|
-
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
|
|
290
|
-
/**
|
|
291
|
-
* Returns a ResultAsync instance that is immediately resolved with a Result.ok(value).
|
|
292
|
-
*
|
|
293
|
-
* @param {T} value - The value to be wrapped in a Result.ok.
|
|
294
|
-
* @return {ResultAsync<T, E>} A ResultAsync instance with the given value and error type E.
|
|
295
|
-
*/
|
|
296
|
-
static okAsync<T, E = never>(this: void, value: T): ResultAsync<T, E>;
|
|
297
|
-
static okAsync<E = never>(this: void, value: void): ResultAsync<void, E>;
|
|
298
|
-
/**
|
|
299
|
-
* Returns a ResultAsync that is immediately resolved with a Result.ok(undefined) value.
|
|
300
|
-
*
|
|
301
|
-
* @return {ResultAsync<undefined, E>} A ResultAsync instance with undefined as the value type and E as the error type.
|
|
302
|
-
*/
|
|
303
|
-
static unitAsync<E = never>(this: void): ResultAsync<undefined, E>;
|
|
304
|
-
/**
|
|
305
|
-
* Returns a ResultAsync instance that is immediately resolved with a Result.err(error).
|
|
306
|
-
*
|
|
307
|
-
* @param {E} error - The error to be wrapped in a Result.err.
|
|
308
|
-
* @return {ResultAsync<T, E>} A ResultAsync instance with the given error and value type T.
|
|
309
|
-
*/
|
|
310
|
-
static errAsync<T = never, E = unknown>(this: void, err: E): ResultAsync<T, E>;
|
|
311
|
-
static errAsync<T = never>(this: void, err: void): ResultAsync<T, void>;
|
|
312
|
-
/**
|
|
313
|
-
* Creates a ResultAsync from a Promise, catching any errors that occur during its execution.
|
|
314
|
-
*
|
|
315
|
-
* @param fn - The Promise or a function returning a Promise to be wrapped in a ResultAsync.
|
|
316
|
-
* @param errorFn - Optional function to transform the caught error into a specific error type.
|
|
317
|
-
* If not provided, the original error will be used.
|
|
318
|
-
* @returns A ResultAsync that will resolve to Ok with the promise's value if successful,
|
|
319
|
-
* or Err with either the transformed error (if errorFn is provided) or the original error.
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
|
-
* ```typescript
|
|
323
|
-
* // Without error transformer
|
|
324
|
-
* const result = ResultAsync.tryCatch(Promise.resolve(42));
|
|
325
|
-
*
|
|
326
|
-
* // With error transformer
|
|
327
|
-
* const result = ResultAsync.tryCatch(
|
|
328
|
-
* fetch('https://api.example.com'),
|
|
329
|
-
* (error) => new CustomError('API request failed')
|
|
330
|
-
* );
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
static tryCatch<T>(this: void, fn: Promise<T> | (() => Promise<T>)): ResultAsync<T, unknown>;
|
|
334
|
-
static tryCatch<T, E>(this: void, fn: Promise<T> | (() => Promise<T>), errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
335
|
-
/**
|
|
336
|
-
* Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
|
|
337
|
-
* based on the provided promise.
|
|
338
|
-
*
|
|
339
|
-
* @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
|
|
340
|
-
* @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
|
|
341
|
-
*/
|
|
342
|
-
static fromSafePromise<T, E = never>(this: void, promise: PromiseLike<T>): ResultAsync<T, E>;
|
|
343
|
-
/**
|
|
344
|
-
* Returns a ResultAsync instance that is resolved with a Result.ok(value) or Result.err(error)
|
|
345
|
-
* based on the provided promise.
|
|
346
|
-
*
|
|
347
|
-
* @param {Promise<T>} promise - The promise to be wrapped in a ResultAsync.
|
|
348
|
-
* @param {(e: unknown) => E} errorFn - A function that transforms the error from the promise into the error type E.
|
|
349
|
-
* @return {ResultAsync<T, E>} A ResultAsync instance with the given promise and error type E.
|
|
350
|
-
*/
|
|
351
|
-
static fromPromise<T, E>(this: void, promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>;
|
|
352
|
-
static combine<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(this: void, asyncResultList: T): CombineResultAsyncs<T>;
|
|
353
|
-
static combine<T extends readonly ResultAsync<unknown, unknown>[]>(this: void, asyncResultList: T): CombineResultAsyncs<T>;
|
|
354
|
-
static combineWithAllErrors<T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
355
|
-
static combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(this: void, asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>;
|
|
356
|
-
/**
|
|
357
|
-
* Wraps a async function with a try catch, creating a new function with the same
|
|
358
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
359
|
-
*
|
|
360
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
361
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
362
|
-
* @returns a new function that returns a `ResultAsync`
|
|
363
|
-
*/
|
|
364
|
-
static fromThrowable<A extends readonly unknown[], T, E>(this: void, fn: (...args: A) => Promise<T>, errorFn?: (err: unknown) => E): (...args: A) => ResultAsync<T, E>;
|
|
365
|
-
private readonly innerPromise;
|
|
366
|
-
constructor(res: Promise<Result<T, E>>);
|
|
367
|
-
then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>;
|
|
368
|
-
mapErr<U>(f: (t: E) => U | Promise<U>): ResultAsync<T, U>;
|
|
369
|
-
map<X>(f: (t: T) => X | Promise<X>): ResultAsync<X, E>;
|
|
370
|
-
filterOrElse<U extends T, F>(predicate: (value: T) => value is U, onFalse: (value: T) => F | Promise<F>): ResultAsync<U, E | F>;
|
|
371
|
-
filterOrElse<F>(predicate: (value: T) => boolean | Promise<boolean>, onFalse: (value: T) => F | Promise<F>): ResultAsync<T, E | F>;
|
|
372
|
-
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>;
|
|
373
|
-
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>;
|
|
374
|
-
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>;
|
|
375
|
-
if(fCondition: (t: T) => boolean): {
|
|
376
|
-
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => {
|
|
377
|
-
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>) => ResultAsync<X1 | X2, Y1 | Y2 | E>;
|
|
378
|
-
};
|
|
379
|
-
};
|
|
380
|
-
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>;
|
|
381
|
-
orElse<R extends ResultAsync<unknown, unknown>>(f: (e: E) => R): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>;
|
|
382
|
-
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>;
|
|
383
|
-
catchTag<const Tag extends TagsOf<E>, R extends Result<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag<E, Tag>) => R): ResultAsync<T | InferOkTypes<R>, ExcludeTag<E, Tag> | InferErrTypes<R>>;
|
|
384
|
-
catchTag<const Tag extends TagsOf<E>, R extends ResultAsync<unknown, unknown>>(tag: Tag, f: (error: ErrorForTag<E, Tag>) => R): ResultAsync<T | InferAsyncOkTypes<R>, ExcludeTag<E, Tag> | InferAsyncErrTypes<R>>;
|
|
385
|
-
catchTag<U, F, const Tag extends TagsOf<E>>(tag: Tag, f: (error: ErrorForTag<E, Tag>) => Result<U, F> | ResultAsync<U, F>): ResultAsync<T | U, ExcludeTag<E, Tag> | F>;
|
|
386
|
-
catchTags<const Handlers extends object>(handlers: Handlers & CatchTagHandlers<E, Handlers>): ResultAsync<T | HandlerOk<CatchTagHandlerResult<Handlers>>, ExcludeTag<E, keyof Handlers & string> | HandlerErr<CatchTagHandlerResult<Handlers>>>;
|
|
387
|
-
match<A, B = A>(ok: (t: T) => A, fnErr: (e: E) => B): Promise<A | B>;
|
|
388
|
-
matchTags<A, const Handlers extends object>(ok: (t: T) => A, handlers: Handlers & MatchTagHandlers<E, Handlers>): Promise<A | MatchTagHandlerResult<Handlers>>;
|
|
389
|
-
matchTagsPartial<A, B, const Handlers extends object>(ok: (t: T) => A, handlers: Handlers & PartialMatchTagHandlers<E, Handlers>, fallback: (error: E) => B): Promise<A | MatchTagHandlerResult<Handlers> | B>;
|
|
390
|
-
unwrapOr<A>(t: A): Promise<T | A>;
|
|
391
|
-
unwrapOrThrow(): Promise<T>;
|
|
392
|
-
pipe<A>(ab: PipeFn<this, A>): A;
|
|
393
|
-
pipe<A, B>(ab: PipeFn<this, A>, bc: PipeFn<A, B>): B;
|
|
394
|
-
pipe<A, B, C>(ab: PipeFn<this, A>, bc: PipeFn<A, B>, cd: PipeFn<B, C>): C;
|
|
395
|
-
/**
|
|
396
|
-
* Runs a side effect for either variant and preserves the original async result.
|
|
397
|
-
*
|
|
398
|
-
* For `Ok`, the callback receives `(value, undefined)`.
|
|
399
|
-
* For `Err`, the callback receives `(undefined, error)`.
|
|
400
|
-
* Callback errors and rejected callback promises are intentionally ignored.
|
|
401
|
-
*
|
|
402
|
-
* Use `map`, `mapErr`, `andThen`, or `orElse` when the callback should
|
|
403
|
-
* transform the result.
|
|
404
|
-
*/
|
|
405
|
-
log(f: (t?: T, e?: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
406
|
-
/**
|
|
407
|
-
* Runs a side effect only for the `Ok` variant and preserves the original async result.
|
|
408
|
-
*
|
|
409
|
-
* Callback errors and rejected callback promises are intentionally ignored.
|
|
410
|
-
* Use `map` or `andThen` when the callback should transform the value.
|
|
411
|
-
*/
|
|
412
|
-
tap(f: (t: T) => void | Promise<void>): ResultAsync<T, E>;
|
|
413
|
-
/**
|
|
414
|
-
* Runs a side effect only for the `Err` variant and preserves the original async result.
|
|
415
|
-
*
|
|
416
|
-
* Callback errors and rejected callback promises are intentionally ignored.
|
|
417
|
-
* Use `mapErr` or `orElse` when the callback should transform the error.
|
|
418
|
-
*/
|
|
419
|
-
tapError(f: (e: E) => void | Promise<void>): ResultAsync<T, E>;
|
|
420
|
-
toAsyncDisposable(f: ResultAsyncFinalizer<T, E>): DisposableResultAsync<T, E>;
|
|
421
|
-
[Symbol.asyncIterator](): AsyncGenerator<Result<never, E>, T>;
|
|
422
|
-
}
|
|
423
|
-
type StrictResultAsync<T, E extends Error = Error> = ResultAsync<T, E>;
|
|
424
|
-
declare const okAsync: typeof ResultAsync.okAsync;
|
|
425
|
-
declare const errAsync: typeof ResultAsync.errAsync;
|
|
426
|
-
declare const fromPromise: typeof ResultAsync.fromPromise;
|
|
427
|
-
declare const fromSafePromise: typeof ResultAsync.fromSafePromise;
|
|
428
|
-
declare const unitAsync: typeof ResultAsync.unitAsync;
|
|
429
|
-
declare const fromThrowableAsync: typeof ResultAsync.fromThrowable;
|
|
430
|
-
declare const tryCatchAsync: typeof ResultAsync.tryCatch;
|
|
431
|
-
type CombineResultAsyncs<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>;
|
|
432
|
-
type CombineResultsWithAllErrorsArrayAsync<T extends readonly ResultAsync<unknown, unknown>[]> = IsLiteralArray<T> extends 1 ? TraverseWithAllErrorsAsync<UnwrapAsync<T>> : ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>;
|
|
433
|
-
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 (infer A)[] ? A extends PromiseLike<infer HI> ? HI extends Result<infer L, infer R> ? Result<L, R>[] : never : never : never;
|
|
434
|
-
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 (infer I)[] ? Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs] ? Oks extends unknown[] ? Errs extends unknown[] ? ResultAsync<EmptyArrayToNever<Oks[number][]>, MembersToUnion<Errs[number][]>> : ResultAsync<EmptyArrayToNever<Oks[number][]>, Errs> : Errs extends unknown[] ? ResultAsync<Oks, MembersToUnion<Errs[number][]>> : ResultAsync<Oks, Errs> : never : never;
|
|
435
|
-
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = TraverseAsync<T, Depth> extends ResultAsync<infer Oks, infer Errs> ? ResultAsync<Oks, Errs[]> : never;
|
|
436
|
-
type Writable<T> = T extends readonly unknown[] ? [...T] : T;
|
|
437
|
-
//#endregion
|
|
438
|
-
export { unit as S, err as _, fromPromise as a, safeTry as b, okAsync as c, DisposableResult as d, ErrResult as f, StrictResult as g, ResultOperations as h, errAsync as i, tryCatchAsync as l, Result as m, ResultAsync as n, fromSafePromise as o, OkResult as p, StrictResultAsync as r, fromThrowableAsync as s, DisposableResultAsync as t, unitAsync as u, fromThrowable as v, tryCatch as x, ok as y };
|
|
439
|
-
//# sourceMappingURL=result-async-CxGBicja.d.ts.map
|