@praha/byethrow 0.0.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +77 -0
  3. package/dist/cjs/exports.cjs +186 -0
  4. package/dist/cjs/exports.d.ts +15 -0
  5. package/dist/cjs/functions/and-then.cjs +44 -0
  6. package/dist/cjs/functions/and-then.d.ts +45 -0
  7. package/dist/cjs/functions/and-through.cjs +50 -0
  8. package/dist/cjs/functions/and-through.d.ts +57 -0
  9. package/dist/cjs/functions/bind.cjs +50 -0
  10. package/dist/cjs/functions/bind.d.ts +61 -0
  11. package/dist/cjs/functions/do.cjs +37 -0
  12. package/dist/cjs/functions/do.d.ts +17 -0
  13. package/dist/cjs/functions/fail.cjs +50 -0
  14. package/dist/cjs/functions/fail.d.ts +38 -0
  15. package/dist/cjs/functions/is-failure.cjs +36 -0
  16. package/dist/cjs/functions/is-failure.d.ts +22 -0
  17. package/dist/cjs/functions/is-success.cjs +36 -0
  18. package/dist/cjs/functions/is-success.d.ts +22 -0
  19. package/dist/cjs/functions/map-error.cjs +46 -0
  20. package/dist/cjs/functions/map-error.d.ts +37 -0
  21. package/dist/cjs/functions/map.cjs +46 -0
  22. package/dist/cjs/functions/map.d.ts +37 -0
  23. package/dist/cjs/functions/pipe.cjs +40 -0
  24. package/dist/cjs/functions/pipe.d.ts +58 -0
  25. package/dist/cjs/functions/succeed.cjs +50 -0
  26. package/dist/cjs/functions/succeed.d.ts +39 -0
  27. package/dist/cjs/functions/try.cjs +52 -0
  28. package/dist/cjs/functions/try.d.ts +84 -0
  29. package/dist/cjs/functions/unwrap-error.cjs +40 -0
  30. package/dist/cjs/functions/unwrap-error.d.ts +31 -0
  31. package/dist/cjs/functions/unwrap.cjs +40 -0
  32. package/dist/cjs/functions/unwrap.d.ts +31 -0
  33. package/dist/cjs/index.cjs +39 -0
  34. package/dist/cjs/index.d.ts +69 -0
  35. package/dist/cjs/internals/helpers/is-promise.cjs +36 -0
  36. package/dist/cjs/internals/helpers/is-promise.d.ts +9 -0
  37. package/dist/cjs/internals/types/has-promise.cjs +18 -0
  38. package/dist/cjs/internals/types/has-promise.d.ts +17 -0
  39. package/dist/cjs/result.cjs +18 -0
  40. package/dist/cjs/result.d.ts +193 -0
  41. package/dist/esm/exports.d.ts +15 -0
  42. package/dist/esm/exports.js +15 -0
  43. package/dist/esm/functions/and-then.d.ts +45 -0
  44. package/dist/esm/functions/and-then.js +10 -0
  45. package/dist/esm/functions/and-through.d.ts +57 -0
  46. package/dist/esm/functions/and-through.js +16 -0
  47. package/dist/esm/functions/bind.d.ts +61 -0
  48. package/dist/esm/functions/bind.js +16 -0
  49. package/dist/esm/functions/do.d.ts +17 -0
  50. package/dist/esm/functions/do.js +3 -0
  51. package/dist/esm/functions/fail.d.ts +38 -0
  52. package/dist/esm/functions/fail.js +16 -0
  53. package/dist/esm/functions/is-failure.d.ts +22 -0
  54. package/dist/esm/functions/is-failure.js +2 -0
  55. package/dist/esm/functions/is-success.d.ts +22 -0
  56. package/dist/esm/functions/is-success.js +2 -0
  57. package/dist/esm/functions/map-error.d.ts +37 -0
  58. package/dist/esm/functions/map-error.js +12 -0
  59. package/dist/esm/functions/map.d.ts +37 -0
  60. package/dist/esm/functions/map.js +12 -0
  61. package/dist/esm/functions/pipe.d.ts +58 -0
  62. package/dist/esm/functions/pipe.js +6 -0
  63. package/dist/esm/functions/succeed.d.ts +39 -0
  64. package/dist/esm/functions/succeed.js +16 -0
  65. package/dist/esm/functions/try.d.ts +84 -0
  66. package/dist/esm/functions/try.js +18 -0
  67. package/dist/esm/functions/unwrap-error.d.ts +31 -0
  68. package/dist/esm/functions/unwrap-error.js +6 -0
  69. package/dist/esm/functions/unwrap.d.ts +31 -0
  70. package/dist/esm/functions/unwrap.js +6 -0
  71. package/dist/esm/index.d.ts +69 -0
  72. package/dist/esm/index.js +2 -0
  73. package/dist/esm/internals/helpers/is-promise.d.ts +9 -0
  74. package/dist/esm/internals/helpers/is-promise.js +2 -0
  75. package/dist/esm/internals/types/has-promise.d.ts +17 -0
  76. package/dist/esm/internals/types/has-promise.js +0 -0
  77. package/dist/esm/result.d.ts +193 -0
  78. package/dist/esm/result.js +0 -0
  79. package/package.json +61 -0
