@rotomeca/rop 0.0.2 → 0.0.3
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/classes/Fail.js +91 -0
- package/dist/cjs/classes/Success.js +90 -0
- package/dist/cjs/classes/abstracts/ATresult.js +28 -0
- package/dist/cjs/decorators/ErrorPath.internal/functions.js +37 -0
- package/dist/cjs/decorators/ErrorPath.js +29 -0
- package/dist/cjs/decorators/HappyPath.internal/functions.js +37 -0
- package/dist/cjs/decorators/HappyPath.js +29 -0
- package/dist/cjs/decorators/Risky.internal/functions.js +42 -0
- package/dist/cjs/decorators/Risky.js +28 -0
- package/{src/functions/applyMatch.ts → dist/cjs/functions/applyMatch.js} +10 -12
- package/dist/cjs/interfaces/ITResult.js +19 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types/ResultMatch.js +4 -0
- package/dist/cjs/types/resultsCallbacks.js +4 -0
- package/{src/classes/Fail.ts → dist/esm/classes/Fail.d.ts} +14 -37
- package/dist/esm/classes/Fail.js +87 -0
- package/{src/classes/Success.ts → dist/esm/classes/Success.d.ts} +14 -37
- package/dist/esm/classes/Success.js +86 -0
- package/{src/classes/abstracts/ATresult.ts → dist/esm/classes/abstracts/ATresult.d.ts} +11 -19
- package/dist/esm/classes/abstracts/ATresult.js +24 -0
- package/dist/esm/decorators/ErrorPath.d.ts +7 -0
- package/dist/esm/decorators/ErrorPath.internal/functions.d.ts +19 -0
- package/{src/decorators/ErrorPath.internal/functions.ts → dist/esm/decorators/ErrorPath.internal/functions.js} +9 -14
- package/{src/decorators/ErrorPath.ts → dist/esm/decorators/ErrorPath.js} +7 -10
- package/dist/esm/decorators/HappyPath.d.ts +7 -0
- package/dist/esm/decorators/HappyPath.internal/functions.d.ts +19 -0
- package/{src/decorators/HappyPath.internal/functions.ts → dist/esm/decorators/HappyPath.internal/functions.js} +9 -14
- package/{src/decorators/HappyPath.ts → dist/esm/decorators/HappyPath.js} +7 -10
- package/dist/esm/decorators/Risky.d.ts +5 -0
- package/dist/esm/decorators/Risky.internal/functions.d.ts +17 -0
- package/{src/decorators/Risky.internal/functions.ts → dist/esm/decorators/Risky.internal/functions.js} +9 -12
- package/{src/decorators/Risky.ts → dist/esm/decorators/Risky.js} +6 -9
- package/dist/esm/functions/applyMatch.d.ts +10 -0
- package/dist/esm/functions/applyMatch.js +23 -0
- package/{src/interfaces/ITResult.ts → dist/esm/interfaces/ITResult.d.ts} +12 -17
- package/dist/esm/interfaces/ITResult.js +16 -0
- package/{src/types/ResultMatch.ts → dist/esm/types/ResultMatch.d.ts} +2 -7
- package/dist/esm/types/ResultMatch.js +3 -0
- package/{src/types/resultsCallbacks.ts → dist/esm/types/resultsCallbacks.d.ts} +7 -11
- package/dist/esm/types/resultsCallbacks.js +3 -0
- package/package.json +20 -4
- package/.prettierc.json +0 -9
- package/index.ts +0 -14
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
// type: functions
|
|
2
2
|
// description: Functions to handle risky operations by converting exceptions into Result types.
|
|
3
|
-
|
|
4
3
|
import { ATresult } from "../../classes/abstracts/ATresult";
|
|
5
4
|
import { Fail } from "../../classes/Fail";
|
|
6
5
|
import { Success } from "../../classes/Success";
|
|
7
|
-
|
|
8
6
|
/**
|
|
9
7
|
* Handles synchronous risky operations by converting exceptions into Result types.
|
|
10
8
|
* @param this The context in which the original function is called.
|
|
@@ -12,16 +10,15 @@ import { Success } from "../../classes/Success";
|
|
|
12
10
|
* @param args Arguments to be passed to the original function.
|
|
13
11
|
* @returns An ATresult representing success or failure.
|
|
14
12
|
*/
|
|
15
|
-
export function riskySync(
|
|
13
|
+
export function riskySync(original, ...args) {
|
|
16
14
|
try {
|
|
17
15
|
const result = original.apply(this, args);
|
|
18
|
-
|
|
19
16
|
return result instanceof ATresult ? result : Success.Create(result);
|
|
20
|
-
}
|
|
21
|
-
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
return error instanceof ATresult ? error : Fail.Create(error);
|
|
22
20
|
}
|
|
23
21
|
}
|
|
24
|
-
|
|
25
22
|
/**
|
|
26
23
|
* Handles asynchronous risky operations by converting exceptions into Result types.
|
|
27
24
|
* @param this The context in which the original function is called.
|
|
@@ -29,12 +26,12 @@ export function riskySync(this: any, original: Function, ...args: any[]): ATresu
|
|
|
29
26
|
* @param args Arguments to be passed to the original function.
|
|
30
27
|
* @returns A Promise that resolves to an ATresult representing success or failure.
|
|
31
28
|
*/
|
|
32
|
-
export async function riskyAsync(
|
|
29
|
+
export async function riskyAsync(original, ...args) {
|
|
33
30
|
try {
|
|
34
31
|
const result = await original.apply(this, args);
|
|
35
|
-
|
|
36
32
|
return result instanceof ATresult ? result : Success.Create(result);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
return error instanceof ATresult ? error : Fail.Create(error as Error);
|
|
39
33
|
}
|
|
40
|
-
|
|
34
|
+
catch (error) {
|
|
35
|
+
return error instanceof ATresult ? error : Fail.Create(error);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,27 +1,24 @@
|
|
|
1
1
|
// type: decorator
|
|
2
2
|
// description: Decorator to handle risky operations by converting exceptions into Result types.
|
|
3
3
|
import { riskyAsync, riskySync } from "./Risky.internal/functions";
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* Decorator to handle risky operations by converting exceptions into Result types.
|
|
7
6
|
* @returns A method decorator that wraps the original method with error handling.
|
|
8
7
|
*/
|
|
9
8
|
export function Risky() {
|
|
10
|
-
return function (target
|
|
9
|
+
return function (target, key, descriptor) {
|
|
11
10
|
const original = descriptor.value;
|
|
12
|
-
|
|
13
11
|
const isAsync = original.constructor.name === "AsyncFunction";
|
|
14
|
-
|
|
15
12
|
if (isAsync) {
|
|
16
|
-
descriptor.value = async function (
|
|
13
|
+
descriptor.value = async function (...args) {
|
|
17
14
|
return riskyAsync.call(this, original, ...args);
|
|
18
15
|
};
|
|
19
|
-
}
|
|
20
|
-
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
descriptor.value = function (...args) {
|
|
21
19
|
return riskySync.call(this, original, ...args);
|
|
22
20
|
};
|
|
23
21
|
}
|
|
24
|
-
|
|
25
22
|
return descriptor;
|
|
26
|
-
}
|
|
23
|
+
};
|
|
27
24
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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>;
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|
|
@@ -1,72 +1,67 @@
|
|
|
1
|
-
// type: interface
|
|
2
|
-
// description: Interface for a result type that can represent either a success or an error.
|
|
3
|
-
|
|
4
1
|
import { ResultMatch } from "../types/ResultMatch";
|
|
5
2
|
import { ChainedCallback, ErrorMapCallback, SuccessMapCallback } from "../types/resultsCallbacks";
|
|
6
|
-
|
|
7
3
|
/**
|
|
8
4
|
* Enumeration representing the state of the result.
|
|
9
5
|
*/
|
|
10
|
-
export enum ResultState {
|
|
6
|
+
export declare enum ResultState {
|
|
11
7
|
/**
|
|
12
8
|
* The result represents a successful outcome.
|
|
13
9
|
*/
|
|
14
|
-
Success,
|
|
10
|
+
Success = 0,
|
|
15
11
|
/**
|
|
16
12
|
* The result represents an error outcome.
|
|
17
13
|
*/
|
|
18
|
-
Error
|
|
14
|
+
Error = 1
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
/**
|
|
22
17
|
* Interface representing a result that can be either a success or an error.
|
|
23
|
-
*
|
|
18
|
+
*
|
|
24
19
|
* @template Success - The type of the success value.
|
|
25
20
|
* @template Error - The type of the error value.
|
|
26
21
|
*/
|
|
27
22
|
export declare interface ITResult<Success, Error> {
|
|
28
23
|
/**
|
|
29
24
|
* Gets the current state of the result.
|
|
30
|
-
*
|
|
25
|
+
*
|
|
31
26
|
* @returns The state of the result, either Success or Error.
|
|
32
27
|
*/
|
|
33
28
|
state(): ResultState;
|
|
34
29
|
/**
|
|
35
30
|
* Checks if the result is a success.
|
|
36
|
-
*
|
|
31
|
+
*
|
|
37
32
|
* @returns True if the result is a success, false otherwise.
|
|
38
33
|
*/
|
|
39
34
|
isSuccess(): boolean;
|
|
40
35
|
/**
|
|
41
36
|
* Checks if the result is an error.
|
|
42
|
-
*
|
|
37
|
+
*
|
|
43
38
|
* @returns True if the result is an error, false otherwise.
|
|
44
39
|
*/
|
|
45
40
|
isError(): boolean;
|
|
46
41
|
/**
|
|
47
42
|
* Matches the result with the provided matcher functions.
|
|
48
|
-
*
|
|
43
|
+
*
|
|
49
44
|
* @param matcher An object containing functions to handle success and error cases.
|
|
50
45
|
* @returns The result of applying the appropriate matcher function.
|
|
51
46
|
*/
|
|
52
47
|
match<Result>(matcher: ResultMatch<Success, Error, Result>): Result;
|
|
53
48
|
/**
|
|
54
49
|
* Chains another result-producing function to be executed if this result is a success.
|
|
55
|
-
*
|
|
50
|
+
*
|
|
56
51
|
* @param fn A function that takes the success value and returns a new result.
|
|
57
52
|
* @returns The new result produced by the function, or the current error result.
|
|
58
53
|
*/
|
|
59
54
|
andThen<NewSuccess>(fn: ChainedCallback<Success, NewSuccess, Error>): ITResult<NewSuccess, Error>;
|
|
60
55
|
/**
|
|
61
56
|
* Transforms the success value using the provided function.
|
|
62
|
-
*
|
|
57
|
+
*
|
|
63
58
|
* @param fn A function that takes the success value and returns a new success value.
|
|
64
59
|
* @returns A new result with the transformed success value, or the current error result.
|
|
65
60
|
*/
|
|
66
61
|
map<NewSuccess>(fn: SuccessMapCallback<Success, NewSuccess>): ITResult<NewSuccess, Error>;
|
|
67
62
|
/**
|
|
68
63
|
* Transforms the error value using the provided function.
|
|
69
|
-
*
|
|
64
|
+
*
|
|
70
65
|
* @param fn A function that takes the error value and returns a new error value.
|
|
71
66
|
* @returns A new result with the transformed error value, or the current success result.
|
|
72
67
|
*/
|
|
@@ -83,4 +78,4 @@ export declare interface ITResult<Success, Error> {
|
|
|
83
78
|
* @returns The success value if the result is a success, otherwise the default value.
|
|
84
79
|
*/
|
|
85
80
|
unwrapOr(defaultValue: Success): Success;
|
|
86
|
-
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// type: interface
|
|
2
|
+
// description: Interface for a result type that can represent either a success or an error.
|
|
3
|
+
/**
|
|
4
|
+
* Enumeration representing the state of the result.
|
|
5
|
+
*/
|
|
6
|
+
export var ResultState;
|
|
7
|
+
(function (ResultState) {
|
|
8
|
+
/**
|
|
9
|
+
* The result represents a successful outcome.
|
|
10
|
+
*/
|
|
11
|
+
ResultState[ResultState["Success"] = 0] = "Success";
|
|
12
|
+
/**
|
|
13
|
+
* The result represents an error outcome.
|
|
14
|
+
*/
|
|
15
|
+
ResultState[ResultState["Error"] = 1] = "Error";
|
|
16
|
+
})(ResultState || (ResultState = {}));
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
// type: type
|
|
2
|
-
// description: Type representing the matcher functions for a result type.
|
|
3
|
-
|
|
4
1
|
import { ErrorCallback, SuccessCallback } from "./resultsCallbacks";
|
|
5
|
-
|
|
6
2
|
/**
|
|
7
3
|
* Type representing the matcher functions for a result type.
|
|
8
|
-
*
|
|
4
|
+
*
|
|
9
5
|
* @template Success - The type of the success value.
|
|
10
6
|
* @template Error - The type of the error value.
|
|
11
7
|
* @template Result - The type of the result produced by the matcher functions.
|
|
@@ -19,5 +15,4 @@ export type ResultMatch<Success, Error, Result> = {
|
|
|
19
15
|
* Callback function to handle the error case.
|
|
20
16
|
*/
|
|
21
17
|
Err: ErrorCallback<Error, Result>;
|
|
22
|
-
}
|
|
23
|
-
|
|
18
|
+
};
|
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
// type: types
|
|
2
|
-
// description: Callback types for handling results in a functional way.
|
|
3
|
-
|
|
4
1
|
import { ITResult } from "../interfaces/ITResult";
|
|
5
|
-
|
|
6
2
|
/**
|
|
7
3
|
* Callback type for handling success values.
|
|
8
|
-
*
|
|
4
|
+
*
|
|
9
5
|
* @template Success - The type of the success value.
|
|
10
6
|
* @template Result - The type of the result produced by the callback.
|
|
11
7
|
*/
|
|
12
8
|
export declare type SuccessCallback<Success, Result> = (value: Success) => Result;
|
|
13
9
|
/**
|
|
14
10
|
* Callback type for handling error values.
|
|
15
|
-
*
|
|
11
|
+
*
|
|
16
12
|
* @template Error - The type of the error value.
|
|
17
13
|
* @template Result - The type of the result produced by the callback.
|
|
18
14
|
*/
|
|
19
15
|
export declare type ErrorCallback<Error, Result> = (error: Error) => Result;
|
|
20
16
|
/**
|
|
21
17
|
* Callback type for chaining result-producing functions.
|
|
22
|
-
*
|
|
18
|
+
*
|
|
23
19
|
* @template Success - The type of the success value.
|
|
24
20
|
* @template NewSuccess - The type of the new success value produced by the chained function.
|
|
25
21
|
* @template Error - The type of the error value.
|
|
@@ -27,15 +23,15 @@ export declare type ErrorCallback<Error, Result> = (error: Error) => Result;
|
|
|
27
23
|
export declare type ChainedCallback<Success, NewSuccess, Error> = (value: Success) => ITResult<NewSuccess, Error>;
|
|
28
24
|
/**
|
|
29
25
|
* Callback type for transforming success values.
|
|
30
|
-
*
|
|
26
|
+
*
|
|
31
27
|
* @template Success - The type of the success value.
|
|
32
28
|
* @template NewSuccess - The type of the new success value after transformation.
|
|
33
29
|
*/
|
|
34
|
-
export declare type SuccessMapCallback<Success, NewSuccess> = (value: Success) => NewSuccess
|
|
30
|
+
export declare type SuccessMapCallback<Success, NewSuccess> = (value: Success) => NewSuccess;
|
|
35
31
|
/**
|
|
36
32
|
* Callback type for transforming error values.
|
|
37
|
-
*
|
|
33
|
+
*
|
|
38
34
|
* @template Error - The type of the error value.
|
|
39
35
|
* @template NewError - The type of the new error value after transformation.
|
|
40
36
|
*/
|
|
41
|
-
export declare type ErrorMapCallback<Error, NewError> = (value: Error) => NewError
|
|
37
|
+
export declare type ErrorMapCallback<Error, NewError> = (value: Error) => NewError;
|
package/package.json
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rotomeca/rop",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/cjs/index.js",
|
|
6
|
+
"module": "./dist/esm/index.js",
|
|
7
|
+
"types": "./dist/esm/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/esm/index.d.ts",
|
|
11
|
+
"import": "./dist/esm/index.js",
|
|
12
|
+
"require": "./dist/cjs/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
5
19
|
"scripts": {
|
|
6
|
-
"
|
|
20
|
+
"build": "npm run build:esm && npm run build:cjs",
|
|
21
|
+
"build:esm": "tsc -p tsconfig.json",
|
|
22
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json"
|
|
7
23
|
},
|
|
8
24
|
"author": "Rotomeca",
|
|
9
25
|
"license": "ISC",
|
|
@@ -15,4 +31,4 @@
|
|
|
15
31
|
"prettier-eslint": "^16.4.2",
|
|
16
32
|
"typescript": "^4.9.5"
|
|
17
33
|
}
|
|
18
|
-
}
|
|
34
|
+
}
|
package/.prettierc.json
DELETED
package/index.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// Décorateurs (API Publique principale)
|
|
2
|
-
export * from './src/decorators/Risky';
|
|
3
|
-
export * from './src/decorators/HappyPath';
|
|
4
|
-
export * from './src/decorators/ErrorPath';
|
|
5
|
-
|
|
6
|
-
// Classes & Abstractions (Pour le typage et création manuelle)
|
|
7
|
-
export * from './src/classes/Fail';
|
|
8
|
-
export * from './src/classes/Success';
|
|
9
|
-
export {ATresult as Result} from './src/classes/abstracts/ATresult';
|
|
10
|
-
|
|
11
|
-
// Types (Pour l'IntelliSense)
|
|
12
|
-
export * from './src/types/resultsCallbacks';
|
|
13
|
-
export * from './src/types/ResultMatch';
|
|
14
|
-
|