@zireal/result-kit 1.0.0 → 1.0.2
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/core/index.cjs +1 -1
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +1 -1
- package/dist/index-DioLW1WH.d.cts +482 -0
- package/dist/index-SLgCKCJe.d.mts +482 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/nest/index.cjs +59 -1
- package/dist/nest/index.d.cts +72 -1
- package/dist/nest/index.d.mts +72 -1
- package/dist/nest/index.mjs +59 -1
- package/dist/result-DdlZMLaP.d.cts +44 -0
- package/dist/result-hklHYniG.d.mts +44 -0
- package/dist/result-kit-DdhY1ph2.cjs +565 -0
- package/dist/result-kit-pgireOSz.mjs +554 -0
- package/package.json +13 -13
- package/dist/index-Ch6k0lVU.d.mts +0 -70
- package/dist/index-EJ_Ol5Fu.d.cts +0 -70
- package/dist/result-CI_HBDHK.d.mts +0 -13
- package/dist/result-DbQ8o1Cs.d.cts +0 -13
- package/dist/result-kit-C5ZY61pO.mjs +0 -172
- package/dist/result-kit-Ck1xAp-J.cjs +0 -183
package/dist/nest/index.d.mts
CHANGED
|
@@ -1,20 +1,91 @@
|
|
|
1
|
-
import { n as Result } from "../result-
|
|
1
|
+
import { n as Result } from "../result-hklHYniG.mjs";
|
|
2
2
|
import { HttpException } from "@nestjs/common";
|
|
3
3
|
|
|
4
4
|
//#region src/nest/http.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Describes the HTTP exception payload that should be produced for a failure.
|
|
7
|
+
*
|
|
8
|
+
* Returning this shape from `mapError` lets callers control HTTP status and
|
|
9
|
+
* response body without constructing a Nest `HttpException` instance
|
|
10
|
+
* themselves.
|
|
11
|
+
*/
|
|
5
12
|
interface HttpExceptionDescriptor {
|
|
13
|
+
/**
|
|
14
|
+
* HTTP status code to use for the generated exception.
|
|
15
|
+
*/
|
|
6
16
|
readonly status?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Stable application error code included in the response body.
|
|
19
|
+
*/
|
|
7
20
|
readonly code?: string | number;
|
|
21
|
+
/**
|
|
22
|
+
* Human-readable error message included in the response body.
|
|
23
|
+
*/
|
|
8
24
|
readonly message?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Optional structured metadata added to the response body.
|
|
27
|
+
*/
|
|
9
28
|
readonly details?: Record<string, unknown>;
|
|
29
|
+
/**
|
|
30
|
+
* Optional top-level Nest-style error label.
|
|
31
|
+
*/
|
|
10
32
|
readonly error?: string;
|
|
11
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Configures how result failures should be mapped into Nest HTTP exceptions.
|
|
36
|
+
*/
|
|
12
37
|
interface NestErrorOptions<E> {
|
|
38
|
+
/**
|
|
39
|
+
* Optional custom mapper for converting a domain error into either a ready
|
|
40
|
+
* `HttpException` or a descriptor used to build one.
|
|
41
|
+
*/
|
|
13
42
|
readonly mapError?: (error: E) => HttpException | HttpExceptionDescriptor | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Fallback message used for unknown failures when a better message cannot be
|
|
45
|
+
* derived from the error value.
|
|
46
|
+
*/
|
|
14
47
|
readonly fallbackMessage?: string;
|
|
15
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Converts an arbitrary error value into a Nest `HttpException`.
|
|
51
|
+
*
|
|
52
|
+
* Resolution order is:
|
|
53
|
+
* 1. `options.mapError` returning a `HttpException`
|
|
54
|
+
* 2. `options.mapError` returning a {@link HttpExceptionDescriptor}
|
|
55
|
+
* 3. An incoming `HttpException`
|
|
56
|
+
* 4. A core typed error
|
|
57
|
+
* 5. A generic JavaScript `Error`
|
|
58
|
+
* 6. An unknown fallback internal server error
|
|
59
|
+
*
|
|
60
|
+
* @param error Error value to convert.
|
|
61
|
+
* @param options Optional mapping and fallback configuration.
|
|
62
|
+
* @returns A Nest `HttpException` representing the provided error.
|
|
63
|
+
*/
|
|
16
64
|
declare const toHttpException: <E>(error: E, options?: NestErrorOptions<E>) => HttpException;
|
|
65
|
+
/**
|
|
66
|
+
* Extracts the success value from a result or throws an HTTP exception derived
|
|
67
|
+
* from the failure.
|
|
68
|
+
*
|
|
69
|
+
* This is intended for Nest controller and adapter boundaries where a result
|
|
70
|
+
* should be converted into framework-native exception flow.
|
|
71
|
+
*
|
|
72
|
+
* @param result Result to unwrap.
|
|
73
|
+
* @param options Optional error mapping and fallback configuration.
|
|
74
|
+
* @returns The successful value when `result` is successful.
|
|
75
|
+
* @throws {HttpException} When `result` is failed.
|
|
76
|
+
*/
|
|
17
77
|
declare const unwrapOrThrow: <T, E>(result: Result<T, E>, options?: NestErrorOptions<E>) => T;
|
|
78
|
+
/**
|
|
79
|
+
* Awaits a promised result and extracts its success value or throws a mapped
|
|
80
|
+
* HTTP exception.
|
|
81
|
+
*
|
|
82
|
+
* Use this when service methods already resolve to `Promise<Result<...>>`.
|
|
83
|
+
*
|
|
84
|
+
* @param promise Promise resolving to a result.
|
|
85
|
+
* @param options Optional error mapping and fallback configuration.
|
|
86
|
+
* @returns A promise resolving to the successful value.
|
|
87
|
+
* @throws {HttpException} When the resolved result is failed.
|
|
88
|
+
*/
|
|
18
89
|
declare const unwrapPromise: <T, E>(promise: Promise<Result<T, E>>, options?: NestErrorOptions<E>) => Promise<T>;
|
|
19
90
|
//#endregion
|
|
20
91
|
export { type HttpExceptionDescriptor, type NestErrorOptions, toHttpException, unwrapOrThrow, unwrapPromise };
|
package/dist/nest/index.mjs
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
|
-
import { n as isTypedError, t as ResultKit } from "../result-kit-
|
|
1
|
+
import { n as isTypedError, t as ResultKit } from "../result-kit-pgireOSz.mjs";
|
|
2
2
|
import { HttpException, HttpStatus, InternalServerErrorException } from "@nestjs/common";
|
|
3
3
|
//#region src/nest/http.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default message used when the incoming error value does not expose a usable
|
|
6
|
+
* message.
|
|
7
|
+
*/
|
|
4
8
|
const DEFAULT_UNKNOWN_MESSAGE = "An unknown error occurred";
|
|
9
|
+
/**
|
|
10
|
+
* Default application error code used for internal server failures.
|
|
11
|
+
*/
|
|
5
12
|
const DEFAULT_ERROR_CODE = "INTERNAL_SERVER_ERROR";
|
|
13
|
+
/**
|
|
14
|
+
* Normalizes an arbitrary error type string into an uppercase code suitable
|
|
15
|
+
* for HTTP responses.
|
|
16
|
+
*
|
|
17
|
+
* @param value Raw error type value.
|
|
18
|
+
* @returns A sanitized, uppercase error code or the internal default code.
|
|
19
|
+
*/
|
|
6
20
|
const normalizeErrorCode = (value) => value.trim().replace(/[^a-zA-Z0-9]+/g, "_").replace(/^_+|_+$/g, "").toUpperCase() || DEFAULT_ERROR_CODE;
|
|
21
|
+
/**
|
|
22
|
+
* Builds a Nest `HttpException` from a lightweight descriptor object.
|
|
23
|
+
*
|
|
24
|
+
* @param descriptor Response descriptor to convert.
|
|
25
|
+
* @returns A concrete `HttpException` with normalized payload defaults.
|
|
26
|
+
*/
|
|
7
27
|
const toHttpExceptionFromDescriptor = (descriptor) => {
|
|
8
28
|
const status = descriptor.status ?? HttpStatus.INTERNAL_SERVER_ERROR;
|
|
9
29
|
return new HttpException({
|
|
@@ -13,6 +33,21 @@ const toHttpExceptionFromDescriptor = (descriptor) => {
|
|
|
13
33
|
...descriptor.error ? { error: descriptor.error } : {}
|
|
14
34
|
}, status);
|
|
15
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Converts an arbitrary error value into a Nest `HttpException`.
|
|
38
|
+
*
|
|
39
|
+
* Resolution order is:
|
|
40
|
+
* 1. `options.mapError` returning a `HttpException`
|
|
41
|
+
* 2. `options.mapError` returning a {@link HttpExceptionDescriptor}
|
|
42
|
+
* 3. An incoming `HttpException`
|
|
43
|
+
* 4. A core typed error
|
|
44
|
+
* 5. A generic JavaScript `Error`
|
|
45
|
+
* 6. An unknown fallback internal server error
|
|
46
|
+
*
|
|
47
|
+
* @param error Error value to convert.
|
|
48
|
+
* @param options Optional mapping and fallback configuration.
|
|
49
|
+
* @returns A Nest `HttpException` representing the provided error.
|
|
50
|
+
*/
|
|
16
51
|
const toHttpException = (error, options) => {
|
|
17
52
|
const mapped = options?.mapError?.(error);
|
|
18
53
|
if (mapped instanceof HttpException) return mapped;
|
|
@@ -32,10 +67,33 @@ const toHttpException = (error, options) => {
|
|
|
32
67
|
message: options?.fallbackMessage || DEFAULT_UNKNOWN_MESSAGE
|
|
33
68
|
});
|
|
34
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Extracts the success value from a result or throws an HTTP exception derived
|
|
72
|
+
* from the failure.
|
|
73
|
+
*
|
|
74
|
+
* This is intended for Nest controller and adapter boundaries where a result
|
|
75
|
+
* should be converted into framework-native exception flow.
|
|
76
|
+
*
|
|
77
|
+
* @param result Result to unwrap.
|
|
78
|
+
* @param options Optional error mapping and fallback configuration.
|
|
79
|
+
* @returns The successful value when `result` is successful.
|
|
80
|
+
* @throws {HttpException} When `result` is failed.
|
|
81
|
+
*/
|
|
35
82
|
const unwrapOrThrow = (result, options) => {
|
|
36
83
|
if (ResultKit.isSuccess(result)) return result.value;
|
|
37
84
|
throw toHttpException(result.error, options);
|
|
38
85
|
};
|
|
86
|
+
/**
|
|
87
|
+
* Awaits a promised result and extracts its success value or throws a mapped
|
|
88
|
+
* HTTP exception.
|
|
89
|
+
*
|
|
90
|
+
* Use this when service methods already resolve to `Promise<Result<...>>`.
|
|
91
|
+
*
|
|
92
|
+
* @param promise Promise resolving to a result.
|
|
93
|
+
* @param options Optional error mapping and fallback configuration.
|
|
94
|
+
* @returns A promise resolving to the successful value.
|
|
95
|
+
* @throws {HttpException} When the resolved result is failed.
|
|
96
|
+
*/
|
|
39
97
|
const unwrapPromise = async (promise, options) => unwrapOrThrow(await promise, options);
|
|
40
98
|
//#endregion
|
|
41
99
|
export { toHttpException, unwrapOrThrow, unwrapPromise };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/core/result.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the successful branch of a {@link Result}.
|
|
4
|
+
*
|
|
5
|
+
* Use this shape when an operation completed normally and produced a value
|
|
6
|
+
* that should continue through the rest of the pipeline.
|
|
7
|
+
*/
|
|
8
|
+
interface Success<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Discriminant used to identify successful results at runtime.
|
|
11
|
+
*/
|
|
12
|
+
readonly ok: true;
|
|
13
|
+
/**
|
|
14
|
+
* Value produced by the successful operation.
|
|
15
|
+
*/
|
|
16
|
+
readonly value: T;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents the failed branch of a {@link Result}.
|
|
20
|
+
*
|
|
21
|
+
* Use this shape when an operation did not produce a value and instead
|
|
22
|
+
* returned domain, validation, transport, or infrastructure error data.
|
|
23
|
+
*/
|
|
24
|
+
interface Failure<E> {
|
|
25
|
+
/**
|
|
26
|
+
* Discriminant used to identify failed results at runtime.
|
|
27
|
+
*/
|
|
28
|
+
readonly ok: false;
|
|
29
|
+
/**
|
|
30
|
+
* Error payload carried by the failed operation.
|
|
31
|
+
*/
|
|
32
|
+
readonly error: E;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Models an operation that can either succeed with a value of `T`
|
|
36
|
+
* or fail with an error of `E`.
|
|
37
|
+
*
|
|
38
|
+
* This is the package's core transport type for explicit, exception-free
|
|
39
|
+
* control flow.
|
|
40
|
+
*/
|
|
41
|
+
type Result<T, E> = Success<T> | Failure<E>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { Result as n, Success as r, Failure as t };
|
|
44
|
+
//# sourceMappingURL=result-DdlZMLaP.d.cts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/core/result.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Represents the successful branch of a {@link Result}.
|
|
4
|
+
*
|
|
5
|
+
* Use this shape when an operation completed normally and produced a value
|
|
6
|
+
* that should continue through the rest of the pipeline.
|
|
7
|
+
*/
|
|
8
|
+
interface Success<T> {
|
|
9
|
+
/**
|
|
10
|
+
* Discriminant used to identify successful results at runtime.
|
|
11
|
+
*/
|
|
12
|
+
readonly ok: true;
|
|
13
|
+
/**
|
|
14
|
+
* Value produced by the successful operation.
|
|
15
|
+
*/
|
|
16
|
+
readonly value: T;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents the failed branch of a {@link Result}.
|
|
20
|
+
*
|
|
21
|
+
* Use this shape when an operation did not produce a value and instead
|
|
22
|
+
* returned domain, validation, transport, or infrastructure error data.
|
|
23
|
+
*/
|
|
24
|
+
interface Failure<E> {
|
|
25
|
+
/**
|
|
26
|
+
* Discriminant used to identify failed results at runtime.
|
|
27
|
+
*/
|
|
28
|
+
readonly ok: false;
|
|
29
|
+
/**
|
|
30
|
+
* Error payload carried by the failed operation.
|
|
31
|
+
*/
|
|
32
|
+
readonly error: E;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Models an operation that can either succeed with a value of `T`
|
|
36
|
+
* or fail with an error of `E`.
|
|
37
|
+
*
|
|
38
|
+
* This is the package's core transport type for explicit, exception-free
|
|
39
|
+
* control flow.
|
|
40
|
+
*/
|
|
41
|
+
type Result<T, E> = Success<T> | Failure<E>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { Result as n, Success as r, Failure as t };
|
|
44
|
+
//# sourceMappingURL=result-hklHYniG.d.mts.map
|