@praha/byethrow 0.2.0 → 0.2.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.
@@ -28,6 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  });
29
29
  const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
30
30
  const unwrapError = (result)=>{
31
+ if (result instanceof Promise) return result.then((r)=>{
32
+ if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) throw r.value;
33
+ return r.error;
34
+ });
31
35
  if ((0, external_is_success_cjs_namespaceObject.isSuccess)(result)) throw result.value;
32
36
  return result.error;
33
37
  };
@@ -1,31 +1,49 @@
1
- import type { Result } from '../result';
1
+ import type { HasPromise } from '../internals/types/has-promise';
2
+ import type { InferFailure, ResultMaybeAsync } from '../result';
2
3
  /**
3
- * Extracts the error value from a {@link Result}.
4
+ * Extracts the error value from a {@link Result}, {@link ResultAsync}.
4
5
  *
5
6
  * If the result is a {@link Success}, it throws the success value (this is rare but symmetric to `unwrap`).
7
+ * For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
6
8
  *
7
9
  * @function
8
- * @typeParam E - The type of the error value.
9
- * @param result - The {@link Result} to unwrap the error from.
10
- * @returns The error value.
10
+ * @typeParam R - The type of the result to unwrap.
11
+ * @param result - The {@link Result}, {@link ResultAsync}.
12
+ * @returns The error value, or a Promise of the error value for async results.
11
13
  * @throws The success value if the result is a {@link Success}.
12
14
  *
13
15
  * @example Failure Case
14
16
  * ```ts
15
17
  * import { Result } from '@praha/byethrow';
16
18
  *
17
- * const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
18
- * const error = Result.unwrapError(result); // 'Something went wrong'
19
+ * const result: Result.Result<number, string> = Result.fail('Oops');
20
+ * const error = Result.unwrapError(result); // 'Oops'
19
21
  * ```
20
22
  *
21
23
  * @example Success Case
22
24
  * ```ts
23
25
  * import { Result } from '@praha/byethrow';
24
26
  *
25
- * const result: Result.Result<number, string> = { type: 'Success', value: 123 };
26
- * const error = Result.unwrapError(result); // throws 123
27
+ * const result: Result.Result<number, string> = Result.succeed(100);
28
+ * const error = Result.unwrapError(result); // throws 100
29
+ * ```
30
+ *
31
+ * @example Async Failure Case
32
+ * ```ts
33
+ * import { Result } from '@praha/byethrow';
34
+ *
35
+ * const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
36
+ * const error = Result.unwrapError(result); // 'Oops'
37
+ * ```
38
+ *
39
+ * @example Async Success Case
40
+ * ```ts
41
+ * import { Result } from '@praha/byethrow';
42
+ *
43
+ * const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
44
+ * const error = Result.unwrapError(result); // throws 100
27
45
  * ```
28
46
  *
29
47
  * @category Unwraps
30
48
  */
31
- export declare const unwrapError: <E>(result: Result<unknown, E>) => E;
49
+ export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
@@ -28,6 +28,10 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  });
29
29
  const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
30
30
  const unwrap = (result)=>{
31
+ if (result instanceof Promise) return result.then((r)=>{
32
+ if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) throw r.error;
33
+ return r.value;
34
+ });
31
35
  if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) throw result.error;
32
36
  return result.value;
33
37
  };
