@praha/byethrow 0.8.2 → 0.10.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.
Files changed (50) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/exports.cjs +45 -31
  3. package/dist/cjs/exports.d.ts +2 -0
  4. package/dist/cjs/functions/and-then.cjs +1 -1
  5. package/dist/cjs/functions/and-through.cjs +1 -1
  6. package/dist/cjs/functions/assert-failure.cjs +1 -1
  7. package/dist/cjs/functions/assert-failure.d.ts +13 -21
  8. package/dist/cjs/functions/assert-success.cjs +1 -1
  9. package/dist/cjs/functions/assert-success.d.ts +12 -20
  10. package/dist/cjs/functions/bind.cjs +1 -1
  11. package/dist/cjs/functions/collect.cjs +1 -1
  12. package/dist/cjs/functions/do.cjs +1 -1
  13. package/dist/cjs/functions/fail.cjs +1 -1
  14. package/dist/cjs/functions/fn.cjs +55 -0
  15. package/dist/cjs/functions/fn.d.ts +84 -0
  16. package/dist/cjs/functions/inspect-error.cjs +1 -1
  17. package/dist/cjs/functions/inspect.cjs +1 -1
  18. package/dist/cjs/functions/is-failure.cjs +1 -1
  19. package/dist/cjs/functions/is-result.cjs +1 -1
  20. package/dist/cjs/functions/is-success.cjs +1 -1
  21. package/dist/cjs/functions/map-error.cjs +1 -1
  22. package/dist/cjs/functions/map.cjs +1 -1
  23. package/dist/cjs/functions/or-else.cjs +1 -1
  24. package/dist/cjs/functions/or-through.cjs +50 -0
  25. package/dist/cjs/functions/or-through.d.ts +64 -0
  26. package/dist/cjs/functions/parse.cjs +1 -1
  27. package/dist/cjs/functions/pipe.cjs +1 -1
  28. package/dist/cjs/functions/sequence.cjs +1 -1
  29. package/dist/cjs/functions/succeed.cjs +1 -1
  30. package/dist/cjs/functions/try.cjs +4 -5
  31. package/dist/cjs/functions/try.d.ts +2 -77
  32. package/dist/cjs/functions/unwrap-error.cjs +1 -1
  33. package/dist/cjs/functions/unwrap.cjs +1 -1
  34. package/dist/cjs/index.cjs +1 -1
  35. package/dist/cjs/index.d.ts +2 -2
  36. package/dist/cjs/internals/helpers/is-promise.cjs +1 -1
  37. package/dist/cjs/internals/types/has-promise.cjs +1 -1
  38. package/dist/cjs/result.cjs +1 -1
  39. package/dist/esm/exports.d.ts +2 -0
  40. package/dist/esm/exports.js +2 -0
  41. package/dist/esm/functions/assert-failure.d.ts +13 -21
  42. package/dist/esm/functions/assert-success.d.ts +12 -20
  43. package/dist/esm/functions/fn.d.ts +84 -0
  44. package/dist/esm/functions/fn.js +21 -0
  45. package/dist/esm/functions/or-through.d.ts +64 -0
  46. package/dist/esm/functions/or-through.js +16 -0
  47. package/dist/esm/functions/try.d.ts +2 -77
  48. package/dist/esm/functions/try.js +3 -4
  49. package/dist/esm/index.d.ts +2 -2
  50. package/package.json +6 -6
