unthrown 1.1.0 → 3.0.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 +3 -3
- package/dist/index.cjs +92 -65
- package/dist/index.d.cts +106 -86
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +106 -86
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +92 -65
- package/dist/index.mjs.map +1 -1
- package/docs/index.md +164 -200
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,10 +11,10 @@ pnpm add unthrown
|
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { Ok, Err, fromPromise,
|
|
14
|
+
import { Ok, Err, fromPromise, type Result } from "unthrown";
|
|
15
15
|
|
|
16
|
-
const user = fromPromise(fetchUser(id), (cause) =>
|
|
17
|
-
cause instanceof NotFoundError ? new NotFound() :
|
|
16
|
+
const user = fromPromise(fetchUser(id), (cause, defect) =>
|
|
17
|
+
cause instanceof NotFoundError ? new NotFound() : defect(cause),
|
|
18
18
|
);
|
|
19
19
|
|
|
20
20
|
const status = await user.match({
|
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
6
6
|
* `Ok`.
|
|
7
7
|
*
|
|
8
8
|
* @remarks
|
|
9
|
+
* The offending value is exposed two ways: the typed {@link UnwrapError.error}
|
|
10
|
+
* property for programmatic access, and the standard `Error.cause` for the
|
|
11
|
+
* runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
12
|
+
* its original stack is printed under "caused by".
|
|
13
|
+
*
|
|
9
14
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
10
15
|
* re-thrown (with its original stack) instead.
|
|
11
16
|
*
|
|
@@ -18,7 +23,7 @@ var UnwrapError = class extends Error {
|
|
|
18
23
|
*/
|
|
19
24
|
error;
|
|
20
25
|
constructor(error) {
|
|
21
|
-
super("unthrown: called unwrap on a non-matching Result");
|
|
26
|
+
super("unthrown: called unwrap on a non-matching Result", { cause: error });
|
|
22
27
|
this.name = "UnwrapError";
|
|
23
28
|
this.error = error;
|
|
24
29
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -279,7 +284,7 @@ function passThrough(self) {
|
|
|
279
284
|
* though, so a primitive `Ok` (e.g. `Ok(5).bind(...)`, or a chain whose value was
|
|
280
285
|
* `map`-ped away from its scope) could reach it. Rather than let `{ ...5 }`
|
|
281
286
|
* silently collapse to `{}` and drop the prior scope, we throw here — the
|
|
282
|
-
* surrounding `try` turns it into a
|
|
287
|
+
* surrounding `try` turns it into a `Defect`, surfacing the misuse as the
|
|
283
288
|
* bug it is (a defect is a bug, not an absent value). A `this: object` constraint
|
|
284
289
|
* was rejected: TypeScript does not hard-enforce a constraint inferred solely
|
|
285
290
|
* from `this`, and it breaks `AsyncRes implements AsyncResult`.
|
|
@@ -541,41 +546,6 @@ function isDefect(r) {
|
|
|
541
546
|
return r.tag === "Defect";
|
|
542
547
|
}
|
|
543
548
|
//#endregion
|
|
544
|
-
//#region src/defect.ts
|
|
545
|
-
const DEFECT = Symbol("unthrown/Defect");
|
|
546
|
-
/**
|
|
547
|
-
* Wrap a cause as a {@link Defect} — the value you return from a `qualify`
|
|
548
|
-
* function when a failure is **not** a modeled domain error.
|
|
549
|
-
*
|
|
550
|
-
* @param cause - the original thrown/rejected value.
|
|
551
|
-
* @returns an opaque Defect marker carrying `cause`.
|
|
552
|
-
*
|
|
553
|
-
* @example
|
|
554
|
-
* ```ts
|
|
555
|
-
* import { fromPromise, Defect } from "unthrown";
|
|
556
|
-
*
|
|
557
|
-
* const user = fromPromise(fetchUser(id), (cause) =>
|
|
558
|
-
* cause instanceof NotFoundError ? cause : Defect(cause),
|
|
559
|
-
* );
|
|
560
|
-
* ```
|
|
561
|
-
*/
|
|
562
|
-
function Defect(cause) {
|
|
563
|
-
return {
|
|
564
|
-
[DEFECT]: true,
|
|
565
|
-
cause
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Internal guard for the qualify-time marker. Distinct from the public
|
|
570
|
-
* {@link isDefect} state guard — this one narrows the `E | Defect` union a
|
|
571
|
-
* `qualify` function returns, not a `Result`.
|
|
572
|
-
*
|
|
573
|
-
* @internal
|
|
574
|
-
*/
|
|
575
|
-
function isDefectMarker(x) {
|
|
576
|
-
return typeof x === "object" && x !== null && x[DEFECT] === true;
|
|
577
|
-
}
|
|
578
|
-
//#endregion
|
|
579
549
|
//#region src/do.ts
|
|
580
550
|
/**
|
|
581
551
|
* Start a do-notation chain with an empty object scope, grown step by step with
|
|
@@ -603,6 +573,36 @@ function Do() {
|
|
|
603
573
|
return Ok({});
|
|
604
574
|
}
|
|
605
575
|
//#endregion
|
|
576
|
+
//#region src/defect.ts
|
|
577
|
+
const DEFECT = Symbol("unthrown/Defect");
|
|
578
|
+
/**
|
|
579
|
+
* Wrap a cause as a `Defect` marker — the value returned from a `qualify`
|
|
580
|
+
* function when a failure is **not** a modeled domain error. The boundary
|
|
581
|
+
* (`fromPromise` / `fromThrowable`) passes this in as `qualify`'s second
|
|
582
|
+
* argument, so domain code never imports it.
|
|
583
|
+
*
|
|
584
|
+
* @param cause - the original thrown/rejected value.
|
|
585
|
+
* @returns an opaque Defect marker carrying `cause`.
|
|
586
|
+
*
|
|
587
|
+
* @internal
|
|
588
|
+
*/
|
|
589
|
+
function defect(cause) {
|
|
590
|
+
return {
|
|
591
|
+
[DEFECT]: true,
|
|
592
|
+
cause
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Internal guard for the qualify-time marker. Distinct from the public
|
|
597
|
+
* {@link isDefect} state guard — this one narrows the `E | Defect` union a
|
|
598
|
+
* `qualify` function returns, not a `Result`.
|
|
599
|
+
*
|
|
600
|
+
* @internal
|
|
601
|
+
*/
|
|
602
|
+
function isDefectMarker(x) {
|
|
603
|
+
return typeof x === "object" && x !== null && x[DEFECT] === true;
|
|
604
|
+
}
|
|
605
|
+
//#endregion
|
|
606
606
|
//#region src/interop.ts
|
|
607
607
|
/**
|
|
608
608
|
* Bridge a nullable value into a {@link Result}: absence becomes a **modeled**
|
|
@@ -632,12 +632,13 @@ function fromNullable(value, onAbsent) {
|
|
|
632
632
|
*
|
|
633
633
|
* @remarks
|
|
634
634
|
* `qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
635
|
-
*
|
|
636
|
-
* in `E`. A throw inside `qualify` itself is treated
|
|
635
|
+
* `Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
636
|
+
* path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
637
|
+
* as a `Defect`.
|
|
637
638
|
*
|
|
638
639
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
639
640
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
640
|
-
* `qualify` that returns *only* `
|
|
641
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
641
642
|
* out-of-band and must not pollute the error channel); reach for
|
|
642
643
|
* {@link fromSafePromise} when every failure is a Defect.
|
|
643
644
|
*
|
|
@@ -646,13 +647,14 @@ function fromNullable(value, onAbsent) {
|
|
|
646
647
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
647
648
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
648
649
|
* @param fn - the throwing function to wrap.
|
|
649
|
-
* @param qualify - triages a thrown cause into `E
|
|
650
|
+
* @param qualify - triages a thrown `cause` into a modeled `E`, or marks it
|
|
651
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
650
652
|
* @returns a function with the same arguments returning `Result<T, E>`.
|
|
651
653
|
*
|
|
652
654
|
* @example
|
|
653
655
|
* ```ts
|
|
654
|
-
* import { fromThrowable
|
|
655
|
-
* const parse = fromThrowable(JSON.parse, (cause) =>
|
|
656
|
+
* import { fromThrowable } from "unthrown";
|
|
657
|
+
* const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
656
658
|
* parse("{}").unwrap();
|
|
657
659
|
* ```
|
|
658
660
|
*/
|
|
@@ -672,26 +674,27 @@ function fromThrowable(fn, qualify) {
|
|
|
672
674
|
*
|
|
673
675
|
* @remarks
|
|
674
676
|
* `qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
675
|
-
*
|
|
676
|
-
* `await`-ing it always yields a
|
|
677
|
-
* `Defect`.
|
|
677
|
+
* `Defect` (via the injected `defect` helper, its second argument). The returned
|
|
678
|
+
* `AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
679
|
+
* `Result`. A throw inside `qualify` is itself a `Defect`.
|
|
678
680
|
*
|
|
679
681
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
680
682
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
681
|
-
* `qualify` that returns *only* `
|
|
683
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
682
684
|
* rejection is a Defect, prefer {@link fromSafePromise}.
|
|
683
685
|
*
|
|
684
686
|
* @typeParam T - the resolved value type.
|
|
685
687
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
686
688
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
687
689
|
* @param promise - the promise, or a thunk returning one.
|
|
688
|
-
* @param qualify - triages a rejection cause into `E
|
|
690
|
+
* @param qualify - triages a rejection `cause` into a modeled `E`, or marks it
|
|
691
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
689
692
|
*
|
|
690
693
|
* @example
|
|
691
694
|
* ```ts
|
|
692
|
-
* import { fromPromise
|
|
693
|
-
* const user = await fromPromise(fetchUser(id), (cause) =>
|
|
694
|
-
* cause instanceof NotFoundError ? ("not_found" as const) :
|
|
695
|
+
* import { fromPromise } from "unthrown";
|
|
696
|
+
* const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
697
|
+
* cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
695
698
|
* );
|
|
696
699
|
* ```
|
|
697
700
|
*/
|
|
@@ -716,7 +719,7 @@ function fromSafePromise(promise) {
|
|
|
716
719
|
}
|
|
717
720
|
function qualifyToResult(cause, qualify) {
|
|
718
721
|
try {
|
|
719
|
-
const q = qualify(cause);
|
|
722
|
+
const q = qualify(cause, defect);
|
|
720
723
|
return isDefectMarker(q) ? defectRes(q.cause) : errRes(q);
|
|
721
724
|
} catch (qErr) {
|
|
722
725
|
return defectRes(qErr);
|
|
@@ -845,13 +848,11 @@ function allFromDictAsync(results) {
|
|
|
845
848
|
//#endregion
|
|
846
849
|
//#region src/facade.ts
|
|
847
850
|
/**
|
|
848
|
-
* Companion object grouping the
|
|
849
|
-
* discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
850
|
-
* {@link Result.
|
|
851
|
-
* {@link Result.
|
|
852
|
-
* {@link Result.
|
|
853
|
-
* {@link Result.allFromDictAsync}, {@link Result.isOk}, {@link Result.isErr},
|
|
854
|
-
* {@link Result.isDefect}, {@link Result.isResult}.
|
|
851
|
+
* Companion object grouping the **`Result`-producing** entry points under a
|
|
852
|
+
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
853
|
+
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
854
|
+
* {@link Result.all}, {@link Result.allFromDict}, {@link Result.isOk},
|
|
855
|
+
* {@link Result.isErr}, {@link Result.isDefect}, {@link Result.isResult}.
|
|
855
856
|
*
|
|
856
857
|
* @remarks
|
|
857
858
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -859,6 +860,10 @@ function allFromDictAsync(results) {
|
|
|
859
860
|
* `{ Ok }` never pulls this object in. The value `Result` and the type
|
|
860
861
|
* {@link Result} share one name (the companion-object pattern).
|
|
861
862
|
*
|
|
863
|
+
* The **async** entry points live on the sibling {@link AsyncResult} companion
|
|
864
|
+
* (`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
865
|
+
* return — a static lives in exactly one namespace.
|
|
866
|
+
*
|
|
862
867
|
* @example
|
|
863
868
|
* ```ts
|
|
864
869
|
* import { Result } from "unthrown";
|
|
@@ -868,21 +873,43 @@ function allFromDictAsync(results) {
|
|
|
868
873
|
const Result = {
|
|
869
874
|
Ok,
|
|
870
875
|
Err,
|
|
871
|
-
Defect,
|
|
872
876
|
Do,
|
|
873
877
|
fromNullable,
|
|
874
878
|
fromThrowable,
|
|
875
|
-
fromPromise,
|
|
876
|
-
fromSafePromise,
|
|
877
879
|
all,
|
|
878
|
-
allAsync,
|
|
879
880
|
allFromDict,
|
|
880
|
-
allFromDictAsync,
|
|
881
881
|
isOk,
|
|
882
882
|
isErr,
|
|
883
883
|
isDefect,
|
|
884
884
|
isResult
|
|
885
885
|
};
|
|
886
|
+
/**
|
|
887
|
+
* Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
888
|
+
* the matching namespace: {@link AsyncResult.fromPromise},
|
|
889
|
+
* {@link AsyncResult.fromSafePromise}, {@link AsyncResult.all},
|
|
890
|
+
* {@link AsyncResult.allFromDict}.
|
|
891
|
+
*
|
|
892
|
+
* @remarks
|
|
893
|
+
* The async sibling of {@link Result}. Statics are grouped by what they
|
|
894
|
+
* **return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
895
|
+
* here rather than on {@link Result}; the namespace already conveys "async", so
|
|
896
|
+
* the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
897
|
+
* `allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
898
|
+
* {@link Result}, the free functions remain the primary, tree-shakeable API; the
|
|
899
|
+
* value `AsyncResult` and the type {@link AsyncResult} share one name.
|
|
900
|
+
*
|
|
901
|
+
* @example
|
|
902
|
+
* ```ts
|
|
903
|
+
* import { AsyncResult } from "unthrown";
|
|
904
|
+
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
905
|
+
* ```
|
|
906
|
+
*/
|
|
907
|
+
const AsyncResult = {
|
|
908
|
+
fromPromise,
|
|
909
|
+
fromSafePromise,
|
|
910
|
+
all: allAsync,
|
|
911
|
+
allFromDict: allFromDictAsync
|
|
912
|
+
};
|
|
886
913
|
//#endregion
|
|
887
914
|
//#region src/tagged.ts
|
|
888
915
|
/**
|
|
@@ -951,7 +978,7 @@ function matchTags(result, handlers) {
|
|
|
951
978
|
});
|
|
952
979
|
}
|
|
953
980
|
//#endregion
|
|
954
|
-
exports.
|
|
981
|
+
exports.AsyncResult = AsyncResult;
|
|
955
982
|
exports.Do = Do;
|
|
956
983
|
exports.Err = Err;
|
|
957
984
|
exports.Ok = Ok;
|
package/dist/index.d.cts
CHANGED
|
@@ -261,7 +261,7 @@ type ResultMethods<T, E> = {
|
|
|
261
261
|
isOk(): this is OkView<T, E>; /** Whether this result is `Err` — narrows `this` to its {@link ErrView} on `true`. */
|
|
262
262
|
isErr(): this is ErrView<E, T>; /** Whether this result is a `Defect` — narrows `this` to its {@link DefectView} on `true`. */
|
|
263
263
|
isDefect(): this is DefectView<T, E>; /** Lift this synchronous `Result` into an {@link AsyncResult}. */
|
|
264
|
-
toAsync(): AsyncResult<T, E>;
|
|
264
|
+
toAsync(): AsyncResult$1<T, E>;
|
|
265
265
|
};
|
|
266
266
|
/** The `Ok` variant of a {@link Result}: a success carrying a `value`. */
|
|
267
267
|
type OkView<T, E = never> = ResultMethods<T, E> & {
|
|
@@ -351,40 +351,40 @@ type Awaitable<T> = {
|
|
|
351
351
|
* @typeParam T - the success value type.
|
|
352
352
|
* @typeParam E - the modeled error type.
|
|
353
353
|
*/
|
|
354
|
-
type AsyncResult<T, E> = Awaitable<Result$1<T, E>> & {
|
|
355
|
-
/** Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. */map<U>(f: (value: T) => U): AsyncResult<U, E>;
|
|
354
|
+
type AsyncResult$1<T, E> = Awaitable<Result$1<T, E>> & {
|
|
355
|
+
/** Asynchronous `map`. `f` is synchronous; a throw becomes a `Defect`. */map<U>(f: (value: T) => U): AsyncResult$1<U, E>;
|
|
356
356
|
/**
|
|
357
357
|
* Asynchronous `flatMap`. `f` may return a `Result` **or** an `AsyncResult`
|
|
358
358
|
* (never a raw `Promise`); a throw becomes a `Defect`.
|
|
359
359
|
*/
|
|
360
|
-
flatMap<U, E2>(f: (value: T) => Result$1<U, E2> | AsyncResult<U, E2>): AsyncResult<U, E | E2>; /** Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
361
|
-
tap(f: (value: T) => void): AsyncResult<T, E>;
|
|
360
|
+
flatMap<U, E2>(f: (value: T) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<U, E | E2>; /** Asynchronous `tap`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
361
|
+
tap(f: (value: T) => void): AsyncResult$1<T, E>;
|
|
362
362
|
/**
|
|
363
363
|
* Asynchronous `flatTap` — a failable tap that keeps the original value. `f`
|
|
364
364
|
* may return a `Result` **or** an `AsyncResult`; its `Ok` value is discarded,
|
|
365
365
|
* an `Err`/`Defect` short-circuits, and a throw becomes a `Defect`.
|
|
366
366
|
*/
|
|
367
|
-
flatTap<E2>(f: (value: T) => Result$1<unknown, E2> | AsyncResult<unknown, E2>): AsyncResult<T, E | E2>;
|
|
367
|
+
flatTap<E2>(f: (value: T) => Result$1<unknown, E2> | AsyncResult$1<unknown, E2>): AsyncResult$1<T, E | E2>;
|
|
368
368
|
/**
|
|
369
369
|
* Asynchronous `bind` (do-notation). `f` may return a `Result` **or** an
|
|
370
370
|
* `AsyncResult`; its value is bound under `name` in the accumulating scope.
|
|
371
371
|
*/
|
|
372
|
-
bind<K extends string, U, E2>(name: K, f: (scope: T) => Result$1<U, E2> | AsyncResult<U, E2>): AsyncResult<Bound<T, K, U>, E | E2>; /** Asynchronous `let` (do-notation). `f` returns a plain value, bound under `name`. */
|
|
373
|
-
let<K extends string, U>(name: K, f: (scope: T) => U): AsyncResult<Bound<T, K, U>, E>; /** Asynchronous `as`. */
|
|
374
|
-
as<U>(value: U): AsyncResult<U, E>; /** Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
375
|
-
mapErr<E2>(f: (error: E) => E2): AsyncResult<T, E2>; /** Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. */
|
|
376
|
-
orElse<U, E2>(f: (error: E) => Result$1<U, E2> | AsyncResult<U, E2>): AsyncResult<T | U, E2>; /** Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
377
|
-
recover<U>(f: (error: E) => U): AsyncResult<T | U, never>; /** Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
378
|
-
tapErr(f: (error: E) => void): AsyncResult<T, E>;
|
|
372
|
+
bind<K extends string, U, E2>(name: K, f: (scope: T) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<Bound<T, K, U>, E | E2>; /** Asynchronous `let` (do-notation). `f` returns a plain value, bound under `name`. */
|
|
373
|
+
let<K extends string, U>(name: K, f: (scope: T) => U): AsyncResult$1<Bound<T, K, U>, E>; /** Asynchronous `as`. */
|
|
374
|
+
as<U>(value: U): AsyncResult$1<U, E>; /** Asynchronous `mapErr`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
375
|
+
mapErr<E2>(f: (error: E) => E2): AsyncResult$1<T, E2>; /** Asynchronous `orElse`. `f` may return a `Result` or an `AsyncResult`. */
|
|
376
|
+
orElse<U, E2>(f: (error: E) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<T | U, E2>; /** Asynchronous `recover`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
377
|
+
recover<U>(f: (error: E) => U): AsyncResult$1<T | U, never>; /** Asynchronous `tapErr`. `f` is synchronous; a throw becomes a `Defect`. */
|
|
378
|
+
tapErr(f: (error: E) => void): AsyncResult$1<T, E>;
|
|
379
379
|
/**
|
|
380
380
|
* Asynchronous `flatTapErr` — a failable tap on the error that keeps the
|
|
381
381
|
* original error. `f` may return a `Result` **or** an `AsyncResult`; its `Ok`
|
|
382
382
|
* value is discarded, an `Err`/`Defect` from `f` threads through, and a throw
|
|
383
383
|
* becomes a `Defect`.
|
|
384
384
|
*/
|
|
385
|
-
flatTapErr<E2>(f: (error: E) => Result$1<unknown, E2> | AsyncResult<unknown, E2>): AsyncResult<T, E | E2>; /** Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. */
|
|
386
|
-
recoverDefect<U, E2>(f: (cause: unknown) => Result$1<U, E2> | AsyncResult<U, E2>): AsyncResult<T | U, E | E2>; /** Asynchronous `tapDefect`. */
|
|
387
|
-
tapDefect(f: (cause: unknown) => void): AsyncResult<T, E>; /** Asynchronous `match`. Handlers are synchronous; resolves to `R`. */
|
|
385
|
+
flatTapErr<E2>(f: (error: E) => Result$1<unknown, E2> | AsyncResult$1<unknown, E2>): AsyncResult$1<T, E | E2>; /** Asynchronous `recoverDefect`. `f` may return a `Result` or an `AsyncResult`. */
|
|
386
|
+
recoverDefect<U, E2>(f: (cause: unknown) => Result$1<U, E2> | AsyncResult$1<U, E2>): AsyncResult$1<T | U, E | E2>; /** Asynchronous `tapDefect`. */
|
|
387
|
+
tapDefect(f: (cause: unknown) => void): AsyncResult$1<T, E>; /** Asynchronous `match`. Handlers are synchronous; resolves to `R`. */
|
|
388
388
|
match<R>(cases: {
|
|
389
389
|
ok: (value: T) => R;
|
|
390
390
|
err: (error: E) => R;
|
|
@@ -420,13 +420,13 @@ type ErrOf<R> = R extends {
|
|
|
420
420
|
*
|
|
421
421
|
* @typeParam R - the `AsyncResult` type to inspect.
|
|
422
422
|
*/
|
|
423
|
-
type AsyncOkOf<R> = R extends AsyncResult<infer T, unknown> ? T : never;
|
|
423
|
+
type AsyncOkOf<R> = R extends AsyncResult$1<infer T, unknown> ? T : never;
|
|
424
424
|
/**
|
|
425
425
|
* Extract the error type `E` from an {@link AsyncResult}.
|
|
426
426
|
*
|
|
427
427
|
* @typeParam R - the `AsyncResult` type to inspect.
|
|
428
428
|
*/
|
|
429
|
-
type AsyncErrOf<R> = R extends AsyncResult<unknown, infer E> ? E : never;
|
|
429
|
+
type AsyncErrOf<R> = R extends AsyncResult$1<unknown, infer E> ? E : never;
|
|
430
430
|
//#endregion
|
|
431
431
|
//#region src/constructors.d.ts
|
|
432
432
|
/**
|
|
@@ -488,6 +488,11 @@ declare function isDefect<T, E>(r: Result$1<T, E>): r is DefectView<T, E>;
|
|
|
488
488
|
* `Ok`.
|
|
489
489
|
*
|
|
490
490
|
* @remarks
|
|
491
|
+
* The offending value is exposed two ways: the typed {@link UnwrapError.error}
|
|
492
|
+
* property for programmatic access, and the standard `Error.cause` for the
|
|
493
|
+
* runtime and devtools to chain — when `E` is an `Error` (e.g. a `TaggedError`)
|
|
494
|
+
* its original stack is printed under "caused by".
|
|
495
|
+
*
|
|
491
496
|
* A `Defect` is never wrapped in an `UnwrapError`: its original cause is
|
|
492
497
|
* re-thrown (with its original stack) instead.
|
|
493
498
|
*
|
|
@@ -515,40 +520,6 @@ declare class UnwrapError<E = unknown> extends Error {
|
|
|
515
520
|
*/
|
|
516
521
|
declare function isResult(x: unknown): x is Result$1<unknown, unknown>;
|
|
517
522
|
//#endregion
|
|
518
|
-
//#region src/defect.d.ts
|
|
519
|
-
declare const DEFECT: unique symbol;
|
|
520
|
-
/**
|
|
521
|
-
* The marker a `qualify` function returns to triage a cause as **unexpected**.
|
|
522
|
-
*
|
|
523
|
-
* @remarks
|
|
524
|
-
* `qualify` (passed to {@link fromPromise} / {@link fromThrowable}) returns
|
|
525
|
-
* `E | Defect`: either a modeled domain error, or a `Defect` produced by
|
|
526
|
-
* {@link Defect} to say "this failure is not modeled". A `Defect` is opaque —
|
|
527
|
-
* it carries the original cause for the boundary to convert into the third
|
|
528
|
-
* runtime state of a `Result`.
|
|
529
|
-
*/
|
|
530
|
-
type Defect = {
|
|
531
|
-
readonly [DEFECT]: true;
|
|
532
|
-
readonly cause: unknown;
|
|
533
|
-
};
|
|
534
|
-
/**
|
|
535
|
-
* Wrap a cause as a {@link Defect} — the value you return from a `qualify`
|
|
536
|
-
* function when a failure is **not** a modeled domain error.
|
|
537
|
-
*
|
|
538
|
-
* @param cause - the original thrown/rejected value.
|
|
539
|
-
* @returns an opaque Defect marker carrying `cause`.
|
|
540
|
-
*
|
|
541
|
-
* @example
|
|
542
|
-
* ```ts
|
|
543
|
-
* import { fromPromise, Defect } from "unthrown";
|
|
544
|
-
*
|
|
545
|
-
* const user = fromPromise(fetchUser(id), (cause) =>
|
|
546
|
-
* cause instanceof NotFoundError ? cause : Defect(cause),
|
|
547
|
-
* );
|
|
548
|
-
* ```
|
|
549
|
-
*/
|
|
550
|
-
declare function Defect(cause: unknown): Defect;
|
|
551
|
-
//#endregion
|
|
552
523
|
//#region src/do.d.ts
|
|
553
524
|
/**
|
|
554
525
|
* Start a do-notation chain with an empty object scope, grown step by step with
|
|
@@ -574,6 +545,27 @@ declare function Defect(cause: unknown): Defect;
|
|
|
574
545
|
*/
|
|
575
546
|
declare function Do(): Result$1<{}, never>;
|
|
576
547
|
//#endregion
|
|
548
|
+
//#region src/defect.d.ts
|
|
549
|
+
declare const DEFECT: unique symbol;
|
|
550
|
+
/**
|
|
551
|
+
* The opaque marker a `qualify` function returns to triage a cause as
|
|
552
|
+
* **unexpected**.
|
|
553
|
+
*
|
|
554
|
+
* @remarks
|
|
555
|
+
* `qualify` (passed to {@link fromPromise} / {@link fromThrowable}) returns
|
|
556
|
+
* `E | Defect`: either a modeled domain error, or a `Defect` produced by the
|
|
557
|
+
* injected `defect` helper to say "this failure is not modeled". A `Defect` is
|
|
558
|
+
* opaque — it carries the original cause for the boundary to convert into the
|
|
559
|
+
* third runtime state of a `Result`. It is **not** a public value; the only way
|
|
560
|
+
* to mint one is the `defect` helper the boundary passes to `qualify`.
|
|
561
|
+
*
|
|
562
|
+
* @internal
|
|
563
|
+
*/
|
|
564
|
+
type Defect = {
|
|
565
|
+
readonly [DEFECT]: true;
|
|
566
|
+
readonly cause: unknown;
|
|
567
|
+
};
|
|
568
|
+
//#endregion
|
|
577
569
|
//#region src/interop.d.ts
|
|
578
570
|
/**
|
|
579
571
|
* Bridge a nullable value into a {@link Result}: absence becomes a **modeled**
|
|
@@ -601,12 +593,13 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
601
593
|
*
|
|
602
594
|
* @remarks
|
|
603
595
|
* `qualify` **must** triage every thrown cause into a modeled error `E` or a
|
|
604
|
-
*
|
|
605
|
-
* in `E`. A throw inside `qualify` itself is treated
|
|
596
|
+
* `Defect` (via the injected `defect` helper, its second argument) — there is no
|
|
597
|
+
* path that leaves `unknown` in `E`. A throw inside `qualify` itself is treated
|
|
598
|
+
* as a `Defect`.
|
|
606
599
|
*
|
|
607
600
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
608
601
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
609
|
-
* `qualify` that returns *only* `
|
|
602
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never` (a Defect is
|
|
610
603
|
* out-of-band and must not pollute the error channel); reach for
|
|
611
604
|
* {@link fromSafePromise} when every failure is a Defect.
|
|
612
605
|
*
|
|
@@ -615,47 +608,49 @@ declare function fromNullable<T, E>(value: T | null | undefined, onAbsent: () =>
|
|
|
615
608
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
616
609
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
617
610
|
* @param fn - the throwing function to wrap.
|
|
618
|
-
* @param qualify - triages a thrown cause into `E
|
|
611
|
+
* @param qualify - triages a thrown `cause` into a modeled `E`, or marks it
|
|
612
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
619
613
|
* @returns a function with the same arguments returning `Result<T, E>`.
|
|
620
614
|
*
|
|
621
615
|
* @example
|
|
622
616
|
* ```ts
|
|
623
|
-
* import { fromThrowable
|
|
624
|
-
* const parse = fromThrowable(JSON.parse, (cause) =>
|
|
617
|
+
* import { fromThrowable } from "unthrown";
|
|
618
|
+
* const parse = fromThrowable(JSON.parse, (cause, defect) => defect(cause));
|
|
625
619
|
* parse("{}").unwrap();
|
|
626
620
|
* ```
|
|
627
621
|
*/
|
|
628
|
-
declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T, qualify: (cause: unknown) => R): (...args: A) => Result$1<T, Exclude<R, Defect>>;
|
|
622
|
+
declare function fromThrowable<A extends unknown[], T, R>(fn: (...args: A) => T, qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): (...args: A) => Result$1<T, Exclude<R, Defect>>;
|
|
629
623
|
/**
|
|
630
624
|
* Wrap a `Promise` (or a thunk producing one) as an {@link AsyncResult}, forcing
|
|
631
625
|
* every rejection to be triaged.
|
|
632
626
|
*
|
|
633
627
|
* @remarks
|
|
634
628
|
* `qualify` **must** map each rejection cause into a modeled error `E` or a
|
|
635
|
-
*
|
|
636
|
-
* `await`-ing it always yields a
|
|
637
|
-
* `Defect`.
|
|
629
|
+
* `Defect` (via the injected `defect` helper, its second argument). The returned
|
|
630
|
+
* `AsyncResult`'s internal promise never rejects; `await`-ing it always yields a
|
|
631
|
+
* `Result`. A throw inside `qualify` is itself a `Defect`.
|
|
638
632
|
*
|
|
639
633
|
* The modeled error type is `Exclude<R, Defect>` — the `Defect` arm of
|
|
640
634
|
* `qualify`'s return is **subtracted** from `E`, never inferred into it. So a
|
|
641
|
-
* `qualify` that returns *only* `
|
|
635
|
+
* `qualify` that returns *only* `defect(cause)` yields `E = never`; when every
|
|
642
636
|
* rejection is a Defect, prefer {@link fromSafePromise}.
|
|
643
637
|
*
|
|
644
638
|
* @typeParam T - the resolved value type.
|
|
645
639
|
* @typeParam R - `qualify`'s return type; the modeled error `E` is
|
|
646
640
|
* `Exclude<R, Defect>` (its `Defect` arm, if any, is subtracted).
|
|
647
641
|
* @param promise - the promise, or a thunk returning one.
|
|
648
|
-
* @param qualify - triages a rejection cause into `E
|
|
642
|
+
* @param qualify - triages a rejection `cause` into a modeled `E`, or marks it
|
|
643
|
+
* unmodeled by returning `defect(cause)` (the helper passed as its second arg).
|
|
649
644
|
*
|
|
650
645
|
* @example
|
|
651
646
|
* ```ts
|
|
652
|
-
* import { fromPromise
|
|
653
|
-
* const user = await fromPromise(fetchUser(id), (cause) =>
|
|
654
|
-
* cause instanceof NotFoundError ? ("not_found" as const) :
|
|
647
|
+
* import { fromPromise } from "unthrown";
|
|
648
|
+
* const user = await fromPromise(fetchUser(id), (cause, defect) =>
|
|
649
|
+
* cause instanceof NotFoundError ? ("not_found" as const) : defect(cause),
|
|
655
650
|
* );
|
|
656
651
|
* ```
|
|
657
652
|
*/
|
|
658
|
-
declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qualify: (cause: unknown) => R): AsyncResult<T, Exclude<R, Defect>>;
|
|
653
|
+
declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qualify: (cause: unknown, defect: (cause: unknown) => Defect) => R): AsyncResult$1<T, Exclude<R, Defect>>;
|
|
659
654
|
/**
|
|
660
655
|
* Wrap a `Promise` asserted **not** to fail in any modeled way: any rejection
|
|
661
656
|
* becomes a `Defect`.
|
|
@@ -668,7 +663,7 @@ declare function fromPromise<T, R>(promise: Promise<T> | (() => Promise<T>), qua
|
|
|
668
663
|
* @typeParam T - the resolved value type.
|
|
669
664
|
* @param promise - the promise, or a thunk returning one.
|
|
670
665
|
*/
|
|
671
|
-
declare function fromSafePromise<T>(promise: Promise<T> | (() => Promise<T>)): AsyncResult<T, never>;
|
|
666
|
+
declare function fromSafePromise<T>(promise: Promise<T> | (() => Promise<T>)): AsyncResult$1<T, never>;
|
|
672
667
|
/**
|
|
673
668
|
* The success channel of {@link all} / {@link allAsync}: a **positional tuple**
|
|
674
669
|
* for a fixed-length input (including the empty tuple), or a homogeneous
|
|
@@ -690,7 +685,7 @@ type AllOk<Rs extends readonly unknown[], Ts extends readonly unknown[]> = numbe
|
|
|
690
685
|
/** A record of `Result`s — the input to {@link allFromDict}. */
|
|
691
686
|
type ResultRecord = Record<string, Result$1<unknown, unknown>>;
|
|
692
687
|
/** A record of `AsyncResult`s — the input to {@link allFromDictAsync}. */
|
|
693
|
-
type AsyncResultRecord = Record<string, AsyncResult<unknown, unknown>>;
|
|
688
|
+
type AsyncResultRecord = Record<string, AsyncResult$1<unknown, unknown>>;
|
|
694
689
|
/**
|
|
695
690
|
* Collect a tuple/array of {@link Result}s into a single `Result` of all their
|
|
696
691
|
* success values.
|
|
@@ -744,7 +739,7 @@ declare function allFromDict<R extends ResultRecord>(results: R): Result$1<{ [K
|
|
|
744
739
|
* await allAsync([fromSafePromise(a()), fromSafePromise(b())]);
|
|
745
740
|
* ```
|
|
746
741
|
*/
|
|
747
|
-
declare function allAsync<Rs extends readonly AsyncResult<unknown, unknown>[]>(results: readonly [...Rs]): AsyncResult<AllOk<Rs, { [K in keyof Rs]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
742
|
+
declare function allAsync<Rs extends readonly AsyncResult$1<unknown, unknown>[]>(results: readonly [...Rs]): AsyncResult$1<AllOk<Rs, { [K in keyof Rs]: AsyncOkOf<Rs[K]> }>, AsyncErrOf<Rs[number]>>;
|
|
748
743
|
/**
|
|
749
744
|
* The asynchronous counterpart of {@link allFromDict}: combine a record of
|
|
750
745
|
* {@link AsyncResult}s into one `AsyncResult` of a record of their values.
|
|
@@ -759,17 +754,15 @@ declare function allAsync<Rs extends readonly AsyncResult<unknown, unknown>[]>(r
|
|
|
759
754
|
* await allFromDictAsync({ a: fromSafePromise(a()), b: fromSafePromise(b()) });
|
|
760
755
|
* ```
|
|
761
756
|
*/
|
|
762
|
-
declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): AsyncResult<{ [K in keyof R]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
757
|
+
declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): AsyncResult$1<{ [K in keyof R]: AsyncOkOf<R[K]> }, AsyncErrOf<R[keyof R]>>;
|
|
763
758
|
//#endregion
|
|
764
759
|
//#region src/facade.d.ts
|
|
765
760
|
/**
|
|
766
|
-
* Companion object grouping the
|
|
767
|
-
* discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
768
|
-
* {@link Result.
|
|
769
|
-
* {@link Result.
|
|
770
|
-
* {@link Result.
|
|
771
|
-
* {@link Result.allFromDictAsync}, {@link Result.isOk}, {@link Result.isErr},
|
|
772
|
-
* {@link Result.isDefect}, {@link Result.isResult}.
|
|
761
|
+
* Companion object grouping the **`Result`-producing** entry points under a
|
|
762
|
+
* single, discoverable namespace: {@link Result.Ok}, {@link Result.Err},
|
|
763
|
+
* {@link Result.Do}, {@link Result.fromNullable}, {@link Result.fromThrowable},
|
|
764
|
+
* {@link Result.all}, {@link Result.allFromDict}, {@link Result.isOk},
|
|
765
|
+
* {@link Result.isErr}, {@link Result.isDefect}, {@link Result.isResult}.
|
|
773
766
|
*
|
|
774
767
|
* @remarks
|
|
775
768
|
* Purely additive sugar — each member **is** the corresponding free function.
|
|
@@ -777,6 +770,10 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
777
770
|
* `{ Ok }` never pulls this object in. The value `Result` and the type
|
|
778
771
|
* {@link Result} share one name (the companion-object pattern).
|
|
779
772
|
*
|
|
773
|
+
* The **async** entry points live on the sibling {@link AsyncResult} companion
|
|
774
|
+
* (`AsyncResult.fromPromise`, `AsyncResult.all`, …), grouped by what they
|
|
775
|
+
* return — a static lives in exactly one namespace.
|
|
776
|
+
*
|
|
780
777
|
* @example
|
|
781
778
|
* ```ts
|
|
782
779
|
* import { Result } from "unthrown";
|
|
@@ -786,22 +783,45 @@ declare function allFromDictAsync<R extends AsyncResultRecord>(results: R): Asyn
|
|
|
786
783
|
declare const Result: {
|
|
787
784
|
readonly Ok: typeof Ok;
|
|
788
785
|
readonly Err: typeof Err;
|
|
789
|
-
readonly Defect: typeof Defect;
|
|
790
786
|
readonly Do: typeof Do;
|
|
791
787
|
readonly fromNullable: typeof fromNullable;
|
|
792
788
|
readonly fromThrowable: typeof fromThrowable;
|
|
793
|
-
readonly fromPromise: typeof fromPromise;
|
|
794
|
-
readonly fromSafePromise: typeof fromSafePromise;
|
|
795
789
|
readonly all: typeof all;
|
|
796
|
-
readonly allAsync: typeof allAsync;
|
|
797
790
|
readonly allFromDict: typeof allFromDict;
|
|
798
|
-
readonly allFromDictAsync: typeof allFromDictAsync;
|
|
799
791
|
readonly isOk: typeof isOk;
|
|
800
792
|
readonly isErr: typeof isErr;
|
|
801
793
|
readonly isDefect: typeof isDefect;
|
|
802
794
|
readonly isResult: typeof isResult;
|
|
803
795
|
};
|
|
804
796
|
type Result<T, E> = Result$1<T, E>;
|
|
797
|
+
/**
|
|
798
|
+
* Companion object grouping the **`AsyncResult`-producing** entry points under
|
|
799
|
+
* the matching namespace: {@link AsyncResult.fromPromise},
|
|
800
|
+
* {@link AsyncResult.fromSafePromise}, {@link AsyncResult.all},
|
|
801
|
+
* {@link AsyncResult.allFromDict}.
|
|
802
|
+
*
|
|
803
|
+
* @remarks
|
|
804
|
+
* The async sibling of {@link Result}. Statics are grouped by what they
|
|
805
|
+
* **return**, so `fromPromise`/`fromSafePromise` and the async aggregates sit
|
|
806
|
+
* here rather than on {@link Result}; the namespace already conveys "async", so
|
|
807
|
+
* the aggregates drop the `Async` suffix (`AsyncResult.all` is the free function
|
|
808
|
+
* `allAsync`; `AsyncResult.allFromDict` is `allFromDictAsync`). Like
|
|
809
|
+
* {@link Result}, the free functions remain the primary, tree-shakeable API; the
|
|
810
|
+
* value `AsyncResult` and the type {@link AsyncResult} share one name.
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```ts
|
|
814
|
+
* import { AsyncResult } from "unthrown";
|
|
815
|
+
* const user = await AsyncResult.fromPromise(fetchUser(id), (c, defect) => defect(c));
|
|
816
|
+
* ```
|
|
817
|
+
*/
|
|
818
|
+
declare const AsyncResult: {
|
|
819
|
+
readonly fromPromise: typeof fromPromise;
|
|
820
|
+
readonly fromSafePromise: typeof fromSafePromise;
|
|
821
|
+
readonly all: typeof allAsync;
|
|
822
|
+
readonly allFromDict: typeof allFromDictAsync;
|
|
823
|
+
};
|
|
824
|
+
type AsyncResult<T, E> = AsyncResult$1<T, E>;
|
|
805
825
|
//#endregion
|
|
806
826
|
//#region src/tagged.d.ts
|
|
807
827
|
type Props = Record<string, unknown>;
|
|
@@ -923,7 +943,7 @@ declare function matchTags<T, E extends {
|
|
|
923
943
|
}, R>(result: Result$1<T, E>, handlers: TagHandlers<T, E, R>): R;
|
|
924
944
|
declare function matchTags<T, E extends {
|
|
925
945
|
_tag: string;
|
|
926
|
-
}, R>(result: AsyncResult<T, E>, handlers: TagHandlers<T, E, R>): Promise<R>;
|
|
946
|
+
}, R>(result: AsyncResult$1<T, E>, handlers: TagHandlers<T, E, R>): Promise<R>;
|
|
927
947
|
//#endregion
|
|
928
|
-
export { type AsyncErrOf, type AsyncOkOf,
|
|
948
|
+
export { type AsyncErrOf, type AsyncOkOf, AsyncResult, type Awaitable, type DefectView, Do, Err, type ErrOf, type ErrView, Ok, type OkOf, type OkView, Result, type TagHandlers, TaggedError, type TaggedErrorConstructor, type TaggedErrorInstance, UnwrapError, all, allAsync, allFromDict, allFromDictAsync, fromNullable, fromPromise, fromSafePromise, fromThrowable, isDefect, isErr, isOk, isResult, matchTags };
|
|
929
949
|
//# sourceMappingURL=index.d.cts.map
|