@@ -1,20 +1,22 @@
1
- import type { Result } from '../result';
1
+ import type { HasPromise } from '../internals/types/has-promise';
2
+ import type { InferSuccess, ResultMaybeAsync } from '../result';
2
3
  /**
3
- * Extracts the success value from a {@link Result}.
4
+ * Extracts the success value from a {@link Result}, {@link ResultAsync}.
4
5
  *
5
6
  * If the result is a {@link Failure}, it throws the contained error.
7
+ * For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
6
8
  *
7
9
  * @function
8
- * @typeParam T - The type of the success value.
9
- * @param result - The {@link Result} to unwrap.
10
- * @returns The success value.
10
+ * @typeParam R - The type of the result to unwrap.
11
+ * @param result - The {@link Result}, {@link ResultAsync}.
12
+ * @returns The success value, or a Promise of the success value for async results.
11
13
  * @throws The error value if the result is a {@link Failure}.
12
14
  *
13
15
  * @example Success Case
14
16
  * ```ts
15
17
  * import { Result } from '@praha/byethrow';
16
18
  *
17
- * const result: Result.Result<number, string> = { type: 'Success', value: 100 };
19
+ * const result: Result.Result<number, string> = Result.succeed(100);
18
20
  * const value = Result.unwrap(result); // 100
19
21
  * ```
20
22
  *
@@ -22,10 +24,26 @@ import type { Result } from '../result';
22
24
  * ```ts
23
25
  * import { Result } from '@praha/byethrow';
24
26
  *
25
- * const result: Result.Result<number, string> = { type: 'Failure', error: 'Oops' };
27
+ * const result: Result.Result<number, string> = Result.fail('Oops');
26
28
  * const value = Result.unwrap(result); // throws 'Oops'
27
29
  * ```
28
30
  *
31
+ * @example Async Success Case
32
+ * ```ts
33
+ * import { Result } from '@praha/byethrow';
34
+ *
35
+ * const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
36
+ * const value = await Result.unwrap(result); // 100
37
+ * ```
38
+ *
39
+ * @example Async Failure Case
40
+ * ```ts
41
+ * import { Result } from '@praha/byethrow';
42
+ *
43
+ * const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
44
+ * const value = await Result.unwrap(result); // throws 'Oops'
45
+ * ```
46
+ *
29
47
  * @category Unwraps
30
48
  */
31
- export declare const unwrap: <T>(result: Result<T, unknown>) => T;
49
+ export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
@@ -1,31 +1,49 @@
1
- import type { Result } from '../result';
1
+ import type { HasPromise } from '../internals/types/has-promise';
2
+ import type { InferFailure, ResultMaybeAsync } from '../result';
2
3
  /**
3
- * Extracts the error value from a {@link Result}.
4
+ * Extracts the error value from a {@link Result}, {@link ResultAsync}.
4
5
  *
5
6
  * If the result is a {@link Success}, it throws the success value (this is rare but symmetric to `unwrap`).
7
+ * For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
6
8
  *
7
9
  * @function
8
- * @typeParam E - The type of the error value.
9
- * @param result - The {@link Result} to unwrap the error from.
10
- * @returns The error value.
10
+ * @typeParam R - The type of the result to unwrap.
11
+ * @param result - The {@link Result}, {@link ResultAsync}.
12
+ * @returns The error value, or a Promise of the error value for async results.
11
13
  * @throws The success value if the result is a {@link Success}.
12
14
  *
13
15
  * @example Failure Case
14
16
  * ```ts
15
17
  * import { Result } from '@praha/byethrow';
16
18
  *
17
- * const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
18
- * const error = Result.unwrapError(result); // 'Something went wrong'
19
+ * const result: Result.Result<number, string> = Result.fail('Oops');
20
+ * const error = Result.unwrapError(result); // 'Oops'
19
21
  * ```
20
22
  *
21
23
  * @example Success Case
22
24
  * ```ts
23
25
  * import { Result } from '@praha/byethrow';
24
26
  *
25
- * const result: Result.Result<number, string> = { type: 'Success', value: 123 };
26
- * const error = Result.unwrapError(result); // throws 123
27
+ * const result: Result.Result<number, string> = Result.succeed(100);
28
+ * const error = Result.unwrapError(result); // throws 100
29
+ * ```
30
+ *
31
+ * @example Async Failure Case
32
+ * ```ts
33
+ * import { Result } from '@praha/byethrow';
34
+ *
35
+ * const result: Result.Result<number, string> = Result,fail(Promise.resolve('Oops'));
36
+ * const error = Result.unwrapError(result); // 'Oops'
37
+ * ```
38
+ *
39
+ * @example Async Success Case
40
+ * ```ts
41
+ * import { Result } from '@praha/byethrow';
42
+ *
43
+ * const result: Result.Result<number, string> = Result.succeed(Promise.resolve(100));
44
+ * const error = Result.unwrapError(result); // throws 100
27
45
  * ```
28
46
  *
29
47
  * @category Unwraps
30
48
  */
