@praha/byethrow 0.6.3 → 0.7.1
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/dist/cjs/exports.d.ts +23 -23
- package/dist/cjs/functions/and-then.d.ts +5 -1
- package/dist/cjs/functions/and-through.d.ts +2 -2
- package/dist/cjs/functions/assert-failure.d.ts +6 -5
- package/dist/cjs/functions/assert-success.d.ts +6 -5
- package/dist/cjs/functions/bind.d.ts +1 -1
- package/dist/cjs/functions/combine.d.ts +1 -1
- package/dist/cjs/functions/do.d.ts +1 -1
- package/dist/cjs/functions/fail.d.ts +1 -1
- package/dist/cjs/functions/inspect-error.d.ts +1 -1
- package/dist/cjs/functions/inspect.d.ts +1 -1
- package/dist/cjs/functions/is-failure.d.ts +1 -1
- package/dist/cjs/functions/is-result.d.ts +1 -1
- package/dist/cjs/functions/is-success.d.ts +1 -1
- package/dist/cjs/functions/map-error.d.ts +1 -1
- package/dist/cjs/functions/map.d.ts +1 -1
- package/dist/cjs/functions/or-else.d.ts +5 -1
- package/dist/cjs/functions/parse.d.ts +1 -1
- package/dist/cjs/functions/succeed.d.ts +1 -1
- package/dist/cjs/functions/try.cjs +3 -5
- package/dist/cjs/functions/try.d.ts +77 -33
- package/dist/cjs/functions/unwrap-error.d.ts +2 -2
- package/dist/cjs/functions/unwrap.d.ts +2 -2
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/result.d.ts +1 -1
- package/dist/esm/exports.d.ts +23 -23
- package/dist/esm/functions/and-then.d.ts +5 -1
- package/dist/esm/functions/and-through.d.ts +2 -2
- package/dist/esm/functions/assert-failure.d.ts +6 -5
- package/dist/esm/functions/assert-success.d.ts +6 -5
- package/dist/esm/functions/bind.d.ts +1 -1
- package/dist/esm/functions/combine.d.ts +1 -1
- package/dist/esm/functions/do.d.ts +1 -1
- package/dist/esm/functions/fail.d.ts +1 -1
- package/dist/esm/functions/inspect-error.d.ts +1 -1
- package/dist/esm/functions/inspect.d.ts +1 -1
- package/dist/esm/functions/is-failure.d.ts +1 -1
- package/dist/esm/functions/is-result.d.ts +1 -1
- package/dist/esm/functions/is-success.d.ts +1 -1
- package/dist/esm/functions/map-error.d.ts +1 -1
- package/dist/esm/functions/map.d.ts +1 -1
- package/dist/esm/functions/or-else.d.ts +5 -1
- package/dist/esm/functions/parse.d.ts +1 -1
- package/dist/esm/functions/succeed.d.ts +1 -1
- package/dist/esm/functions/try.d.ts +77 -33
- package/dist/esm/functions/try.js +3 -5
- package/dist/esm/functions/unwrap-error.d.ts +2 -2
- package/dist/esm/functions/unwrap.d.ts +2 -2
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/result.d.ts +1 -1
- package/package.json +6 -6
package/dist/cjs/exports.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
export * from './result';
|
|
2
|
-
export * from './functions/and-then';
|
|
3
|
-
export * from './functions/and-through';
|
|
4
|
-
export * from './functions/assert-failure';
|
|
5
|
-
export * from './functions/assert-success';
|
|
6
|
-
export * from './functions/bind';
|
|
7
|
-
export * from './functions/combine';
|
|
8
|
-
export * from './functions/do';
|
|
9
|
-
export * from './functions/fail';
|
|
10
|
-
export * from './functions/inspect';
|
|
11
|
-
export * from './functions/inspect-error';
|
|
12
|
-
export * from './functions/is-failure';
|
|
13
|
-
export * from './functions/is-result';
|
|
14
|
-
export * from './functions/is-success';
|
|
15
|
-
export * from './functions/map';
|
|
16
|
-
export * from './functions/map-error';
|
|
17
|
-
export * from './functions/or-else';
|
|
18
|
-
export * from './functions/parse';
|
|
19
|
-
export * from './functions/pipe';
|
|
20
|
-
export * from './functions/succeed';
|
|
21
|
-
export * from './functions/try';
|
|
22
|
-
export * from './functions/unwrap';
|
|
23
|
-
export * from './functions/unwrap-error';
|
|
1
|
+
export * from './result.js';
|
|
2
|
+
export * from './functions/and-then.js';
|
|
3
|
+
export * from './functions/and-through.js';
|
|
4
|
+
export * from './functions/assert-failure.js';
|
|
5
|
+
export * from './functions/assert-success.js';
|
|
6
|
+
export * from './functions/bind.js';
|
|
7
|
+
export * from './functions/combine.js';
|
|
8
|
+
export * from './functions/do.js';
|
|
9
|
+
export * from './functions/fail.js';
|
|
10
|
+
export * from './functions/inspect.js';
|
|
11
|
+
export * from './functions/inspect-error.js';
|
|
12
|
+
export * from './functions/is-failure.js';
|
|
13
|
+
export * from './functions/is-result.js';
|
|
14
|
+
export * from './functions/is-success.js';
|
|
15
|
+
export * from './functions/map.js';
|
|
16
|
+
export * from './functions/map-error.js';
|
|
17
|
+
export * from './functions/or-else.js';
|
|
18
|
+
export * from './functions/parse.js';
|
|
19
|
+
export * from './functions/pipe.js';
|
|
20
|
+
export * from './functions/succeed.js';
|
|
21
|
+
export * from './functions/try.js';
|
|
22
|
+
export * from './functions/unwrap.js';
|
|
23
|
+
export * from './functions/unwrap-error.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains the next computation using the success value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the original result is a {@link Failure}, it is returned unchanged.
|
|
@@ -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)),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Runs an additional computation using the success value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* but **returns the original result** if the additional computation is successful.
|
|
@@ -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
|
* }),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
1
|
+
import type { Failure, InferFailure, Result, ResultAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Failure} and returns it.
|
|
4
4
|
* If the result is a {@link Success}, throws an error.
|
|
@@ -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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
1
|
+
import type { InferSuccess, Result, ResultAsync, Success } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Success} and returns it.
|
|
4
4
|
* If the result is a {@link Failure}, throws an error.
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains another {@link Result}-producing computation and **merges its success value**
|
|
4
4
|
* into the existing object under the specified key.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Combines multiple {@link Result} or {@link ResultAsync} values into a single result.
|
|
4
4
|
* If all inputs are {@link Success}, returns a {@link Success} with combined values.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Executes a side effect function on the error value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* without modifying the original result. This is useful for debugging, logging, or performing
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Executes a side effect function on the success value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* without modifying the original result. This is useful for debugging, logging, or performing
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Applies a transformation function to the error value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the input is a {@link Success}, it will be returned unchanged.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Applies a transformation function to the success value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the input is a {@link Failure}, it will be returned unchanged.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains the next computation using the error value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the original result is a {@link Success}, it is returned unchanged.
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import type { Result } from '../result';
|
|
1
|
+
import type { Result } from '../result.js';
|
|
2
2
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
3
|
/**
|
|
4
4
|
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
|
@@ -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 === [
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Result, ResultAsync } from '../result';
|
|
1
|
+
import type { Result, ResultAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
|
|
4
4
|
* capturing errors and returning them in a structured way.
|
|
@@ -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 };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { HasPromise } from '../internals/types/has-promise';
|
|
2
|
-
import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { InferFailure, ResultMaybeAsync } from '../result.js';
|
|
3
3
|
/**
|
|
4
4
|
* Extracts the error value from a {@link Result} or {@link ResultAsync}.
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { HasPromise } from '../internals/types/has-promise';
|
|
2
|
-
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result.js';
|
|
3
3
|
/**
|
|
4
4
|
* Extracts the success value from a {@link Result} or {@link ResultAsync}.
|
|
5
5
|
*
|
package/dist/cjs/index.d.ts
CHANGED
package/dist/cjs/result.d.ts
CHANGED
package/dist/esm/exports.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
export * from './result';
|
|
2
|
-
export * from './functions/and-then';
|
|
3
|
-
export * from './functions/and-through';
|
|
4
|
-
export * from './functions/assert-failure';
|
|
5
|
-
export * from './functions/assert-success';
|
|
6
|
-
export * from './functions/bind';
|
|
7
|
-
export * from './functions/combine';
|
|
8
|
-
export * from './functions/do';
|
|
9
|
-
export * from './functions/fail';
|
|
10
|
-
export * from './functions/inspect';
|
|
11
|
-
export * from './functions/inspect-error';
|
|
12
|
-
export * from './functions/is-failure';
|
|
13
|
-
export * from './functions/is-result';
|
|
14
|
-
export * from './functions/is-success';
|
|
15
|
-
export * from './functions/map';
|
|
16
|
-
export * from './functions/map-error';
|
|
17
|
-
export * from './functions/or-else';
|
|
18
|
-
export * from './functions/parse';
|
|
19
|
-
export * from './functions/pipe';
|
|
20
|
-
export * from './functions/succeed';
|
|
21
|
-
export * from './functions/try';
|
|
22
|
-
export * from './functions/unwrap';
|
|
23
|
-
export * from './functions/unwrap-error';
|
|
1
|
+
export * from './result.js';
|
|
2
|
+
export * from './functions/and-then.js';
|
|
3
|
+
export * from './functions/and-through.js';
|
|
4
|
+
export * from './functions/assert-failure.js';
|
|
5
|
+
export * from './functions/assert-success.js';
|
|
6
|
+
export * from './functions/bind.js';
|
|
7
|
+
export * from './functions/combine.js';
|
|
8
|
+
export * from './functions/do.js';
|
|
9
|
+
export * from './functions/fail.js';
|
|
10
|
+
export * from './functions/inspect.js';
|
|
11
|
+
export * from './functions/inspect-error.js';
|
|
12
|
+
export * from './functions/is-failure.js';
|
|
13
|
+
export * from './functions/is-result.js';
|
|
14
|
+
export * from './functions/is-success.js';
|
|
15
|
+
export * from './functions/map.js';
|
|
16
|
+
export * from './functions/map-error.js';
|
|
17
|
+
export * from './functions/or-else.js';
|
|
18
|
+
export * from './functions/parse.js';
|
|
19
|
+
export * from './functions/pipe.js';
|
|
20
|
+
export * from './functions/succeed.js';
|
|
21
|
+
export * from './functions/try.js';
|
|
22
|
+
export * from './functions/unwrap.js';
|
|
23
|
+
export * from './functions/unwrap-error.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains the next computation using the success value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the original result is a {@link Failure}, it is returned unchanged.
|
|
@@ -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)),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Runs an additional computation using the success value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* but **returns the original result** if the additional computation is successful.
|
|
@@ -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
|
* }),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Failure, InferFailure, Result, ResultAsync } from '../result';
|
|
1
|
+
import type { Failure, InferFailure, Result, ResultAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Failure} and returns it.
|
|
4
4
|
* If the result is a {@link Success}, throws an error.
|
|
@@ -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
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferSuccess, Result, ResultAsync, Success } from '../result';
|
|
1
|
+
import type { InferSuccess, Result, ResultAsync, Success } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Success} and returns it.
|
|
4
4
|
* If the result is a {@link Failure}, throws an error.
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains another {@link Result}-producing computation and **merges its success value**
|
|
4
4
|
* into the existing object under the specified key.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Combines multiple {@link Result} or {@link ResultAsync} values into a single result.
|
|
4
4
|
* If all inputs are {@link Success}, returns a {@link Success} with combined values.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Executes a side effect function on the error value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* without modifying the original result. This is useful for debugging, logging, or performing
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Executes a side effect function on the success value of a {@link Result} or {@link ResultAsync},
|
|
4
4
|
* without modifying the original result. This is useful for debugging, logging, or performing
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Applies a transformation function to the error value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the input is a {@link Success}, it will be returned unchanged.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Applies a transformation function to the success value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the input is a {@link Failure}, it will be returned unchanged.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Chains the next computation using the error value of a {@link Result} or {@link ResultAsync}.
|
|
4
4
|
* If the original result is a {@link Success}, it is returned unchanged.
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import type { Result } from '../result';
|
|
1
|
+
import type { Result } from '../result.js';
|
|
2
2
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
3
|
/**
|
|
4
4
|
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Result, ResultAsync } from '../result';
|
|
1
|
+
import type { Result, ResultAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
3
|
* Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
|
|
4
4
|
* capturing errors and returning them in a structured way.
|
|
@@ -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 };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { HasPromise } from '../internals/types/has-promise';
|
|
2
|
-
import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { InferFailure, ResultMaybeAsync } from '../result.js';
|
|
3
3
|
/**
|
|
4
4
|
* Extracts the error value from a {@link Result} or {@link ResultAsync}.
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { HasPromise } from '../internals/types/has-promise';
|
|
2
|
-
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { InferSuccess, ResultMaybeAsync } from '../result.js';
|
|
3
3
|
/**
|
|
4
4
|
* Extracts the success value from a {@link Result} or {@link ResultAsync}.
|
|
5
5
|
*
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/result.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@praha/byethrow",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
@@ -44,11 +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
|
-
"typedoc-plugin-markdown": "4.
|
|
51
|
-
"typescript": "5.
|
|
47
|
+
"@rslib/core": "0.13.3",
|
|
48
|
+
"eslint": "9.36.0",
|
|
49
|
+
"typedoc": "0.28.13",
|
|
50
|
+
"typedoc-plugin-markdown": "4.9.0",
|
|
51
|
+
"typescript": "5.9.2",
|
|
52
52
|
"vitest": "3.2.4"
|
|
53
53
|
},
|
|
54
54
|
"publishConfig": {
|