@praha/byethrow 0.4.0 → 0.5.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/dist/cjs/exports.cjs +24 -15
- package/dist/cjs/exports.d.ts +1 -0
- package/dist/cjs/functions/parse.cjs +46 -0
- package/dist/cjs/functions/parse.d.ts +37 -0
- package/dist/cjs/functions/try.cjs +7 -1
- package/dist/cjs/functions/try.d.ts +36 -4
- package/dist/cjs/functions/unwrap-error.cjs +28 -7
- package/dist/cjs/functions/unwrap-error.d.ts +20 -18
- package/dist/cjs/functions/unwrap.cjs +28 -7
- package/dist/cjs/functions/unwrap.d.ts +22 -20
- package/dist/esm/exports.d.ts +1 -0
- package/dist/esm/exports.js +1 -0
- package/dist/esm/functions/parse.d.ts +37 -0
- package/dist/esm/functions/parse.js +12 -0
- package/dist/esm/functions/try.d.ts +36 -4
- package/dist/esm/functions/try.js +7 -1
- package/dist/esm/functions/unwrap-error.d.ts +20 -18
- package/dist/esm/functions/unwrap-error.js +28 -7
- package/dist/esm/functions/unwrap.d.ts +22 -20
- package/dist/esm/functions/unwrap.js +28 -7
- package/package.json +5 -2
package/dist/cjs/exports.cjs
CHANGED
@@ -42,6 +42,9 @@ var __webpack_modules__ = {
|
|
42
42
|
"./functions/or-else": function(module) {
|
43
43
|
module.exports = require("./functions/or-else.cjs");
|
44
44
|
},
|
45
|
+
"./functions/parse": function(module) {
|
46
|
+
module.exports = require("./functions/parse.cjs");
|
47
|
+
},
|
45
48
|
"./functions/pipe": function(module) {
|
46
49
|
module.exports = require("./functions/pipe.cjs");
|
47
50
|
},
|
@@ -194,34 +197,40 @@ var __webpack_exports__ = {};
|
|
194
197
|
return _functions_or_else__WEBPACK_IMPORTED_MODULE_14__[key];
|
195
198
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
196
199
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
197
|
-
var
|
200
|
+
var _functions_parse__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/parse");
|
201
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
202
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_parse__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
203
|
+
return _functions_parse__WEBPACK_IMPORTED_MODULE_15__[key];
|
204
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
205
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
206
|
+
var _functions_pipe__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/pipe");
|
198
207
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
199
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
200
|
-
return
|
208
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_16__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
209
|
+
return _functions_pipe__WEBPACK_IMPORTED_MODULE_16__[key];
|
201
210
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
202
211
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
203
|
-
var
|
212
|
+
var _functions_succeed__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/succeed");
|
204
213
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
205
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
206
|
-
return
|
214
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_17__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
215
|
+
return _functions_succeed__WEBPACK_IMPORTED_MODULE_17__[key];
|
207
216
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
208
217
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
209
|
-
var
|
218
|
+
var _functions_try__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/try");
|
210
219
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
211
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
212
|
-
return
|
220
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_18__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
221
|
+
return _functions_try__WEBPACK_IMPORTED_MODULE_18__[key];
|
213
222
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
214
223
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
215
|
-
var
|
224
|
+
var _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__("./functions/unwrap");
|
216
225
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
217
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
218
|
-
return
|
226
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
227
|
+
return _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__[key];
|
219
228
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
220
229
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
221
|
-
var
|
230
|
+
var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__("./functions/unwrap-error");
|
222
231
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
223
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
224
|
-
return
|
232
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
233
|
+
return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__[key];
|
225
234
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
226
235
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
227
236
|
})();
|
package/dist/cjs/exports.d.ts
CHANGED
@@ -13,6 +13,7 @@ export * from './functions/is-success';
|
|
13
13
|
export * from './functions/map';
|
14
14
|
export * from './functions/map-error';
|
15
15
|
export * from './functions/or-else';
|
16
|
+
export * from './functions/parse';
|
16
17
|
export * from './functions/pipe';
|
17
18
|
export * from './functions/succeed';
|
18
19
|
export * from './functions/try';
|
@@ -0,0 +1,46 @@
|
|
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
|
+
parse: ()=>parse
|
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 parse = (schema, ...args)=>{
|
33
|
+
const apply = (input)=>{
|
34
|
+
const result = schema['~standard']['validate'](input);
|
35
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) throw new TypeError('Schema validation must be synchronous');
|
36
|
+
return result.issues ? (0, external_fail_cjs_namespaceObject.fail)(result.issues) : (0, external_succeed_cjs_namespaceObject.succeed)(result.value);
|
37
|
+
};
|
38
|
+
return args.length <= 0 ? apply : apply(args[0]);
|
39
|
+
};
|
40
|
+
exports.parse = __webpack_exports__.parse;
|
41
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
42
|
+
"parse"
|
43
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
44
|
+
Object.defineProperty(exports, '__esModule', {
|
45
|
+
value: true
|
46
|
+
});
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import type { Result } from '../result';
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
3
|
+
/**
|
4
|
+
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
5
|
+
* Returns a {@link Result} with the parsed value on success or validation errors on failure.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam S - The Standard Schema V1 compatible schema type.
|
9
|
+
* @returns A {@link Result} with the parsed value or validation errors.
|
10
|
+
*
|
11
|
+
* @example Basic Usage
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
* import { z } from 'zod';
|
15
|
+
*
|
16
|
+
* const schema = z.object({ name: z.string() });
|
17
|
+
* const result = Result.parse(schema, { name: 'John' });
|
18
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Curried Usage
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
* import { z } from 'zod';
|
25
|
+
*
|
26
|
+
* const schema = z.object({ name: z.string() });
|
27
|
+
* const parser = Result.parse(schema);
|
28
|
+
* const result = parser({ name: 'John' });
|
29
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* @category Utilities
|
33
|
+
*/
|
34
|
+
export declare const parse: {
|
35
|
+
<S extends StandardSchemaV1>(schema: S): (value: unknown) => Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
36
|
+
<S extends StandardSchemaV1>(schema: S, value: unknown): Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
37
|
+
};
|
@@ -29,7 +29,12 @@ __webpack_require__.d(__webpack_exports__, {
|
|
29
29
|
const external_fail_cjs_namespaceObject = require("./fail.cjs");
|
30
30
|
const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
|
31
31
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
32
|
-
const try_ = (options)=>
|
32
|
+
const try_ = (options)=>{
|
33
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(options.try)) {
|
34
|
+
if ('safe' in options && options.safe) return (0, external_succeed_cjs_namespaceObject.succeed)(options.try);
|
35
|
+
return options.try.then((value)=>(0, external_succeed_cjs_namespaceObject.succeed)(value), (error)=>(0, external_fail_cjs_namespaceObject.fail)(options.catch(error)));
|
36
|
+
}
|
37
|
+
return (...args)=>{
|
33
38
|
try {
|
34
39
|
const output = options.try(...args);
|
35
40
|
if ((0, is_promise_cjs_namespaceObject.isPromise)(output)) {
|
@@ -43,6 +48,7 @@ const try_ = (options)=>(...args)=>{
|
|
43
48
|
return (0, external_fail_cjs_namespaceObject.fail)(options.catch(error));
|
44
49
|
}
|
45
50
|
};
|
51
|
+
};
|
46
52
|
exports["try"] = __webpack_exports__["try"];
|
47
53
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
48
54
|
"try"
|
@@ -1,15 +1,39 @@
|
|
1
1
|
import type { Result, ResultAsync } from '../result';
|
2
2
|
/**
|
3
|
-
* Wraps a function execution (sync or async) in a {@link Result} or {@link ResultAsync} type,
|
3
|
+
* Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
|
4
4
|
* capturing errors and returning them in a structured way.
|
5
5
|
*
|
6
6
|
* You can use either a custom `catch` handler or rely on the `safe: true` option
|
7
7
|
* to assume the function cannot throw.
|
8
8
|
*
|
9
9
|
* @function
|
10
|
-
* @typeParam T - The function type to execute (sync or async).
|
10
|
+
* @typeParam T - The function type to execute (sync or async) or a Promise type.
|
11
11
|
* @typeParam E - The error type to return if `catch` is used.
|
12
12
|
*
|
13
|
+
* @example Promise Try-Catch
|
14
|
+
* ```ts
|
15
|
+
* import { Result } from '@praha/byethrow';
|
16
|
+
*
|
17
|
+
* const result = Result.try({
|
18
|
+
* try: fetch('/api/data'),
|
19
|
+
* catch: (error) => new Error('Fetch failed', { cause: error }),
|
20
|
+
* });
|
21
|
+
*
|
22
|
+
* // result is ResultAsync<Response, Error>
|
23
|
+
* ```
|
24
|
+
*
|
25
|
+
* @example Promise Safe
|
26
|
+
* ```ts
|
27
|
+
* import { Result } from '@praha/byethrow';
|
28
|
+
*
|
29
|
+
* const result = Result.try({
|
30
|
+
* safe: true,
|
31
|
+
* try: Promise.resolve('ok'),
|
32
|
+
* });
|
33
|
+
*
|
34
|
+
* // result is ResultAsync<string, never>
|
35
|
+
* ```
|
36
|
+
*
|
13
37
|
* @example Sync Try-Catch
|
14
38
|
* ```ts
|
15
39
|
* import { Result } from '@praha/byethrow';
|
@@ -37,7 +61,7 @@ import type { Result, ResultAsync } from '../result';
|
|
37
61
|
* const result = fn(1); // Result.Result<number, never>
|
38
62
|
* ```
|
39
63
|
*
|
40
|
-
* @example Async Try-Catch
|
64
|
+
* @example Async Function Try-Catch
|
41
65
|
* ```ts
|
42
66
|
* import { Result } from '@praha/byethrow';
|
43
67
|
*
|
@@ -49,7 +73,7 @@ import type { Result, ResultAsync } from '../result';
|
|
49
73
|
* const result = await fn('abc'); // Result.ResultAsync<Response, Error>
|
50
74
|
* ```
|
51
75
|
*
|
52
|
-
* @example Async Safe
|
76
|
+
* @example Async Function Safe
|
53
77
|
* ```ts
|
54
78
|
* import { Result } from '@praha/byethrow';
|
55
79
|
*
|
@@ -64,6 +88,14 @@ import type { Result, ResultAsync } from '../result';
|
|
64
88
|
* @category Creators
|
65
89
|
*/
|
66
90
|
declare const try_: {
|
91
|
+
<T extends Promise<any>, E>(options: {
|
92
|
+
try: T;
|
93
|
+
catch: (error: unknown) => E;
|
94
|
+
}): ResultAsync<Awaited<T>, E>;
|
95
|
+
<T extends Promise<any>>(options: {
|
96
|
+
try: T;
|
97
|
+
safe: true;
|
98
|
+
}): ResultAsync<Awaited<T>, never>;
|
67
99
|
<T extends (...args: readonly any[]) => Promise<any>, E>(options: {
|
68
100
|
try: T;
|
69
101
|
catch: (error: unknown) => E;
|
@@ -26,15 +26,36 @@ __webpack_require__.r(__webpack_exports__);
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
27
27
|
unwrapError: ()=>unwrapError
|
28
28
|
});
|
29
|
+
const external_is_result_cjs_namespaceObject = require("./is-result.cjs");
|
29
30
|
const external_is_success_cjs_namespaceObject = require("./is-success.cjs");
|
30
31
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
31
|
-
const unwrapError = (
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
const unwrapError = (...args)=>{
|
33
|
+
const firstArgument = args[0];
|
34
|
+
if ((0, external_is_result_cjs_namespaceObject.isResult)(firstArgument) || (0, is_promise_cjs_namespaceObject.isPromise)(firstArgument)) {
|
35
|
+
const result = firstArgument;
|
36
|
+
const hasDefault = 2 === args.length;
|
37
|
+
const defaultValue = hasDefault ? args[1] : void 0;
|
38
|
+
const apply = (r)=>{
|
39
|
+
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) {
|
40
|
+
if (hasDefault) return defaultValue;
|
41
|
+
throw new Error(String(r.value));
|
42
|
+
}
|
43
|
+
return r.error;
|
44
|
+
};
|
45
|
+
return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
|
46
|
+
}
|
47
|
+
const hasDefault = 1 === args.length;
|
48
|
+
const defaultValue = hasDefault ? args[0] : void 0;
|
49
|
+
return (result)=>{
|
50
|
+
const apply = (r)=>{
|
51
|
+
if ((0, external_is_success_cjs_namespaceObject.isSuccess)(r)) {
|
52
|
+
if (hasDefault) return defaultValue;
|
53
|
+
throw new Error(String(r.value));
|
54
|
+
}
|
55
|
+
return r.error;
|
56
|
+
};
|
57
|
+
return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
|
58
|
+
};
|
38
59
|
};
|
39
60
|
exports.unwrapError = __webpack_exports__.unwrapError;
|
40
61
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
@@ -1,18 +1,15 @@
|
|
1
1
|
import type { HasPromise } from '../internals/types/has-promise';
|
2
2
|
import type { InferFailure, ResultMaybeAsync } from '../result';
|
3
3
|
/**
|
4
|
-
* Extracts the error value from a {@link Result}
|
4
|
+
* Extracts the error value from a {@link Result} or {@link ResultAsync}.
|
5
5
|
*
|
6
|
-
* If the
|
7
|
-
* For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
|
6
|
+
* If the input is a {@link Success}, it will throw the success value or return the default value if provided.
|
8
7
|
*
|
9
8
|
* @function
|
10
|
-
* @typeParam R - The
|
11
|
-
* @
|
12
|
-
* @returns The error value, or a Promise of the error value for async results.
|
13
|
-
* @throws The success value if the result is a {@link Success}.
|
9
|
+
* @typeParam R - The input {@link Result} or {@link ResultAsync}.
|
10
|
+
* @typeParam T - The default value type (optional).
|
14
11
|
*
|
15
|
-
* @example Failure Case
|
12
|
+
* @example Failure Case (without default)
|
16
13
|
* ```ts
|
17
14
|
* import { Result } from '@praha/byethrow';
|
18
15
|
*
|
@@ -20,30 +17,35 @@ import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
20
17
|
* const error = Result.unwrapError(result); // 'Oops'
|
21
18
|
* ```
|
22
19
|
*
|
23
|
-
* @example
|
20
|
+
* @example Failure Case (with default)
|
24
21
|
* ```ts
|
25
22
|
* import { Result } from '@praha/byethrow';
|
26
23
|
*
|
27
|
-
* const result: Result.Result<number, string> = Result.
|
28
|
-
* const error = Result.unwrapError(result); //
|
24
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
25
|
+
* const error = Result.unwrapError(result, 'default'); // 'Oops'
|
29
26
|
* ```
|
30
27
|
*
|
31
|
-
* @example
|
28
|
+
* @example Success Case (without default)
|
32
29
|
* ```ts
|
33
30
|
* import { Result } from '@praha/byethrow';
|
34
31
|
*
|
35
|
-
* const result: Result.Result<number, string> = Result
|
36
|
-
* const error = Result.unwrapError(result); //
|
32
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
33
|
+
* const error = Result.unwrapError(result); // throws 100
|
37
34
|
* ```
|
38
35
|
*
|
39
|
-
* @example
|
36
|
+
* @example Success Case (with default)
|
40
37
|
* ```ts
|
41
38
|
* import { Result } from '@praha/byethrow';
|
42
39
|
*
|
43
|
-
* const result: Result.Result<number, string> = Result.succeed(
|
44
|
-
* const error = Result.unwrapError(result); //
|
40
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
41
|
+
* const error = Result.unwrapError(result, 0); // 0
|
45
42
|
* ```
|
46
43
|
*
|
47
44
|
* @category Unwraps
|
48
45
|
*/
|
49
|
-
export declare const unwrapError:
|
46
|
+
export declare const unwrapError: {
|
47
|
+
<R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
48
|
+
<R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
|
49
|
+
<R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
50
|
+
<R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
|
51
|
+
};
|
@@ -27,14 +27,35 @@ __webpack_require__.d(__webpack_exports__, {
|
|
27
27
|
unwrap: ()=>unwrap
|
28
28
|
});
|
29
29
|
const external_is_failure_cjs_namespaceObject = require("./is-failure.cjs");
|
30
|
+
const external_is_result_cjs_namespaceObject = require("./is-result.cjs");
|
30
31
|
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
31
|
-
const unwrap = (
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
const unwrap = (...args)=>{
|
33
|
+
const firstArgument = args[0];
|
34
|
+
if ((0, external_is_result_cjs_namespaceObject.isResult)(firstArgument) || (0, is_promise_cjs_namespaceObject.isPromise)(firstArgument)) {
|
35
|
+
const result = firstArgument;
|
36
|
+
const hasDefault = 2 === args.length;
|
37
|
+
const defaultValue = hasDefault ? args[1] : void 0;
|
38
|
+
const apply = (r)=>{
|
39
|
+
if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) {
|
40
|
+
if (hasDefault) return defaultValue;
|
41
|
+
throw new Error(String(r.error));
|
42
|
+
}
|
43
|
+
return r.value;
|
44
|
+
};
|
45
|
+
return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
|
46
|
+
}
|
47
|
+
const hasDefault = 1 === args.length;
|
48
|
+
const defaultValue = hasDefault ? args[0] : void 0;
|
49
|
+
return (result)=>{
|
50
|
+
const apply = (r)=>{
|
51
|
+
if ((0, external_is_failure_cjs_namespaceObject.isFailure)(r)) {
|
52
|
+
if (hasDefault) return defaultValue;
|
53
|
+
throw new Error(String(r.error));
|
54
|
+
}
|
55
|
+
return r.value;
|
56
|
+
};
|
57
|
+
return (0, is_promise_cjs_namespaceObject.isPromise)(result) ? result.then(apply) : apply(result);
|
58
|
+
};
|
38
59
|
};
|
39
60
|
exports.unwrap = __webpack_exports__.unwrap;
|
40
61
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
@@ -1,49 +1,51 @@
|
|
1
1
|
import type { HasPromise } from '../internals/types/has-promise';
|
2
2
|
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
3
3
|
/**
|
4
|
-
* Extracts the success value from a {@link Result}
|
4
|
+
* Extracts the success value from a {@link Result} or {@link ResultAsync}.
|
5
5
|
*
|
6
|
-
* If the
|
7
|
-
* For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
|
6
|
+
* If the input is a {@link Failure}, it will throw the error or return the default value if provided.
|
8
7
|
*
|
9
8
|
* @function
|
10
|
-
* @typeParam R - The
|
11
|
-
* @
|
12
|
-
* @returns The success value, or a Promise of the success value for async results.
|
13
|
-
* @throws The error value if the result is a {@link Failure}.
|
9
|
+
* @typeParam R - The input {@link Result} or {@link ResultAsync}.
|
10
|
+
* @typeParam T - The default value type (optional).
|
14
11
|
*
|
15
|
-
* @example Success Case
|
12
|
+
* @example Success Case (without default)
|
16
13
|
* ```ts
|
17
14
|
* import { Result } from '@praha/byethrow';
|
18
15
|
*
|
19
|
-
* const result
|
20
|
-
* const value = Result.unwrap(result); //
|
16
|
+
* const result = Result.succeed(42);
|
17
|
+
* const value = Result.unwrap(result); // 42
|
21
18
|
* ```
|
22
19
|
*
|
23
|
-
* @example
|
20
|
+
* @example Success Case (with default)
|
24
21
|
* ```ts
|
25
22
|
* import { Result } from '@praha/byethrow';
|
26
23
|
*
|
27
|
-
* const result
|
28
|
-
* const value = Result.unwrap(result); //
|
24
|
+
* const result = Result.succeed(42);
|
25
|
+
* const value = Result.unwrap(result, 0); // 42
|
29
26
|
* ```
|
30
27
|
*
|
31
|
-
* @example
|
28
|
+
* @example Failure Case (without default) - throws error
|
32
29
|
* ```ts
|
33
30
|
* import { Result } from '@praha/byethrow';
|
34
31
|
*
|
35
|
-
* const result
|
36
|
-
*
|
32
|
+
* const result = Result.fail('error');
|
33
|
+
* Result.unwrap(result); // throws 'error'
|
37
34
|
* ```
|
38
35
|
*
|
39
|
-
* @example
|
36
|
+
* @example Failure Case (with default)
|
40
37
|
* ```ts
|
41
38
|
* import { Result } from '@praha/byethrow';
|
42
39
|
*
|
43
|
-
* const result
|
44
|
-
* const value =
|
40
|
+
* const result = Result.fail('error');
|
41
|
+
* const value = Result.unwrap(result, 0); // 0
|
45
42
|
* ```
|
46
43
|
*
|
47
44
|
* @category Unwraps
|
48
45
|
*/
|
49
|
-
export declare const unwrap:
|
46
|
+
export declare const unwrap: {
|
47
|
+
<R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
48
|
+
<R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
|
49
|
+
<R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
50
|
+
<R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
|
51
|
+
};
|
package/dist/esm/exports.d.ts
CHANGED
@@ -13,6 +13,7 @@ export * from './functions/is-success';
|
|
13
13
|
export * from './functions/map';
|
14
14
|
export * from './functions/map-error';
|
15
15
|
export * from './functions/or-else';
|
16
|
+
export * from './functions/parse';
|
16
17
|
export * from './functions/pipe';
|
17
18
|
export * from './functions/succeed';
|
18
19
|
export * from './functions/try';
|
package/dist/esm/exports.js
CHANGED
@@ -13,6 +13,7 @@ export * from "./functions/is-success.js";
|
|
13
13
|
export * from "./functions/map.js";
|
14
14
|
export * from "./functions/map-error.js";
|
15
15
|
export * from "./functions/or-else.js";
|
16
|
+
export * from "./functions/parse.js";
|
16
17
|
export * from "./functions/pipe.js";
|
17
18
|
export * from "./functions/succeed.js";
|
18
19
|
export * from "./functions/try.js";
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import type { Result } from '../result';
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
3
|
+
/**
|
4
|
+
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
5
|
+
* Returns a {@link Result} with the parsed value on success or validation errors on failure.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam S - The Standard Schema V1 compatible schema type.
|
9
|
+
* @returns A {@link Result} with the parsed value or validation errors.
|
10
|
+
*
|
11
|
+
* @example Basic Usage
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
* import { z } from 'zod';
|
15
|
+
*
|
16
|
+
* const schema = z.object({ name: z.string() });
|
17
|
+
* const result = Result.parse(schema, { name: 'John' });
|
18
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Curried Usage
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
* import { z } from 'zod';
|
25
|
+
*
|
26
|
+
* const schema = z.object({ name: z.string() });
|
27
|
+
* const parser = Result.parse(schema);
|
28
|
+
* const result = parser({ name: 'John' });
|
29
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* @category Utilities
|
33
|
+
*/
|
34
|
+
export declare const parse: {
|
35
|
+
<S extends StandardSchemaV1>(schema: S): (value: unknown) => Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
36
|
+
<S extends StandardSchemaV1>(schema: S, value: unknown): Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
37
|
+
};
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { fail } from "./fail.js";
|
2
|
+
import { succeed } from "./succeed.js";
|
3
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
4
|
+
const parse = (schema, ...args)=>{
|
5
|
+
const apply = (input)=>{
|
6
|
+
const result = schema['~standard']['validate'](input);
|
7
|
+
if (isPromise(result)) throw new TypeError('Schema validation must be synchronous');
|
8
|
+
return result.issues ? fail(result.issues) : succeed(result.value);
|
9
|
+
};
|
10
|
+
return args.length <= 0 ? apply : apply(args[0]);
|
11
|
+
};
|
12
|
+
export { parse };
|
@@ -1,15 +1,39 @@
|
|
1
1
|
import type { Result, ResultAsync } from '../result';
|
2
2
|
/**
|
3
|
-
* Wraps a function execution (sync or async) in a {@link Result} or {@link ResultAsync} type,
|
3
|
+
* Wraps a function execution (sync or async) or a Promise in a {@link Result} or {@link ResultAsync} type,
|
4
4
|
* capturing errors and returning them in a structured way.
|
5
5
|
*
|
6
6
|
* You can use either a custom `catch` handler or rely on the `safe: true` option
|
7
7
|
* to assume the function cannot throw.
|
8
8
|
*
|
9
9
|
* @function
|
10
|
-
* @typeParam T - The function type to execute (sync or async).
|
10
|
+
* @typeParam T - The function type to execute (sync or async) or a Promise type.
|
11
11
|
* @typeParam E - The error type to return if `catch` is used.
|
12
12
|
*
|
13
|
+
* @example Promise Try-Catch
|
14
|
+
* ```ts
|
15
|
+
* import { Result } from '@praha/byethrow';
|
16
|
+
*
|
17
|
+
* const result = Result.try({
|
18
|
+
* try: fetch('/api/data'),
|
19
|
+
* catch: (error) => new Error('Fetch failed', { cause: error }),
|
20
|
+
* });
|
21
|
+
*
|
22
|
+
* // result is ResultAsync<Response, Error>
|
23
|
+
* ```
|
24
|
+
*
|
25
|
+
* @example Promise Safe
|
26
|
+
* ```ts
|
27
|
+
* import { Result } from '@praha/byethrow';
|
28
|
+
*
|
29
|
+
* const result = Result.try({
|
30
|
+
* safe: true,
|
31
|
+
* try: Promise.resolve('ok'),
|
32
|
+
* });
|
33
|
+
*
|
34
|
+
* // result is ResultAsync<string, never>
|
35
|
+
* ```
|
36
|
+
*
|
13
37
|
* @example Sync Try-Catch
|
14
38
|
* ```ts
|
15
39
|
* import { Result } from '@praha/byethrow';
|
@@ -37,7 +61,7 @@ import type { Result, ResultAsync } from '../result';
|
|
37
61
|
* const result = fn(1); // Result.Result<number, never>
|
38
62
|
* ```
|
39
63
|
*
|
40
|
-
* @example Async Try-Catch
|
64
|
+
* @example Async Function Try-Catch
|
41
65
|
* ```ts
|
42
66
|
* import { Result } from '@praha/byethrow';
|
43
67
|
*
|
@@ -49,7 +73,7 @@ import type { Result, ResultAsync } from '../result';
|
|
49
73
|
* const result = await fn('abc'); // Result.ResultAsync<Response, Error>
|
50
74
|
* ```
|
51
75
|
*
|
52
|
-
* @example Async Safe
|
76
|
+
* @example Async Function Safe
|
53
77
|
* ```ts
|
54
78
|
* import { Result } from '@praha/byethrow';
|
55
79
|
*
|
@@ -64,6 +88,14 @@ import type { Result, ResultAsync } from '../result';
|
|
64
88
|
* @category Creators
|
65
89
|
*/
|
66
90
|
declare const try_: {
|
91
|
+
<T extends Promise<any>, E>(options: {
|
92
|
+
try: T;
|
93
|
+
catch: (error: unknown) => E;
|
94
|
+
}): ResultAsync<Awaited<T>, E>;
|
95
|
+
<T extends Promise<any>>(options: {
|
96
|
+
try: T;
|
97
|
+
safe: true;
|
98
|
+
}): ResultAsync<Awaited<T>, never>;
|
67
99
|
<T extends (...args: readonly any[]) => Promise<any>, E>(options: {
|
68
100
|
try: T;
|
69
101
|
catch: (error: unknown) => E;
|
@@ -1,7 +1,12 @@
|
|
1
1
|
import { fail } from "./fail.js";
|
2
2
|
import { succeed } from "./succeed.js";
|
3
3
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
4
|
-
const try_ = (options)=>
|
4
|
+
const try_ = (options)=>{
|
5
|
+
if (isPromise(options.try)) {
|
6
|
+
if ('safe' in options && options.safe) return succeed(options.try);
|
7
|
+
return options.try.then((value)=>succeed(value), (error)=>fail(options.catch(error)));
|
8
|
+
}
|
9
|
+
return (...args)=>{
|
5
10
|
try {
|
6
11
|
const output = options.try(...args);
|
7
12
|
if (isPromise(output)) {
|
@@ -15,4 +20,5 @@ const try_ = (options)=>(...args)=>{
|
|
15
20
|
return fail(options.catch(error));
|
16
21
|
}
|
17
22
|
};
|
23
|
+
};
|
18
24
|
export { try_ as try };
|
@@ -1,18 +1,15 @@
|
|
1
1
|
import type { HasPromise } from '../internals/types/has-promise';
|
2
2
|
import type { InferFailure, ResultMaybeAsync } from '../result';
|
3
3
|
/**
|
4
|
-
* Extracts the error value from a {@link Result}
|
4
|
+
* Extracts the error value from a {@link Result} or {@link ResultAsync}.
|
5
5
|
*
|
6
|
-
* If the
|
7
|
-
* For {@link ResultAsync}, it returns a Promise that resolves to the error value or rejects with the success value.
|
6
|
+
* If the input is a {@link Success}, it will throw the success value or return the default value if provided.
|
8
7
|
*
|
9
8
|
* @function
|
10
|
-
* @typeParam R - The
|
11
|
-
* @
|
12
|
-
* @returns The error value, or a Promise of the error value for async results.
|
13
|
-
* @throws The success value if the result is a {@link Success}.
|
9
|
+
* @typeParam R - The input {@link Result} or {@link ResultAsync}.
|
10
|
+
* @typeParam T - The default value type (optional).
|
14
11
|
*
|
15
|
-
* @example Failure Case
|
12
|
+
* @example Failure Case (without default)
|
16
13
|
* ```ts
|
17
14
|
* import { Result } from '@praha/byethrow';
|
18
15
|
*
|
@@ -20,30 +17,35 @@ import type { InferFailure, ResultMaybeAsync } from '../result';
|
|
20
17
|
* const error = Result.unwrapError(result); // 'Oops'
|
21
18
|
* ```
|
22
19
|
*
|
23
|
-
* @example
|
20
|
+
* @example Failure Case (with default)
|
24
21
|
* ```ts
|
25
22
|
* import { Result } from '@praha/byethrow';
|
26
23
|
*
|
27
|
-
* const result: Result.Result<number, string> = Result.
|
28
|
-
* const error = Result.unwrapError(result); //
|
24
|
+
* const result: Result.Result<number, string> = Result.fail('Oops');
|
25
|
+
* const error = Result.unwrapError(result, 'default'); // 'Oops'
|
29
26
|
* ```
|
30
27
|
*
|
31
|
-
* @example
|
28
|
+
* @example Success Case (without default)
|
32
29
|
* ```ts
|
33
30
|
* import { Result } from '@praha/byethrow';
|
34
31
|
*
|
35
|
-
* const result: Result.Result<number, string> = Result
|
36
|
-
* const error = Result.unwrapError(result); //
|
32
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
33
|
+
* const error = Result.unwrapError(result); // throws 100
|
37
34
|
* ```
|
38
35
|
*
|
39
|
-
* @example
|
36
|
+
* @example Success Case (with default)
|
40
37
|
* ```ts
|
41
38
|
* import { Result } from '@praha/byethrow';
|
42
39
|
*
|
43
|
-
* const result: Result.Result<number, string> = Result.succeed(
|
44
|
-
* const error = Result.unwrapError(result); //
|
40
|
+
* const result: Result.Result<number, string> = Result.succeed(100);
|
41
|
+
* const error = Result.unwrapError(result, 0); // 0
|
45
42
|
* ```
|
46
43
|
*
|
47
44
|
* @category Unwraps
|
48
45
|
*/
|
49
|
-
export declare const unwrapError:
|
46
|
+
export declare const unwrapError: {
|
47
|
+
<R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
48
|
+
<R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
|
49
|
+
<R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R>> : InferFailure<R>;
|
50
|
+
<R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferFailure<R> | T> : InferFailure<R> | T;
|
51
|
+
};
|
@@ -1,11 +1,32 @@
|
|
1
|
+
import { isResult } from "./is-result.js";
|
1
2
|
import { isSuccess } from "./is-success.js";
|
2
3
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
3
|
-
const unwrapError = (
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
const unwrapError = (...args)=>{
|
5
|
+
const firstArgument = args[0];
|
6
|
+
if (isResult(firstArgument) || isPromise(firstArgument)) {
|
7
|
+
const result = firstArgument;
|
8
|
+
const hasDefault = 2 === args.length;
|
9
|
+
const defaultValue = hasDefault ? args[1] : void 0;
|
10
|
+
const apply = (r)=>{
|
11
|
+
if (isSuccess(r)) {
|
12
|
+
if (hasDefault) return defaultValue;
|
13
|
+
throw new Error(String(r.value));
|
14
|
+
}
|
15
|
+
return r.error;
|
16
|
+
};
|
17
|
+
return isPromise(result) ? result.then(apply) : apply(result);
|
18
|
+
}
|
19
|
+
const hasDefault = 1 === args.length;
|
20
|
+
const defaultValue = hasDefault ? args[0] : void 0;
|
21
|
+
return (result)=>{
|
22
|
+
const apply = (r)=>{
|
23
|
+
if (isSuccess(r)) {
|
24
|
+
if (hasDefault) return defaultValue;
|
25
|
+
throw new Error(String(r.value));
|
26
|
+
}
|
27
|
+
return r.error;
|
28
|
+
};
|
29
|
+
return isPromise(result) ? result.then(apply) : apply(result);
|
30
|
+
};
|
10
31
|
};
|
11
32
|
export { unwrapError };
|
@@ -1,49 +1,51 @@
|
|
1
1
|
import type { HasPromise } from '../internals/types/has-promise';
|
2
2
|
import type { InferSuccess, ResultMaybeAsync } from '../result';
|
3
3
|
/**
|
4
|
-
* Extracts the success value from a {@link Result}
|
4
|
+
* Extracts the success value from a {@link Result} or {@link ResultAsync}.
|
5
5
|
*
|
6
|
-
* If the
|
7
|
-
* For {@link ResultAsync}, it returns a Promise that resolves to the success value or rejects with the error.
|
6
|
+
* If the input is a {@link Failure}, it will throw the error or return the default value if provided.
|
8
7
|
*
|
9
8
|
* @function
|
10
|
-
* @typeParam R - The
|
11
|
-
* @
|
12
|
-
* @returns The success value, or a Promise of the success value for async results.
|
13
|
-
* @throws The error value if the result is a {@link Failure}.
|
9
|
+
* @typeParam R - The input {@link Result} or {@link ResultAsync}.
|
10
|
+
* @typeParam T - The default value type (optional).
|
14
11
|
*
|
15
|
-
* @example Success Case
|
12
|
+
* @example Success Case (without default)
|
16
13
|
* ```ts
|
17
14
|
* import { Result } from '@praha/byethrow';
|
18
15
|
*
|
19
|
-
* const result
|
20
|
-
* const value = Result.unwrap(result); //
|
16
|
+
* const result = Result.succeed(42);
|
17
|
+
* const value = Result.unwrap(result); // 42
|
21
18
|
* ```
|
22
19
|
*
|
23
|
-
* @example
|
20
|
+
* @example Success Case (with default)
|
24
21
|
* ```ts
|
25
22
|
* import { Result } from '@praha/byethrow';
|
26
23
|
*
|
27
|
-
* const result
|
28
|
-
* const value = Result.unwrap(result); //
|
24
|
+
* const result = Result.succeed(42);
|
25
|
+
* const value = Result.unwrap(result, 0); // 42
|
29
26
|
* ```
|
30
27
|
*
|
31
|
-
* @example
|
28
|
+
* @example Failure Case (without default) - throws error
|
32
29
|
* ```ts
|
33
30
|
* import { Result } from '@praha/byethrow';
|
34
31
|
*
|
35
|
-
* const result
|
36
|
-
*
|
32
|
+
* const result = Result.fail('error');
|
33
|
+
* Result.unwrap(result); // throws 'error'
|
37
34
|
* ```
|
38
35
|
*
|
39
|
-
* @example
|
36
|
+
* @example Failure Case (with default)
|
40
37
|
* ```ts
|
41
38
|
* import { Result } from '@praha/byethrow';
|
42
39
|
*
|
43
|
-
* const result
|
44
|
-
* const value =
|
40
|
+
* const result = Result.fail('error');
|
41
|
+
* const value = Result.unwrap(result, 0); // 0
|
45
42
|
* ```
|
46
43
|
*
|
47
44
|
* @category Unwraps
|
48
45
|
*/
|
49
|
-
export declare const unwrap:
|
46
|
+
export declare const unwrap: {
|
47
|
+
<R extends ResultMaybeAsync<any, any>>(result: R): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
48
|
+
<R extends ResultMaybeAsync<any, any>, T>(result: R, defaultValue: T): true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
|
49
|
+
<R extends ResultMaybeAsync<any, any>>(): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>;
|
50
|
+
<R extends ResultMaybeAsync<any, any>, T>(defaultValue: T): (result: R) => true extends HasPromise<R> ? Promise<InferSuccess<R> | T> : InferSuccess<R> | T;
|
51
|
+
};
|
@@ -1,11 +1,32 @@
|
|
1
1
|
import { isFailure } from "./is-failure.js";
|
2
|
+
import { isResult } from "./is-result.js";
|
2
3
|
import { isPromise } from "../internals/helpers/is-promise.js";
|
3
|
-
const unwrap = (
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
const unwrap = (...args)=>{
|
5
|
+
const firstArgument = args[0];
|
6
|
+
if (isResult(firstArgument) || isPromise(firstArgument)) {
|
7
|
+
const result = firstArgument;
|
8
|
+
const hasDefault = 2 === args.length;
|
9
|
+
const defaultValue = hasDefault ? args[1] : void 0;
|
10
|
+
const apply = (r)=>{
|
11
|
+
if (isFailure(r)) {
|
12
|
+
if (hasDefault) return defaultValue;
|
13
|
+
throw new Error(String(r.error));
|
14
|
+
}
|
15
|
+
return r.value;
|
16
|
+
};
|
17
|
+
return isPromise(result) ? result.then(apply) : apply(result);
|
18
|
+
}
|
19
|
+
const hasDefault = 1 === args.length;
|
20
|
+
const defaultValue = hasDefault ? args[0] : void 0;
|
21
|
+
return (result)=>{
|
22
|
+
const apply = (r)=>{
|
23
|
+
if (isFailure(r)) {
|
24
|
+
if (hasDefault) return defaultValue;
|
25
|
+
throw new Error(String(r.error));
|
26
|
+
}
|
27
|
+
return r.value;
|
28
|
+
};
|
29
|
+
return isPromise(result) ? result.then(apply) : apply(result);
|
30
|
+
};
|
10
31
|
};
|
11
32
|
export { unwrap };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@praha/byethrow",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
5
5
|
"keywords": [
|
6
6
|
"javascript",
|
@@ -40,9 +40,12 @@
|
|
40
40
|
"LICENSE",
|
41
41
|
"README.md"
|
42
42
|
],
|
43
|
+
"dependencies": {
|
44
|
+
"@standard-schema/spec": "^1.0.0"
|
45
|
+
},
|
43
46
|
"devDependencies": {
|
44
47
|
"@rslib/core": "0.10.4",
|
45
|
-
"eslint": "9.30.
|
48
|
+
"eslint": "9.30.1",
|
46
49
|
"typedoc": "0.28.7",
|
47
50
|
"typescript": "5.8.3",
|
48
51
|
"vitest": "3.2.4"
|