@praha/byethrow 0.9.0 → 0.10.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/README.md +1 -1
- package/dist/cjs/exports.cjs +44 -30
- package/dist/cjs/exports.d.ts +2 -0
- package/dist/cjs/functions/assert-failure.d.ts +13 -21
- package/dist/cjs/functions/assert-success.d.ts +12 -20
- package/dist/cjs/functions/fn.cjs +55 -0
- package/dist/cjs/functions/fn.d.ts +84 -0
- package/dist/cjs/functions/is-failure.d.ts +5 -3
- package/dist/cjs/functions/is-success.d.ts +5 -3
- package/dist/cjs/functions/or-through.cjs +50 -0
- package/dist/cjs/functions/or-through.d.ts +64 -0
- package/dist/cjs/functions/try.cjs +3 -4
- package/dist/cjs/functions/try.d.ts +2 -77
- package/dist/cjs/index.d.ts +2 -2
- package/dist/esm/exports.d.ts +2 -0
- package/dist/esm/exports.js +2 -0
- package/dist/esm/functions/assert-failure.d.ts +13 -21
- package/dist/esm/functions/assert-success.d.ts +12 -20
- package/dist/esm/functions/fn.d.ts +84 -0
- package/dist/esm/functions/fn.js +21 -0
- package/dist/esm/functions/is-failure.d.ts +5 -3
- package/dist/esm/functions/is-success.d.ts +5 -3
- package/dist/esm/functions/or-through.d.ts +64 -0
- package/dist/esm/functions/or-through.js +16 -0
- package/dist/esm/functions/try.d.ts +2 -77
- package/dist/esm/functions/try.js +3 -4
- package/dist/esm/index.d.ts +2 -2
- package/package.json +6 -6
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Result, ResultAsync } from '../result.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
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 = (
|
|
5
|
+
const fn = ()=>{
|
|
6
6
|
try {
|
|
7
|
-
const output = options.try(
|
|
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
|
-
|
|
20
|
-
return fn;
|
|
19
|
+
return fn();
|
|
21
20
|
};
|
|
22
21
|
export { try_ as try };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* return Result.succeed();
|
|
16
16
|
* };
|
|
17
17
|
*
|
|
18
|
-
* const findUser = Result.
|
|
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.
|
|
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.
|
|
3
|
+
"version": "0.10.1",
|
|
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.
|
|
48
|
-
"eslint": "9.39.
|
|
49
|
-
"typedoc": "0.28.
|
|
50
|
-
"typedoc-plugin-markdown": "4.
|
|
47
|
+
"@rslib/core": "0.20.0",
|
|
48
|
+
"eslint": "9.39.4",
|
|
49
|
+
"typedoc": "0.28.17",
|
|
50
|
+
"typedoc-plugin-markdown": "4.11.0",
|
|
51
51
|
"typescript": "5.9.3",
|
|
52
|
-
"vitest": "4.0
|
|
52
|
+
"vitest": "4.1.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"typescript": ">=5.0.0"
|