@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.
- package/README.md +1 -1
- package/dist/cjs/exports.cjs +45 -31
- package/dist/cjs/exports.d.ts +2 -0
- package/dist/cjs/functions/and-then.cjs +1 -1
- package/dist/cjs/functions/and-through.cjs +1 -1
- package/dist/cjs/functions/assert-failure.cjs +1 -1
- package/dist/cjs/functions/assert-failure.d.ts +13 -21
- package/dist/cjs/functions/assert-success.cjs +1 -1
- package/dist/cjs/functions/assert-success.d.ts +12 -20
- package/dist/cjs/functions/bind.cjs +1 -1
- package/dist/cjs/functions/collect.cjs +1 -1
- package/dist/cjs/functions/do.cjs +1 -1
- package/dist/cjs/functions/fail.cjs +1 -1
- package/dist/cjs/functions/fn.cjs +55 -0
- package/dist/cjs/functions/fn.d.ts +84 -0
- package/dist/cjs/functions/inspect-error.cjs +1 -1
- package/dist/cjs/functions/inspect.cjs +1 -1
- package/dist/cjs/functions/is-failure.cjs +1 -1
- package/dist/cjs/functions/is-result.cjs +1 -1
- package/dist/cjs/functions/is-success.cjs +1 -1
- package/dist/cjs/functions/map-error.cjs +1 -1
- package/dist/cjs/functions/map.cjs +1 -1
- package/dist/cjs/functions/or-else.cjs +1 -1
- package/dist/cjs/functions/or-through.cjs +50 -0
- package/dist/cjs/functions/or-through.d.ts +64 -0
- package/dist/cjs/functions/parse.cjs +1 -1
- package/dist/cjs/functions/pipe.cjs +1 -1
- package/dist/cjs/functions/sequence.cjs +1 -1
- package/dist/cjs/functions/succeed.cjs +1 -1
- package/dist/cjs/functions/try.cjs +4 -5
- package/dist/cjs/functions/try.d.ts +2 -77
- package/dist/cjs/functions/unwrap-error.cjs +1 -1
- package/dist/cjs/functions/unwrap.cjs +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/internals/helpers/is-promise.cjs +1 -1
- package/dist/cjs/internals/types/has-promise.cjs +1 -1
- package/dist/cjs/result.cjs +1 -1
- 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/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
package/README.md
CHANGED
package/dist/cjs/exports.cjs
CHANGED
|
@@ -24,6 +24,9 @@ var __webpack_modules__ = {
|
|
|
24
24
|
"./functions/fail" (module) {
|
|
25
25
|
module.exports = require("./functions/fail.cjs");
|
|
26
26
|
},
|
|
27
|
+
"./functions/fn" (module) {
|
|
28
|
+
module.exports = require("./functions/fn.cjs");
|
|
29
|
+
},
|
|
27
30
|
"./functions/inspect-error" (module) {
|
|
28
31
|
module.exports = require("./functions/inspect-error.cjs");
|
|
29
32
|
},
|
|
@@ -48,6 +51,9 @@ var __webpack_modules__ = {
|
|
|
48
51
|
"./functions/or-else" (module) {
|
|
49
52
|
module.exports = require("./functions/or-else.cjs");
|
|
50
53
|
},
|
|
54
|
+
"./functions/or-through" (module) {
|
|
55
|
+
module.exports = require("./functions/or-through.cjs");
|
|
56
|
+
},
|
|
51
57
|
"./functions/parse" (module) {
|
|
52
58
|
module.exports = require("./functions/parse.cjs");
|
|
53
59
|
},
|
|
@@ -105,7 +111,7 @@ function __webpack_require__(moduleId) {
|
|
|
105
111
|
})();
|
|
106
112
|
(()=>{
|
|
107
113
|
__webpack_require__.r = (exports1)=>{
|
|
108
|
-
if (
|
|
114
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
109
115
|
value: 'Module'
|
|
110
116
|
});
|
|
111
117
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -152,65 +158,73 @@ var __webpack_exports__ = {};
|
|
|
152
158
|
var __rspack_reexport = {};
|
|
153
159
|
for(const __rspack_import_key in _functions_fail__rspack_import_8)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_fail__rspack_import_8[__rspack_import_key];
|
|
154
160
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
155
|
-
var
|
|
161
|
+
var _functions_fn__rspack_import_9 = __webpack_require__("./functions/fn");
|
|
162
|
+
var __rspack_reexport = {};
|
|
163
|
+
for(const __rspack_import_key in _functions_fn__rspack_import_9)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_fn__rspack_import_9[__rspack_import_key];
|
|
164
|
+
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
165
|
+
var _functions_inspect__rspack_import_10 = __webpack_require__("./functions/inspect");
|
|
166
|
+
var __rspack_reexport = {};
|
|
167
|
+
for(const __rspack_import_key in _functions_inspect__rspack_import_10)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_inspect__rspack_import_10[__rspack_import_key];
|
|
168
|
+
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
169
|
+
var _functions_inspect_error__rspack_import_11 = __webpack_require__("./functions/inspect-error");
|
|
156
170
|
var __rspack_reexport = {};
|
|
157
|
-
for(const __rspack_import_key in
|
|
171
|
+
for(const __rspack_import_key in _functions_inspect_error__rspack_import_11)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_inspect_error__rspack_import_11[__rspack_import_key];
|
|
158
172
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
159
|
-
var
|
|
173
|
+
var _functions_is_failure__rspack_import_12 = __webpack_require__("./functions/is-failure");
|
|
160
174
|
var __rspack_reexport = {};
|
|
161
|
-
for(const __rspack_import_key in
|
|
175
|
+
for(const __rspack_import_key in _functions_is_failure__rspack_import_12)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_is_failure__rspack_import_12[__rspack_import_key];
|
|
162
176
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
163
|
-
var
|
|
177
|
+
var _functions_is_result__rspack_import_13 = __webpack_require__("./functions/is-result");
|
|
164
178
|
var __rspack_reexport = {};
|
|
165
|
-
for(const __rspack_import_key in
|
|
179
|
+
for(const __rspack_import_key in _functions_is_result__rspack_import_13)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_is_result__rspack_import_13[__rspack_import_key];
|
|
166
180
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
167
|
-
var
|
|
181
|
+
var _functions_is_success__rspack_import_14 = __webpack_require__("./functions/is-success");
|
|
168
182
|
var __rspack_reexport = {};
|
|
169
|
-
for(const __rspack_import_key in
|
|
183
|
+
for(const __rspack_import_key in _functions_is_success__rspack_import_14)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_is_success__rspack_import_14[__rspack_import_key];
|
|
170
184
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
171
|
-
var
|
|
185
|
+
var _functions_map__rspack_import_15 = __webpack_require__("./functions/map");
|
|
172
186
|
var __rspack_reexport = {};
|
|
173
|
-
for(const __rspack_import_key in
|
|
187
|
+
for(const __rspack_import_key in _functions_map__rspack_import_15)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_map__rspack_import_15[__rspack_import_key];
|
|
174
188
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
175
|
-
var
|
|
189
|
+
var _functions_map_error__rspack_import_16 = __webpack_require__("./functions/map-error");
|
|
176
190
|
var __rspack_reexport = {};
|
|
177
|
-
for(const __rspack_import_key in
|
|
191
|
+
for(const __rspack_import_key in _functions_map_error__rspack_import_16)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_map_error__rspack_import_16[__rspack_import_key];
|
|
178
192
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
179
|
-
var
|
|
193
|
+
var _functions_or_else__rspack_import_17 = __webpack_require__("./functions/or-else");
|
|
180
194
|
var __rspack_reexport = {};
|
|
181
|
-
for(const __rspack_import_key in
|
|
195
|
+
for(const __rspack_import_key in _functions_or_else__rspack_import_17)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_or_else__rspack_import_17[__rspack_import_key];
|
|
182
196
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
183
|
-
var
|
|
197
|
+
var _functions_or_through__rspack_import_18 = __webpack_require__("./functions/or-through");
|
|
184
198
|
var __rspack_reexport = {};
|
|
185
|
-
for(const __rspack_import_key in
|
|
199
|
+
for(const __rspack_import_key in _functions_or_through__rspack_import_18)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_or_through__rspack_import_18[__rspack_import_key];
|
|
186
200
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
187
|
-
var
|
|
201
|
+
var _functions_parse__rspack_import_19 = __webpack_require__("./functions/parse");
|
|
188
202
|
var __rspack_reexport = {};
|
|
189
|
-
for(const __rspack_import_key in
|
|
203
|
+
for(const __rspack_import_key in _functions_parse__rspack_import_19)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_parse__rspack_import_19[__rspack_import_key];
|
|
190
204
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
191
|
-
var
|
|
205
|
+
var _functions_pipe__rspack_import_20 = __webpack_require__("./functions/pipe");
|
|
192
206
|
var __rspack_reexport = {};
|
|
193
|
-
for(const __rspack_import_key in
|
|
207
|
+
for(const __rspack_import_key in _functions_pipe__rspack_import_20)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_pipe__rspack_import_20[__rspack_import_key];
|
|
194
208
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
195
|
-
var
|
|
209
|
+
var _functions_sequence__rspack_import_21 = __webpack_require__("./functions/sequence");
|
|
196
210
|
var __rspack_reexport = {};
|
|
197
|
-
for(const __rspack_import_key in
|
|
211
|
+
for(const __rspack_import_key in _functions_sequence__rspack_import_21)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_sequence__rspack_import_21[__rspack_import_key];
|
|
198
212
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
199
|
-
var
|
|
213
|
+
var _functions_succeed__rspack_import_22 = __webpack_require__("./functions/succeed");
|
|
200
214
|
var __rspack_reexport = {};
|
|
201
|
-
for(const __rspack_import_key in
|
|
215
|
+
for(const __rspack_import_key in _functions_succeed__rspack_import_22)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_succeed__rspack_import_22[__rspack_import_key];
|
|
202
216
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
203
|
-
var
|
|
217
|
+
var _functions_try__rspack_import_23 = __webpack_require__("./functions/try");
|
|
204
218
|
var __rspack_reexport = {};
|
|
205
|
-
for(const __rspack_import_key in
|
|
219
|
+
for(const __rspack_import_key in _functions_try__rspack_import_23)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_try__rspack_import_23[__rspack_import_key];
|
|
206
220
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
207
|
-
var
|
|
221
|
+
var _functions_unwrap__rspack_import_24 = __webpack_require__("./functions/unwrap");
|
|
208
222
|
var __rspack_reexport = {};
|
|
209
|
-
for(const __rspack_import_key in
|
|
223
|
+
for(const __rspack_import_key in _functions_unwrap__rspack_import_24)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_unwrap__rspack_import_24[__rspack_import_key];
|
|
210
224
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
211
|
-
var
|
|
225
|
+
var _functions_unwrap_error__rspack_import_25 = __webpack_require__("./functions/unwrap-error");
|
|
212
226
|
var __rspack_reexport = {};
|
|
213
|
-
for(const __rspack_import_key in
|
|
227
|
+
for(const __rspack_import_key in _functions_unwrap_error__rspack_import_25)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_functions_unwrap_error__rspack_import_25[__rspack_import_key];
|
|
214
228
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
215
229
|
})();
|
|
216
230
|
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
package/dist/cjs/exports.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './functions/bind.js';
|
|
|
7
7
|
export * from './functions/collect.js';
|
|
8
8
|
export * from './functions/do.js';
|
|
9
9
|
export * from './functions/fail.js';
|
|
10
|
+
export * from './functions/fn.js';
|
|
10
11
|
export * from './functions/inspect.js';
|
|
11
12
|
export * from './functions/inspect-error.js';
|
|
12
13
|
export * from './functions/is-failure.js';
|
|
@@ -15,6 +16,7 @@ export * from './functions/is-success.js';
|
|
|
15
16
|
export * from './functions/map.js';
|
|
16
17
|
export * from './functions/map-error.js';
|
|
17
18
|
export * from './functions/or-else.js';
|
|
19
|
+
export * from './functions/or-through.js';
|
|
18
20
|
export * from './functions/parse.js';
|
|
19
21
|
export * from './functions/pipe.js';
|
|
20
22
|
export * from './functions/sequence.js';
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { Failure, InferFailure, ResultMaybeAsync } from '../result.js';
|
|
2
3
|
/**
|
|
3
4
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Failure} and returns it.
|
|
4
|
-
*
|
|
5
|
+
* This function requires that the result's success type is `never`, meaning the result is
|
|
6
|
+
* guaranteed to be a {@link Failure} at the type level.
|
|
7
|
+
* If the result is a {@link Success} at runtime, throws an error.
|
|
5
8
|
*
|
|
6
9
|
* @function
|
|
7
|
-
* @typeParam
|
|
10
|
+
* @typeParam R - The result type that extends {@link ResultMaybeAsync} with `never` as the success type.
|
|
8
11
|
* @param result - The {@link Result} or {@link ResultAsync} to assert as a {@link Failure}.
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
12
|
+
* The success type must be `never`.
|
|
13
|
+
* @returns The {@link Failure} result or a Promise of {@link Failure} result.
|
|
14
|
+
* @throws {Error} If the result is a {@link Success} at runtime.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```ts
|
|
@@ -18,24 +22,15 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result.js';
|
|
|
18
22
|
* // failure: { type: 'Failure', error: 'error' }
|
|
19
23
|
* ```
|
|
20
24
|
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* // @errors: 2769
|
|
24
|
-
* import { Result } from '@praha/byethrow';
|
|
25
|
-
*
|
|
26
|
-
* const result = Result.succeed(42);
|
|
27
|
-
* Result.assertFailure(result); // throws Error
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example Safe unwrap with assertFailure
|
|
25
|
+
* @example Type-safe usage after narrowing success type
|
|
31
26
|
* ```ts
|
|
32
27
|
* import { Result } from '@praha/byethrow';
|
|
33
28
|
*
|
|
34
29
|
* const getResult = (): Result.Result<number, string> => Result.fail('error');
|
|
35
30
|
* const value = Result.pipe(
|
|
36
31
|
* getResult(),
|
|
37
|
-
* Result.andThen(() => Result.fail('die')),
|
|
38
|
-
* Result.assertFailure,
|
|
32
|
+
* Result.andThen(() => Result.fail('die')), // Success type becomes never
|
|
33
|
+
* Result.assertFailure, // Type-safe: success type is now never
|
|
39
34
|
* Result.unwrapError(), // Safe unwrap after assertion
|
|
40
35
|
* );
|
|
41
36
|
* ```
|
|
@@ -44,7 +39,4 @@ import type { Failure, InferFailure, Result, ResultAsync } from '../result.js';
|
|
|
44
39
|
*
|
|
45
40
|
* @category Assertions
|
|
46
41
|
*/
|
|
47
|
-
export declare const assertFailure:
|
|
48
|
-
<R extends ResultAsync<never, any>>(result: R): Promise<Failure<InferFailure<R>>>;
|
|
49
|
-
<R extends Result<never, any>>(result: R): Failure<InferFailure<R>>;
|
|
50
|
-
};
|
|
42
|
+
export declare const assertFailure: <R extends ResultMaybeAsync<never, any>>(result: R) => true extends HasPromise<R> ? Promise<Failure<InferFailure<R>>> : Failure<InferFailure<R>>;
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HasPromise } from '../internals/types/has-promise.js';
|
|
2
|
+
import type { InferSuccess, ResultMaybeAsync, Success } from '../result.js';
|
|
2
3
|
/**
|
|
3
4
|
* Asserts that a {@link Result} or {@link ResultAsync} is a {@link Success} and returns it.
|
|
4
|
-
*
|
|
5
|
+
* This function requires that the result's error type is `never`, meaning the result is
|
|
6
|
+
* guaranteed to be a {@link Success} at the type level.
|
|
7
|
+
* If the result is a {@link Failure} at runtime, throws an error.
|
|
5
8
|
*
|
|
6
9
|
* @function
|
|
7
|
-
* @typeParam
|
|
10
|
+
* @typeParam R - The result type that extends {@link ResultMaybeAsync} with `never` as the error type.
|
|
8
11
|
* @param result - The {@link Result} or {@link ResultAsync} to assert as a {@link Success}.
|
|
12
|
+
* The error type must be `never`.
|
|
9
13
|
* @returns The {@link Success} result or a Promise of {@link Success} result.
|
|
10
|
-
* @throws {Error} If the result is a {@link Failure}.
|
|
14
|
+
* @throws {Error} If the result is a {@link Failure} at runtime.
|
|
11
15
|
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```ts
|
|
@@ -18,24 +22,15 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result.js';
|
|
|
18
22
|
* // success: { type: 'Success', value: 42 }
|
|
19
23
|
* ```
|
|
20
24
|
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* // @errors: 2769
|
|
24
|
-
* import { Result } from '@praha/byethrow';
|
|
25
|
-
*
|
|
26
|
-
* const result = Result.fail('error');
|
|
27
|
-
* Result.assertSuccess(result); // throws Error
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @example Safe unwrap with assertSuccess
|
|
25
|
+
* @example Type-safe usage after narrowing error type
|
|
31
26
|
* ```ts
|
|
32
27
|
* import { Result } from '@praha/byethrow';
|
|
33
28
|
*
|
|
34
29
|
* const getResult = (): Result.Result<number, string> => Result.succeed(42);
|
|
35
30
|
* const value = Result.pipe(
|
|
36
31
|
* getResult(),
|
|
37
|
-
* Result.orElse(() => Result.succeed(
|
|
38
|
-
* Result.assertSuccess,
|
|
32
|
+
* Result.orElse(() => Result.succeed(0)), // Error type becomes never
|
|
33
|
+
* Result.assertSuccess, // Type-safe: error type is now never
|
|
39
34
|
* Result.unwrap(), // Safe unwrap after assertion
|
|
40
35
|
* );
|
|
41
36
|
* ```
|
|
@@ -44,7 +39,4 @@ import type { InferSuccess, Result, ResultAsync, Success } from '../result.js';
|
|
|
44
39
|
*
|
|
45
40
|
* @category Assertions
|
|
46
41
|
*/
|
|
47
|
-
export declare const assertSuccess:
|
|
48
|
-
<R extends ResultAsync<any, never>>(result: R): Promise<Success<InferSuccess<R>>>;
|
|
49
|
-
<R extends Result<any, never>>(result: R): Success<InferSuccess<R>>;
|
|
50
|
-
};
|
|
42
|
+
export declare const assertSuccess: <R extends ResultMaybeAsync<any, never>>(result: R) => true extends HasPromise<R> ? Promise<Success<InferSuccess<R>>> : Success<InferSuccess<R>>;
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -0,0 +1,55 @@
|
|
|
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 ("u" > 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
|
+
fn: ()=>fn_fn
|
|
28
|
+
});
|
|
29
|
+
const external_fail_cjs_namespaceObject = require("./fail.cjs");
|
|
30
|
+
const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
|
|
31
|
+
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
|
32
|
+
const fn_fn = (options)=>{
|
|
33
|
+
const fn = (...args)=>{
|
|
34
|
+
try {
|
|
35
|
+
const output = options.try(...args);
|
|
36
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(output)) {
|
|
37
|
+
const promise = (0, external_succeed_cjs_namespaceObject.succeed)(output);
|
|
38
|
+
if ('safe' in options && options.safe) return promise;
|
|
39
|
+
return promise.catch((error)=>(0, external_fail_cjs_namespaceObject.fail)(options.catch(error)));
|
|
40
|
+
}
|
|
41
|
+
return (0, external_succeed_cjs_namespaceObject.succeed)(output);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if ('safe' in options && options.safe) throw error;
|
|
44
|
+
return (0, external_fail_cjs_namespaceObject.fail)(options.catch(error));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return fn;
|
|
48
|
+
};
|
|
49
|
+
exports.fn = __webpack_exports__.fn;
|
|
50
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
51
|
+
"fn"
|
|
52
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
53
|
+
Object.defineProperty(exports, '__esModule', {
|
|
54
|
+
value: true
|
|
55
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Result, ResultAsync } from '../result.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a function that may throw and returns a new function that returns a {@link Result} or {@link ResultAsync}.
|
|
4
|
+
*
|
|
5
|
+
* You can use either a custom `catch` handler or rely on the `safe: true` option
|
|
6
|
+
* to assume the function cannot throw.
|
|
7
|
+
*
|
|
8
|
+
* @function
|
|
9
|
+
* @typeParam T - The function type to execute (sync or async) or a Promise type.
|
|
10
|
+
* @typeParam E - The error type to return if `catch` is used.
|
|
11
|
+
* @returns A new function that returns a {@link Result} or {@link ResultAsync} wrapping the original function's return value or the caught error.
|
|
12
|
+
*
|
|
13
|
+
* @example Sync try-catch
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Result } from '@praha/byethrow';
|
|
16
|
+
*
|
|
17
|
+
* const fn = Result.fn({
|
|
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.fn({
|
|
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.fn({
|
|
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.fn({
|
|
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 fn: {
|
|
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
|
+
safe: true;
|
|
73
|
+
try: T;
|
|
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
|
+
safe: true;
|
|
81
|
+
try: T;
|
|
82
|
+
}): (...args: Parameters<T>) => Result<ReturnType<T>, never>;
|
|
83
|
+
};
|
|
84
|
+
export { fn };
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|
|
@@ -13,7 +13,7 @@ var __webpack_require__ = {};
|
|
|
13
13
|
})();
|
|
14
14
|
(()=>{
|
|
15
15
|
__webpack_require__.r = (exports1)=>{
|
|
16
|
-
if (
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
17
|
value: 'Module'
|
|
18
18
|
});
|
|
19
19
|
Object.defineProperty(exports1, '__esModule', {
|