@unthrown/neverthrow 0.1.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/LICENSE +21 -0
- package/README.md +36 -0
- package/dist/index.cjs +78 -0
- package/dist/index.d.cts +61 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +61 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +76 -0
- package/dist/index.mjs.map +1 -0
- package/docs/index.md +147 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benoit Travers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @unthrown/neverthrow
|
|
2
|
+
|
|
3
|
+
> [neverthrow](https://github.com/supermacro/neverthrow) interop for
|
|
4
|
+
> [unthrown](https://github.com/btravstack/unthrown)'s `Result`.
|
|
5
|
+
|
|
6
|
+
๐ **[Documentation](https://btravstack.github.io/unthrown/guide/interop)** ยท
|
|
7
|
+
[API Reference](https://btravstack.github.io/unthrown/api/neverthrow/)
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @unthrown/neverthrow neverthrow
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
neverthrow has two channels (`Ok`/`Err`) and no defect channel. Coming **in**,
|
|
14
|
+
every neverthrow result is an `Ok` or `Err` โ never a `Defect`. Going **out**, a
|
|
15
|
+
`Defect` has nowhere to live, so `toNeverthrow` **forces** you to triage it with
|
|
16
|
+
`onDefect` โ no defect is ever silently folded into your domain error type.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { ok } from "unthrown";
|
|
20
|
+
import { toNeverthrow, fromNeverthrow } from "@unthrown/neverthrow";
|
|
21
|
+
import { ok as ntOk } from "neverthrow";
|
|
22
|
+
|
|
23
|
+
toNeverthrow(ok(1), (cause) => ({ _tag: "Bug", cause })); // neverthrow Ok(1)
|
|
24
|
+
fromNeverthrow(ntOk(1)); // Result<number, never>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- `toNeverthrow(r, onDefect)` / `fromNeverthrow(r)` โ sync `Result โ Result`.
|
|
28
|
+
- `toNeverthrowAsync(ar, onDefect)` / `fromNeverthrowAsync(ra)` โ async
|
|
29
|
+
`AsyncResult โ ResultAsync`. An unexpected rejection inside a `ResultAsync`
|
|
30
|
+
becomes a `Defect` on the way in.
|
|
31
|
+
|
|
32
|
+
`neverthrow` is a peer dependency.
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
[MIT](../../LICENSE) ยฉ Benoit TRAVERS
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let neverthrow = require("neverthrow");
|
|
3
|
+
let unthrown = require("unthrown");
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Convert a `Result` into a neverthrow `Result`, triaging any defect.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s
|
|
10
|
+
* cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,
|
|
11
|
+
* `Defect โ err(onDefect(cause))`.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T - the success value type.
|
|
14
|
+
* @typeParam E - the modeled error type.
|
|
15
|
+
* @param result - the result to convert.
|
|
16
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
17
|
+
*/
|
|
18
|
+
function toNeverthrow(result, onDefect) {
|
|
19
|
+
return result.match({
|
|
20
|
+
ok: (value) => (0, neverthrow.ok)(value),
|
|
21
|
+
err: (error) => (0, neverthrow.err)(error),
|
|
22
|
+
defect: (cause) => (0, neverthrow.err)(onDefect(cause))
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Convert a neverthrow `Result` into a `Result`.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* `ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a
|
|
30
|
+
* `Defect`.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam T - the success value type.
|
|
33
|
+
* @typeParam E - the modeled error type.
|
|
34
|
+
* @param result - the neverthrow result to convert.
|
|
35
|
+
*/
|
|
36
|
+
function fromNeverthrow(result) {
|
|
37
|
+
return result.isOk() ? (0, unthrown.ok)(result.value) : (0, unthrown.err)(result.error);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any
|
|
41
|
+
* defect.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* The async counterpart of {@link toNeverthrow}: `onDefect` is required for the
|
|
45
|
+
* same reason. The `AsyncResult` is awaited (it never rejects) and each settled
|
|
46
|
+
* `Result` is converted.
|
|
47
|
+
*
|
|
48
|
+
* @typeParam T - the success value type.
|
|
49
|
+
* @typeParam E - the modeled error type.
|
|
50
|
+
* @param asyncResult - the async result to convert.
|
|
51
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
52
|
+
*/
|
|
53
|
+
function toNeverthrowAsync(asyncResult, onDefect) {
|
|
54
|
+
return neverthrow.ResultAsync.fromSafePromise(settle(asyncResult)).andThen((result) => toNeverthrow(result, onDefect));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Convert a neverthrow `ResultAsync` into an `AsyncResult`.
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* The async counterpart of {@link fromNeverthrow}. A modeled `Err` stays an
|
|
61
|
+
* `Err`; an *unexpected* rejection inside the neverthrow chain becomes a
|
|
62
|
+
* `Defect`. The returned `AsyncResult` never throws when awaited.
|
|
63
|
+
*
|
|
64
|
+
* @typeParam T - the success value type.
|
|
65
|
+
* @typeParam E - the modeled error type.
|
|
66
|
+
* @param resultAsync - the neverthrow async result to convert.
|
|
67
|
+
*/
|
|
68
|
+
function fromNeverthrowAsync(resultAsync) {
|
|
69
|
+
return (0, unthrown.fromSafePromise)(Promise.resolve(resultAsync)).flatMap((result) => fromNeverthrow(result));
|
|
70
|
+
}
|
|
71
|
+
function settle(asyncResult) {
|
|
72
|
+
return (async () => await asyncResult)();
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
exports.fromNeverthrow = fromNeverthrow;
|
|
76
|
+
exports.fromNeverthrowAsync = fromNeverthrowAsync;
|
|
77
|
+
exports.toNeverthrow = toNeverthrow;
|
|
78
|
+
exports.toNeverthrowAsync = toNeverthrowAsync;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Result, ResultAsync } from "neverthrow";
|
|
2
|
+
import { AsyncResult, Result as Result$1 } from "unthrown";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Convert a `Result` into a neverthrow `Result`, triaging any defect.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s
|
|
10
|
+
* cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,
|
|
11
|
+
* `Defect โ err(onDefect(cause))`.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T - the success value type.
|
|
14
|
+
* @typeParam E - the modeled error type.
|
|
15
|
+
* @param result - the result to convert.
|
|
16
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
17
|
+
*/
|
|
18
|
+
declare function toNeverthrow<T, E>(result: Result$1<T, E>, onDefect: (cause: unknown) => E): Result<T, E>;
|
|
19
|
+
/**
|
|
20
|
+
* Convert a neverthrow `Result` into a `Result`.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* `ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a
|
|
24
|
+
* `Defect`.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T - the success value type.
|
|
27
|
+
* @typeParam E - the modeled error type.
|
|
28
|
+
* @param result - the neverthrow result to convert.
|
|
29
|
+
*/
|
|
30
|
+
declare function fromNeverthrow<T, E>(result: Result<T, E>): Result$1<T, E>;
|
|
31
|
+
/**
|
|
32
|
+
* Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any
|
|
33
|
+
* defect.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* The async counterpart of {@link toNeverthrow}: `onDefect` is required for the
|
|
37
|
+
* same reason. The `AsyncResult` is awaited (it never rejects) and each settled
|
|
38
|
+
* `Result` is converted.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T - the success value type.
|
|
41
|
+
* @typeParam E - the modeled error type.
|
|
42
|
+
* @param asyncResult - the async result to convert.
|
|
43
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
44
|
+
*/
|
|
45
|
+
declare function toNeverthrowAsync<T, E>(asyncResult: AsyncResult<T, E>, onDefect: (cause: unknown) => E): ResultAsync<T, E>;
|
|
46
|
+
/**
|
|
47
|
+
* Convert a neverthrow `ResultAsync` into an `AsyncResult`.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* The async counterpart of {@link fromNeverthrow}. A modeled `Err` stays an
|
|
51
|
+
* `Err`; an *unexpected* rejection inside the neverthrow chain becomes a
|
|
52
|
+
* `Defect`. The returned `AsyncResult` never throws when awaited.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam T - the success value type.
|
|
55
|
+
* @typeParam E - the modeled error type.
|
|
56
|
+
* @param resultAsync - the neverthrow async result to convert.
|
|
57
|
+
*/
|
|
58
|
+
declare function fromNeverthrowAsync<T, E>(resultAsync: ResultAsync<T, E>): AsyncResult<T, E>;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { fromNeverthrow, fromNeverthrowAsync, toNeverthrow, toNeverthrowAsync };
|
|
61
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;AAqCA;;;;;;;;;;;;iBAAgB,YAAA,OACd,MAAA,EAAQ,QAAA,CAAO,CAAA,EAAG,CAAA,GAClB,QAAA,GAAW,KAAA,cAAmB,CAAA,GAC7B,MAAA,CAAiB,CAAA,EAAG,CAAA;;;;;;;;;;;;iBAmBP,cAAA,OAAqB,MAAA,EAAQ,MAAA,CAAiB,CAAA,EAAG,CAAA,IAAK,QAAA,CAAO,CAAA,EAAG,CAAA;AAnBxD;AAmBxB;;;;;;;;;;;;;AAnBwB,iBAqCR,iBAAA,OACd,WAAA,EAAa,WAAA,CAAY,CAAA,EAAG,CAAA,GAC5B,QAAA,GAAW,KAAA,cAAmB,CAAA,GAC7B,WAAA,CAAsB,CAAA,EAAG,CAAA;;;;;;;;AArBqD;AAkBjF;;;;iBAqBgB,mBAAA,OACd,WAAA,EAAa,WAAA,CAAsB,CAAA,EAAG,CAAA,IACrC,WAAA,CAAY,CAAA,EAAG,CAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Result, ResultAsync } from "neverthrow";
|
|
2
|
+
import { AsyncResult, Result as Result$1 } from "unthrown";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Convert a `Result` into a neverthrow `Result`, triaging any defect.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s
|
|
10
|
+
* cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,
|
|
11
|
+
* `Defect โ err(onDefect(cause))`.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T - the success value type.
|
|
14
|
+
* @typeParam E - the modeled error type.
|
|
15
|
+
* @param result - the result to convert.
|
|
16
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
17
|
+
*/
|
|
18
|
+
declare function toNeverthrow<T, E>(result: Result$1<T, E>, onDefect: (cause: unknown) => E): Result<T, E>;
|
|
19
|
+
/**
|
|
20
|
+
* Convert a neverthrow `Result` into a `Result`.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* `ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a
|
|
24
|
+
* `Defect`.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T - the success value type.
|
|
27
|
+
* @typeParam E - the modeled error type.
|
|
28
|
+
* @param result - the neverthrow result to convert.
|
|
29
|
+
*/
|
|
30
|
+
declare function fromNeverthrow<T, E>(result: Result<T, E>): Result$1<T, E>;
|
|
31
|
+
/**
|
|
32
|
+
* Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any
|
|
33
|
+
* defect.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* The async counterpart of {@link toNeverthrow}: `onDefect` is required for the
|
|
37
|
+
* same reason. The `AsyncResult` is awaited (it never rejects) and each settled
|
|
38
|
+
* `Result` is converted.
|
|
39
|
+
*
|
|
40
|
+
* @typeParam T - the success value type.
|
|
41
|
+
* @typeParam E - the modeled error type.
|
|
42
|
+
* @param asyncResult - the async result to convert.
|
|
43
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
44
|
+
*/
|
|
45
|
+
declare function toNeverthrowAsync<T, E>(asyncResult: AsyncResult<T, E>, onDefect: (cause: unknown) => E): ResultAsync<T, E>;
|
|
46
|
+
/**
|
|
47
|
+
* Convert a neverthrow `ResultAsync` into an `AsyncResult`.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* The async counterpart of {@link fromNeverthrow}. A modeled `Err` stays an
|
|
51
|
+
* `Err`; an *unexpected* rejection inside the neverthrow chain becomes a
|
|
52
|
+
* `Defect`. The returned `AsyncResult` never throws when awaited.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam T - the success value type.
|
|
55
|
+
* @typeParam E - the modeled error type.
|
|
56
|
+
* @param resultAsync - the neverthrow async result to convert.
|
|
57
|
+
*/
|
|
58
|
+
declare function fromNeverthrowAsync<T, E>(resultAsync: ResultAsync<T, E>): AsyncResult<T, E>;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { fromNeverthrow, fromNeverthrowAsync, toNeverthrow, toNeverthrowAsync };
|
|
61
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;AAqCA;;;;;;;;;;;;iBAAgB,YAAA,OACd,MAAA,EAAQ,QAAA,CAAO,CAAA,EAAG,CAAA,GAClB,QAAA,GAAW,KAAA,cAAmB,CAAA,GAC7B,MAAA,CAAiB,CAAA,EAAG,CAAA;;;;;;;;;;;;iBAmBP,cAAA,OAAqB,MAAA,EAAQ,MAAA,CAAiB,CAAA,EAAG,CAAA,IAAK,QAAA,CAAO,CAAA,EAAG,CAAA;AAnBxD;AAmBxB;;;;;;;;;;;;;AAnBwB,iBAqCR,iBAAA,OACd,WAAA,EAAa,WAAA,CAAY,CAAA,EAAG,CAAA,GAC5B,QAAA,GAAW,KAAA,cAAmB,CAAA,GAC7B,WAAA,CAAsB,CAAA,EAAG,CAAA;;;;;;;;AArBqD;AAkBjF;;;;iBAqBgB,mBAAA,OACd,WAAA,EAAa,WAAA,CAAsB,CAAA,EAAG,CAAA,IACrC,WAAA,CAAY,CAAA,EAAG,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ResultAsync, err, ok } from "neverthrow";
|
|
2
|
+
import { err as err$1, fromSafePromise, ok as ok$1 } from "unthrown";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Convert a `Result` into a neverthrow `Result`, triaging any defect.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s
|
|
9
|
+
* cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,
|
|
10
|
+
* `Defect โ err(onDefect(cause))`.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - the success value type.
|
|
13
|
+
* @typeParam E - the modeled error type.
|
|
14
|
+
* @param result - the result to convert.
|
|
15
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
16
|
+
*/
|
|
17
|
+
function toNeverthrow(result, onDefect) {
|
|
18
|
+
return result.match({
|
|
19
|
+
ok: (value) => ok(value),
|
|
20
|
+
err: (error) => err(error),
|
|
21
|
+
defect: (cause) => err(onDefect(cause))
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a neverthrow `Result` into a `Result`.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* `ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a
|
|
29
|
+
* `Defect`.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam T - the success value type.
|
|
32
|
+
* @typeParam E - the modeled error type.
|
|
33
|
+
* @param result - the neverthrow result to convert.
|
|
34
|
+
*/
|
|
35
|
+
function fromNeverthrow(result) {
|
|
36
|
+
return result.isOk() ? ok$1(result.value) : err$1(result.error);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any
|
|
40
|
+
* defect.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* The async counterpart of {@link toNeverthrow}: `onDefect` is required for the
|
|
44
|
+
* same reason. The `AsyncResult` is awaited (it never rejects) and each settled
|
|
45
|
+
* `Result` is converted.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam T - the success value type.
|
|
48
|
+
* @typeParam E - the modeled error type.
|
|
49
|
+
* @param asyncResult - the async result to convert.
|
|
50
|
+
* @param onDefect - folds a defect's unknown cause into a modeled `E`.
|
|
51
|
+
*/
|
|
52
|
+
function toNeverthrowAsync(asyncResult, onDefect) {
|
|
53
|
+
return ResultAsync.fromSafePromise(settle(asyncResult)).andThen((result) => toNeverthrow(result, onDefect));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Convert a neverthrow `ResultAsync` into an `AsyncResult`.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* The async counterpart of {@link fromNeverthrow}. A modeled `Err` stays an
|
|
60
|
+
* `Err`; an *unexpected* rejection inside the neverthrow chain becomes a
|
|
61
|
+
* `Defect`. The returned `AsyncResult` never throws when awaited.
|
|
62
|
+
*
|
|
63
|
+
* @typeParam T - the success value type.
|
|
64
|
+
* @typeParam E - the modeled error type.
|
|
65
|
+
* @param resultAsync - the neverthrow async result to convert.
|
|
66
|
+
*/
|
|
67
|
+
function fromNeverthrowAsync(resultAsync) {
|
|
68
|
+
return fromSafePromise(Promise.resolve(resultAsync)).flatMap((result) => fromNeverthrow(result));
|
|
69
|
+
}
|
|
70
|
+
function settle(asyncResult) {
|
|
71
|
+
return (async () => await asyncResult)();
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { fromNeverthrow, fromNeverthrowAsync, toNeverthrow, toNeverthrowAsync };
|
|
75
|
+
|
|
76
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["neverthrowOk","neverthrowErr","ok","err","NeverthrowResultAsync"],"sources":["../src/index.ts"],"sourcesContent":["// @unthrown/neverthrow โ interop between unthrown's `Result`/`AsyncResult` and\n// neverthrow's `Result`/`ResultAsync`.\n//\n// neverthrow has two channels (`Ok`/`Err`), so it cannot represent unthrown's\n// third one. Coming *in*, every neverthrow result is an `Ok` or `Err` โ never a\n// `Defect`. Going *out*, a `Defect` has nowhere to live, so `toNeverthrow`\n// forces you to triage it with `onDefect` (Thesis #3): no defect is ever\n// silently folded into your domain error type.\n//\n// import { ok } from \"unthrown\";\n// import { toNeverthrow, fromNeverthrow } from \"@unthrown/neverthrow\";\n//\n// toNeverthrow(ok(1), (cause) => ({ _tag: \"Bug\", cause }));\n// fromNeverthrow(neverthrowOk(1)); // Result<number, never>\n\nimport {\n err as neverthrowErr,\n ok as neverthrowOk,\n ResultAsync as NeverthrowResultAsync,\n} from \"neverthrow\";\nimport type { Result as NeverthrowResult } from \"neverthrow\";\nimport { err, fromSafePromise, ok } from \"unthrown\";\nimport type { AsyncResult, Result } from \"unthrown\";\n\n/**\n * Convert a `Result` into a neverthrow `Result`, triaging any defect.\n *\n * @remarks\n * neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s\n * cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,\n * `Defect โ err(onDefect(cause))`.\n *\n * @typeParam T - the success value type.\n * @typeParam E - the modeled error type.\n * @param result - the result to convert.\n * @param onDefect - folds a defect's unknown cause into a modeled `E`.\n */\nexport function toNeverthrow<T, E>(\n result: Result<T, E>,\n onDefect: (cause: unknown) => E,\n): NeverthrowResult<T, E> {\n return result.match<NeverthrowResult<T, E>>({\n ok: (value) => neverthrowOk(value),\n err: (error) => neverthrowErr(error),\n defect: (cause) => neverthrowErr(onDefect(cause)),\n });\n}\n\n/**\n * Convert a neverthrow `Result` into a `Result`.\n *\n * @remarks\n * `ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a\n * `Defect`.\n *\n * @typeParam T - the success value type.\n * @typeParam E - the modeled error type.\n * @param result - the neverthrow result to convert.\n */\nexport function fromNeverthrow<T, E>(result: NeverthrowResult<T, E>): Result<T, E> {\n return result.isOk() ? ok(result.value) : err(result.error);\n}\n\n/**\n * Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any\n * defect.\n *\n * @remarks\n * The async counterpart of {@link toNeverthrow}: `onDefect` is required for the\n * same reason. The `AsyncResult` is awaited (it never rejects) and each settled\n * `Result` is converted.\n *\n * @typeParam T - the success value type.\n * @typeParam E - the modeled error type.\n * @param asyncResult - the async result to convert.\n * @param onDefect - folds a defect's unknown cause into a modeled `E`.\n */\nexport function toNeverthrowAsync<T, E>(\n asyncResult: AsyncResult<T, E>,\n onDefect: (cause: unknown) => E,\n): NeverthrowResultAsync<T, E> {\n return NeverthrowResultAsync.fromSafePromise(settle(asyncResult)).andThen((result) =>\n toNeverthrow(result, onDefect),\n );\n}\n\n/**\n * Convert a neverthrow `ResultAsync` into an `AsyncResult`.\n *\n * @remarks\n * The async counterpart of {@link fromNeverthrow}. A modeled `Err` stays an\n * `Err`; an *unexpected* rejection inside the neverthrow chain becomes a\n * `Defect`. The returned `AsyncResult` never throws when awaited.\n *\n * @typeParam T - the success value type.\n * @typeParam E - the modeled error type.\n * @param resultAsync - the neverthrow async result to convert.\n */\nexport function fromNeverthrowAsync<T, E>(\n resultAsync: NeverthrowResultAsync<T, E>,\n): AsyncResult<T, E> {\n return fromSafePromise(Promise.resolve(resultAsync)).flatMap((result) => fromNeverthrow(result));\n}\n\nfunction settle<T, E>(asyncResult: AsyncResult<T, E>): Promise<Result<T, E>> {\n return (async () => await asyncResult)();\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAqCA,SAAgB,aACd,QACA,UACwB;CACxB,OAAO,OAAO,MAA8B;EAC1C,KAAK,UAAUA,GAAa,KAAK;EACjC,MAAM,UAAUC,IAAc,KAAK;EACnC,SAAS,UAAUA,IAAc,SAAS,KAAK,CAAC;CAClD,CAAC;AACH;;;;;;;;;;;;AAaA,SAAgB,eAAqB,QAA8C;CACjF,OAAO,OAAO,KAAK,IAAIC,KAAG,OAAO,KAAK,IAAIC,MAAI,OAAO,KAAK;AAC5D;;;;;;;;;;;;;;;AAgBA,SAAgB,kBACd,aACA,UAC6B;CAC7B,OAAOC,YAAsB,gBAAgB,OAAO,WAAW,CAAC,CAAC,CAAC,SAAS,WACzE,aAAa,QAAQ,QAAQ,CAC/B;AACF;;;;;;;;;;;;;AAcA,SAAgB,oBACd,aACmB;CACnB,OAAO,gBAAgB,QAAQ,QAAQ,WAAW,CAAC,CAAC,CAAC,SAAS,WAAW,eAAe,MAAM,CAAC;AACjG;AAEA,SAAS,OAAa,aAAuD;CAC3E,QAAQ,YAAY,MAAM,YAAA,CAAa;AACzC"}
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
**@unthrown/neverthrow**
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
# @unthrown/neverthrow
|
|
6
|
+
|
|
7
|
+
## Functions
|
|
8
|
+
|
|
9
|
+
### fromNeverthrow()
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
function fromNeverthrow<T, E>(result): Result<T, E>;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Defined in: index.ts:60
|
|
16
|
+
|
|
17
|
+
Convert a neverthrow `Result` into a `Result`.
|
|
18
|
+
|
|
19
|
+
#### Type Parameters
|
|
20
|
+
|
|
21
|
+
| Type Parameter | Description |
|
|
22
|
+
| ------ | ------ |
|
|
23
|
+
| `T` | the success value type. |
|
|
24
|
+
| `E` | the modeled error type. |
|
|
25
|
+
|
|
26
|
+
#### Parameters
|
|
27
|
+
|
|
28
|
+
| Parameter | Type | Description |
|
|
29
|
+
| ------ | ------ | ------ |
|
|
30
|
+
| `result` | `Result`<`T`, `E`> | the neverthrow result to convert. |
|
|
31
|
+
|
|
32
|
+
#### Returns
|
|
33
|
+
|
|
34
|
+
`Result`<`T`, `E`>
|
|
35
|
+
|
|
36
|
+
#### Remarks
|
|
37
|
+
|
|
38
|
+
`ok โ Ok`, `err โ Err`. neverthrow carries no defect, so the result is never a
|
|
39
|
+
`Defect`.
|
|
40
|
+
|
|
41
|
+
***
|
|
42
|
+
|
|
43
|
+
### fromNeverthrowAsync()
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
function fromNeverthrowAsync<T, E>(resultAsync): AsyncResult<T, E>;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Defined in: index.ts:99
|
|
50
|
+
|
|
51
|
+
Convert a neverthrow `ResultAsync` into an `AsyncResult`.
|
|
52
|
+
|
|
53
|
+
#### Type Parameters
|
|
54
|
+
|
|
55
|
+
| Type Parameter | Description |
|
|
56
|
+
| ------ | ------ |
|
|
57
|
+
| `T` | the success value type. |
|
|
58
|
+
| `E` | the modeled error type. |
|
|
59
|
+
|
|
60
|
+
#### Parameters
|
|
61
|
+
|
|
62
|
+
| Parameter | Type | Description |
|
|
63
|
+
| ------ | ------ | ------ |
|
|
64
|
+
| `resultAsync` | `ResultAsync`<`T`, `E`> | the neverthrow async result to convert. |
|
|
65
|
+
|
|
66
|
+
#### Returns
|
|
67
|
+
|
|
68
|
+
`AsyncResult`<`T`, `E`>
|
|
69
|
+
|
|
70
|
+
#### Remarks
|
|
71
|
+
|
|
72
|
+
The async counterpart of [fromNeverthrow](#fromneverthrow). A modeled `Err` stays an
|
|
73
|
+
`Err`; an *unexpected* rejection inside the neverthrow chain becomes a
|
|
74
|
+
`Defect`. The returned `AsyncResult` never throws when awaited.
|
|
75
|
+
|
|
76
|
+
***
|
|
77
|
+
|
|
78
|
+
### toNeverthrow()
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
function toNeverthrow<T, E>(result, onDefect): Result<T, E>;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Defined in: index.ts:38
|
|
85
|
+
|
|
86
|
+
Convert a `Result` into a neverthrow `Result`, triaging any defect.
|
|
87
|
+
|
|
88
|
+
#### Type Parameters
|
|
89
|
+
|
|
90
|
+
| Type Parameter | Description |
|
|
91
|
+
| ------ | ------ |
|
|
92
|
+
| `T` | the success value type. |
|
|
93
|
+
| `E` | the modeled error type. |
|
|
94
|
+
|
|
95
|
+
#### Parameters
|
|
96
|
+
|
|
97
|
+
| Parameter | Type | Description |
|
|
98
|
+
| ------ | ------ | ------ |
|
|
99
|
+
| `result` | `Result`<`T`, `E`> | the result to convert. |
|
|
100
|
+
| `onDefect` | (`cause`) => `E` | folds a defect's unknown cause into a modeled `E`. |
|
|
101
|
+
|
|
102
|
+
#### Returns
|
|
103
|
+
|
|
104
|
+
`Result`<`T`, `E`>
|
|
105
|
+
|
|
106
|
+
#### Remarks
|
|
107
|
+
|
|
108
|
+
neverthrow has no defect channel, so `onDefect` **must** fold a `Defect`'s
|
|
109
|
+
cause into a modeled error `E` (an `Err`). `Ok โ ok`, `Err โ err`,
|
|
110
|
+
`Defect โ err(onDefect(cause))`.
|
|
111
|
+
|
|
112
|
+
***
|
|
113
|
+
|
|
114
|
+
### toNeverthrowAsync()
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
function toNeverthrowAsync<T, E>(asyncResult, onDefect): ResultAsync<T, E>;
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Defined in: index.ts:78
|
|
121
|
+
|
|
122
|
+
Convert an `AsyncResult` into a neverthrow `ResultAsync`, triaging any
|
|
123
|
+
defect.
|
|
124
|
+
|
|
125
|
+
#### Type Parameters
|
|
126
|
+
|
|
127
|
+
| Type Parameter | Description |
|
|
128
|
+
| ------ | ------ |
|
|
129
|
+
| `T` | the success value type. |
|
|
130
|
+
| `E` | the modeled error type. |
|
|
131
|
+
|
|
132
|
+
#### Parameters
|
|
133
|
+
|
|
134
|
+
| Parameter | Type | Description |
|
|
135
|
+
| ------ | ------ | ------ |
|
|
136
|
+
| `asyncResult` | `AsyncResult`<`T`, `E`> | the async result to convert. |
|
|
137
|
+
| `onDefect` | (`cause`) => `E` | folds a defect's unknown cause into a modeled `E`. |
|
|
138
|
+
|
|
139
|
+
#### Returns
|
|
140
|
+
|
|
141
|
+
`ResultAsync`<`T`, `E`>
|
|
142
|
+
|
|
143
|
+
#### Remarks
|
|
144
|
+
|
|
145
|
+
The async counterpart of [toNeverthrow](#toneverthrow): `onDefect` is required for the
|
|
146
|
+
same reason. The `AsyncResult` is awaited (it never rejects) and each settled
|
|
147
|
+
`Result` is converted.
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unthrown/neverthrow",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "neverthrow interop for unthrown",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"errors-as-values",
|
|
7
|
+
"neverthrow",
|
|
8
|
+
"result",
|
|
9
|
+
"resultasync",
|
|
10
|
+
"typescript",
|
|
11
|
+
"unthrown"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/btravstack/unthrown#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/btravstack/unthrown/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Benoit TRAVERS <benoit.travers.fr@gmail.com>",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/btravstack/unthrown.git",
|
|
22
|
+
"directory": "packages/neverthrow"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"docs"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"default": "./dist/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"unthrown": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "24.13.2",
|
|
50
|
+
"@vitest/coverage-v8": "4.1.8",
|
|
51
|
+
"neverthrow": "8.2.0",
|
|
52
|
+
"tsdown": "0.22.2",
|
|
53
|
+
"typedoc": "0.28.19",
|
|
54
|
+
"typedoc-plugin-markdown": "4.12.0",
|
|
55
|
+
"typescript": "6.0.3",
|
|
56
|
+
"vitest": "4.1.8",
|
|
57
|
+
"@unthrown/typedoc": "0.1.0",
|
|
58
|
+
"@unthrown/tsconfig": "0.1.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"neverthrow": "^8"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=22.19"
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsdown src/index.ts --format cjs,esm --dts --clean",
|
|
68
|
+
"build:docs": "typedoc",
|
|
69
|
+
"dev": "tsdown src/index.ts --format cjs,esm --dts --watch",
|
|
70
|
+
"test": "vitest run",
|
|
71
|
+
"typecheck": "tsc --noEmit"
|
|
72
|
+
}
|
|
73
|
+
}
|