@@ -0,0 +1,21 @@
1
+ import { fail } from "./fail.js";
2
+ import { succeed } from "./succeed.js";
3
+ import { isPromise } from "../internals/helpers/is-promise.js";
4
+ const fn_fn = (options)=>{
5
+ const fn = (...args)=>{
6
+ try {
7
+ const output = options.try(...args);
8
+ if (isPromise(output)) {
9
+ const promise = succeed(output);
10
+ if ('safe' in options && options.safe) return promise;
11
+ return promise.catch((error)=>fail(options.catch(error)));
12
+ }
13
+ return succeed(output);
14
+ } catch (error) {
15
+ if ('safe' in options && options.safe) throw error;
16
+ return fail(options.catch(error));
17
+ }
18
+ };
19
+ return fn;
20
+ };
21
+ export { fn_fn as fn };
@@ -0,0 +1,64 @@
1
+ import type { InferFailure, InferSuccess, ResultFor, ResultMaybeAsync } from '../result.js';
2
+ /**
3
+ * Runs an additional computation using the error value of a {@link Result} or {@link ResultAsync},
4
+ * but **returns the original failure** if the additional computation is successful.
5
+ *
6
+ * If the original result is a {@link Success}, it is returned immediately without running the function.
7
+ * If the original result is a {@link Failure}, the function is executed with the error value.
8
+ * If the function returns a {@link Success}, the original failure is returned.
9
+ * If the function returns a {@link Failure}, that new failure is returned.
10
+ *
11
+ * Useful for running error recovery or fallback logic while preserving the original error on success.
12
+ *
13
+ * @function
14
+ * @typeParam R1 - The input {@link Result} or {@link ResultAsync}.
15
+ * @typeParam R2 - The result type returned by `fn`.
16
+ *
17
+ * @example Success Case
18
+ * ```ts
19
+ * import { Result } from '@praha/byethrow';
20
+ *
21
+ * const result = Result.pipe(
22
+ * Result.succeed(5),
23
+ * Result.orThrough((error) => {
24
+ * return Result.succeed(null);
25
+ * }),
26
+ * );
27
+ * // { type: 'Success', value: 5 }
28
+ * ```
29
+ *
30
+ * @example Failure Case (function returns a Success)
31
+ * ```ts
32
+ * import { Result } from '@praha/byethrow';
33
+ *
34
+ * const result = Result.pipe(
35
+ * Result.fail('error'),
36
+ * Result.orThrough((error) => {
37
+ * console.log('Logging error:', error);
38
+ * return Result.succeed(null);
39
+ * }),
40
+ * );
41
+ * // { type: 'Failure', error: 'error' }
42
+ * ```
43
+ *
44
+ * @example Failure Case (function returns a Failure)
45
+ * ```ts
46
+ * import { Result } from '@praha/byethrow';
47
+ *
48
+ * const result = Result.pipe(
49
+ * Result.fail('original error'),
50
+ * Result.orThrough((error) => {
51
+ * return Result.fail('new error');
52
+ * }),
53
+ * );
54
+ * // { type: 'Failure', error: 'new error' }
55
+ * ```
56
+ *
57
+ * @see {@link pipe} - It is recommended to use this function with the {@link pipe} function for better readability and composability.
58
+ *
59
+ * @category Combinators
60
+ */
61
+ export declare const orThrough: {
62
+ <R1 extends ResultMaybeAsync<any, any>, R2 extends ResultMaybeAsync<any, any>>(fn: (a: InferFailure<R1>) => R2): (result: R1) => ResultFor<R1 | R2, InferSuccess<R1>, InferFailure<R1> | InferFailure<R2>>;
63
+ <F extends (a: any) => ResultMaybeAsync<any, any>>(fn: F): <R1 extends ResultMaybeAsync<any, Parameters<F>[0]>>(result: R1) => ResultFor<R1 | ReturnType<F>, InferSuccess<R1>, InferFailure<R1> | InferFailure<F>>;
64
+ };
@@ -0,0 +1,16 @@
1
+ import { isSuccess } from "./is-success.js";
2
+ import { isPromise } from "../internals/helpers/is-promise.js";
3
+ const orThrough = (fn)=>(result)=>{
4
+ const apply = (r)=>{
5
+ if (isSuccess(r)) return r;
6
+ const next = fn(r.error);
7
+ if (isPromise(next)) return next.then((n)=>{
8
+ if (isSuccess(n)) return r;
9
+ return n;
10
+ });
11
+ if (isSuccess(next)) return r;
12
+ return next;
13
+ };
14
+ return isPromise(result) ? result.then(apply) : apply(result);
15
+ };
16
+ export { orThrough };
@@ -1,7 +1,6 @@
1
1
  import type { Result, ResultAsync } from '../result.js';
