@praha/byethrow 0.6.2 → 0.7.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/cjs/functions/and-then.d.ts +4 -0
- package/dist/cjs/functions/and-through.d.ts +1 -1
- package/dist/cjs/functions/assert-failure.d.ts +5 -4
- package/dist/cjs/functions/assert-success.d.ts +5 -4
- package/dist/cjs/functions/fail.cjs +2 -2
- package/dist/cjs/functions/or-else.d.ts +4 -0
- package/dist/cjs/functions/succeed.cjs +2 -2
- package/dist/cjs/functions/try.cjs +3 -5
- package/dist/cjs/functions/try.d.ts +76 -32
- package/dist/cjs/result.d.ts +2 -10
- package/dist/esm/functions/and-then.d.ts +4 -0
- package/dist/esm/functions/and-through.d.ts +1 -1
- package/dist/esm/functions/assert-failure.d.ts +5 -4
- package/dist/esm/functions/assert-success.d.ts +5 -4
- package/dist/esm/functions/fail.js +2 -2
- package/dist/esm/functions/or-else.d.ts +4 -0
- package/dist/esm/functions/succeed.js +2 -2
- package/dist/esm/functions/try.d.ts +76 -32
- package/dist/esm/functions/try.js +3 -5
- package/dist/esm/result.d.ts +2 -10
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @praha/byethrow
|
|
1
|
+
# @praha/byethrow 👋
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@praha/byethrow)
|
|
4
4
|
[](https://www.npmjs.com/package/@praha/byethrow)
|
|
@@ -29,8 +29,8 @@ npm install @praha/byethrow
|
|
|
29
29
|
|
|
30
30
|
## 📚 Documentation
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
- Website: For detailed information and usage examples, visit the [byethrow website](https://praha-inc.github.io/byethrow).
|
|
33
|
+
- API Reference: For a complete API reference of the core package effect, see the [byethrow API Reference](https://praha-inc.github.io/byethrow/api).
|
|
34
34
|
|
|
35
35
|
## 🚀 Quick Example
|
|
36
36
|
|
|
@@ -21,6 +21,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
21
21
|
*
|
|
22
22
|
* @example Failure Case (input is a Failure)
|
|
23
23
|
* ```ts
|
|
24
|
+
* import { Result } from '@praha/byethrow';
|
|
25
|
+
*
|
|
24
26
|
* const result = Result.pipe(
|
|
25
27
|
* Result.fail('error'),
|
|
26
28
|
* Result.andThen((x) => Result.succeed(x * 2)),
|
|
@@ -30,6 +32,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
30
32
|
*
|
|
31
33
|
* @example Failure Case (function returns a Failure)
|
|
32
34
|
* ```ts
|
|
35
|
+
* import { Result } from '@praha/byethrow';
|
|
36
|
+
*
|
|
33
37
|
* const result = Result.pipe(
|
|
34
38
|
* Result.succeed(3),
|
|
35
39
|
* Result.andThen((x) => Result.fail('error: ' + x)),
|
|
@@ -28,7 +28,7 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
28
28
|
* import { Result } from '@praha/byethrow';
|
|
29
29
|
*
|
|
30
30
|
* const result = Result.pipe(
|
|
31
|
-
* Result.fail('error),
|
|
31
|
+
* Result.fail('error'),
|
|
32
32
|
* Result.andThrough((x) => {
|
|
33
33
|
* return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
|
|
34
34
|
* }),
|
|
@@ -13,16 +13,17 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
|
13
13
|
* ```ts
|
|
14
14
|
* import { Result } from '@praha/byethrow';
|
|
15
15
|
*
|
|
16
|
-
* const result
|
|
16
|
+
* const result = Result.fail('error');
|
|
17
17
|
* const failure = Result.assertFailure(result);
|
|
18
18
|
* // failure: { type: 'Failure', error: 'error' }
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @example Throws on Success
|
|
22
22
|
* ```ts
|
|
23
|
+
* // @errors: 2769
|
|
23
24
|
* import { Result } from '@praha/byethrow';
|
|
24
25
|
*
|
|
25
|
-
* const result
|
|
26
|
+
* const result = Result.succeed(42);
|
|
26
27
|
* Result.assertFailure(result); // throws Error
|
|
27
28
|
* ```
|
|
28
29
|
*
|
|
@@ -30,9 +31,9 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
|
30
31
|
* ```ts
|
|
31
32
|
* import { Result } from '@praha/byethrow';
|
|
32
33
|
*
|
|
33
|
-
* const
|
|
34
|
+
* const getResult = (): Result.Result<number, string> => Result.fail('error');
|
|
34
35
|
* const value = Result.pipe(
|
|
35
|
-
*
|
|
36
|
+
* getResult(),
|
|
36
37
|
* Result.andThen(() => Result.fail('die')),
|
|
37
38
|
* Result.assertFailure,
|
|
38
39
|
* Result.unwrapError(), // Safe unwrap after assertion
|
|
@@ -13,16 +13,17 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
|
13
13
|
* ```ts
|
|
14
14
|
* import { Result } from '@praha/byethrow';
|
|
15
15
|
*
|
|
16
|
-
* const result
|
|
16
|
+
* const result = Result.succeed(42);
|
|
17
17
|
* const success = Result.assertSuccess(result);
|
|
18
18
|
* // success: { type: 'Success', value: 42 }
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @example Throws on Failure
|
|
22
22
|
* ```ts
|
|
23
|
+
* // @errors: 2769
|
|
23
24
|
* import { Result } from '@praha/byethrow';
|
|
24
25
|
*
|
|
25
|
-
* const result
|
|
26
|
+
* const result = Result.fail('error');
|
|
26
27
|
* Result.assertSuccess(result); // throws Error
|
|
27
28
|
* ```
|
|
28
29
|
*
|
|
@@ -30,9 +31,9 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
|
30
31
|
* ```ts
|
|
31
32
|
* import { Result } from '@praha/byethrow';
|
|
32
33
|
*
|
|
33
|
-
* const
|
|
34
|
+
* const getResult = (): Result.Result<number, string> => Result.succeed(42);
|
|
34
35
|
* const value = Result.pipe(
|
|
35
|
-
*
|
|
36
|
+
* getResult(),
|
|
36
37
|
* Result.orElse(() => Result.succeed('fallback')),
|
|
37
38
|
* Result.assertSuccess,
|
|
38
39
|
* Result.unwrap(), // Safe unwrap after assertion
|
|
@@ -28,10 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
28
28
|
});
|
|
29
29
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
|
30
30
|
const fail = (...args)=>{
|
|
31
|
-
|
|
32
|
-
if (void 0 === error) return {
|
|
31
|
+
if (args.length <= 0) return {
|
|
33
32
|
type: 'Failure'
|
|
34
33
|
};
|
|
34
|
+
const error = args[0];
|
|
35
35
|
if ((0, is_promise_cjs_namespaceObject.isPromise)(error)) return error.then((error)=>({
|
|
36
36
|
type: 'Failure',
|
|
37
37
|
error: error
|
|
@@ -21,6 +21,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
21
21
|
*
|
|
22
22
|
* @example Failure Case (function returns a Success)
|
|
23
23
|
* ```ts
|
|
24
|
+
* import { Result } from '@praha/byethrow';
|
|
25
|
+
*
|
|
24
26
|
* const result = Result.pipe(
|
|
25
27
|
* Result.fail('original error'),
|
|
26
28
|
* Result.orElse((error) => Result.succeed('default value')),
|
|
@@ -30,6 +32,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
30
32
|
*
|
|
31
33
|
* @example Failure Case (function returns a Failure)
|
|
32
34
|
* ```ts
|
|
35
|
+
* import { Result } from '@praha/byethrow';
|
|
36
|
+
*
|
|
33
37
|
* const result = Result.pipe(
|
|
34
38
|
* Result.fail('original error'),
|
|
35
39
|
* Result.orElse((error) => Result.fail('new error: ' + error)),
|
|
@@ -28,10 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
28
28
|
});
|
|
29
29
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
|
30
30
|
const succeed = (...args)=>{
|
|
31
|
-
|
|
32
|
-
if (void 0 === value) return {
|
|
31
|
+
if (args.length <= 0) return {
|
|
33
32
|
type: 'Success'
|
|
34
33
|
};
|
|
34
|
+
const value = args[0];
|
|
35
35
|
if ((0, is_promise_cjs_namespaceObject.isPromise)(value)) return value.then((value)=>({
|
|
36
36
|
type: 'Success',
|
|
37
37
|
value: value
|
|
@@ -30,11 +30,7 @@ const external_fail_cjs_namespaceObject = require("./fail.cjs");
|
|
|
30
30
|
const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
|
|
31
31
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
|
32
32
|
const try_ = (options)=>{
|
|
33
|
-
|
|
34
|
-
if ('safe' in options && options.safe) return (0, external_succeed_cjs_namespaceObject.succeed)(options.try);
|
|
35
|
-
return options.try.then((value)=>(0, external_succeed_cjs_namespaceObject.succeed)(value), (error)=>(0, external_fail_cjs_namespaceObject.fail)(options.catch(error)));
|
|
36
|
-
}
|
|
37
|
-
return (...args)=>{
|
|
33
|
+
const fn = (...args)=>{
|
|
38
34
|
try {
|
|
39
35
|
const output = options.try(...args);
|
|
40
36
|
if ((0, is_promise_cjs_namespaceObject.isPromise)(output)) {
|
|
@@ -48,6 +44,8 @@ const try_ = (options)=>{
|
|
|
48
44
|
return (0, external_fail_cjs_namespaceObject.fail)(options.catch(error));
|
|
49
45
|
}
|
|
50
46
|
};
|
|
47
|
+
if ('immediate' in options && options.immediate) return fn();
|
|
48
|
+
return fn;
|
|
51
49
|
};
|
|
52
50
|
exports["try"] = __webpack_exports__["try"];
|
|
53
51
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
@@ -10,58 +10,64 @@ import type { Result, ResultAsync } from '../result';
|
|
|
10
10
|
* @typeParam T - The function type to execute (sync or async) or a Promise type.
|
|
11
11
|
* @typeParam E - The error type to return if `catch` is used.
|
|
12
12
|
*
|
|
13
|
-
* @example
|
|
13
|
+
* @example Sync try-catch
|
|
14
14
|
* ```ts
|
|
15
15
|
* import { Result } from '@praha/byethrow';
|
|
16
16
|
*
|
|
17
|
-
* const
|
|
18
|
-
* try:
|
|
19
|
-
*
|
|
17
|
+
* const fn = Result.try({
|
|
18
|
+
* try: (x: number) => {
|
|
19
|
+
* if (x < 0) throw new Error('Negative!');
|
|
20
|
+
* return x * 2;
|
|
21
|
+
* },
|
|
22
|
+
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
20
23
|
* });
|
|
21
24
|
*
|
|
22
|
-
*
|
|
25
|
+
* const result = fn(5); // Result.Result<number, Error>
|
|
23
26
|
* ```
|
|
24
27
|
*
|
|
25
|
-
* @example
|
|
28
|
+
* @example Sync try-catch with immediate execution
|
|
26
29
|
* ```ts
|
|
27
30
|
* import { Result } from '@praha/byethrow';
|
|
28
31
|
*
|
|
29
32
|
* const result = Result.try({
|
|
30
|
-
*
|
|
31
|
-
* try:
|
|
33
|
+
* immediate: true,
|
|
34
|
+
* try: () => {
|
|
35
|
+
* const x = Math.random() * 10 - 5;
|
|
36
|
+
* if (x < 0) throw new Error('Negative!');
|
|
37
|
+
* return x * 2;
|
|
38
|
+
* },
|
|
39
|
+
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
32
40
|
* });
|
|
33
41
|
*
|
|
34
|
-
* // result is
|
|
42
|
+
* // result is Result<number, Error>
|
|
35
43
|
* ```
|
|
36
44
|
*
|
|
37
|
-
* @example Sync
|
|
45
|
+
* @example Sync safe
|
|
38
46
|
* ```ts
|
|
39
47
|
* import { Result } from '@praha/byethrow';
|
|
40
48
|
*
|
|
41
49
|
* const fn = Result.try({
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* return x * 2;
|
|
45
|
-
* },
|
|
46
|
-
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
50
|
+
* safe: true,
|
|
51
|
+
* try: (x: number) => x + 1,
|
|
47
52
|
* });
|
|
48
53
|
*
|
|
49
|
-
* const result = fn(
|
|
54
|
+
* const result = fn(1); // Result.Result<number, never>
|
|
50
55
|
* ```
|
|
51
56
|
*
|
|
52
|
-
* @example Sync
|
|
57
|
+
* @example Sync safe with immediate execution
|
|
53
58
|
* ```ts
|
|
54
59
|
* import { Result } from '@praha/byethrow';
|
|
55
60
|
*
|
|
56
|
-
* const
|
|
61
|
+
* const result = Result.try({
|
|
57
62
|
* safe: true,
|
|
58
|
-
*
|
|
63
|
+
* immediate: true,
|
|
64
|
+
* try: () => Math.random() + 1,
|
|
59
65
|
* });
|
|
60
66
|
*
|
|
61
|
-
*
|
|
67
|
+
* // result is Result<number, never>
|
|
62
68
|
* ```
|
|
63
69
|
*
|
|
64
|
-
* @example Async
|
|
70
|
+
* @example Async try-catch
|
|
65
71
|
* ```ts
|
|
66
72
|
* import { Result } from '@praha/byethrow';
|
|
67
73
|
*
|
|
@@ -73,7 +79,20 @@ import type { Result, ResultAsync } from '../result';
|
|
|
73
79
|
* const result = await fn('abc'); // Result.ResultAsync<Response, Error>
|
|
74
80
|
* ```
|
|
75
81
|
*
|
|
76
|
-
* @example Async
|
|
82
|
+
* @example Async try-catch with immediate execution
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { Result } from '@praha/byethrow';
|
|
85
|
+
*
|
|
86
|
+
* const result = Result.try({
|
|
87
|
+
* immediate: true,
|
|
88
|
+
* try: () => fetch('/api/data'),
|
|
89
|
+
* catch: (error) => new Error('Fetch failed', { cause: error }),
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* // result is ResultAsync<Response, Error>
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example Async safe
|
|
77
96
|
* ```ts
|
|
78
97
|
* import { Result } from '@praha/byethrow';
|
|
79
98
|
*
|
|
@@ -85,32 +104,57 @@ import type { Result, ResultAsync } from '../result';
|
|
|
85
104
|
* const result = await fn(); // Result.ResultAsync<string, never>
|
|
86
105
|
* ```
|
|
87
106
|
*
|
|
107
|
+
* @example Async safe with immediate execution
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { Result } from '@praha/byethrow';
|
|
110
|
+
*
|
|
111
|
+
* const result = Result.try({
|
|
112
|
+
* safe: true,
|
|
113
|
+
* immediate: true,
|
|
114
|
+
* try: () => Promise.resolve('ok'),
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* // result is ResultAsync<string, never>
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
88
120
|
* @category Creators
|
|
89
121
|
*/
|
|
90
122
|
declare const try_: {
|
|
91
|
-
<T extends Promise<any>, E>(options: {
|
|
92
|
-
try: T;
|
|
93
|
-
catch: (error: unknown) => E;
|
|
94
|
-
}): ResultAsync<Awaited<T>, E>;
|
|
95
|
-
<T extends Promise<any>>(options: {
|
|
96
|
-
try: T;
|
|
97
|
-
safe: true;
|
|
98
|
-
}): ResultAsync<Awaited<T>, never>;
|
|
99
123
|
<T extends (...args: readonly any[]) => Promise<any>, E>(options: {
|
|
100
124
|
try: T;
|
|
101
125
|
catch: (error: unknown) => E;
|
|
102
126
|
}): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, E>;
|
|
103
|
-
<T extends (
|
|
127
|
+
<T extends () => Promise<any>, E>(options: {
|
|
128
|
+
immediate: true;
|
|
104
129
|
try: T;
|
|
130
|
+
catch: (error: unknown) => E;
|
|
131
|
+
}): ResultAsync<Awaited<ReturnType<T>>, E>;
|
|
132
|
+
<T extends (...args: readonly any[]) => Promise<any>>(options: {
|
|
105
133
|
safe: true;
|
|
134
|
+
try: T;
|
|
106
135
|
}): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, never>;
|
|
136
|
+
<T extends () => Promise<any>>(options: {
|
|
137
|
+
safe: true;
|
|
138
|
+
immediate: true;
|
|
139
|
+
try: T;
|
|
140
|
+
}): ResultAsync<Awaited<ReturnType<T>>, never>;
|
|
107
141
|
<T extends (...args: readonly any[]) => any, E>(options: {
|
|
108
142
|
try: T;
|
|
109
143
|
catch: (error: unknown) => E;
|
|
110
144
|
}): (...args: Parameters<T>) => Result<ReturnType<T>, E>;
|
|
111
|
-
<T extends (
|
|
145
|
+
<T extends () => any, E>(options: {
|
|
146
|
+
immediate: true;
|
|
112
147
|
try: T;
|
|
148
|
+
catch: (error: unknown) => E;
|
|
149
|
+
}): Result<ReturnType<T>, E>;
|
|
150
|
+
<T extends (...args: readonly any[]) => any>(options: {
|
|
113
151
|
safe: true;
|
|
152
|
+
try: T;
|
|
114
153
|
}): (...args: Parameters<T>) => Result<ReturnType<T>, never>;
|
|
154
|
+
<T extends () => any>(options: {
|
|
155
|
+
safe: true;
|
|
156
|
+
immediate: true;
|
|
157
|
+
try: T;
|
|
158
|
+
}): Result<ReturnType<T>, never>;
|
|
115
159
|
};
|
|
116
160
|
export { try_ as try };
|
package/dist/cjs/result.d.ts
CHANGED
|
@@ -158,11 +158,7 @@ export type ResultFor<R, T, E> = true extends HasPromise<R> ? ResultAsync<T, E>
|
|
|
158
158
|
*
|
|
159
159
|
* @category Infer Types
|
|
160
160
|
*/
|
|
161
|
-
export type InferSuccess<T> = [
|
|
162
|
-
T
|
|
163
|
-
] extends [(...args: any[]) => ResultMaybeAsync<infer U, any>] ? U : [
|
|
164
|
-
T
|
|
165
|
-
] extends [ResultMaybeAsync<infer U, any>] ? U : never;
|
|
161
|
+
export type InferSuccess<T> = [T] extends [(...args: any[]) => ResultMaybeAsync<infer U, any>] ? U : [T] extends [ResultMaybeAsync<infer U, any>] ? U : never;
|
|
166
162
|
/**
|
|
167
163
|
* Infers the {@link Failure} value type `E` from a Result or a function returning a Result.
|
|
168
164
|
*
|
|
@@ -186,8 +182,4 @@ export type InferSuccess<T> = [
|
|
|
186
182
|
*
|
|
187
183
|
* @category Infer Types
|
|
188
184
|
*/
|
|
189
|
-
export type InferFailure<T> = [
|
|
190
|
-
T
|
|
191
|
-
] extends [(...args: any[]) => ResultMaybeAsync<any, infer U>] ? U : [
|
|
192
|
-
T
|
|
193
|
-
] extends [ResultMaybeAsync<any, infer U>] ? U : never;
|
|
185
|
+
export type InferFailure<T> = [T] extends [(...args: any[]) => ResultMaybeAsync<any, infer U>] ? U : [T] extends [ResultMaybeAsync<any, infer U>] ? U : never;
|
|
@@ -21,6 +21,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
21
21
|
*
|
|
22
22
|
* @example Failure Case (input is a Failure)
|
|
23
23
|
* ```ts
|
|
24
|
+
* import { Result } from '@praha/byethrow';
|
|
25
|
+
*
|
|
24
26
|
* const result = Result.pipe(
|
|
25
27
|
* Result.fail('error'),
|
|
26
28
|
* Result.andThen((x) => Result.succeed(x * 2)),
|
|
@@ -30,6 +32,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
30
32
|
*
|
|
31
33
|
* @example Failure Case (function returns a Failure)
|
|
32
34
|
* ```ts
|
|
35
|
+
* import { Result } from '@praha/byethrow';
|
|
36
|
+
*
|
|
33
37
|
* const result = Result.pipe(
|
|
34
38
|
* Result.succeed(3),
|
|
35
39
|
* Result.andThen((x) => Result.fail('error: ' + x)),
|
|
@@ -28,7 +28,7 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
28
28
|
* import { Result } from '@praha/byethrow';
|
|
29
29
|
*
|
|
30
30
|
* const result = Result.pipe(
|
|
31
|
-
* Result.fail('error),
|
|
31
|
+
* Result.fail('error'),
|
|
32
32
|
* Result.andThrough((x) => {
|
|
33
33
|
* return x > 0 ? Result.succeed(null) : Result.fail('Must be > 0');
|
|
34
34
|
* }),
|
|
@@ -13,16 +13,17 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
|
13
13
|
* ```ts
|
|
14
14
|
* import { Result } from '@praha/byethrow';
|
|
15
15
|
*
|
|
16
|
-
* const result
|
|
16
|
+
* const result = Result.fail('error');
|
|
17
17
|
* const failure = Result.assertFailure(result);
|
|
18
18
|
* // failure: { type: 'Failure', error: 'error' }
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @example Throws on Success
|
|
22
22
|
* ```ts
|
|
23
|
+
* // @errors: 2769
|
|
23
24
|
* import { Result } from '@praha/byethrow';
|
|
24
25
|
*
|
|
25
|
-
* const result
|
|
26
|
+
* const result = Result.succeed(42);
|
|
26
27
|
* Result.assertFailure(result); // throws Error
|
|
27
28
|
* ```
|
|
28
29
|
*
|
|
@@ -30,9 +31,9 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
|
30
31
|
* ```ts
|
|
31
32
|
* import { Result } from '@praha/byethrow';
|
|
32
33
|
*
|
|
33
|
-
* const
|
|
34
|
+
* const getResult = (): Result.Result<number, string> => Result.fail('error');
|
|
34
35
|
* const value = Result.pipe(
|
|
35
|
-
*
|
|
36
|
+
* getResult(),
|
|
36
37
|
* Result.andThen(() => Result.fail('die')),
|
|
37
38
|
* Result.assertFailure,
|
|
38
39
|
* Result.unwrapError(), // Safe unwrap after assertion
|
|
@@ -13,16 +13,17 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
|
13
13
|
* ```ts
|
|
14
14
|
* import { Result } from '@praha/byethrow';
|
|
15
15
|
*
|
|
16
|
-
* const result
|
|
16
|
+
* const result = Result.succeed(42);
|
|
17
17
|
* const success = Result.assertSuccess(result);
|
|
18
18
|
* // success: { type: 'Success', value: 42 }
|
|
19
19
|
* ```
|
|
20
20
|
*
|
|
21
21
|
* @example Throws on Failure
|
|
22
22
|
* ```ts
|
|
23
|
+
* // @errors: 2769
|
|
23
24
|
* import { Result } from '@praha/byethrow';
|
|
24
25
|
*
|
|
25
|
-
* const result
|
|
26
|
+
* const result = Result.fail('error');
|
|
26
27
|
* Result.assertSuccess(result); // throws Error
|
|
27
28
|
* ```
|
|
28
29
|
*
|
|
@@ -30,9 +31,9 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
|
30
31
|
* ```ts
|
|
31
32
|
* import { Result } from '@praha/byethrow';
|
|
32
33
|
*
|
|
33
|
-
* const
|
|
34
|
+
* const getResult = (): Result.Result<number, string> => Result.succeed(42);
|
|
34
35
|
* const value = Result.pipe(
|
|
35
|
-
*
|
|
36
|
+
* getResult(),
|
|
36
37
|
* Result.orElse(() => Result.succeed('fallback')),
|
|
37
38
|
* Result.assertSuccess,
|
|
38
39
|
* Result.unwrap(), // Safe unwrap after assertion
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
|
2
2
|
const fail = (...args)=>{
|
|
3
|
-
|
|
4
|
-
if (void 0 === error) return {
|
|
3
|
+
if (args.length <= 0) return {
|
|
5
4
|
type: 'Failure'
|
|
6
5
|
};
|
|
6
|
+
const error = args[0];
|
|
7
7
|
if (isPromise(error)) return error.then((error)=>({
|
|
8
8
|
type: 'Failure',
|
|
9
9
|
error: error
|
|
@@ -21,6 +21,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
21
21
|
*
|
|
22
22
|
* @example Failure Case (function returns a Success)
|
|
23
23
|
* ```ts
|
|
24
|
+
* import { Result } from '@praha/byethrow';
|
|
25
|
+
*
|
|
24
26
|
* const result = Result.pipe(
|
|
25
27
|
* Result.fail('original error'),
|
|
26
28
|
* Result.orElse((error) => Result.succeed('default value')),
|
|
@@ -30,6 +32,8 @@ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '..
|
|
|
30
32
|
*
|
|
31
33
|
* @example Failure Case (function returns a Failure)
|
|
32
34
|
* ```ts
|
|
35
|
+
* import { Result } from '@praha/byethrow';
|
|
36
|
+
*
|
|
33
37
|
* const result = Result.pipe(
|
|
34
38
|
* Result.fail('original error'),
|
|
35
39
|
* Result.orElse((error) => Result.fail('new error: ' + error)),
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
|
2
2
|
const succeed = (...args)=>{
|
|
3
|
-
|
|
4
|
-
if (void 0 === value) return {
|
|
3
|
+
if (args.length <= 0) return {
|
|
5
4
|
type: 'Success'
|
|
6
5
|
};
|
|
6
|
+
const value = args[0];
|
|
7
7
|
if (isPromise(value)) return value.then((value)=>({
|
|
8
8
|
type: 'Success',
|
|
9
9
|
value: value
|
|
@@ -10,58 +10,64 @@ import type { Result, ResultAsync } from '../result';
|
|
|
10
10
|
* @typeParam T - The function type to execute (sync or async) or a Promise type.
|
|
11
11
|
* @typeParam E - The error type to return if `catch` is used.
|
|
12
12
|
*
|
|
13
|
-
* @example
|
|
13
|
+
* @example Sync try-catch
|
|
14
14
|
* ```ts
|
|
15
15
|
* import { Result } from '@praha/byethrow';
|
|
16
16
|
*
|
|
17
|
-
* const
|
|
18
|
-
* try:
|
|
19
|
-
*
|
|
17
|
+
* const fn = Result.try({
|
|
18
|
+
* try: (x: number) => {
|
|
19
|
+
* if (x < 0) throw new Error('Negative!');
|
|
20
|
+
* return x * 2;
|
|
21
|
+
* },
|
|
22
|
+
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
20
23
|
* });
|
|
21
24
|
*
|
|
22
|
-
*
|
|
25
|
+
* const result = fn(5); // Result.Result<number, Error>
|
|
23
26
|
* ```
|
|
24
27
|
*
|
|
25
|
-
* @example
|
|
28
|
+
* @example Sync try-catch with immediate execution
|
|
26
29
|
* ```ts
|
|
27
30
|
* import { Result } from '@praha/byethrow';
|
|
28
31
|
*
|
|
29
32
|
* const result = Result.try({
|
|
30
|
-
*
|
|
31
|
-
* try:
|
|
33
|
+
* immediate: true,
|
|
34
|
+
* try: () => {
|
|
35
|
+
* const x = Math.random() * 10 - 5;
|
|
36
|
+
* if (x < 0) throw new Error('Negative!');
|
|
37
|
+
* return x * 2;
|
|
38
|
+
* },
|
|
39
|
+
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
32
40
|
* });
|
|
33
41
|
*
|
|
34
|
-
* // result is
|
|
42
|
+
* // result is Result<number, Error>
|
|
35
43
|
* ```
|
|
36
44
|
*
|
|
37
|
-
* @example Sync
|
|
45
|
+
* @example Sync safe
|
|
38
46
|
* ```ts
|
|
39
47
|
* import { Result } from '@praha/byethrow';
|
|
40
48
|
*
|
|
41
49
|
* const fn = Result.try({
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* return x * 2;
|
|
45
|
-
* },
|
|
46
|
-
* catch: (error) => new Error('Oops!', { cause: error }),
|
|
50
|
+
* safe: true,
|
|
51
|
+
* try: (x: number) => x + 1,
|
|
47
52
|
* });
|
|
48
53
|
*
|
|
49
|
-
* const result = fn(
|
|
54
|
+
* const result = fn(1); // Result.Result<number, never>
|
|
50
55
|
* ```
|
|
51
56
|
*
|
|
52
|
-
* @example Sync
|
|
57
|
+
* @example Sync safe with immediate execution
|
|
53
58
|
* ```ts
|
|
54
59
|
* import { Result } from '@praha/byethrow';
|
|
55
60
|
*
|
|
56
|
-
* const
|
|
61
|
+
* const result = Result.try({
|
|
57
62
|
* safe: true,
|
|
58
|
-
*
|
|
63
|
+
* immediate: true,
|
|
64
|
+
* try: () => Math.random() + 1,
|
|
59
65
|
* });
|
|
60
66
|
*
|
|
61
|
-
*
|
|
67
|
+
* // result is Result<number, never>
|
|
62
68
|
* ```
|
|
63
69
|
*
|
|
64
|
-
* @example Async
|
|
70
|
+
* @example Async try-catch
|
|
65
71
|
* ```ts
|
|
66
72
|
* import { Result } from '@praha/byethrow';
|
|
67
73
|
*
|
|
@@ -73,7 +79,20 @@ import type { Result, ResultAsync } from '../result';
|
|
|
73
79
|
* const result = await fn('abc'); // Result.ResultAsync<Response, Error>
|
|
74
80
|
* ```
|
|
75
81
|
*
|
|
76
|
-
* @example Async
|
|
82
|
+
* @example Async try-catch with immediate execution
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { Result } from '@praha/byethrow';
|
|
85
|
+
*
|
|
86
|
+
* const result = Result.try({
|
|
87
|
+
* immediate: true,
|
|
88
|
+
* try: () => fetch('/api/data'),
|
|
89
|
+
* catch: (error) => new Error('Fetch failed', { cause: error }),
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* // result is ResultAsync<Response, Error>
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example Async safe
|
|
77
96
|
* ```ts
|
|
78
97
|
* import { Result } from '@praha/byethrow';
|
|
79
98
|
*
|
|
@@ -85,32 +104,57 @@ import type { Result, ResultAsync } from '../result';
|
|
|
85
104
|
* const result = await fn(); // Result.ResultAsync<string, never>
|
|
86
105
|
* ```
|
|
87
106
|
*
|
|
107
|
+
* @example Async safe with immediate execution
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { Result } from '@praha/byethrow';
|
|
110
|
+
*
|
|
111
|
+
* const result = Result.try({
|
|
112
|
+
* safe: true,
|
|
113
|
+
* immediate: true,
|
|
114
|
+
* try: () => Promise.resolve('ok'),
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* // result is ResultAsync<string, never>
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
88
120
|
* @category Creators
|
|
89
121
|
*/
|
|
90
122
|
declare const try_: {
|
|
91
|
-
<T extends Promise<any>, E>(options: {
|
|
92
|
-
try: T;
|
|
93
|
-
catch: (error: unknown) => E;
|
|
94
|
-
}): ResultAsync<Awaited<T>, E>;
|
|
95
|
-
<T extends Promise<any>>(options: {
|
|
96
|
-
try: T;
|
|
97
|
-
safe: true;
|
|
98
|
-
}): ResultAsync<Awaited<T>, never>;
|
|
99
123
|
<T extends (...args: readonly any[]) => Promise<any>, E>(options: {
|
|
100
124
|
try: T;
|
|
101
125
|
catch: (error: unknown) => E;
|
|
102
126
|
}): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, E>;
|
|
103
|
-
<T extends (
|
|
127
|
+
<T extends () => Promise<any>, E>(options: {
|
|
128
|
+
immediate: true;
|
|
104
129
|
try: T;
|
|
130
|
+
catch: (error: unknown) => E;
|
|
131
|
+
}): ResultAsync<Awaited<ReturnType<T>>, E>;
|
|
132
|
+
<T extends (...args: readonly any[]) => Promise<any>>(options: {
|
|
105
133
|
safe: true;
|
|
134
|
+
try: T;
|
|
106
135
|
}): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, never>;
|
|
136
|
+
<T extends () => Promise<any>>(options: {
|
|
137
|
+
safe: true;
|
|
138
|
+
immediate: true;
|
|
139
|
+
try: T;
|
|
140
|
+
}): ResultAsync<Awaited<ReturnType<T>>, never>;
|
|
107
141
|
<T extends (...args: readonly any[]) => any, E>(options: {
|
|
108
142
|
try: T;
|
|
109
143
|
catch: (error: unknown) => E;
|
|
110
144
|
}): (...args: Parameters<T>) => Result<ReturnType<T>, E>;
|
|
111
|
-
<T extends (
|
|
145
|
+
<T extends () => any, E>(options: {
|
|
146
|
+
immediate: true;
|
|
112
147
|
try: T;
|
|
148
|
+
catch: (error: unknown) => E;
|
|
149
|
+
}): Result<ReturnType<T>, E>;
|
|
150
|
+
<T extends (...args: readonly any[]) => any>(options: {
|
|
113
151
|
safe: true;
|
|
152
|
+
try: T;
|
|
114
153
|
}): (...args: Parameters<T>) => Result<ReturnType<T>, never>;
|
|
154
|
+
<T extends () => any>(options: {
|
|
155
|
+
safe: true;
|
|
156
|
+
immediate: true;
|
|
157
|
+
try: T;
|
|
158
|
+
}): Result<ReturnType<T>, never>;
|
|
115
159
|
};
|
|
116
160
|
export { try_ as try };
|
|
@@ -2,11 +2,7 @@ import { fail } from "./fail.js";
|
|
|
2
2
|
import { succeed } from "./succeed.js";
|
|
3
3
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
|
4
4
|
const try_ = (options)=>{
|
|
5
|
-
|
|
6
|
-
if ('safe' in options && options.safe) return succeed(options.try);
|
|
7
|
-
return options.try.then((value)=>succeed(value), (error)=>fail(options.catch(error)));
|
|
8
|
-
}
|
|
9
|
-
return (...args)=>{
|
|
5
|
+
const fn = (...args)=>{
|
|
10
6
|
try {
|
|
11
7
|
const output = options.try(...args);
|
|
12
8
|
if (isPromise(output)) {
|
|
@@ -20,5 +16,7 @@ const try_ = (options)=>{
|
|
|
20
16
|
return fail(options.catch(error));
|
|
21
17
|
}
|
|
22
18
|
};
|
|
19
|
+
if ('immediate' in options && options.immediate) return fn();
|
|
20
|
+
return fn;
|
|
23
21
|
};
|
|
24
22
|
export { try_ as try };
|
package/dist/esm/result.d.ts
CHANGED
|
@@ -158,11 +158,7 @@ export type ResultFor<R, T, E> = true extends HasPromise<R> ? ResultAsync<T, E>
|
|
|
158
158
|
*
|
|
159
159
|
* @category Infer Types
|
|
160
160
|
*/
|
|
161
|
-
export type InferSuccess<T> = [
|
|
162
|
-
T
|
|
163
|
-
] extends [(...args: any[]) => ResultMaybeAsync<infer U, any>] ? U : [
|
|
164
|
-
T
|
|
165
|
-
] extends [ResultMaybeAsync<infer U, any>] ? U : never;
|
|
161
|
+
export type InferSuccess<T> = [T] extends [(...args: any[]) => ResultMaybeAsync<infer U, any>] ? U : [T] extends [ResultMaybeAsync<infer U, any>] ? U : never;
|
|
166
162
|
/**
|
|
167
163
|
* Infers the {@link Failure} value type `E` from a Result or a function returning a Result.
|
|
168
164
|
*
|
|
@@ -186,8 +182,4 @@ export type InferSuccess<T> = [
|
|
|
186
182
|
*
|
|
187
183
|
* @category Infer Types
|
|
188
184
|
*/
|
|
189
|
-
export type InferFailure<T> = [
|
|
190
|
-
T
|
|
191
|
-
] extends [(...args: any[]) => ResultMaybeAsync<any, infer U>] ? U : [
|
|
192
|
-
T
|
|
193
|
-
] extends [ResultMaybeAsync<any, infer U>] ? U : never;
|
|
185
|
+
export type InferFailure<T> = [T] extends [(...args: any[]) => ResultMaybeAsync<any, infer U>] ? U : [T] extends [ResultMaybeAsync<any, infer U>] ? U : never;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@praha/byethrow",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
@@ -44,10 +44,11 @@
|
|
|
44
44
|
"@standard-schema/spec": "^1.0.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@rslib/core": "0.
|
|
48
|
-
"eslint": "9.
|
|
49
|
-
"typedoc": "0.28.
|
|
50
|
-
"
|
|
47
|
+
"@rslib/core": "0.13.0",
|
|
48
|
+
"eslint": "9.35.0",
|
|
49
|
+
"typedoc": "0.28.12",
|
|
50
|
+
"typedoc-plugin-markdown": "4.8.1",
|
|
51
|
+
"typescript": "5.9.2",
|
|
51
52
|
"vitest": "3.2.4"
|
|
52
53
|
},
|
|
53
54
|
"publishConfig": {
|