31
- export declare const unwrapError: <E>(result: Result<unknown, E>) => E;
49
+ export declare const unwrapError: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
@@ -1,5 +1,9 @@
1
1
  import { isSuccess } from "./is-success.js";
2
2
  const unwrapError = (result)=>{
3
+ if (result instanceof Promise) return result.then((r)=>{
4
+ if (isSuccess(r)) throw r.value;
5
+ return r.error;
6
+ });
3
7
  if (isSuccess(result)) throw result.value;
4
8
  return result.error;
5
9
  };
@@ -1,20 +1,22 @@
1
- import type { Result } from '../result';
1
+ import type { HasPromise } from '../internals/types/has-promise';
2
+ import type { InferSuccess, ResultMaybeAsync } from '../result';
2
3
  /**
3
- * Extracts the success value from a {@link Result}.
4
+ * Extracts the success value from a {@link Result}, {@link ResultAsync}.
4
5
  *
5
6
  * If the result is a {@link Failure}, it throws the contained error.
7
+ * For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
6
8
  *
7
9
  * @function
8
- * @typeParam T - The type of the success value.
9
- * @param result - The {@link Result} to unwrap.
10
- * @returns The success value.
10
+ * @typeParam R - The type of the result to unwrap.
11
+ * @param result - The {@link Result}, {@link ResultAsync}.
12
+ * @returns The success value, or a Promise of the success value for async results.
11
13
  * @throws The error value if the result is a {@link Failure}.
12
14
  *
13
15
  * @example Success Case
14
16
  * ```ts
15
17
  * import { Result } from '@praha/byethrow';
16
18
  *
17
- * const result: Result.Result<number, string> = { type: 'Success', value: 100 };
19
+ * const result: Result.Result<number, string> = Result.succeed(100);
18
20
  * const value = Result.unwrap(result); // 100
19
21
  * ```
20
22
  *
@@ -22,10 +24,26 @@ import type { Result } from '../result';
22
24
  * ```ts
23
25
  * import { Result } from '@praha/byethrow';
24
26
  *
25
- * const result: Result.Result<number, string> = { type: 'Failure', error: 'Oops' };
27
+ * const result: Result.Result<number, string> = Result.fail('Oops');
26
28
  * const value = Result.unwrap(result); // throws 'Oops'
27
29
  * ```
28
30
  *
31
+ * @example Async Success Case
32
+ * ```ts
33
+ * import { Result } from '@praha/byethrow';
34
+ *
35
+ * const result: Result.ResultAsync<number, string> = Result.succeed(Promise.resolve(100));
36
+ * const value = await Result.unwrap(result); // 100
37
+ * ```
38
+ *
39
+ * @example Async Failure Case
40
+ * ```ts
41
+ * import { Result } from '@praha/byethrow';
42
+ *
43
+ * const result: Result.ResultAsync<number, string> = Result.fail(Promise.resolve('Oops'));
44
+ * const value = await Result.unwrap(result); // throws 'Oops'
45
+ * ```
46
+ *
29
47
  * @category Unwraps
30
48
  */
31
- export declare const unwrap: <T>(result: Result<T, unknown>) => T;
49
+ export declare const unwrap: <R extends ResultMaybeAsync<any, any>>(result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
@@ -1,5 +1,9 @@
1
1
  import { isFailure } from "./is-failure.js";
2
2
  const unwrap = (result)=>{
3
+ if (result instanceof Promise) return result.then((r)=>{
4
+ if (isFailure(r)) throw r.error;
5
+ return r.value;
6
+ });
3
7
  if (isFailure(result)) throw result.error;
4
8
  return result.value;
5
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@praha/byethrow",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
5
5
  "keywords": [
6
6
  "javascript",
@@ -41,7 +41,7 @@
41
41
  "README.md"
42
42
  ],
43
43
  "devDependencies": {
44
- "@rslib/core": "0.10.1",
44
+ "@rslib/core": "0.10.2",
45
45
  "eslint": "9.29.0",
46
46
  "typedoc": "0.28.5",
47
47
  "typescript": "5.8.3",