@j2blasco/ts-result 0.0.1
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 +0 -0
- package/dist/result.d.ts +34 -0
- package/dist/result.js +100 -0
- package/dist/result.test.d.ts +1 -0
- package/dist/result.test.js +55 -0
- package/dist/utils/test/assert-type.d.ts +2 -0
- package/dist/utils/test/assert-type.js +4 -0
- package/package.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 [Your Name]
|
|
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
|
Binary file
|
package/dist/result.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface Result<TSuccess1, TError1> {
|
|
2
|
+
andThen<TResult extends Result<any, any>>(f: (value: TSuccess1) => TResult): Result<SuccessType<TResult>, TError1 | ErrorType<TResult>>;
|
|
3
|
+
andThenAsync<TResult extends Result<any, any>>(f: (value: TSuccess1) => Promise<TResult>): Promise<Result<SuccessType<TResult>, TError1 | ErrorType<TResult>>>;
|
|
4
|
+
catchError<TResult extends Result<any, any>>(f: (error: TError1) => TResult): Result<TSuccess1 | SuccessType<TResult>, ErrorType<TResult>>;
|
|
5
|
+
catchErrorAsync<TResult extends Result<any, any>>(f: (error: TError1) => Promise<TResult>): Promise<Result<TSuccess1 | SuccessType<TResult>, ErrorType<TResult>>>;
|
|
6
|
+
unwrapOrThrow(errorCallback?: (e: TError1) => void): TSuccess1;
|
|
7
|
+
}
|
|
8
|
+
export declare function unwrapSuccessResult<T>(result: Result<T, never>): T;
|
|
9
|
+
export type ErrorWithCode<TCode, TData = object> = {
|
|
10
|
+
code: TCode;
|
|
11
|
+
data: TData;
|
|
12
|
+
};
|
|
13
|
+
export declare function errorWithCode<TCode, TData>(code: TCode, data: TData): ErrorWithCode<TCode, TData>;
|
|
14
|
+
export type ErrorUnknown = {
|
|
15
|
+
code: 'unknown';
|
|
16
|
+
data: {
|
|
17
|
+
message: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export declare function errorUnkown(message: string): ErrorUnknown;
|
|
21
|
+
export type SuccessType<T> = T extends Result<infer S, any> ? S : never;
|
|
22
|
+
export type ErrorType<T> = T extends Result<any, infer E> ? E : never;
|
|
23
|
+
export type SuccessVoid = never;
|
|
24
|
+
export declare function resultSuccessVoid(): Result<SuccessVoid, never>;
|
|
25
|
+
export declare function resultSuccess<TSuccess1>(value: TSuccess1): Result<TSuccess1, never>;
|
|
26
|
+
type EnforceLiteral<T> = string extends T ? never : T;
|
|
27
|
+
export declare class resultError {
|
|
28
|
+
static unknown(message: string): Result<never, ErrorUnknown>;
|
|
29
|
+
static withCode<TCode, TData = object>(code: EnforceLiteral<TCode>, data?: TData): Result<never, ErrorWithCode<TCode, TData>>;
|
|
30
|
+
static fromError<TSuccess1, TError1 extends ErrorWithCode<any, object>>(error: TError1): Result<TSuccess1, TError1>;
|
|
31
|
+
}
|
|
32
|
+
export type UnwrapErrorFromResult<T> = T extends Result<any, infer U> ? U : never;
|
|
33
|
+
export declare function makeSafePromise<T>(promise: Promise<T>): Promise<Result<T, ErrorUnknown>>;
|
|
34
|
+
export {};
|
package/dist/result.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export function unwrapSuccessResult(result) {
|
|
11
|
+
if (result instanceof ResultSuccessImp) {
|
|
12
|
+
return result['value'];
|
|
13
|
+
}
|
|
14
|
+
throw new Error('Unreachable code. Result is an error');
|
|
15
|
+
}
|
|
16
|
+
export function errorWithCode(code, data) {
|
|
17
|
+
return {
|
|
18
|
+
code,
|
|
19
|
+
data,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function errorUnkown(message) {
|
|
23
|
+
return {
|
|
24
|
+
code: 'unknown',
|
|
25
|
+
data: { message },
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export function resultSuccessVoid() {
|
|
29
|
+
return new ResultSuccessImp(undefined);
|
|
30
|
+
}
|
|
31
|
+
export function resultSuccess(value) {
|
|
32
|
+
return new ResultSuccessImp(value);
|
|
33
|
+
}
|
|
34
|
+
export class resultError {
|
|
35
|
+
static unknown(message) {
|
|
36
|
+
return new ResultErrorImp(errorUnkown(message));
|
|
37
|
+
}
|
|
38
|
+
static withCode(code, data = {}) {
|
|
39
|
+
return new ResultErrorImp(errorWithCode(code, data));
|
|
40
|
+
}
|
|
41
|
+
static fromError(error) {
|
|
42
|
+
return new ResultErrorImp(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class ResultSuccessImp {
|
|
46
|
+
constructor(value) {
|
|
47
|
+
this.value = value;
|
|
48
|
+
}
|
|
49
|
+
andThen(f) {
|
|
50
|
+
return f(this.value);
|
|
51
|
+
}
|
|
52
|
+
andThenAsync(f) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
return yield f(this.value);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catchError(_f) {
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
catchErrorAsync(_f) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
return this;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
unwrapOrThrow(_errorCallback) {
|
|
66
|
+
return this.value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
class ResultErrorImp {
|
|
70
|
+
constructor(error) {
|
|
71
|
+
this.error = error;
|
|
72
|
+
}
|
|
73
|
+
andThen(_f) {
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
andThenAsync(_f) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
return this;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catchError(f) {
|
|
82
|
+
return f(this.error);
|
|
83
|
+
}
|
|
84
|
+
catchErrorAsync(f) {
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
return yield f(this.error);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
unwrapOrThrow(errorCallback) {
|
|
90
|
+
if (errorCallback !== undefined) {
|
|
91
|
+
errorCallback(this.error);
|
|
92
|
+
}
|
|
93
|
+
throw this.error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function makeSafePromise(promise) {
|
|
97
|
+
return promise
|
|
98
|
+
.then((value) => resultSuccess(value))
|
|
99
|
+
.catch((error) => resultError.unknown(error.message));
|
|
100
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { resultSuccess, resultError, unwrapSuccessResult, resultSuccessVoid, } from "./result";
|
|
2
|
+
import { assertType, expectToCompile } from "./utils/test/assert-type";
|
|
3
|
+
function createResult() {
|
|
4
|
+
return resultSuccessVoid();
|
|
5
|
+
}
|
|
6
|
+
describe("Result Monad", () => {
|
|
7
|
+
test("resultSuccess should create a success result", () => {
|
|
8
|
+
const success = resultSuccess("test");
|
|
9
|
+
expect(unwrapSuccessResult(success)).toBe("test");
|
|
10
|
+
});
|
|
11
|
+
test("resultError.unknown should create an unknown error result", () => {
|
|
12
|
+
const error = resultError.unknown("An error occurred");
|
|
13
|
+
try {
|
|
14
|
+
error.unwrapOrThrow();
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
expect(e).toMatchObject({
|
|
18
|
+
code: "unknown",
|
|
19
|
+
data: { message: "An error occurred" },
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
test("resultError.withCode should create a coded error result", () => {
|
|
24
|
+
const error = resultError.withCode("test-code", {
|
|
25
|
+
detail: "Some details",
|
|
26
|
+
});
|
|
27
|
+
try {
|
|
28
|
+
error.unwrapOrThrow();
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
expect(e).toMatchObject({
|
|
32
|
+
code: "test-code",
|
|
33
|
+
data: { detail: "Some details" },
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
test("Result Monad success should be chainable with inferred types - string -> int", () => {
|
|
38
|
+
const resultString = createResult();
|
|
39
|
+
const resultInt = resultString.andThen((value) => {
|
|
40
|
+
assertType(true);
|
|
41
|
+
return resultSuccess(42);
|
|
42
|
+
});
|
|
43
|
+
assertType(true);
|
|
44
|
+
expectToCompile();
|
|
45
|
+
});
|
|
46
|
+
test("Result Monad success should be chainable with inferred types - obj -> obj", () => {
|
|
47
|
+
const resultString = createResult();
|
|
48
|
+
const resultInt = resultString.andThen((value) => {
|
|
49
|
+
assertType(true);
|
|
50
|
+
return resultSuccess({ test2: "test" });
|
|
51
|
+
});
|
|
52
|
+
assertType(true);
|
|
53
|
+
expectToCompile();
|
|
54
|
+
});
|
|
55
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@j2blasco/ts-result",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Result Monad for TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"build": "tsc"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@types/jest": "^29.5.14",
|
|
18
|
+
"jest": "^29.7.0",
|
|
19
|
+
"ts-jest": "^29.3.2"
|
|
20
|
+
}
|
|
21
|
+
}
|