@couimet/detailed-result 0.1.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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.d.mts +71 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +133 -0
- package/dist/index.mjs +105 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Charles Ouimet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @couimet/detailed-result
|
|
2
|
+
|
|
3
|
+
Functional Result type for explicit error handling paired with `@couimet/detailed-error`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Create results with the `ok()` and `err()` factories, then check `.success` before accessing `.value` or `.error`:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { DetailedResult } from '@couimet/detailed-result';
|
|
11
|
+
|
|
12
|
+
function divide(a: number, b: number): DetailedResult<number, string> {
|
|
13
|
+
if (b === 0) {
|
|
14
|
+
return DetailedResult.err('Division by zero');
|
|
15
|
+
}
|
|
16
|
+
return DetailedResult.ok(a / b);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const result = divide(10, 2);
|
|
20
|
+
if (result.success) {
|
|
21
|
+
console.log(result.value); // 5
|
|
22
|
+
} else {
|
|
23
|
+
console.error(result.error);
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Pin the error type** by subclassing — override the factories, keep the constructor hidden:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
class MyResult<T> extends DetailedResult<T, MyError> {
|
|
31
|
+
static ok<T>(value: T): MyResult<T> {
|
|
32
|
+
return new MyResult(true, value, undefined);
|
|
33
|
+
}
|
|
34
|
+
static err(error: MyError): MyResult<never> {
|
|
35
|
+
return new MyResult(false, undefined, error);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const result = MyResult.ok(42);
|
|
40
|
+
// result.error is typed as MyError
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Accessing `.value` on an error result (or `.error` on a success result) throws a `DetailedError` with a `DetailedResultErrorCodes` code — these are invariant violations that signal a bug in the calling code (missing `.success` check).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functional error handling Value Object.
|
|
3
|
+
*
|
|
4
|
+
* Represents either a successful value or an error. Use {@link DetailedResult.ok} and
|
|
5
|
+
* {@link DetailedResult.err} factories to create instances, check `.success` before
|
|
6
|
+
* accessing `.value` or `.error`.
|
|
7
|
+
*
|
|
8
|
+
* The constructor is `protected` so subclasses can pin the error type by overriding
|
|
9
|
+
* the static factories.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The success value type.
|
|
12
|
+
* @typeParam E - The error type. Unconstrained — use plain `Error`, {@link DetailedError}, or a project-specific subclass.
|
|
13
|
+
*/
|
|
14
|
+
declare class DetailedResult<T, E> {
|
|
15
|
+
private readonly _success;
|
|
16
|
+
private readonly _value;
|
|
17
|
+
private readonly _error;
|
|
18
|
+
/**
|
|
19
|
+
* Not for public use. Use {@link DetailedResult.ok} or {@link DetailedResult.err} factories instead.
|
|
20
|
+
*
|
|
21
|
+
* Marked `protected` so subclasses can extend to pin the error type.
|
|
22
|
+
*/
|
|
23
|
+
protected constructor(success: boolean, value: T | undefined, error: E | undefined);
|
|
24
|
+
/** Create a successful {@link DetailedResult} containing a value. */
|
|
25
|
+
static ok<T>(value: T): DetailedResult<T, never>;
|
|
26
|
+
/** Create an error {@link DetailedResult} containing an error. */
|
|
27
|
+
static err<E>(error: E): DetailedResult<never, E>;
|
|
28
|
+
/** Check if this {@link DetailedResult} is successful. Always check this before accessing `.value` or `.error`. */
|
|
29
|
+
get success(): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Get the success value. Throws a {@link DetailedError} with code
|
|
32
|
+
* {@link DetailedResultErrorCodes.RESULT_VALUE_ACCESS_ON_ERROR} if this is an error result.
|
|
33
|
+
*/
|
|
34
|
+
get value(): T;
|
|
35
|
+
/**
|
|
36
|
+
* Get the error. Throws a {@link DetailedError} with code
|
|
37
|
+
* {@link DetailedResultErrorCodes.RESULT_ERROR_ACCESS_ON_SUCCESS} if this is a success result.
|
|
38
|
+
*/
|
|
39
|
+
get error(): E;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Error codes for {@link DetailedResult} internal invariant violations.
|
|
44
|
+
*
|
|
45
|
+
* These codes are used when {@link DetailedResult} detects an invalid state,
|
|
46
|
+
* such as accessing `.value` on an error result or `.error` on a success result.
|
|
47
|
+
* They are not expected to appear in normal application flow — they signal a bug
|
|
48
|
+
* in the calling code (missing `.success` check before access).
|
|
49
|
+
*/
|
|
50
|
+
declare enum DetailedResultErrorCodes {
|
|
51
|
+
/**
|
|
52
|
+
* Attempted to access `.error` on a successful {@link DetailedResult}.
|
|
53
|
+
* Always check `.success` before accessing `.error`.
|
|
54
|
+
*/
|
|
55
|
+
RESULT_ERROR_ACCESS_ON_SUCCESS = "RESULT_ERROR_ACCESS_ON_SUCCESS",
|
|
56
|
+
/**
|
|
57
|
+
* {@link DetailedResult} was constructed with an invalid combination of arguments:
|
|
58
|
+
* either a success result with an error defined, or an error result with a value defined.
|
|
59
|
+
* This should never happen through the public factory methods ({@link DetailedResult.ok} /
|
|
60
|
+
* {@link DetailedResult.err}); it can only be triggered by a subclass constructor
|
|
61
|
+
* passing inconsistent arguments.
|
|
62
|
+
*/
|
|
63
|
+
RESULT_INVALID_STATE = "RESULT_INVALID_STATE",
|
|
64
|
+
/**
|
|
65
|
+
* Attempted to access `.value` on an error {@link DetailedResult}.
|
|
66
|
+
* Always check `.success` before accessing `.value`.
|
|
67
|
+
*/
|
|
68
|
+
RESULT_VALUE_ACCESS_ON_ERROR = "RESULT_VALUE_ACCESS_ON_ERROR"
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { DetailedResult, DetailedResultErrorCodes };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functional error handling Value Object.
|
|
3
|
+
*
|
|
4
|
+
* Represents either a successful value or an error. Use {@link DetailedResult.ok} and
|
|
5
|
+
* {@link DetailedResult.err} factories to create instances, check `.success` before
|
|
6
|
+
* accessing `.value` or `.error`.
|
|
7
|
+
*
|
|
8
|
+
* The constructor is `protected` so subclasses can pin the error type by overriding
|
|
9
|
+
* the static factories.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The success value type.
|
|
12
|
+
* @typeParam E - The error type. Unconstrained — use plain `Error`, {@link DetailedError}, or a project-specific subclass.
|
|
13
|
+
*/
|
|
14
|
+
declare class DetailedResult<T, E> {
|
|
15
|
+
private readonly _success;
|
|
16
|
+
private readonly _value;
|
|
17
|
+
private readonly _error;
|
|
18
|
+
/**
|
|
19
|
+
* Not for public use. Use {@link DetailedResult.ok} or {@link DetailedResult.err} factories instead.
|
|
20
|
+
*
|
|
21
|
+
* Marked `protected` so subclasses can extend to pin the error type.
|
|
22
|
+
*/
|
|
23
|
+
protected constructor(success: boolean, value: T | undefined, error: E | undefined);
|
|
24
|
+
/** Create a successful {@link DetailedResult} containing a value. */
|
|
25
|
+
static ok<T>(value: T): DetailedResult<T, never>;
|
|
26
|
+
/** Create an error {@link DetailedResult} containing an error. */
|
|
27
|
+
static err<E>(error: E): DetailedResult<never, E>;
|
|
28
|
+
/** Check if this {@link DetailedResult} is successful. Always check this before accessing `.value` or `.error`. */
|
|
29
|
+
get success(): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Get the success value. Throws a {@link DetailedError} with code
|
|
32
|
+
* {@link DetailedResultErrorCodes.RESULT_VALUE_ACCESS_ON_ERROR} if this is an error result.
|
|
33
|
+
*/
|
|
34
|
+
get value(): T;
|
|
35
|
+
/**
|
|
36
|
+
* Get the error. Throws a {@link DetailedError} with code
|
|
37
|
+
* {@link DetailedResultErrorCodes.RESULT_ERROR_ACCESS_ON_SUCCESS} if this is a success result.
|
|
38
|
+
*/
|
|
39
|
+
get error(): E;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Error codes for {@link DetailedResult} internal invariant violations.
|
|
44
|
+
*
|
|
45
|
+
* These codes are used when {@link DetailedResult} detects an invalid state,
|
|
46
|
+
* such as accessing `.value` on an error result or `.error` on a success result.
|
|
47
|
+
* They are not expected to appear in normal application flow — they signal a bug
|
|
48
|
+
* in the calling code (missing `.success` check before access).
|
|
49
|
+
*/
|
|
50
|
+
declare enum DetailedResultErrorCodes {
|
|
51
|
+
/**
|
|
52
|
+
* Attempted to access `.error` on a successful {@link DetailedResult}.
|
|
53
|
+
* Always check `.success` before accessing `.error`.
|
|
54
|
+
*/
|
|
55
|
+
RESULT_ERROR_ACCESS_ON_SUCCESS = "RESULT_ERROR_ACCESS_ON_SUCCESS",
|
|
56
|
+
/**
|
|
57
|
+
* {@link DetailedResult} was constructed with an invalid combination of arguments:
|
|
58
|
+
* either a success result with an error defined, or an error result with a value defined.
|
|
59
|
+
* This should never happen through the public factory methods ({@link DetailedResult.ok} /
|
|
60
|
+
* {@link DetailedResult.err}); it can only be triggered by a subclass constructor
|
|
61
|
+
* passing inconsistent arguments.
|
|
62
|
+
*/
|
|
63
|
+
RESULT_INVALID_STATE = "RESULT_INVALID_STATE",
|
|
64
|
+
/**
|
|
65
|
+
* Attempted to access `.value` on an error {@link DetailedResult}.
|
|
66
|
+
* Always check `.success` before accessing `.value`.
|
|
67
|
+
*/
|
|
68
|
+
RESULT_VALUE_ACCESS_ON_ERROR = "RESULT_VALUE_ACCESS_ON_ERROR"
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export { DetailedResult, DetailedResultErrorCodes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
DetailedResult: () => DetailedResult,
|
|
24
|
+
DetailedResultErrorCodes: () => DetailedResultErrorCodes
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/DetailedResultErrorCodes.ts
|
|
29
|
+
var DetailedResultErrorCodes = /* @__PURE__ */ ((DetailedResultErrorCodes2) => {
|
|
30
|
+
DetailedResultErrorCodes2["RESULT_ERROR_ACCESS_ON_SUCCESS"] = "RESULT_ERROR_ACCESS_ON_SUCCESS";
|
|
31
|
+
DetailedResultErrorCodes2["RESULT_INVALID_STATE"] = "RESULT_INVALID_STATE";
|
|
32
|
+
DetailedResultErrorCodes2["RESULT_VALUE_ACCESS_ON_ERROR"] = "RESULT_VALUE_ACCESS_ON_ERROR";
|
|
33
|
+
return DetailedResultErrorCodes2;
|
|
34
|
+
})(DetailedResultErrorCodes || {});
|
|
35
|
+
|
|
36
|
+
// src/DetailedResult.ts
|
|
37
|
+
var import_detailed_error = require("@couimet/detailed-error");
|
|
38
|
+
var DetailedResult = class _DetailedResult {
|
|
39
|
+
_success;
|
|
40
|
+
_value;
|
|
41
|
+
_error;
|
|
42
|
+
/**
|
|
43
|
+
* Not for public use. Use {@link DetailedResult.ok} or {@link DetailedResult.err} factories instead.
|
|
44
|
+
*
|
|
45
|
+
* Marked `protected` so subclasses can extend to pin the error type.
|
|
46
|
+
*/
|
|
47
|
+
constructor(success, value, error) {
|
|
48
|
+
if (success && error !== void 0) {
|
|
49
|
+
throw new import_detailed_error.DetailedError({
|
|
50
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
51
|
+
message: "DetailedResult marked as success cannot have an error defined",
|
|
52
|
+
functionName: "DetailedResult.constructor",
|
|
53
|
+
details: {
|
|
54
|
+
success,
|
|
55
|
+
hasValue: value !== void 0,
|
|
56
|
+
hasError: error !== void 0
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (!success && value !== void 0) {
|
|
61
|
+
throw new import_detailed_error.DetailedError({
|
|
62
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
63
|
+
message: "DetailedResult marked as error cannot have a value defined",
|
|
64
|
+
functionName: "DetailedResult.constructor",
|
|
65
|
+
details: {
|
|
66
|
+
success,
|
|
67
|
+
hasValue: value !== void 0,
|
|
68
|
+
hasError: error !== void 0
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (!success && error === void 0) {
|
|
73
|
+
throw new import_detailed_error.DetailedError({
|
|
74
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
75
|
+
message: "DetailedResult marked as error must have an error defined",
|
|
76
|
+
functionName: "DetailedResult.constructor",
|
|
77
|
+
details: {
|
|
78
|
+
success,
|
|
79
|
+
hasValue: value !== void 0,
|
|
80
|
+
hasError: error !== void 0
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
this._success = success;
|
|
85
|
+
this._value = value;
|
|
86
|
+
this._error = error;
|
|
87
|
+
}
|
|
88
|
+
/** Create a successful {@link DetailedResult} containing a value. */
|
|
89
|
+
static ok(value) {
|
|
90
|
+
return new _DetailedResult(true, value, void 0);
|
|
91
|
+
}
|
|
92
|
+
/** Create an error {@link DetailedResult} containing an error. */
|
|
93
|
+
static err(error) {
|
|
94
|
+
return new _DetailedResult(false, void 0, error);
|
|
95
|
+
}
|
|
96
|
+
/** Check if this {@link DetailedResult} is successful. Always check this before accessing `.value` or `.error`. */
|
|
97
|
+
get success() {
|
|
98
|
+
return this._success;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get the success value. Throws a {@link DetailedError} with code
|
|
102
|
+
* {@link DetailedResultErrorCodes.RESULT_VALUE_ACCESS_ON_ERROR} if this is an error result.
|
|
103
|
+
*/
|
|
104
|
+
get value() {
|
|
105
|
+
if (!this._success) {
|
|
106
|
+
throw new import_detailed_error.DetailedError({
|
|
107
|
+
code: "RESULT_VALUE_ACCESS_ON_ERROR" /* RESULT_VALUE_ACCESS_ON_ERROR */,
|
|
108
|
+
message: "Cannot access value on an error DetailedResult. Check .success before accessing .value",
|
|
109
|
+
functionName: "DetailedResult.value"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return this._value;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the error. Throws a {@link DetailedError} with code
|
|
116
|
+
* {@link DetailedResultErrorCodes.RESULT_ERROR_ACCESS_ON_SUCCESS} if this is a success result.
|
|
117
|
+
*/
|
|
118
|
+
get error() {
|
|
119
|
+
if (this._success) {
|
|
120
|
+
throw new import_detailed_error.DetailedError({
|
|
121
|
+
code: "RESULT_ERROR_ACCESS_ON_SUCCESS" /* RESULT_ERROR_ACCESS_ON_SUCCESS */,
|
|
122
|
+
message: "Cannot access error on a successful DetailedResult. Check .success before accessing .error",
|
|
123
|
+
functionName: "DetailedResult.error"
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return this._error;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
130
|
+
0 && (module.exports = {
|
|
131
|
+
DetailedResult,
|
|
132
|
+
DetailedResultErrorCodes
|
|
133
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/DetailedResultErrorCodes.ts
|
|
2
|
+
var DetailedResultErrorCodes = /* @__PURE__ */ ((DetailedResultErrorCodes2) => {
|
|
3
|
+
DetailedResultErrorCodes2["RESULT_ERROR_ACCESS_ON_SUCCESS"] = "RESULT_ERROR_ACCESS_ON_SUCCESS";
|
|
4
|
+
DetailedResultErrorCodes2["RESULT_INVALID_STATE"] = "RESULT_INVALID_STATE";
|
|
5
|
+
DetailedResultErrorCodes2["RESULT_VALUE_ACCESS_ON_ERROR"] = "RESULT_VALUE_ACCESS_ON_ERROR";
|
|
6
|
+
return DetailedResultErrorCodes2;
|
|
7
|
+
})(DetailedResultErrorCodes || {});
|
|
8
|
+
|
|
9
|
+
// src/DetailedResult.ts
|
|
10
|
+
import { DetailedError } from "@couimet/detailed-error";
|
|
11
|
+
var DetailedResult = class _DetailedResult {
|
|
12
|
+
_success;
|
|
13
|
+
_value;
|
|
14
|
+
_error;
|
|
15
|
+
/**
|
|
16
|
+
* Not for public use. Use {@link DetailedResult.ok} or {@link DetailedResult.err} factories instead.
|
|
17
|
+
*
|
|
18
|
+
* Marked `protected` so subclasses can extend to pin the error type.
|
|
19
|
+
*/
|
|
20
|
+
constructor(success, value, error) {
|
|
21
|
+
if (success && error !== void 0) {
|
|
22
|
+
throw new DetailedError({
|
|
23
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
24
|
+
message: "DetailedResult marked as success cannot have an error defined",
|
|
25
|
+
functionName: "DetailedResult.constructor",
|
|
26
|
+
details: {
|
|
27
|
+
success,
|
|
28
|
+
hasValue: value !== void 0,
|
|
29
|
+
hasError: error !== void 0
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (!success && value !== void 0) {
|
|
34
|
+
throw new DetailedError({
|
|
35
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
36
|
+
message: "DetailedResult marked as error cannot have a value defined",
|
|
37
|
+
functionName: "DetailedResult.constructor",
|
|
38
|
+
details: {
|
|
39
|
+
success,
|
|
40
|
+
hasValue: value !== void 0,
|
|
41
|
+
hasError: error !== void 0
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (!success && error === void 0) {
|
|
46
|
+
throw new DetailedError({
|
|
47
|
+
code: "RESULT_INVALID_STATE" /* RESULT_INVALID_STATE */,
|
|
48
|
+
message: "DetailedResult marked as error must have an error defined",
|
|
49
|
+
functionName: "DetailedResult.constructor",
|
|
50
|
+
details: {
|
|
51
|
+
success,
|
|
52
|
+
hasValue: value !== void 0,
|
|
53
|
+
hasError: error !== void 0
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
this._success = success;
|
|
58
|
+
this._value = value;
|
|
59
|
+
this._error = error;
|
|
60
|
+
}
|
|
61
|
+
/** Create a successful {@link DetailedResult} containing a value. */
|
|
62
|
+
static ok(value) {
|
|
63
|
+
return new _DetailedResult(true, value, void 0);
|
|
64
|
+
}
|
|
65
|
+
/** Create an error {@link DetailedResult} containing an error. */
|
|
66
|
+
static err(error) {
|
|
67
|
+
return new _DetailedResult(false, void 0, error);
|
|
68
|
+
}
|
|
69
|
+
/** Check if this {@link DetailedResult} is successful. Always check this before accessing `.value` or `.error`. */
|
|
70
|
+
get success() {
|
|
71
|
+
return this._success;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the success value. Throws a {@link DetailedError} with code
|
|
75
|
+
* {@link DetailedResultErrorCodes.RESULT_VALUE_ACCESS_ON_ERROR} if this is an error result.
|
|
76
|
+
*/
|
|
77
|
+
get value() {
|
|
78
|
+
if (!this._success) {
|
|
79
|
+
throw new DetailedError({
|
|
80
|
+
code: "RESULT_VALUE_ACCESS_ON_ERROR" /* RESULT_VALUE_ACCESS_ON_ERROR */,
|
|
81
|
+
message: "Cannot access value on an error DetailedResult. Check .success before accessing .value",
|
|
82
|
+
functionName: "DetailedResult.value"
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return this._value;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get the error. Throws a {@link DetailedError} with code
|
|
89
|
+
* {@link DetailedResultErrorCodes.RESULT_ERROR_ACCESS_ON_SUCCESS} if this is a success result.
|
|
90
|
+
*/
|
|
91
|
+
get error() {
|
|
92
|
+
if (this._success) {
|
|
93
|
+
throw new DetailedError({
|
|
94
|
+
code: "RESULT_ERROR_ACCESS_ON_SUCCESS" /* RESULT_ERROR_ACCESS_ON_SUCCESS */,
|
|
95
|
+
message: "Cannot access error on a successful DetailedResult. Check .success before accessing .error",
|
|
96
|
+
functionName: "DetailedResult.error"
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return this._error;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
export {
|
|
103
|
+
DetailedResult,
|
|
104
|
+
DetailedResultErrorCodes
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@couimet/detailed-result",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Functional Result type for explicit error handling paired with @couimet/detailed-error",
|
|
5
|
+
"homepage": "https://github.com/couimet/ts-npm-packages/tree/main/packages/detailed-result#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/couimet/ts-npm-packages/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git@github.com:couimet/ts-npm-packages.git",
|
|
12
|
+
"directory": "packages/detailed-result"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Charles Ouimet <charles.ouimet@gmail.com>",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"prettier": "@couimet/eslint-config/prettier",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/jest": "^29.5.14",
|
|
32
|
+
"@types/node": "^24.13.2",
|
|
33
|
+
"eslint": "^10.4.1",
|
|
34
|
+
"jest": "^29.7.0",
|
|
35
|
+
"prettier": "^3.8.4",
|
|
36
|
+
"ts-jest": "^29.4.11",
|
|
37
|
+
"tsup": "^8.5.1",
|
|
38
|
+
"typescript": "^6.0.3",
|
|
39
|
+
"@couimet/detailed-error": "1.0.0",
|
|
40
|
+
"@couimet/detailed-error-testing": "0.1.4",
|
|
41
|
+
"@couimet/eslint-config": "0.6.2"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@couimet/detailed-error": ">=1.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup",
|
|
51
|
+
"clean": "rm -rf dist coverage *.tsbuildinfo",
|
|
52
|
+
"clean:all": "pnpm clean && rm -rf node_modules .eslintcache *.log",
|
|
53
|
+
"clean:deps": "rm -rf node_modules",
|
|
54
|
+
"format": "prettier --check .",
|
|
55
|
+
"test": "jest --coverage",
|
|
56
|
+
"typecheck": "tsc --noEmit"
|
|
57
|
+
}
|
|
58
|
+
}
|