2
2
  /**
3
- * Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
4
- * capturing errors and returning them in a structured way.
3
+ * Executes a function that may throw and wraps the result in a {@link Result} or {@link ResultAsync}.
5
4
  *
6
5
  * You can use either a custom `catch` handler or rely on the `safe: true` option
7
6
  * to assume the function cannot throw.
@@ -9,28 +8,13 @@ import type { Result, ResultAsync } from '../result.js';
9
8
  * @function
10
9
  * @typeParam T - The function type to execute (sync or async) or a Promise type.
11
10
  * @typeParam E - The error type to return if `catch` is used.
11
+ * @returns A {@link Result} or {@link ResultAsync} wrapping the function's return value or the caught error.
12
12
  *
13
13
  * @example Sync try-catch
14
14
  * ```ts
15
15
  * import { Result } from '@praha/byethrow';
16
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 try-catch with immediate execution
29
- * ```ts
30
- * import { Result } from '@praha/byethrow';
31
- *
32
17
  * const result = Result.try({
33
- * immediate: true,
34
18
  * try: () => {
35
19
  * const x = Math.random() * 10 - 5;
36
20
  * if (x < 0) throw new Error('Negative!');
@@ -46,21 +30,8 @@ import type { Result, ResultAsync } from '../result.js';
46
30
  * ```ts
47
31
  * import { Result } from '@praha/byethrow';
48
32
  *
49
- * const fn = Result.try({
50
- * safe: true,
51
- * try: (x: number) => x + 1,
52
- * });
53
- *
54
- * const result = fn(1); // Result.Result<number, never>
55
- * ```
56
- *
57
- * @example Sync safe with immediate execution
58
- * ```ts
59
- * import { Result } from '@praha/byethrow';
60
- *
61
33
  * const result = Result.try({
62
34
  * safe: true,
63
- * immediate: true,
64
35
  * try: () => Math.random() + 1,
65
36
  * });
66
37
  *
@@ -71,20 +42,7 @@ import type { Result, ResultAsync } from '../result.js';
71
42
  * ```ts
72
43
  * import { Result } from '@praha/byethrow';
73
44
  *
74
- * const fn = Result.try({
75
- * try: async (id: string) => await fetch(`/api/data/${id}`),
76
- * catch: (error) => new Error('Oops!', { cause: error }),
77
- * });
78
- *
79
- * const result = await fn('abc'); // Result.ResultAsync<Response, Error>
80
- * ```
81
- *
82
- * @example Async try-catch with immediate execution
83
- * ```ts
84
- * import { Result } from '@praha/byethrow';
85
- *
86
45
  * const result = Result.try({
87
- * immediate: true,
88
46
  * try: () => fetch('/api/data'),
89
47
  * catch: (error) => new Error('Fetch failed', { cause: error }),
90
48
  * });
@@ -96,21 +54,8 @@ import type { Result, ResultAsync } from '../result.js';
96
54
  * ```ts
97
55
  * import { Result } from '@praha/byethrow';
98
56
  *
99
- * const fn = Result.try({
100
- * safe: true,
101
- * try: async () => await Promise.resolve('ok'),
102
- * });
103
- *
104
- * const result = await fn(); // Result.ResultAsync<string, never>
105
- * ```
106
- *
107
- * @example Async safe with immediate execution
108
- * ```ts
109
- * import { Result } from '@praha/byethrow';
110
- *
111
57
  * const result = Result.try({
112
58
  * safe: true,
113
- * immediate: true,
114
59
  * try: () => Promise.resolve('ok'),
115
60
  * });
116
61
  *
@@ -120,40 +65,20 @@ import type { Result, ResultAsync } from '../result.js';
120
65
  * @category Creators
121
66
  */