@@ -0,0 +1,84 @@
1
+ import type { Result, ResultAsync } from '../result';
2
+ /**
3
+ * Wraps a function execution (sync or async) in a {@link Result} or {@link ResultAsync} type,
4
+ * capturing errors and returning them in a structured way.
5
+ *
6
+ * You can use either a custom `catch` handler or rely on the `safe: true` option
7
+ * to assume the function cannot throw.
8
+ *
9
+ * @function
10
+ * @typeParam T - The function type to execute (sync or async).
11
+ * @typeParam E - The error type to return if `catch` is used.
12
+ *
13
+ * @example Sync Try-Catch
14
+ * ```ts
15
+ * import { Result } from '@praha/byethrow';
16
+ *
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 }),
23
+ * });
24
+ *
25
+ * const result = fn(5); // Result.Result<number, Error>
26
+ * ```
27
+ *
28
+ * @example Sync Safe
29
+ * ```ts
30
+ * import { Result } from '@praha/byethrow';
31
+ *
32
+ * const fn = Result.try({
33
+ * safe: true,
34
+ * try: (x: number) => x + 1,
35
+ * });
36
+ *
37
+ * const result = fn(1); // Result.Result<number, never>
38
+ * ```
39
+ *
40
+ * @example Async Try-Catch
41
+ * ```ts
42
+ * import { Result } from '@praha/byethrow';
43
+ *
44
+ * const fn = Result.try({
45
+ * try: async (id: string) => await fetch(`/api/data/${id}`),
46
+ * catch: (error) => new Error('Oops!', { cause: error }),
47
+ * });
48
+ *
49
+ * const result = await fn('abc'); // Result.ResultAsync<Response, Error>
50
+ * ```
51
+ *
52
+ * @example Async Safe
53
+ * ```ts
54
+ * import { Result } from '@praha/byethrow';
55
+ *
56
+ * const fn = Result.try({
57
+ * safe: true,
58
+ * try: async () => await Promise.resolve('ok'),
59
+ * });
60
+ *
61
+ * const result = await fn(); // Result.ResultAsync<string, never>
62
+ * ```
63
+ *
64
+ * @category Creators
65
+ */
66
+ declare const try_: {
67
+ <T extends (...args: readonly any[]) => Promise<any>, E>(options: {
68
+ try: T;
69
+ catch: (error: unknown) => E;
70
+ }): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, E>;
71
+ <T extends (...args: readonly any[]) => Promise<any>>(options: {
72
+ try: T;
73
+ safe: true;
74
+ }): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, never>;
75
+ <T extends (...args: readonly any[]) => any, E>(options: {
76
+ try: T;
77
+ catch: (error: unknown) => E;
78
+ }): (...args: Parameters<T>) => Result<ReturnType<T>, E>;
79
+ <T extends (...args: readonly any[]) => any>(options: {
80
+ try: T;
81
+ safe: true;
82
+ }): (...args: Parameters<T>) => Result<ReturnType<T>, never>;
83
+ };
84
+ export { try_ as try };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ unwrapError: ()=>unwrapError
28
+ });
29
+ const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
30
+ const unwrapError = (result)=>{
31
+ if ((0, external_is_success_cjs_namespaceObject.isSuccess)(result)) throw result.value;
32
+ return result.error;
33
+ };
34
+ exports.unwrapError = __webpack_exports__.unwrapError;
35
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
36
+ "unwrapError"
37
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
38
+ Object.defineProperty(exports, '__esModule', {
39
+ value: true
40
+ });
@@ -0,0 +1,31 @@
1
+ import type { Result } from '../result';
2
+ /**
3
+ * Extracts the error value from a {@link Result}.
4
+ *
5
+ * If the result is a {@link Success}, it throws the success value (this is rare but symmetric to `unwrap`).
6
+ *
7
+ * @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.
11
+ * @throws The success value if the result is a {@link Success}.
12
+ *
13
+ * @example Failure Case
14
+ * ```ts
15
+ * import { Result } from '@praha/byethrow';
16
+ *
17
+ * const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
18
+ * const error = Result.unwrapError(result); // 'Something went wrong'
19
+ * ```
20
+ *
21
+ * @example Success Case
22
+ * ```ts
23
+ * import { Result } from '@praha/byethrow';
24
+ *
25
+ * const result: Result.Result<number, string> = { type: 'Success', value: 123 };
26
+ * const error = Result.unwrapError(result); // throws 123
27
+ * ```
28
+ *
29
+ * @category Unwraps
30
+ */
31
+ export declare const unwrapError: <E>(result: Result<unknown, E>) => E;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ unwrap: ()=>unwrap
28
+ });
29
+ const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
30
+ const unwrap = (result)=>{
31
+ if ((0, external_is_failure_cjs_namespaceObject.isFailure)(result)) throw result.error;
32
+ return result.value;
33
+ };
34
+ exports.unwrap = __webpack_exports__.unwrap;
35
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
36
+ "unwrap"
37
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
38
+ Object.defineProperty(exports, '__esModule', {
39
+ value: true
40
+ });
@@ -0,0 +1,31 @@
1
+ import type { Result } from '../result';
2
+ /**
3
+ * Extracts the success value from a {@link Result}.
4
+ *
5
+ * If the result is a {@link Failure}, it throws the contained error.
6
+ *
7
+ * @function
8
+ * @typeParam T - The type of the success value.
9
+ * @param result - The {@link Result} to unwrap.
10
+ * @returns The success value.
11
+ * @throws The error value if the result is a {@link Failure}.
12
+ *
13
+ * @example Success Case
14
+ * ```ts
15
+ * import { Result } from '@praha/byethrow';
16
+ *
17
+ * const result: Result.Result<number, string> = { type: 'Success', value: 100 };
18
+ * const value = Result.unwrap(result); // 100
19
+ * ```
20
+ *
21
+ * @example Failure Case
22
+ * ```ts
23
+ * import { Result } from '@praha/byethrow';
24
+ *
25
+ * const result: Result.Result<number, string> = { type: 'Failure', error: 'Oops' };
26
+ * const value = Result.unwrap(result); // throws 'Oops'
27
+ * ```
28
+ *
29
+ * @category Unwraps
30
+ */
31
+ export declare const unwrap: <T>(result: Result<T, unknown>) => T;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ R: ()=>external_exports_cjs_namespaceObject,
28
+ Result: ()=>external_exports_cjs_namespaceObject
29
+ });
30
+ const external_exports_cjs_namespaceObject = require("./exports.cjs");
31
+ exports.R = __webpack_exports__.R;
32
+ exports.Result = __webpack_exports__.Result;
33
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
34
+ "R",
35
+ "Result"
36
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
37
+ Object.defineProperty(exports, '__esModule', {
38
+ value: true
39
+ });
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Re-exports core Result-handling utilities under two convenient namespaces:
3
+ *
4
+ * - `Result`: Verbose and explicit usage
5
+ * - `R`: Shorthand alias for more concise code
6
+ *
7
+ * @example Basic Usage
8
+ * ```ts
9
+ * import { Result } from '@praha/byethrow';
10
+ *
11
+ * const validateId = (id: string) => {
12
+ * if (!id.startsWith('u')) {
13
+ * return Result.fail(new Error('Invalid ID format'));
14
+ * }
15
+ * return Result.succeed();
16
+ * };
17
+ *
18
+ * const findUser = Result.try({
19
+ * try: (id: string) => {
20
+ * return { id, name: 'John Doe' };
21
+ * },
22
+ * catch: (error) => new Error('Failed to find user', { cause: error }),
23
+ * });
24
+ *
25
+ * const result = Result.pipe(
26
+ * Result.succeed('u123'),
27
+ * Result.andThrough(validateId),
28
+ * Result.andThen(findUser),
29
+ * );
30
+ *
31
+ * if (Result.isSuccess(result)) {
32
+ * console.log(result.value); // User found: John Doe
33
+ * }
34
+ * ```
35
+ *
36
+ * @example Shorthand Usage
37
+ * ```ts
38
+ * import { R } from '@praha/byethrow';
39
+ *
40
+ * const validateId = (id: string) => {
41
+ * if (!id.startsWith('u')) {
42
+ * return R.fail(new Error('Invalid ID format'));
43
+ * }
44
+ * return R.succeed();
45
+ * };
46
+ *
47
+ * const findUser = R.try({
48
+ * try: (id: string) => {
49
+ * return { id, name: 'John Doe' };
50
+ * },
51
+ * catch: (error) => new Error('Failed to find user', { cause: error }),
52
+ * });
53
+ *
54
+ * const result = R.pipe(
55
+ * R.succeed('u123'),
56
+ * R.andThrough(validateId),
57
+ * R.andThen(findUser),
58
+ * );
59
+ *
60
+ * if (R.isSuccess(result)) {
61
+ * console.log(result.value); // User found: John Doe
62
+ * }
63
+ * ```
64
+ */
65
+ export * as Result from './exports';
66
+ /**
67
+ * Shorthand alias for {@link Result}.
68
+ */
69
+ export * as R from './exports';
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ isPromise: ()=>isPromise
28
+ });
29
+ const isPromise = (value)=>'object' == typeof value && null !== value && 'then' in value && 'function' == typeof value.then && 'catch' in value && 'function' == typeof value.catch;
30
+ exports.isPromise = __webpack_exports__.isPromise;
31
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
32
+ "isPromise"
33
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
34
+ Object.defineProperty(exports, '__esModule', {
35
+ value: true
36
+ });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Determines whether a given value is a `Promise`.
3
+ *
4
+ * @typeParam T - The value type.
5
+ *
6
+ * @param value - A value that might be a `Promise`.
7
+ * @returns `true` if the value is a `Promise`, otherwise `false`.
8
+ */
9
+ export declare const isPromise: <T>(value: T | Promise<T>) => value is Promise<T>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Determines whether the given type `T` is a `Promise`.
3
+ *
4
+ * Returns `true` if `T` extends `Promise<unknown>`, otherwise `false`.
5
+ *
6
+ * @typeParam T - The type to test.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * type A = HasPromise<Promise<number>>; // true
11
+ * type B = HasPromise<Promise<string> | number>; // true
12
+ * type C = HasPromise<number>; // false
13
+ * type D = HasPromise<{}>; // false
14
+ * type E = HasPromise<[]>; // true
15
+ * ```
16
+ */
17
+ export type HasPromise<T> = object extends T ? false : Promise<any> extends T ? true : false;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
@@ -0,0 +1,193 @@
1
+ import type { HasPromise } from './internals/types/has-promise';
2
+ /**
3
+ * Represents a successful result.
4
+ *
5
+ * @typeParam T - The type of the successful value.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { Result } from '@praha/byethrow';
10
+ *
11
+ * const success: Result.Success<number> = {
12
+ * type: 'Success',
13
+ * value: 42,
14
+ * };
15
+ * ```
16
+ *
17
+ * @category Core Types
18
+ */
19
+ export type Success<T> = {
20
+ readonly type: 'Success';
21
+ readonly value: T;
22
+ };
23
+ /**
24
+ * Represents a failed result.
25
+ *
26
+ * @typeParam E - The type of the error.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { Result } from '@praha/byethrow';
31
+ *
32
+ * const failure: Result.Failure<string> = {
33
+ * type: 'Failure',
34
+ * error: 'Something went wrong',
35
+ * };
36
+ * ```
37
+ *
38
+ * @category Core Types
39
+ */
40
+ export type Failure<E> = {
41
+ readonly type: 'Failure';
42
+ readonly error: E;
43
+ };
44
+ /**
45
+ * A union type representing either a success or a failure.
46
+ *
47
+ * @typeParam T - The type of the {@link Success} value.
48
+ * @typeParam E - The type of the {@link Failure} value.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * import { Result } from '@praha/byethrow';
53
+ *
54
+ * const doSomething = (): Result.Result<number, string> => {
55
+ * return Math.random() > 0.5
56
+ * ? { type: 'Success', value: 10 }
57
+ * : { type: 'Failure', error: 'Oops' };
58
+ * };
59
+ * ```
60
+ *
61
+ * @category Core Types
62
+ */
63
+ export type Result<T, E> = Success<T> | Failure<E>;
64
+ /**
65
+ * An asynchronous variant of {@link Result}, wrapped in a `Promise`.
66
+ *
67
+ * @typeParam T - The type of the {@link Success} value.
68
+ * @typeParam E - The type of the {@link Failure} value.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import { Result } from '@praha/byethrow';
73
+ *
74
+ * const fetchData = async (): Result.ResultAsync<string, Error> => {
75
+ * try {
76
+ * const data = await fetch('...');
77
+ * return { type: 'Success', value: await data.text() };
78
+ * } catch (err) {
79
+ * return { type: 'Failure', error: err as Error };
80
+ * }
81
+ * };
82
+ * ```
83
+ *
84
+ * @category Core Types
85
+ */
86
+ export type ResultAsync<T, E> = Promise<Result<T, E>>;
87
+ /**
88
+ * A result that may be either synchronous or asynchronous.
89
+ *
90
+ * @typeParam T - The type of the {@link Success} value.
91
+ * @typeParam E - The type of the {@link Failure} value.
92
+ *
93
+ * @example Synchronous Case
94
+ * ```ts
95
+ * import { Result } from '@praha/byethrow';
96
+ *
97
+ * const result: Result.ResultMaybeAsync<number, string> = { type: 'Success', value: 10 };
98
+ * ```
99
+ *
100
+ * @example Asynchronous Case
101
+ * ```ts
102
+ * import { Result } from '@praha/byethrow';
103
+ *
104
+ * const result: Result.ResultMaybeAsync<number, string> = Promise.resolve({ type: 'Failure', error: 'error' });
105
+ * ```
106
+ *
107
+ * @category Core Types
108
+ */
109
+ export type ResultMaybeAsync<T, E> = Result<T, E> | Promise<Result<T, E>>;
110
+ /**
111
+ * Resolves to the appropriate Result type (sync or async) based on the input type.
112
+ *
113
+ * Typically used to conditionally infer return types based on whether the original computation was async.
114
+ *
115
+ * @typeParam R - The reference type to inspect for a Promise.
116
+ * @typeParam T - The type of the {@link Success} value.
117
+ * @typeParam E - The type of the {@link Failure} value.
118
+ *
119
+ * @example With a Promise-returning function
120
+ * ```ts
121
+ * import { Result } from '@praha/byethrow';
122
+ *
123
+ * type R = Result.ResultAsync<number, string>;
124
+ * type Output = Result.ResultFor<R, number, string>; // Result.ResultAsync<number, string>
125
+ * ```
126
+ *
127
+ * @example With a non-Promise-returning function
128
+ * ```ts
129
+ * import { Result } from '@praha/byethrow';
130
+ *
131
+ * type R = Result.Result<number, string>;
132
+ * type Output = Result.ResultFor<R, number, string>; // Result.Result<number, string>
133
+ * ```
134
+ *
135
+ * @category Core Types
136
+ */
137
+ export type ResultFor<R, T, E> = true extends HasPromise<R> ? ResultAsync<T, E> : Result<T, E>;
138
+ /**
139
+ * Infers the {@link Success} value type `T` from a Result or a function returning a Result.
140
+ *
141
+ * @typeParam T - A {@link ResultMaybeAsync} type or a function returning it.
142
+ *
143
+ * @example From a result object
144
+ * ```ts
145
+ * import { Result } from '@praha/byethrow';
146
+ *
147
+ * type R = Result.Result<number, string>;
148
+ * type SuccessValue = Result.InferSuccess<R>; // number
149
+ * ```
150
+ *
151
+ * @example From a function
152
+ * ```ts
153
+ * import { Result } from '@praha/byethrow';
154
+ *
155
+ * const fn = () => Promise.resolve({ type: 'Success', value: 123 } as const);
156
+ * type SuccessValue = Result.InferSuccess<typeof fn>; // number
157
+ * ```
158
+ *
159
+ * @category Infer Types
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;
166
+ /**
167
+ * Infers the {@link Failure} value type `E` from a Result or a function returning a Result.
168
+ *
169
+ * @typeParam T - A {@link ResultMaybeAsync} type or a function returning it.
170
+ *
171
+ * @example From a result object
172
+ * ```ts
173
+ * import { Result } from '@praha/byethrow';
174
+ *
175
+ * type R = Result.Result<number, string>;
176
+ * type ErrorValue = Result.InferFailure<R>; // string
177
+ * ```
178
+ *
179
+ * @example From a function
180
+ * ```ts
181
+ * import { Result } from '@praha/byethrow';
182
+ *
183
+ * const fn = () => Promise.resolve({ type: 'Failure', error: new Error() } as const);
184
+ * type ErrorValue = Result.InferFailure<typeof fn>; // Error
185
+ * ```
186
+ *
187
+ * @category Infer Types
188
+ */
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;
@@ -0,0 +1,15 @@
1
+ export * from './result';
2
+ export * from './functions/and-then';
3
+ export * from './functions/and-through';
4
+ export * from './functions/bind';
5
+ export * from './functions/do';
6
+ export * from './functions/fail';
7
+ export * from './functions/is-failure';
8
+ export * from './functions/is-success';
9
+ export * from './functions/map';
10
+ export * from './functions/map-error';
11
+ export * from './functions/pipe';
12
+ export * from './functions/succeed';
13
+ export * from './functions/try';
14
+ export * from './functions/unwrap';
15
+ export * from './functions/unwrap-error';
@@ -0,0 +1,15 @@
1
+ export * from "./result.js";
2
+ export * from "./functions/and-then.js";
3
+ export * from "./functions/and-through.js";
4
+ export * from "./functions/bind.js";
5
+ export * from "./functions/do.js";
6
+ export * from "./functions/fail.js";
7
+ export * from "./functions/is-failure.js";
8
+ export * from "./functions/is-success.js";
9
+ export * from "./functions/map.js";
10
+ export * from "./functions/map-error.js";
11
+ export * from "./functions/pipe.js";
12
+ export * from "./functions/succeed.js";
13
+ export * from "./functions/try.js";
14
+ export * from "./functions/unwrap.js";
15
+ export * from "./functions/unwrap-error.js";