@rotomeca/rop 1.0.0 → 2.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 +4 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/src/decorators/ErrorPath.internal/functions.js +16 -10
- package/dist/cjs/src/decorators/ErrorPath.js +19 -17
- package/dist/cjs/src/decorators/HappyPath.internal/functions.js +16 -10
- package/dist/cjs/src/decorators/HappyPath.js +22 -20
- package/dist/cjs/src/decorators/Risky.internal/functions.js +2 -3
- package/dist/cjs/src/decorators/Risky.js +21 -16
- package/dist/cjs/src/decorators/RiskyPath.js +35 -0
- package/dist/cjs/src/interfaces/ITResult.js +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/src/decorators/ErrorPath.d.ts +7 -1
- package/dist/esm/src/decorators/ErrorPath.internal/functions.js +14 -7
- package/dist/esm/src/decorators/ErrorPath.js +17 -15
- package/dist/esm/src/decorators/HappyPath.d.ts +9 -3
- package/dist/esm/src/decorators/HappyPath.internal/functions.js +14 -7
- package/dist/esm/src/decorators/HappyPath.js +20 -18
- package/dist/esm/src/decorators/Risky.d.ts +7 -1
- package/dist/esm/src/decorators/Risky.js +19 -14
- package/dist/esm/src/decorators/RiskyPath.d.ts +12 -0
- package/dist/esm/src/decorators/RiskyPath.js +31 -0
- package/package.json +2 -2
- package/dist/cjs/src/functions/applyMatch.js +0 -27
- package/dist/esm/src/functions/applyMatch.d.ts +0 -10
- package/dist/esm/src/functions/applyMatch.js +0 -23
package/README.md
CHANGED
|
@@ -98,13 +98,16 @@ Automatically wraps the method execution in a `try/catch` block.
|
|
|
98
98
|
* If the method throws, it becomes `Fail(error)`.
|
|
99
99
|
* Works with `async/await` and synchronous code.
|
|
100
100
|
|
|
101
|
+
#### `@RiskyPath()`
|
|
102
|
+
Same as Risky, but use it with ErrorPath and HappyPath.
|
|
103
|
+
|
|
101
104
|
#### `@HappyPath(fn)` & `@ErrorPath(fn)`
|
|
102
105
|
Perfect for **Side Effects** (logging, notifications) without polluting your business logic. These decorators act as "Taps": they inspect the result but **do not** modify the return value.
|
|
103
106
|
|
|
104
107
|
```typescript
|
|
105
108
|
@ErrorPath((e) => Sentry.captureException(e)) // Only if fails
|
|
106
109
|
@HappyPath((data) => Analytics.track('success', data)) // Only if success
|
|
107
|
-
@
|
|
110
|
+
@RiskyPath()
|
|
108
111
|
saveData(data: any) { ... }
|
|
109
112
|
```
|
|
110
113
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.Result = void 0;
|
|
18
18
|
// Décorateurs (API Publique principale)
|
|
19
19
|
__exportStar(require("./src/decorators/Risky"), exports);
|
|
20
|
+
__exportStar(require("./src/decorators/RiskyPath"), exports);
|
|
20
21
|
__exportStar(require("./src/decorators/HappyPath"), exports);
|
|
21
22
|
__exportStar(require("./src/decorators/ErrorPath"), exports);
|
|
22
23
|
// Classes & Abstractions (Pour le typage et création manuelle)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.errorPathSync = errorPathSync;
|
|
4
|
+
exports.errorPathAsync = errorPathAsync;
|
|
4
5
|
//type: functions
|
|
5
6
|
//description: Functions to handle error path operations by executing a callback on errored results.
|
|
6
7
|
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
|
-
const applyMatch_1 = require("../../functions/applyMatch");
|
|
8
8
|
/**
|
|
9
9
|
* Handles the error path for synchronous functions by executing a callback on errored results.
|
|
10
10
|
* @param this The context in which the original function is called.
|
|
@@ -15,11 +15,14 @@ const applyMatch_1 = require("../../functions/applyMatch");
|
|
|
15
15
|
*/
|
|
16
16
|
function errorPathSync(original, path, ...args) {
|
|
17
17
|
const result = original.call(this, ...args);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
if (result instanceof ATresult_1.ATresult) {
|
|
19
|
+
return result.match({
|
|
20
|
+
Ok: (_) => result,
|
|
21
|
+
Err: (val) => val,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return path(result);
|
|
21
25
|
}
|
|
22
|
-
exports.errorPathSync = errorPathSync;
|
|
23
26
|
/**
|
|
24
27
|
* Handles the error path for asynchronous functions by executing a callback on errored results.
|
|
25
28
|
* @param this The context in which the original function is called.
|
|
@@ -30,8 +33,11 @@ exports.errorPathSync = errorPathSync;
|
|
|
30
33
|
*/
|
|
31
34
|
async function errorPathAsync(original, path, ...args) {
|
|
32
35
|
const result = await original.call(this, ...args);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
if (result instanceof ATresult_1.ATresult) {
|
|
37
|
+
return result.match({
|
|
38
|
+
Ok: (_) => result,
|
|
39
|
+
Err: (val) => val,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return path(result);
|
|
36
43
|
}
|
|
37
|
-
exports.errorPathAsync = errorPathAsync;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
//type:
|
|
2
|
+
//type: decorators
|
|
3
3
|
//description: Decorator to handle error path operations by executing a callback on errored results.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.ErrorPath =
|
|
5
|
+
exports.ErrorPath = ErrorPath;
|
|
6
|
+
exports.ErrorPathAsync = ErrorPathAsync;
|
|
6
7
|
const functions_1 = require("./ErrorPath.internal/functions");
|
|
7
8
|
/**
|
|
8
9
|
* Decorator to handle error path operations by executing a callback on errored results.
|
|
@@ -10,20 +11,21 @@ const functions_1 = require("./ErrorPath.internal/functions");
|
|
|
10
11
|
* @returns A method decorator that wraps the original method with error path handling.
|
|
11
12
|
*/
|
|
12
13
|
function ErrorPath(fn) {
|
|
13
|
-
return function (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
14
|
+
return function (originalMethod, context) {
|
|
15
|
+
return function (...args) {
|
|
16
|
+
return functions_1.errorPathSync.call(this, originalMethod, fn, ...args);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decorator to handle error path operations by executing a callback on errored results.
|
|
22
|
+
* @param fn Error path callback to be executed on errored results.
|
|
23
|
+
* @returns A method decorator that wraps the original method with error path handling.
|
|
24
|
+
*/
|
|
25
|
+
function ErrorPathAsync(fn) {
|
|
26
|
+
return function (originalMethod, context) {
|
|
27
|
+
return async function (...args) {
|
|
28
|
+
return functions_1.errorPathAsync.call(this, originalMethod, fn, ...args);
|
|
29
|
+
};
|
|
27
30
|
};
|
|
28
31
|
}
|
|
29
|
-
exports.ErrorPath = ErrorPath;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.happyPathSync = happyPathSync;
|
|
4
|
+
exports.happyPathASync = happyPathASync;
|
|
4
5
|
//type: functions
|
|
5
6
|
//description: Functions to handle happy path operations by executing a callback on successful results.
|
|
6
7
|
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
|
-
const applyMatch_1 = require("../../functions/applyMatch");
|
|
8
8
|
/**
|
|
9
9
|
* Handles the happy path for synchronous functions by executing a callback on successful results.
|
|
10
10
|
* @param this The context in which the original function is called.
|
|
@@ -15,11 +15,14 @@ const applyMatch_1 = require("../../functions/applyMatch");
|
|
|
15
15
|
*/
|
|
16
16
|
function happyPathSync(original, path, ...args) {
|
|
17
17
|
const result = original.call(this, ...args);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
if (result instanceof ATresult_1.ATresult) {
|
|
19
|
+
return result.match({
|
|
20
|
+
Ok: (val) => path(val),
|
|
21
|
+
Err: (e) => result
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return path(result);
|
|
21
25
|
}
|
|
22
|
-
exports.happyPathSync = happyPathSync;
|
|
23
26
|
/**
|
|
24
27
|
* Handles the happy path for asynchronous functions by executing a callback on successful results.
|
|
25
28
|
* @param this The context in which the original function is called.
|
|
@@ -30,8 +33,11 @@ exports.happyPathSync = happyPathSync;
|
|
|
30
33
|
*/
|
|
31
34
|
async function happyPathASync(original, path, ...args) {
|
|
32
35
|
const result = await original.call(this, ...args);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
if (result instanceof ATresult_1.ATresult) {
|
|
37
|
+
return result.match({
|
|
38
|
+
Ok: (val) => path(val),
|
|
39
|
+
Err: (e) => result
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return path(result);
|
|
36
43
|
}
|
|
37
|
-
exports.happyPathASync = happyPathASync;
|
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
//type:
|
|
3
|
-
//description: Decorator to handle
|
|
2
|
+
//type: decorators
|
|
3
|
+
//description: Decorator to handle error path operations by executing a callback on errored results.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.HappyPath =
|
|
5
|
+
exports.HappyPath = HappyPath;
|
|
6
|
+
exports.HappyPathAsync = HappyPathAsync;
|
|
6
7
|
const functions_1 = require("./HappyPath.internal/functions");
|
|
7
8
|
/**
|
|
8
9
|
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
9
|
-
* @param fn
|
|
10
|
-
* @returns
|
|
10
|
+
* @param fn A success callback to be executed on successful results.
|
|
11
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
11
12
|
*/
|
|
12
13
|
function HappyPath(fn) {
|
|
13
|
-
return function (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
14
|
+
return function (originalMethod, context) {
|
|
15
|
+
return function (...args) {
|
|
16
|
+
return functions_1.happyPathSync.call(this, originalMethod, fn, ...args);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
22
|
+
* @param fn A success callback to be executed on successful results.
|
|
23
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
24
|
+
*/
|
|
25
|
+
function HappyPathAsync(fn) {
|
|
26
|
+
return function (originalMethod, context) {
|
|
27
|
+
return async function (...args) {
|
|
28
|
+
return await functions_1.happyPathASync.call(this, originalMethod, fn, ...args);
|
|
29
|
+
};
|
|
27
30
|
};
|
|
28
31
|
}
|
|
29
|
-
exports.HappyPath = HappyPath;
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// type: functions
|
|
3
3
|
// description: Functions to handle risky operations by converting exceptions into Result types.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.
|
|
5
|
+
exports.riskySync = riskySync;
|
|
6
|
+
exports.riskyAsync = riskyAsync;
|
|
6
7
|
const ATresult_1 = require("../../classes/abstracts/ATresult");
|
|
7
8
|
const Fail_1 = require("../../classes/Fail");
|
|
8
9
|
const Success_1 = require("../../classes/Success");
|
|
@@ -22,7 +23,6 @@ function riskySync(original, ...args) {
|
|
|
22
23
|
return error instanceof ATresult_1.ATresult ? error : Fail_1.Fail.Create(error);
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
|
-
exports.riskySync = riskySync;
|
|
26
26
|
/**
|
|
27
27
|
* Handles asynchronous risky operations by converting exceptions into Result types.
|
|
28
28
|
* @param this The context in which the original function is called.
|
|
@@ -39,4 +39,3 @@ async function riskyAsync(original, ...args) {
|
|
|
39
39
|
return error instanceof ATresult_1.ATresult ? error : Fail_1.Fail.Create(error);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
exports.riskyAsync = riskyAsync;
|
|
@@ -1,28 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Risky =
|
|
4
|
-
|
|
5
|
-
// description: Decorator to handle risky operations by converting exceptions into Result types.
|
|
3
|
+
exports.Risky = Risky;
|
|
4
|
+
exports.RiskyAsync = RiskyAsync;
|
|
6
5
|
const functions_1 = require("./Risky.internal/functions");
|
|
7
6
|
/**
|
|
8
7
|
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
9
8
|
* @returns A method decorator that wraps the original method with error handling.
|
|
10
9
|
*/
|
|
11
10
|
function Risky() {
|
|
12
|
-
return function (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (isAsync) {
|
|
16
|
-
descriptor.value = async function (...args) {
|
|
17
|
-
return functions_1.riskyAsync.call(this, original, ...args);
|
|
18
|
-
};
|
|
11
|
+
return function (originalMethod, context) {
|
|
12
|
+
if (context.kind !== "method") {
|
|
13
|
+
throw new Error("Risky can only be applied to methods.");
|
|
19
14
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
return (function (...args) {
|
|
16
|
+
return functions_1.riskySync.call(this, originalMethod, ...args);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
22
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
23
|
+
*/
|
|
24
|
+
function RiskyAsync() {
|
|
25
|
+
return function (originalMethod, context) {
|
|
26
|
+
if (context.kind !== "method") {
|
|
27
|
+
throw new Error("RiskyAsync can only be applied to methods.");
|
|
24
28
|
}
|
|
25
|
-
return
|
|
29
|
+
return (async function (...args) {
|
|
30
|
+
return await functions_1.riskyAsync.call(this, originalMethod, ...args);
|
|
31
|
+
});
|
|
26
32
|
};
|
|
27
33
|
}
|
|
28
|
-
exports.Risky = Risky;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RiskyPath = RiskyPath;
|
|
4
|
+
exports.RiskyPathAsync = RiskyPathAsync;
|
|
5
|
+
const functions_1 = require("./Risky.internal/functions");
|
|
6
|
+
/**
|
|
7
|
+
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
8
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
9
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
10
|
+
*/
|
|
11
|
+
function RiskyPath() {
|
|
12
|
+
return function (originalMethod, context) {
|
|
13
|
+
if (context.kind !== "method") {
|
|
14
|
+
throw new Error("RiskyPath can only be applied to methods.");
|
|
15
|
+
}
|
|
16
|
+
return (function (...args) {
|
|
17
|
+
return functions_1.riskySync.call(this, originalMethod, ...args);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
23
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
24
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
25
|
+
*/
|
|
26
|
+
function RiskyPathAsync() {
|
|
27
|
+
return function (originalMethod, context) {
|
|
28
|
+
if (context.kind !== "method") {
|
|
29
|
+
throw new Error("RiskyPathAsync can only be applied to methods.");
|
|
30
|
+
}
|
|
31
|
+
return (async function (...args) {
|
|
32
|
+
return await functions_1.riskyAsync.call(this, originalMethod, ...args);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Décorateurs (API Publique principale)
|
|
2
2
|
export * from './src/decorators/Risky';
|
|
3
|
+
export * from './src/decorators/RiskyPath';
|
|
3
4
|
export * from './src/decorators/HappyPath';
|
|
4
5
|
export * from './src/decorators/ErrorPath';
|
|
5
6
|
// Classes & Abstractions (Pour le typage et création manuelle)
|
|
@@ -4,4 +4,10 @@ import { ErrorCallback } from "../types/resultsCallbacks";
|
|
|
4
4
|
* @param fn Error path callback to be executed on errored results.
|
|
5
5
|
* @returns A method decorator that wraps the original method with error path handling.
|
|
6
6
|
*/
|
|
7
|
-
export declare function ErrorPath<T>(fn: ErrorCallback<T, void>): (
|
|
7
|
+
export declare function ErrorPath<T>(fn: ErrorCallback<T, void>): (this: any, originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => any;
|
|
8
|
+
/**
|
|
9
|
+
* Decorator to handle error path operations by executing a callback on errored results.
|
|
10
|
+
* @param fn Error path callback to be executed on errored results.
|
|
11
|
+
* @returns A method decorator that wraps the original method with error path handling.
|
|
12
|
+
*/
|
|
13
|
+
export declare function ErrorPathAsync<T>(fn: ErrorCallback<T, void>): (this: any, originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => Promise<any>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
//type: functions
|
|
2
2
|
//description: Functions to handle error path operations by executing a callback on errored results.
|
|
3
3
|
import { ATresult } from "../../classes/abstracts/ATresult";
|
|
4
|
-
import { applyMatch } from "../../functions/applyMatch";
|
|
5
4
|
/**
|
|
6
5
|
* Handles the error path for synchronous functions by executing a callback on errored results.
|
|
7
6
|
* @param this The context in which the original function is called.
|
|
@@ -12,9 +11,13 @@ import { applyMatch } from "../../functions/applyMatch";
|
|
|
12
11
|
*/
|
|
13
12
|
export function errorPathSync(original, path, ...args) {
|
|
14
13
|
const result = original.call(this, ...args);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (result instanceof ATresult) {
|
|
15
|
+
return result.match({
|
|
16
|
+
Ok: (_) => result,
|
|
17
|
+
Err: (val) => val,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return path(result);
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
23
|
* Handles the error path for asynchronous functions by executing a callback on errored results.
|
|
@@ -26,7 +29,11 @@ export function errorPathSync(original, path, ...args) {
|
|
|
26
29
|
*/
|
|
27
30
|
export async function errorPathAsync(original, path, ...args) {
|
|
28
31
|
const result = await original.call(this, ...args);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
if (result instanceof ATresult) {
|
|
33
|
+
return result.match({
|
|
34
|
+
Ok: (_) => result,
|
|
35
|
+
Err: (val) => val,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return path(result);
|
|
32
39
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//type:
|
|
1
|
+
//type: decorators
|
|
2
2
|
//description: Decorator to handle error path operations by executing a callback on errored results.
|
|
3
3
|
import { errorPathAsync, errorPathSync } from "./ErrorPath.internal/functions";
|
|
4
4
|
/**
|
|
@@ -7,19 +7,21 @@ import { errorPathAsync, errorPathSync } from "./ErrorPath.internal/functions";
|
|
|
7
7
|
* @returns A method decorator that wraps the original method with error path handling.
|
|
8
8
|
*/
|
|
9
9
|
export function ErrorPath(fn) {
|
|
10
|
-
return function (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
10
|
+
return function (originalMethod, context) {
|
|
11
|
+
return function (...args) {
|
|
12
|
+
return errorPathSync.call(this, originalMethod, fn, ...args);
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator to handle error path operations by executing a callback on errored results.
|
|
18
|
+
* @param fn Error path callback to be executed on errored results.
|
|
19
|
+
* @returns A method decorator that wraps the original method with error path handling.
|
|
20
|
+
*/
|
|
21
|
+
export function ErrorPathAsync(fn) {
|
|
22
|
+
return function (originalMethod, context) {
|
|
23
|
+
return async function (...args) {
|
|
24
|
+
return errorPathAsync.call(this, originalMethod, fn, ...args);
|
|
25
|
+
};
|
|
24
26
|
};
|
|
25
27
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { SuccessCallback } from "../types/resultsCallbacks";
|
|
2
2
|
/**
|
|
3
3
|
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
4
|
-
* @param fn
|
|
5
|
-
* @returns
|
|
4
|
+
* @param fn A success callback to be executed on successful results.
|
|
5
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
6
6
|
*/
|
|
7
|
-
export declare function HappyPath<
|
|
7
|
+
export declare function HappyPath<TSuccess, TReturn>(fn: SuccessCallback<TSuccess, TReturn>): (this: any, originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => any;
|
|
8
|
+
/**
|
|
9
|
+
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
10
|
+
* @param fn A success callback to be executed on successful results.
|
|
11
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
12
|
+
*/
|
|
13
|
+
export declare function HappyPathAsync<TSuccess, TReturn>(fn: SuccessCallback<TSuccess, TReturn>): (this: any, originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => Promise<any>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
//type: functions
|
|
2
2
|
//description: Functions to handle happy path operations by executing a callback on successful results.
|
|
3
3
|
import { ATresult } from "../../classes/abstracts/ATresult";
|
|
4
|
-
import { applyMatch } from "../../functions/applyMatch";
|
|
5
4
|
/**
|
|
6
5
|
* Handles the happy path for synchronous functions by executing a callback on successful results.
|
|
7
6
|
* @param this The context in which the original function is called.
|
|
@@ -12,9 +11,13 @@ import { applyMatch } from "../../functions/applyMatch";
|
|
|
12
11
|
*/
|
|
13
12
|
export function happyPathSync(original, path, ...args) {
|
|
14
13
|
const result = original.call(this, ...args);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (result instanceof ATresult) {
|
|
15
|
+
return result.match({
|
|
16
|
+
Ok: (val) => path(val),
|
|
17
|
+
Err: (e) => result
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return path(result);
|
|
18
21
|
}
|
|
19
22
|
/**
|
|
20
23
|
* Handles the happy path for asynchronous functions by executing a callback on successful results.
|
|
@@ -26,7 +29,11 @@ export function happyPathSync(original, path, ...args) {
|
|
|
26
29
|
*/
|
|
27
30
|
export async function happyPathASync(original, path, ...args) {
|
|
28
31
|
const result = await original.call(this, ...args);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
if (result instanceof ATresult) {
|
|
33
|
+
return result.match({
|
|
34
|
+
Ok: (val) => path(val),
|
|
35
|
+
Err: (e) => result
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return path(result);
|
|
32
39
|
}
|
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
//type:
|
|
2
|
-
//description: Decorator to handle
|
|
1
|
+
//type: decorators
|
|
2
|
+
//description: Decorator to handle error path operations by executing a callback on errored results.
|
|
3
3
|
import { happyPathASync, happyPathSync } from "./HappyPath.internal/functions";
|
|
4
4
|
/**
|
|
5
5
|
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
6
|
-
* @param fn
|
|
7
|
-
* @returns
|
|
6
|
+
* @param fn A success callback to be executed on successful results.
|
|
7
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
8
8
|
*/
|
|
9
9
|
export function HappyPath(fn) {
|
|
10
|
-
return function (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
10
|
+
return function (originalMethod, context) {
|
|
11
|
+
return function (...args) {
|
|
12
|
+
return happyPathSync.call(this, originalMethod, fn, ...args);
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator to handle happy path operations by executing a callback on successful results.
|
|
18
|
+
* @param fn A success callback to be executed on successful results.
|
|
19
|
+
* @returns Returns a method decorator that wraps the original method with happy path handling.
|
|
20
|
+
*/
|
|
21
|
+
export function HappyPathAsync(fn) {
|
|
22
|
+
return function (originalMethod, context) {
|
|
23
|
+
return async function (...args) {
|
|
24
|
+
return await happyPathASync.call(this, originalMethod, fn, ...args);
|
|
25
|
+
};
|
|
24
26
|
};
|
|
25
27
|
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
import { ATresult } from "../classes/abstracts/ATresult";
|
|
1
2
|
/**
|
|
2
3
|
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
3
4
|
* @returns A method decorator that wraps the original method with error handling.
|
|
4
5
|
*/
|
|
5
|
-
export declare function Risky(): (
|
|
6
|
+
export declare function Risky(): (originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => ATresult<any>;
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
9
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
10
|
+
*/
|
|
11
|
+
export declare function RiskyAsync(): (originalMethod: Function, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => Promise<ATresult<any>>;
|
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
// type: decorator
|
|
2
|
-
// description: Decorator to handle risky operations by converting exceptions into Result types.
|
|
3
1
|
import { riskyAsync, riskySync } from "./Risky.internal/functions";
|
|
4
2
|
/**
|
|
5
3
|
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
6
4
|
* @returns A method decorator that wraps the original method with error handling.
|
|
7
5
|
*/
|
|
8
6
|
export function Risky() {
|
|
9
|
-
return function (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (isAsync) {
|
|
13
|
-
descriptor.value = async function (...args) {
|
|
14
|
-
return riskyAsync.call(this, original, ...args);
|
|
15
|
-
};
|
|
7
|
+
return function (originalMethod, context) {
|
|
8
|
+
if (context.kind !== "method") {
|
|
9
|
+
throw new Error("Risky can only be applied to methods.");
|
|
16
10
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
return (function (...args) {
|
|
12
|
+
return riskySync.call(this, originalMethod, ...args);
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
18
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
19
|
+
*/
|
|
20
|
+
export function RiskyAsync() {
|
|
21
|
+
return function (originalMethod, context) {
|
|
22
|
+
if (context.kind !== "method") {
|
|
23
|
+
throw new Error("RiskyAsync can only be applied to methods.");
|
|
21
24
|
}
|
|
22
|
-
return
|
|
25
|
+
return (async function (...args) {
|
|
26
|
+
return await riskyAsync.call(this, originalMethod, ...args);
|
|
27
|
+
});
|
|
23
28
|
};
|
|
24
29
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
3
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
4
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
5
|
+
*/
|
|
6
|
+
export declare function RiskyPath(): (originalMethod: Function, context: ClassMethodDecoratorContext) => any;
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
9
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
10
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
11
|
+
*/
|
|
12
|
+
export declare function RiskyPathAsync(): (originalMethod: Function, context: ClassMethodDecoratorContext) => any;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { riskyAsync, riskySync } from "./Risky.internal/functions";
|
|
2
|
+
/**
|
|
3
|
+
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
4
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
5
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
6
|
+
*/
|
|
7
|
+
export function RiskyPath() {
|
|
8
|
+
return function (originalMethod, context) {
|
|
9
|
+
if (context.kind !== "method") {
|
|
10
|
+
throw new Error("RiskyPath can only be applied to methods.");
|
|
11
|
+
}
|
|
12
|
+
return (function (...args) {
|
|
13
|
+
return riskySync.call(this, originalMethod, ...args);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Decorator to handle risky async operations by converting exceptions into Result types.
|
|
19
|
+
* Use with HappyPath or ErrorPath to handle the respective paths.
|
|
20
|
+
* @returns A method decorator that wraps the original method with error handling.
|
|
21
|
+
*/
|
|
22
|
+
export function RiskyPathAsync() {
|
|
23
|
+
return function (originalMethod, context) {
|
|
24
|
+
if (context.kind !== "method") {
|
|
25
|
+
throw new Error("RiskyPathAsync can only be applied to methods.");
|
|
26
|
+
}
|
|
27
|
+
return (async function (...args) {
|
|
28
|
+
return await riskyAsync.call(this, originalMethod, ...args);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rotomeca/rop",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -30,6 +30,6 @@
|
|
|
30
30
|
"eslint": "^8.57.1",
|
|
31
31
|
"prettier": "^3.8.1",
|
|
32
32
|
"prettier-eslint": "^16.4.2",
|
|
33
|
-
"typescript": "^
|
|
33
|
+
"typescript": "^5.6.3"
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
//type: function
|
|
3
|
-
//description: Function to apply match on a result with optional callbacks.
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.applyMatch = void 0;
|
|
6
|
-
/**
|
|
7
|
-
* Applies match on the given result with optional success and error callbacks.
|
|
8
|
-
* @param result Result to apply match on.
|
|
9
|
-
* @param onSuccess Additional callback for success case.
|
|
10
|
-
* @param onError Additional callback for error case.
|
|
11
|
-
* @returns Returns the original result after applying the match.
|
|
12
|
-
*/
|
|
13
|
-
function applyMatch(result, onSuccess, onError) {
|
|
14
|
-
return result.match({
|
|
15
|
-
Ok: (val) => {
|
|
16
|
-
if (onSuccess)
|
|
17
|
-
onSuccess(val);
|
|
18
|
-
return result;
|
|
19
|
-
},
|
|
20
|
-
Err: (err) => {
|
|
21
|
-
if (onError)
|
|
22
|
-
onError(err);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.applyMatch = applyMatch;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ATresult } from "../classes/abstracts/ATresult";
|
|
2
|
-
import { SuccessCallback, ErrorCallback } from "../types/resultsCallbacks";
|
|
3
|
-
/**
|
|
4
|
-
* Applies match on the given result with optional success and error callbacks.
|
|
5
|
-
* @param result Result to apply match on.
|
|
6
|
-
* @param onSuccess Additional callback for success case.
|
|
7
|
-
* @param onError Additional callback for error case.
|
|
8
|
-
* @returns Returns the original result after applying the match.
|
|
9
|
-
*/
|
|
10
|
-
export declare function applyMatch(result: ATresult<any>, onSuccess?: SuccessCallback<any, any>, onError?: ErrorCallback<any, any>): ATresult<any>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//type: function
|
|
2
|
-
//description: Function to apply match on a result with optional callbacks.
|
|
3
|
-
/**
|
|
4
|
-
* Applies match on the given result with optional success and error callbacks.
|
|
5
|
-
* @param result Result to apply match on.
|
|
6
|
-
* @param onSuccess Additional callback for success case.
|
|
7
|
-
* @param onError Additional callback for error case.
|
|
8
|
-
* @returns Returns the original result after applying the match.
|
|
9
|
-
*/
|
|
10
|
-
export function applyMatch(result, onSuccess, onError) {
|
|
11
|
-
return result.match({
|
|
12
|
-
Ok: (val) => {
|
|
13
|
-
if (onSuccess)
|
|
14
|
-
onSuccess(val);
|
|
15
|
-
return result;
|
|
16
|
-
},
|
|
17
|
-
Err: (err) => {
|
|
18
|
-
if (onError)
|
|
19
|
-
onError(err);
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|