122
67
  declare const try_: {
123
- <T extends (...args: readonly any[]) => Promise<any>, E>(options: {
124
- try: T;
125
- catch: (error: unknown) => E;
126
- }): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, E>;
127
68
  <T extends () => Promise<any>, E>(options: {
128
- immediate: true;
129
69
  try: T;
130
70
  catch: (error: unknown) => E;
131
71
  }): ResultAsync<Awaited<ReturnType<T>>, E>;
132
- <T extends (...args: readonly any[]) => Promise<any>>(options: {
133
- safe: true;
134
- try: T;
135
- }): (...args: Parameters<T>) => ResultAsync<Awaited<ReturnType<T>>, never>;
136
72
  <T extends () => Promise<any>>(options: {
137
73
  safe: true;
138
- immediate: true;
139
74
  try: T;
140
75
  }): ResultAsync<Awaited<ReturnType<T>>, never>;
141
- <T extends (...args: readonly any[]) => any, E>(options: {
142
- try: T;
143
- catch: (error: unknown) => E;
144
- }): (...args: Parameters<T>) => Result<ReturnType<T>, E>;
145
76
  <T extends () => any, E>(options: {
146
- immediate: true;
147
77
  try: T;
148
78
  catch: (error: unknown) => E;
149
79
  }): Result<ReturnType<T>, E>;
150
- <T extends (...args: readonly any[]) => any>(options: {
151
- safe: true;
152
- try: T;
153
- }): (...args: Parameters<T>) => Result<ReturnType<T>, never>;
154
80
  <T extends () => any>(options: {
155
81
  safe: true;
156
- immediate: true;
157
82
  try: T;
158
83
  }): Result<ReturnType<T>, never>;
159
84
  };
@@ -2,9 +2,9 @@ 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
- const fn = (...args)=>{
5
+ const fn = ()=>{
6
6
  try {
7
- const output = options.try(...args);
7
+ const output = options.try();
8
8
  if (isPromise(output)) {
9
9
  const promise = succeed(output);
10
10
  if ('safe' in options && options.safe) return promise;
@@ -16,7 +16,6 @@ const try_ = (options)=>{
16
16
  return fail(options.catch(error));
17
17
  }
18
18
  };
19
- if ('immediate' in options && options.immediate) return fn();
20
- return fn;
19
+ return fn();
21
20
  };
22
21
  export { try_ as try };
@@ -15,7 +15,7 @@
15
15
  * return Result.succeed();
16
16
  * };
17
17
  *
18
- * const findUser = Result.try({
18
+ * const findUser = Result.fn({
19
19
  * try: (id: string) => {
20
20
  * return { id, name: 'John Doe' };
21
21
  * },
@@ -44,7 +44,7 @@
44
44
  * return R.succeed();
45
45
  * };
46
46
  *
47
- * const findUser = R.try({
47
+ * const findUser = R.fn({
48
48
  * try: (id: string) => {
49
49
  * return { id, name: 'John Doe' };
50
50
  * },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@praha/byethrow",
3
- "version": "0.8.2",
3
+ "version": "0.10.0",
4
4
  "description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
5
5
  "keywords": [
6
6
  "javascript",
@@ -44,12 +44,12 @@
44
44
  "@standard-schema/spec": "^1.0.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@rslib/core": "0.18.4",
48
- "eslint": "9.39.1",
49
- "typedoc": "0.28.15",
50
- "typedoc-plugin-markdown": "4.9.0",
47
+ "@rslib/core": "0.19.5",
48
+ "eslint": "9.39.2",
49
+ "typedoc": "0.28.17",
50
+ "typedoc-plugin-markdown": "4.10.0",
51
51
  "typescript": "5.9.3",
52
- "vitest": "4.0.15"
52
+ "vitest": "4.0.18"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "typescript": ">=5.0.0"