@rotomeca/rop 0.0.2 → 1.0.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 +9 -0
- package/dist/cjs/index.js +29 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/src/classes/Fail.js +91 -0
- package/dist/cjs/src/classes/Success.js +90 -0
- package/dist/cjs/src/classes/abstracts/ATresult.js +28 -0
- package/dist/cjs/src/decorators/ErrorPath.internal/functions.js +37 -0
- package/dist/cjs/src/decorators/ErrorPath.js +29 -0
- package/dist/cjs/src/decorators/HappyPath.internal/functions.js +37 -0
- package/dist/cjs/src/decorators/HappyPath.js +29 -0
- package/dist/cjs/src/decorators/Risky.internal/functions.js +42 -0
- package/dist/cjs/src/decorators/Risky.js +28 -0
- package/{src/functions/applyMatch.ts → dist/cjs/src/functions/applyMatch.js} +10 -12
- package/dist/cjs/src/interfaces/ITResult.js +19 -0
- package/dist/cjs/src/types/ResultMatch.js +4 -0
- package/dist/cjs/src/types/resultsCallbacks.js +4 -0
- package/dist/esm/index.d.ts +8 -0
- package/{index.ts → dist/esm/index.js} +2 -5
- package/{src/classes/Fail.ts → dist/esm/src/classes/Fail.d.ts} +14 -37
- package/dist/esm/src/classes/Fail.js +87 -0
- package/{src/classes/Success.ts → dist/esm/src/classes/Success.d.ts} +14 -37
- package/dist/esm/src/classes/Success.js +86 -0
- package/{src/classes/abstracts/ATresult.ts → dist/esm/src/classes/abstracts/ATresult.d.ts} +11 -19
- package/dist/esm/src/classes/abstracts/ATresult.js +24 -0
- package/dist/esm/src/decorators/ErrorPath.d.ts +7 -0
- package/dist/esm/src/decorators/ErrorPath.internal/functions.d.ts +19 -0
- package/{src/decorators/ErrorPath.internal/functions.ts → dist/esm/src/decorators/ErrorPath.internal/functions.js} +9 -14
- package/{src/decorators/ErrorPath.ts → dist/esm/src/decorators/ErrorPath.js} +7 -10
- package/dist/esm/src/decorators/HappyPath.d.ts +7 -0
- package/dist/esm/src/decorators/HappyPath.internal/functions.d.ts +19 -0
- package/{src/decorators/HappyPath.internal/functions.ts → dist/esm/src/decorators/HappyPath.internal/functions.js} +9 -14
- package/{src/decorators/HappyPath.ts → dist/esm/src/decorators/HappyPath.js} +7 -10
- package/dist/esm/src/decorators/Risky.d.ts +5 -0
- package/dist/esm/src/decorators/Risky.internal/functions.d.ts +17 -0
- package/{src/decorators/Risky.internal/functions.ts → dist/esm/src/decorators/Risky.internal/functions.js} +9 -12
- package/{src/decorators/Risky.ts → dist/esm/src/decorators/Risky.js} +6 -9
- package/dist/esm/src/functions/applyMatch.d.ts +10 -0
- package/dist/esm/src/functions/applyMatch.js +23 -0
- package/{src/interfaces/ITResult.ts → dist/esm/src/interfaces/ITResult.d.ts} +12 -17
- package/dist/esm/src/interfaces/ITResult.js +16 -0
- package/{src/types/ResultMatch.ts → dist/esm/src/types/ResultMatch.d.ts} +2 -7
- package/dist/esm/src/types/ResultMatch.js +3 -0
- package/{src/types/resultsCallbacks.ts → dist/esm/src/types/resultsCallbacks.d.ts} +7 -11
- package/dist/esm/src/types/resultsCallbacks.js +3 -0
- package/package.json +22 -5
- package/.prettierc.json +0 -9
package/README.md
CHANGED
|
@@ -108,6 +108,15 @@ Perfect for **Side Effects** (logging, notifications) without polluting your bus
|
|
|
108
108
|
saveData(data: any) { ... }
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
+
**Note:** To use the decorators, ensure you have enabled experimentalDecorators in your tsconfig.json:
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"compilerOptions": {
|
|
115
|
+
"experimentalDecorators": true
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
111
120
|
## 📚 API Reference
|
|
112
121
|
|
|
113
122
|
### `Result<T, E>` Methods
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Result = void 0;
|
|
18
|
+
// Décorateurs (API Publique principale)
|
|
19
|
+
__exportStar(require("./src/decorators/Risky"), exports);
|
|
20
|
+
__exportStar(require("./src/decorators/HappyPath"), exports);
|
|
21
|
+
__exportStar(require("./src/decorators/ErrorPath"), exports);
|
|
22
|
+
// Classes & Abstractions (Pour le typage et création manuelle)
|
|
23
|
+
__exportStar(require("./src/classes/Fail"), exports);
|
|
24
|
+
__exportStar(require("./src/classes/Success"), exports);
|
|
25
|
+
var ATresult_1 = require("./src/classes/abstracts/ATresult");
|
|
26
|
+
Object.defineProperty(exports, "Result", { enumerable: true, get: function () { return ATresult_1.ATresult; } });
|
|
27
|
+
// Types (Pour l'IntelliSense)
|
|
28
|
+
__exportStar(require("./src/types/resultsCallbacks"), exports);
|
|
29
|
+
__exportStar(require("./src/types/ResultMatch"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// type: class
|
|
3
|
+
// description: Class representing a error result.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.Fail = void 0;
|
|
6
|
+
const ITResult_1 = require("../interfaces/ITResult");
|
|
7
|
+
const ATresult_1 = require("./abstracts/ATresult");
|
|
8
|
+
/**
|
|
9
|
+
* Class representing a failed result.
|
|
10
|
+
* @template TSuccess - The type of the success value.
|
|
11
|
+
* @template TE - The type of the error value (default is Error).
|
|
12
|
+
*/
|
|
13
|
+
class Fail extends ATresult_1.ATresult {
|
|
14
|
+
/**
|
|
15
|
+
* Creates an instance of Fail.
|
|
16
|
+
* @param error The error value.
|
|
17
|
+
*/
|
|
18
|
+
constructor(error) {
|
|
19
|
+
super();
|
|
20
|
+
this.error = error;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the state of the result.
|
|
24
|
+
* @returns The state indicating failure.
|
|
25
|
+
*/
|
|
26
|
+
state() {
|
|
27
|
+
return ITResult_1.ResultState.Error;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Matches the result with the provided matcher functions.
|
|
31
|
+
* @param matcher The matcher object containing functions for success and error cases.
|
|
32
|
+
* @returns The result of the matched function.
|
|
33
|
+
*/
|
|
34
|
+
match(matcher) {
|
|
35
|
+
return matcher.Err(this.error);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Chains the result with another operation that returns a new result.
|
|
39
|
+
* @param fn The function to apply if the result is successful.
|
|
40
|
+
* @returns The result of the chained operation.
|
|
41
|
+
*/
|
|
42
|
+
andThen(_) {
|
|
43
|
+
return new Fail(this.error);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Maps the success value to a new value.
|
|
47
|
+
* @param fn The function to apply to the success value.
|
|
48
|
+
* @returns A new Success instance containing the mapped value.
|
|
49
|
+
*/
|
|
50
|
+
map(_) {
|
|
51
|
+
return new Fail(this.error);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Maps the error value to a new error value.
|
|
55
|
+
* @param _ The function to apply to the error value.
|
|
56
|
+
* @returns A new Success instance containing the original success value.
|
|
57
|
+
*/
|
|
58
|
+
mapError(fn) {
|
|
59
|
+
return new Fail(fn(this.error));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns the success value or throws an error if the result is an error.
|
|
63
|
+
* @throws The error value if the result is an error.
|
|
64
|
+
* @returns The success value.
|
|
65
|
+
*/
|
|
66
|
+
throwIfError() {
|
|
67
|
+
throw this.error;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Retrieves the success value or returns the provided default value if the result is an error.
|
|
71
|
+
* @param defaultValue The value to return if the result is an error.
|
|
72
|
+
* @returns The success value if the result is a success, otherwise the default value.
|
|
73
|
+
*/
|
|
74
|
+
unwrapOr(defaultValue) {
|
|
75
|
+
return defaultValue;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new Fail instance.
|
|
79
|
+
* @param error The error value.
|
|
80
|
+
* @returns A new Fail instance containing the error.
|
|
81
|
+
*
|
|
82
|
+
* @template TSuccess - The type of the success value.
|
|
83
|
+
* @template TE - The type of the error value (default is Error).
|
|
84
|
+
*
|
|
85
|
+
* @static
|
|
86
|
+
*/
|
|
87
|
+
static Create(error) {
|
|
88
|
+
return new Fail(error);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.Fail = Fail;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// type: class
|
|
3
|
+
// description: Class representing a successful result.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.Success = void 0;
|
|
6
|
+
const ITResult_1 = require("../interfaces/ITResult");
|
|
7
|
+
const ATresult_1 = require("./abstracts/ATresult");
|
|
8
|
+
/**
|
|
9
|
+
* Class representing a successful result.
|
|
10
|
+
* @template TSuccess - The type of the success value.
|
|
11
|
+
* @template TE - The type of the error value (default is Error).
|
|
12
|
+
*/
|
|
13
|
+
class Success extends ATresult_1.ATresult {
|
|
14
|
+
/**
|
|
15
|
+
* Creates an instance of Success.
|
|
16
|
+
* @param value The success value.
|
|
17
|
+
*/
|
|
18
|
+
constructor(value) {
|
|
19
|
+
super();
|
|
20
|
+
this.value = value;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the state of the result.
|
|
24
|
+
* @returns The state indicating success.
|
|
25
|
+
*/
|
|
26
|
+
state() {
|
|
27
|
+
return ITResult_1.ResultState.Success;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Matches the result with the provided matcher functions.
|
|
31
|
+
* @param matcher The matcher object containing functions for success and error cases.
|
|
32
|
+
* @returns The result of the matched function.
|
|
33
|
+
*/
|
|
34
|
+
match(matcher) {
|
|
35
|
+
return matcher.Ok(this.value);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Chains the result with another operation that returns a new result.
|
|
39
|
+
* @param fn The function to apply if the result is successful.
|
|
40
|
+
* @returns The result of the chained operation.
|
|
41
|
+
*/
|
|
42
|
+
andThen(fn) {
|
|
43
|
+
return fn(this.value);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Maps the success value to a new value.
|
|
47
|
+
* @param fn The function to apply to the success value.
|
|
48
|
+
* @returns A new Success instance containing the mapped value.
|
|
49
|
+
*/
|
|
50
|
+
map(fn) {
|
|
51
|
+
return new Success(fn(this.value));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Maps the error value to a new error value.
|
|
55
|
+
* @param _ The function to apply to the error value.
|
|
56
|
+
* @returns A new Success instance containing the original success value.
|
|
57
|
+
*/
|
|
58
|
+
mapError(_) {
|
|
59
|
+
return new Success(this.value);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns the success value or throws an error if the result is an error.
|
|
63
|
+
* @returns The success value.
|
|
64
|
+
*/
|
|
65
|
+
throwIfError() {
|
|
66
|
+
return this.value;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the success value or returns the provided default value if the result is an error.
|
|
70
|
+
* @param _ The value to return if the result is an error.
|
|
71
|
+
* @returns The success value if the result is a success, otherwise the default value.
|
|
72
|
+
*/
|
|
73
|
+
unwrapOr(_) {
|
|
74
|
+
return this.value;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates a new Success instance.
|
|
78
|
+
* @param value The success value.
|
|
79
|
+
* @returns A new Success instance containing the value.
|
|
80
|
+
*
|
|
81
|
+
* @template TSuccess - The type of the success value.
|
|
82
|
+
* @template TE - The type of the error value (default is Error).
|
|
83
|
+
*
|
|
84
|
+
* @static
|
|
85
|
+
*/
|
|
86
|
+
static Create(value) {
|
|
87
|
+
return new Success(value);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.Success = Success;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// type: class
|
|
3
|
+
// description: Abstract class implementing the ITResult interface.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.ATresult = void 0;
|
|
6
|
+
const ITResult_1 = require("../../interfaces/ITResult");
|
|
7
|
+
/**
|
|
8
|
+
* Abstract class representing a result that can be either a success or an error.
|
|
9
|
+
*/
|
|
10
|
+
class ATresult {
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the result is a success.
|
|
13
|
+
*
|
|
14
|
+
* @returns True if the result is a success, false otherwise.
|
|
15
|
+
*/
|
|
16
|
+
isSuccess() {
|
|
17
|
+
return this.state() === ITResult_1.ResultState.Success;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Checks if the result is an error.
|
|
21
|
+
*
|
|
22
|
+
* @returns True if the result is an error, false otherwise.
|
|
23
|
+
*/
|
|
24
|
+
isError() {
|
|
25
|
+
return this.state() === ITResult_1.ResultState.Error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.ATresult = ATresult;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorPathAsync = exports.errorPathSync = void 0;
|
|
4
|
+
//type: functions
|
|
5
|
+
//description: Functions to handle error path operations by executing a callback on errored results.
|
|
6
|
+
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
|
+
const applyMatch_1 = require("../../functions/applyMatch");
|
|
8
|
+
/**
|
|
9
|
+
* Handles the error path for synchronous functions by executing a callback on errored results.
|
|
10
|
+
* @param this The context in which the original function is called.
|
|
11
|
+
* @param original The original function to be wrapped.
|
|
12
|
+
* @param path The error callback to be executed on errored results.
|
|
13
|
+
* @param args Arguments to be passed to the original function.
|
|
14
|
+
* @returns The result of the original function or the result of the error callback.
|
|
15
|
+
*/
|
|
16
|
+
function errorPathSync(original, path, ...args) {
|
|
17
|
+
const result = original.call(this, ...args);
|
|
18
|
+
return result instanceof ATresult_1.ATresult
|
|
19
|
+
? (0, applyMatch_1.applyMatch)(result, undefined, path)
|
|
20
|
+
: result;
|
|
21
|
+
}
|
|
22
|
+
exports.errorPathSync = errorPathSync;
|
|
23
|
+
/**
|
|
24
|
+
* Handles the error path for asynchronous functions by executing a callback on errored results.
|
|
25
|
+
* @param this The context in which the original function is called.
|
|
26
|
+
* @param original The original function to be wrapped.
|
|
27
|
+
* @param path The error callback to be executed on errored results.
|
|
28
|
+
* @param args Arguments to be passed to the original function.
|
|
29
|
+
* @returns The result of the original function or the result of the error callback.
|
|
30
|
+
*/
|
|
31
|
+
async function errorPathAsync(original, path, ...args) {
|
|
32
|
+
const result = await original.call(this, ...args);
|
|
33
|
+
return result instanceof ATresult_1.ATresult
|
|
34
|
+
? (0, applyMatch_1.applyMatch)(result, undefined, path)
|
|
35
|
+
: result;
|
|
36
|
+
}
|
|
37
|
+
exports.errorPathAsync = errorPathAsync;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//type: decorator
|
|
3
|
+
//description: Decorator to handle error path operations by executing a callback on errored results.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.ErrorPath = void 0;
|
|
6
|
+
const functions_1 = require("./ErrorPath.internal/functions");
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to handle error path operations by executing a callback on errored results.
|
|
9
|
+
* @param fn Error path callback to be executed on errored results.
|
|
10
|
+
* @returns A method decorator that wraps the original method with error path handling.
|
|
11
|
+
*/
|
|
12
|
+
function ErrorPath(fn) {
|
|
13
|
+
return function (target, key, descriptor) {
|
|
14
|
+
const original = descriptor.value;
|
|
15
|
+
const isAsync = original.constructor.name === "AsyncFunction";
|
|
16
|
+
if (isAsync) {
|
|
17
|
+
descriptor.value = async function (...args) {
|
|
18
|
+
return await functions_1.errorPathAsync.call(this, original, fn, ...args);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
descriptor.value = function (...args) {
|
|
23
|
+
return functions_1.errorPathSync.call(this, original, fn, ...args);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return descriptor;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
exports.ErrorPath = ErrorPath;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.happyPathASync = exports.happyPathSync = void 0;
|
|
4
|
+
//type: functions
|
|
5
|
+
//description: Functions to handle happy path operations by executing a callback on successful results.
|
|
6
|
+
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
|
+
const applyMatch_1 = require("../../functions/applyMatch");
|
|
8
|
+
/**
|
|
9
|
+
* Handles the happy path for synchronous functions by executing a callback on successful results.
|
|
10
|
+
* @param this The context in which the original function is called.
|
|
11
|
+
* @param original The original function to be wrapped.
|
|
12
|
+
* @param path The success callback to be executed on successful results.
|
|
13
|
+
* @param args Arguments to be passed to the original function.
|
|
14
|
+
* @returns The result of the original function or the result of the success callback.
|
|
15
|
+
*/
|
|
16
|
+
function happyPathSync(original, path, ...args) {
|
|
17
|
+
const result = original.call(this, ...args);
|
|
18
|
+
return result instanceof ATresult_1.ATresult
|
|
19
|
+
? (0, applyMatch_1.applyMatch)(result, path, undefined)
|
|
20
|
+
: result;
|
|
21
|
+
}
|
|
22
|
+
exports.happyPathSync = happyPathSync;
|
|
23
|
+
/**
|
|
24
|
+
* Handles the happy path for asynchronous functions by executing a callback on successful results.
|
|
25
|
+
* @param this The context in which the original function is called.
|
|
26
|
+
* @param original The original function to be wrapped.
|
|
27
|
+
* @param path The success callback to be executed on successful results.
|
|
28
|
+
* @param args Arguments to be passed to the original function.
|
|
29
|
+
* @returns The result of the original function or the result of the success callback.
|
|
30
|
+
*/
|
|
31
|
+
async function happyPathASync(original, path, ...args) {
|
|
32
|
+
const result = await original.call(this, ...args);
|
|
33
|
+
return result instanceof ATresult_1.ATresult
|
|
34
|
+
? (0, applyMatch_1.applyMatch)(result, path, undefined)
|
|
35
|
+
: result;
|
|
36
|
+
}
|
|
37
|
+
exports.happyPathASync = happyPathASync;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//type: decorator
|
|
3
|
+
//description: Decorator to handle happy path operations by executing a callback on successful results.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.HappyPath = void 0;
|
|
6
|
+
const functions_1 = require("./HappyPath.internal/functions");
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
9
|
+
* @param fn Happy path callback to be executed on successful results.
|
|
10
|
+
* @returns A method decorator that wraps the original method with happy path handling.
|
|
11
|
+
*/
|
|
12
|
+
function HappyPath(fn) {
|
|
13
|
+
return function (target, key, descriptor) {
|
|
14
|
+
const original = descriptor.value;
|
|
15
|
+
const isAsync = original.constructor.name === "AsyncFunction";
|
|
16
|
+
if (isAsync) {
|
|
17
|
+
descriptor.value = async function (...args) {
|
|
18
|
+
return await functions_1.happyPathASync.call(this, original, fn, ...args);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
descriptor.value = function (...args) {
|
|
23
|
+
return functions_1.happyPathSync.call(this, original, fn, ...args);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return descriptor;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
exports.HappyPath = HappyPath;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// type: functions
|
|
3
|
+
// description: Functions to handle risky operations by converting exceptions into Result types.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.riskyAsync = exports.riskySync = void 0;
|
|
6
|
+
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
|
+
const Fail_1 = require("../../classes/Fail");
|
|
8
|
+
const Success_1 = require("../../classes/Success");
|
|
9
|
+
/**
|
|
10
|
+
* Handles synchronous risky operations by converting exceptions into Result types.
|
|
11
|
+
* @param this The context in which the original function is called.
|
|
12
|
+
* @param original The original function to be executed.
|
|
13
|
+
* @param args Arguments to be passed to the original function.
|
|
14
|
+
* @returns An ATresult representing success or failure.
|
|
15
|
+
*/
|
|
16
|
+
function riskySync(original, ...args) {
|
|
17
|
+
try {
|
|
18
|
+
const result = original.apply(this, args);
|
|
19
|
+
return result instanceof ATresult_1.ATresult ? result : Success_1.Success.Create(result);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return error instanceof ATresult_1.ATresult ? error : Fail_1.Fail.Create(error);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.riskySync = riskySync;
|
|
26
|
+
/**
|
|
27
|
+
* Handles asynchronous risky operations by converting exceptions into Result types.
|
|
28
|
+
* @param this The context in which the original function is called.
|
|
29
|
+
* @param original The original function to be executed.
|
|
30
|
+
* @param args Arguments to be passed to the original function.
|
|
31
|
+
* @returns A Promise that resolves to an ATresult representing success or failure.
|
|
32
|
+
*/
|
|
33
|
+
async function riskyAsync(original, ...args) {
|
|
34
|
+
try {
|
|
35
|
+
const result = await original.apply(this, args);
|
|
36
|
+
return result instanceof ATresult_1.ATresult ? result : Success_1.Success.Create(result);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
return error instanceof ATresult_1.ATresult ? error : Fail_1.Fail.Create(error);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.riskyAsync = riskyAsync;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Risky = void 0;
|
|
4
|
+
// type: decorator
|
|
5
|
+
// description: Decorator to handle risky operations by converting exceptions into Result types.
|
|
6
|
+
const functions_1 = require("./Risky.internal/functions");
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
9
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
10
|
+
*/
|
|
11
|
+
function Risky() {
|
|
12
|
+
return function (target, key, descriptor) {
|
|
13
|
+
const original = descriptor.value;
|
|
14
|
+
const isAsync = original.constructor.name === "AsyncFunction";
|
|
15
|
+
if (isAsync) {
|
|
16
|
+
descriptor.value = async function (...args) {
|
|
17
|
+
return functions_1.riskyAsync.call(this, original, ...args);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
descriptor.value = function (...args) {
|
|
22
|
+
return functions_1.riskySync.call(this, original, ...args);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return descriptor;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
exports.Risky = Risky;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
//type: function
|
|
2
3
|
//description: Function to apply match on a result with optional callbacks.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { SuccessCallback, ErrorCallback } from "../types/resultsCallbacks";
|
|
6
|
-
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.applyMatch = void 0;
|
|
7
6
|
/**
|
|
8
7
|
* Applies match on the given result with optional success and error callbacks.
|
|
9
8
|
* @param result Result to apply match on.
|
|
@@ -11,19 +10,18 @@ import { SuccessCallback, ErrorCallback } from "../types/resultsCallbacks";
|
|
|
11
10
|
* @param onError Additional callback for error case.
|
|
12
11
|
* @returns Returns the original result after applying the match.
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
|
-
result: ATresult<any>,
|
|
16
|
-
onSuccess?: SuccessCallback<any, any>,
|
|
17
|
-
onError?: ErrorCallback<any, any>
|
|
18
|
-
): ATresult<any> {
|
|
13
|
+
function applyMatch(result, onSuccess, onError) {
|
|
19
14
|
return result.match({
|
|
20
15
|
Ok: (val) => {
|
|
21
|
-
if (onSuccess)
|
|
16
|
+
if (onSuccess)
|
|
17
|
+
onSuccess(val);
|
|
22
18
|
return result;
|
|
23
19
|
},
|
|
24
20
|
Err: (err) => {
|
|
25
|
-
if (onError)
|
|
21
|
+
if (onError)
|
|
22
|
+
onError(err);
|
|
26
23
|
return result;
|
|
27
24
|
}
|
|
28
25
|
});
|
|
29
|
-
}
|
|
26
|
+
}
|
|
27
|
+
exports.applyMatch = applyMatch;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// type: interface
|
|
3
|
+
// description: Interface for a result type that can represent either a success or an error.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.ResultState = void 0;
|
|
6
|
+
/**
|
|
7
|
+
* Enumeration representing the state of the result.
|
|
8
|
+
*/
|
|
9
|
+
var ResultState;
|
|
10
|
+
(function (ResultState) {
|
|
11
|
+
/**
|
|
12
|
+
* The result represents a successful outcome.
|
|
13
|
+
*/
|
|
14
|
+
ResultState[ResultState["Success"] = 0] = "Success";
|
|
15
|
+
/**
|
|
16
|
+
* The result represents an error outcome.
|
|
17
|
+
*/
|
|
18
|
+
ResultState[ResultState["Error"] = 1] = "Error";
|
|
19
|
+
})(ResultState = exports.ResultState || (exports.ResultState = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './src/decorators/Risky';
|
|
2
|
+
export * from './src/decorators/HappyPath';
|
|
3
|
+
export * from './src/decorators/ErrorPath';
|
|
4
|
+
export * from './src/classes/Fail';
|
|
5
|
+
export * from './src/classes/Success';
|
|
6
|
+
export { ATresult as Result } from './src/classes/abstracts/ATresult';
|
|
7
|
+
export * from './src/types/resultsCallbacks';
|
|
8
|
+
export * from './src/types/ResultMatch';
|
|
@@ -2,13 +2,10 @@
|
|
|
2
2
|
export * from './src/decorators/Risky';
|
|
3
3
|
export * from './src/decorators/HappyPath';
|
|
4
4
|
export * from './src/decorators/ErrorPath';
|
|
5
|
-
|
|
6
5
|
// Classes & Abstractions (Pour le typage et création manuelle)
|
|
7
6
|
export * from './src/classes/Fail';
|
|
8
7
|
export * from './src/classes/Success';
|
|
9
|
-
export {ATresult as Result} from './src/classes/abstracts/ATresult';
|
|
10
|
-
|
|
8
|
+
export { ATresult as Result } from './src/classes/abstracts/ATresult';
|
|
11
9
|
// Types (Pour l'IntelliSense)
|
|
12
|
-
export * from './src/types/resultsCallbacks';
|
|
10
|
+
export * from './src/types/resultsCallbacks';
|
|
13
11
|
export * from './src/types/ResultMatch';
|
|
14
|
-
|