@adobe/aio-commerce-lib-core 0.2.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/.turbo/turbo-build.log +36 -0
- package/CHANGELOG.md +12 -0
- package/LICENSE +201 -0
- package/README.md +154 -0
- package/dist/cjs/chunk-CsX-DzYB.cjs +42 -0
- package/dist/cjs/index.cjs +15 -0
- package/dist/cjs/index.d.cts +3 -0
- package/dist/cjs/lib/result.cjs +10 -0
- package/dist/cjs/lib/validation.cjs +4 -0
- package/dist/cjs/result-B_qN_g1r.d.cts +26 -0
- package/dist/cjs/result-fxmTDycS.cjs +160 -0
- package/dist/cjs/validation-12iMy71C.cjs +71 -0
- package/dist/cjs/validation-BonM9W9Z.d.cts +21 -0
- package/dist/es/chunk-Cl8Af3a2.js +11 -0
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +4 -0
- package/dist/es/lib/result.d.ts +2 -0
- package/dist/es/lib/result.js +3 -0
- package/dist/es/lib/validation.d.ts +3 -0
- package/dist/es/lib/validation.js +3 -0
- package/dist/es/result-B394X-_m.js +107 -0
- package/dist/es/result-BooxElIJ.d.ts +26 -0
- package/dist/es/validation-B2JQpDTl.d.ts +21 -0
- package/dist/es/validation-DmGRxtJ7.js +54 -0
- package/dist/lib/result.d.cts +2 -0
- package/dist/lib/validation.d.cts +3 -0
- package/package.json +77 -0
- package/source/index.ts +15 -0
- package/source/lib/result.ts +156 -0
- package/source/lib/validation.ts +97 -0
- package/test/result.test.ts +56 -0
- package/test/validation.test.ts +71 -0
- package/tsconfig.json +15 -0
- package/tsdown.config.ts +11 -0
- package/vitest.config.ts +16 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CsX-DzYB.cjs');
|
|
2
|
+
const ansis = require_chunk.__toESM(require("ansis"));
|
|
3
|
+
const valibot = require_chunk.__toESM(require("valibot"));
|
|
4
|
+
|
|
5
|
+
//#region source/lib/validation.ts
|
|
6
|
+
var validation_exports = {};
|
|
7
|
+
require_chunk.__export(validation_exports, {
|
|
8
|
+
makeValidationError: () => makeValidationError,
|
|
9
|
+
summarizeValidationError: () => summarizeValidationError
|
|
10
|
+
});
|
|
11
|
+
const LAST_RETURN_CHAR = "└── ";
|
|
12
|
+
const RETURN_CHAR = "├── ";
|
|
13
|
+
const ISSUE_KIND_TO_ERROR_TITLE = {
|
|
14
|
+
schema: "Schema validation error",
|
|
15
|
+
validation: "Input error",
|
|
16
|
+
transformation: "Transformation error"
|
|
17
|
+
};
|
|
18
|
+
function issueToDisplay(issues) {
|
|
19
|
+
const lines = issues.map((issue, index) => {
|
|
20
|
+
const returnChar = (0, ansis.cyanBright)(index === issues.length - 1 ? LAST_RETURN_CHAR : RETURN_CHAR);
|
|
21
|
+
const kindText = (0, ansis.yellowBright)(ISSUE_KIND_TO_ERROR_TITLE[issue.kind] || "Unknown issue kind");
|
|
22
|
+
const dotPath = (0, valibot.getDotPath)(issue);
|
|
23
|
+
const path = dotPath ? (0, ansis.cyanBright)(dotPath) + (0, ansis.whiteBright)((0, ansis.dim)(" →")) : "";
|
|
24
|
+
const issueLine = `${kindText} ${(0, ansis.whiteBright)("at")} ${path} ${(0, ansis.whiteBright)(issue.message)}`;
|
|
25
|
+
return `${returnChar} ${issueLine}`;
|
|
26
|
+
});
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Summarizes a validation error by displaying the error message and the issues.
|
|
31
|
+
* @param error The validation error to summarize.
|
|
32
|
+
* @returns A pretty-printed string containing the summary of the validation error.
|
|
33
|
+
*/
|
|
34
|
+
function summarizeValidationError(error) {
|
|
35
|
+
return `${(0, ansis.whiteBright)(error.message)}\n${issueToDisplay(error.issues)}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a validation error.
|
|
39
|
+
* @param message The validation error message.
|
|
40
|
+
* @param issues The issues that occurred during validation.
|
|
41
|
+
* @param info Additional information about the error.
|
|
42
|
+
* @returns A validation error.
|
|
43
|
+
*/
|
|
44
|
+
function makeValidationError(message, issues, info = {}) {
|
|
45
|
+
return {
|
|
46
|
+
_tag: "ValidationError",
|
|
47
|
+
message,
|
|
48
|
+
issues,
|
|
49
|
+
...info
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
Object.defineProperty(exports, 'makeValidationError', {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return makeValidationError;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, 'summarizeValidationError', {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () {
|
|
63
|
+
return summarizeValidationError;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(exports, 'validation_exports', {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () {
|
|
69
|
+
return validation_exports;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ErrorType, __export } from "./result-B_qN_g1r.cjs";
|
|
2
|
+
import { GenericIssue } from "valibot";
|
|
3
|
+
|
|
4
|
+
//#region source/lib/validation.d.ts
|
|
5
|
+
declare namespace validation_d_exports {
|
|
6
|
+
export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError };
|
|
7
|
+
}
|
|
8
|
+
type ValidationErrorTag = `${string}ValidationError`;
|
|
9
|
+
declare function summarizeValidationError(error: ValidationError): string;
|
|
10
|
+
declare function makeValidationError<TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>>(message: string, issues: TIssues, info?: TInfo): {
|
|
11
|
+
_tag: "ValidationError";
|
|
12
|
+
message: string;
|
|
13
|
+
issues: TIssues;
|
|
14
|
+
} & TInfo;
|
|
15
|
+
type ValidationErrorType<TErrorTag extends ValidationErrorTag = "ValidationError", TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>> = ErrorType<TErrorTag, TInfo & {
|
|
16
|
+
message: string;
|
|
17
|
+
issues: TIssues;
|
|
18
|
+
}>;
|
|
19
|
+
type ValidationError = ValidationErrorType<ValidationErrorTag, GenericIssue[], Record<string, unknown>>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError, validation_d_exports };
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { __export } from "./chunk-Cl8Af3a2.js";
|
|
2
|
+
|
|
3
|
+
//#region source/lib/result.ts
|
|
4
|
+
var result_exports = {};
|
|
5
|
+
__export(result_exports, {
|
|
6
|
+
err: () => err,
|
|
7
|
+
isErr: () => isErr,
|
|
8
|
+
isOk: () => isOk,
|
|
9
|
+
map: () => map,
|
|
10
|
+
mapErr: () => mapErr,
|
|
11
|
+
ok: () => ok,
|
|
12
|
+
unwrap: () => unwrap,
|
|
13
|
+
unwrapErr: () => unwrapErr
|
|
14
|
+
});
|
|
15
|
+
const SuccessOrFailure = {
|
|
16
|
+
SUCCESS: "success",
|
|
17
|
+
FAILURE: "failure"
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Wraps the given value in a successful result.
|
|
21
|
+
* @param value The value to wrap.
|
|
22
|
+
* @returns A successful result containing the given value.
|
|
23
|
+
*/
|
|
24
|
+
function ok(value) {
|
|
25
|
+
return {
|
|
26
|
+
type: SuccessOrFailure.SUCCESS,
|
|
27
|
+
value
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Wraps the given error in a failed result.
|
|
32
|
+
* @param error The error to wrap.
|
|
33
|
+
* @returns A failed result containing the given error.
|
|
34
|
+
*/
|
|
35
|
+
function err(error) {
|
|
36
|
+
return {
|
|
37
|
+
type: SuccessOrFailure.FAILURE,
|
|
38
|
+
error
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Unwraps a result to retrieve its value.
|
|
43
|
+
* If the result is a failure, an error will be thrown.
|
|
44
|
+
*
|
|
45
|
+
* @param result The result to unwrap.
|
|
46
|
+
* @returns The value contained in the successful result.
|
|
47
|
+
* @throws An error if the result is a failure.
|
|
48
|
+
*/
|
|
49
|
+
function unwrap(result) {
|
|
50
|
+
if (result.type === SuccessOrFailure.SUCCESS) return result.value;
|
|
51
|
+
throw new Error("Can't unwrap the value from this result because it's a failure");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Unwraps a result to retrieve its error.
|
|
55
|
+
* If the result is a success, an error will be thrown.
|
|
56
|
+
*
|
|
57
|
+
* @param result The result to unwrap.
|
|
58
|
+
* @returns The error contained in the failed result.
|
|
59
|
+
* @throws An error if the result is a success.
|
|
60
|
+
*/
|
|
61
|
+
function unwrapErr(result) {
|
|
62
|
+
if (result.type === SuccessOrFailure.FAILURE) return result.error;
|
|
63
|
+
throw new Error("Can't unwrap the error from this result because it's a success");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks if a result is a success.
|
|
67
|
+
* @param result The result to check.
|
|
68
|
+
* @returns True if the result is a success, false otherwise.
|
|
69
|
+
*/
|
|
70
|
+
function isOk(result) {
|
|
71
|
+
return result.type === SuccessOrFailure.SUCCESS;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Checks if a result is a failure.
|
|
75
|
+
* @param result The result to check.
|
|
76
|
+
* @returns True if the result is a failure, false otherwise.
|
|
77
|
+
*/
|
|
78
|
+
function isErr(result) {
|
|
79
|
+
return result.type === SuccessOrFailure.FAILURE;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Maps a successful result to another value.
|
|
83
|
+
* If the result is a failure, it is returned unchanged.
|
|
84
|
+
*
|
|
85
|
+
* @param result The result to map.
|
|
86
|
+
* @param fn A function to transform the success value.
|
|
87
|
+
* @returns A new {@link Result} with the transformed value or the original error.
|
|
88
|
+
*/
|
|
89
|
+
function map(result, fn) {
|
|
90
|
+
if (isOk(result)) return ok(fn(result.value));
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Maps a failed result's error to another error value.
|
|
95
|
+
* If the result is a success, it is returned unchanged.
|
|
96
|
+
*
|
|
97
|
+
* @param result The result to map.
|
|
98
|
+
* @param fn A function to transform the error value.
|
|
99
|
+
* @returns A new Result with the original value or transformed error.
|
|
100
|
+
*/
|
|
101
|
+
function mapErr(result, fn) {
|
|
102
|
+
if (isErr(result)) return err(fn(result.error));
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
export { err, isErr, isOk, map, mapErr, ok, result_exports, unwrap, unwrapErr };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
declare namespace result_d_exports {
|
|
2
|
+
export { Err, ErrorTag, ErrorType, Ok, Result, err, isErr, isOk, map, mapErr, ok, unwrap, unwrapErr };
|
|
3
|
+
}
|
|
4
|
+
type ErrorTag = `${string}Error`;
|
|
5
|
+
type Ok<T> = {
|
|
6
|
+
type: "success";
|
|
7
|
+
value: T;
|
|
8
|
+
};
|
|
9
|
+
type Err<E extends ErrorType<ErrorTag>> = {
|
|
10
|
+
type: "failure";
|
|
11
|
+
error: E;
|
|
12
|
+
};
|
|
13
|
+
type Result<T, E extends ErrorType<ErrorTag>> = Ok<T> | Err<E>;
|
|
14
|
+
type ErrorType<TTag extends ErrorTag = `Error`, TInfo extends Record<string, unknown> = Record<string, unknown>> = TInfo & {
|
|
15
|
+
_tag: TTag;
|
|
16
|
+
};
|
|
17
|
+
declare function ok<T>(value: T): Ok<T>;
|
|
18
|
+
declare function err<E extends ErrorType<ErrorTag>>(error: E): Err<E>;
|
|
19
|
+
declare function unwrap<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): T;
|
|
20
|
+
declare function unwrapErr<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): E;
|
|
21
|
+
declare function isOk<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): result is Ok<T>;
|
|
22
|
+
declare function isErr<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>): result is Err<E>;
|
|
23
|
+
declare function map<T, U, E extends ErrorType<ErrorTag>>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
24
|
+
declare function mapErr<T, E extends ErrorType<ErrorTag>, F extends ErrorType<ErrorTag>>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { Err, ErrorTag, ErrorType, Ok, Result, err, isErr, isOk, map, mapErr, ok, result_d_exports, unwrap, unwrapErr };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ErrorType } from "./result-BooxElIJ.js";
|
|
2
|
+
import { GenericIssue } from "valibot";
|
|
3
|
+
|
|
4
|
+
//#region source/lib/validation.d.ts
|
|
5
|
+
declare namespace validation_d_exports {
|
|
6
|
+
export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError };
|
|
7
|
+
}
|
|
8
|
+
type ValidationErrorTag = `${string}ValidationError`;
|
|
9
|
+
declare function summarizeValidationError(error: ValidationError): string;
|
|
10
|
+
declare function makeValidationError<TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>>(message: string, issues: TIssues, info?: TInfo): {
|
|
11
|
+
_tag: "ValidationError";
|
|
12
|
+
message: string;
|
|
13
|
+
issues: TIssues;
|
|
14
|
+
} & TInfo;
|
|
15
|
+
type ValidationErrorType<TErrorTag extends ValidationErrorTag = "ValidationError", TIssues extends GenericIssue[] = GenericIssue[], TInfo extends Record<string, unknown> = Record<string, unknown>> = ErrorType<TErrorTag, TInfo & {
|
|
16
|
+
message: string;
|
|
17
|
+
issues: TIssues;
|
|
18
|
+
}>;
|
|
19
|
+
type ValidationError = ValidationErrorType<ValidationErrorTag, GenericIssue[], Record<string, unknown>>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ValidationError, ValidationErrorType, makeValidationError, summarizeValidationError, validation_d_exports };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { __export } from "./chunk-Cl8Af3a2.js";
|
|
2
|
+
import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
|
|
3
|
+
import { getDotPath } from "valibot";
|
|
4
|
+
|
|
5
|
+
//#region source/lib/validation.ts
|
|
6
|
+
var validation_exports = {};
|
|
7
|
+
__export(validation_exports, {
|
|
8
|
+
makeValidationError: () => makeValidationError,
|
|
9
|
+
summarizeValidationError: () => summarizeValidationError
|
|
10
|
+
});
|
|
11
|
+
const LAST_RETURN_CHAR = "└── ";
|
|
12
|
+
const RETURN_CHAR = "├── ";
|
|
13
|
+
const ISSUE_KIND_TO_ERROR_TITLE = {
|
|
14
|
+
schema: "Schema validation error",
|
|
15
|
+
validation: "Input error",
|
|
16
|
+
transformation: "Transformation error"
|
|
17
|
+
};
|
|
18
|
+
function issueToDisplay(issues) {
|
|
19
|
+
const lines = issues.map((issue, index) => {
|
|
20
|
+
const returnChar = cyanBright(index === issues.length - 1 ? LAST_RETURN_CHAR : RETURN_CHAR);
|
|
21
|
+
const kindText = yellowBright(ISSUE_KIND_TO_ERROR_TITLE[issue.kind] || "Unknown issue kind");
|
|
22
|
+
const dotPath = getDotPath(issue);
|
|
23
|
+
const path = dotPath ? cyanBright(dotPath) + whiteBright(dim(" →")) : "";
|
|
24
|
+
const issueLine = `${kindText} ${whiteBright("at")} ${path} ${whiteBright(issue.message)}`;
|
|
25
|
+
return `${returnChar} ${issueLine}`;
|
|
26
|
+
});
|
|
27
|
+
return lines.join("\n");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Summarizes a validation error by displaying the error message and the issues.
|
|
31
|
+
* @param error The validation error to summarize.
|
|
32
|
+
* @returns A pretty-printed string containing the summary of the validation error.
|
|
33
|
+
*/
|
|
34
|
+
function summarizeValidationError(error) {
|
|
35
|
+
return `${whiteBright(error.message)}\n${issueToDisplay(error.issues)}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a validation error.
|
|
39
|
+
* @param message The validation error message.
|
|
40
|
+
* @param issues The issues that occurred during validation.
|
|
41
|
+
* @param info Additional information about the error.
|
|
42
|
+
* @returns A validation error.
|
|
43
|
+
*/
|
|
44
|
+
function makeValidationError(message, issues, info = {}) {
|
|
45
|
+
return {
|
|
46
|
+
_tag: "ValidationError",
|
|
47
|
+
message,
|
|
48
|
+
issues,
|
|
49
|
+
...info
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { makeValidationError, summarizeValidationError, validation_exports };
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adobe/aio-commerce-lib-core",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"private": false,
|
|
6
|
+
"author": "Adobe Inc.",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22"
|
|
9
|
+
},
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"description": "Core utilities for AIO Commerce SDK Libraries",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"aio",
|
|
14
|
+
"adobe-io",
|
|
15
|
+
"commerce",
|
|
16
|
+
"adobe-commerce",
|
|
17
|
+
"adobe-commerce-sdk",
|
|
18
|
+
"aio-commerce-sdk",
|
|
19
|
+
"aio-commerce-lib-core",
|
|
20
|
+
"aio-commerce-sdk-lib-core"
|
|
21
|
+
],
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/adobe/aio-commerce-sdk/issues"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/adobe/aio-commerce-sdk.git",
|
|
28
|
+
"directory": "packages/aio-commerce-lib-core"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/cjs/index.cjs",
|
|
31
|
+
"module": "./dist/es/index.js",
|
|
32
|
+
"types": "./dist/es/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": {
|
|
36
|
+
"types": "./dist/es/index.d.ts",
|
|
37
|
+
"default": "./dist/es/index.js"
|
|
38
|
+
},
|
|
39
|
+
"require": {
|
|
40
|
+
"types": "./dist/cjs/index.d.cts",
|
|
41
|
+
"default": "./dist/cjs/index.cjs"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"./*": {
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./dist/es/lib/*.d.ts",
|
|
47
|
+
"default": "./dist/es/lib/*.js"
|
|
48
|
+
},
|
|
49
|
+
"require": {
|
|
50
|
+
"types": "./dist/cjs/lib/*.d.cts",
|
|
51
|
+
"default": "./dist/cjs/lib/*.cjs"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"./package.json": "./package.json"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"ansis": "^4.1.0",
|
|
58
|
+
"valibot": "^1.1.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"vitest": "^2.0.0",
|
|
62
|
+
"@aio-commerce-sdk/config-tsdown": "1.0.0",
|
|
63
|
+
"@aio-commerce-sdk/config-typescript": "1.0.0"
|
|
64
|
+
},
|
|
65
|
+
"sideEffects": false,
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsdown",
|
|
68
|
+
"check:ci": "pnpm -w check:ci .",
|
|
69
|
+
"format": "pnpm -w format .",
|
|
70
|
+
"format:check": "pnpm -w format:check .",
|
|
71
|
+
"lint": "pnpm -w lint .",
|
|
72
|
+
"lint:fix": "pnpm -w lint:fix .",
|
|
73
|
+
"typecheck": "tsc --noEmit && echo '✅ No type errors found.'",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest --watch"
|
|
76
|
+
}
|
|
77
|
+
}
|
package/source/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// Export here the public API of the package.
|
|
14
|
+
export * as Result from "./lib/result";
|
|
15
|
+
export * as Validation from "./lib/validation";
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const SuccessOrFailure = {
|
|
14
|
+
SUCCESS: "success",
|
|
15
|
+
FAILURE: "failure",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
/** Defines the tag of an error type. */
|
|
19
|
+
export type ErrorTag = `${string}Error`;
|
|
20
|
+
|
|
21
|
+
/** Defines a successful result. */
|
|
22
|
+
export type Ok<T> = { type: "success"; value: T };
|
|
23
|
+
|
|
24
|
+
/** Defines a failed result. */
|
|
25
|
+
export type Err<E extends ErrorType<ErrorTag>> = {
|
|
26
|
+
type: "failure";
|
|
27
|
+
error: E;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Defines a result that can be either a success or a failure. */
|
|
31
|
+
export type Result<T, E extends ErrorType<ErrorTag>> = Ok<T> | Err<E>;
|
|
32
|
+
|
|
33
|
+
/** Defines an error type that can contain any additional properties. */
|
|
34
|
+
export type ErrorType<
|
|
35
|
+
TTag extends ErrorTag = `Error`,
|
|
36
|
+
TInfo extends Record<string, unknown> = Record<string, unknown>,
|
|
37
|
+
> = TInfo & {
|
|
38
|
+
_tag: TTag;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Wraps the given value in a successful result.
|
|
43
|
+
* @param value The value to wrap.
|
|
44
|
+
* @returns A successful result containing the given value.
|
|
45
|
+
*/
|
|
46
|
+
export function ok<T>(value: T): Ok<T> {
|
|
47
|
+
return { type: SuccessOrFailure.SUCCESS, value };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Wraps the given error in a failed result.
|
|
52
|
+
* @param error The error to wrap.
|
|
53
|
+
* @returns A failed result containing the given error.
|
|
54
|
+
*/
|
|
55
|
+
export function err<E extends ErrorType<ErrorTag>>(error: E): Err<E> {
|
|
56
|
+
return { type: SuccessOrFailure.FAILURE, error };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Unwraps a result to retrieve its value.
|
|
61
|
+
* If the result is a failure, an error will be thrown.
|
|
62
|
+
*
|
|
63
|
+
* @param result The result to unwrap.
|
|
64
|
+
* @returns The value contained in the successful result.
|
|
65
|
+
* @throws An error if the result is a failure.
|
|
66
|
+
*/
|
|
67
|
+
export function unwrap<T, E extends ErrorType<ErrorTag>>(result: Result<T, E>) {
|
|
68
|
+
if (result.type === SuccessOrFailure.SUCCESS) {
|
|
69
|
+
return result.value satisfies T;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
throw new Error(
|
|
73
|
+
"Can't unwrap the value from this result because it's a failure",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Unwraps a result to retrieve its error.
|
|
79
|
+
* If the result is a success, an error will be thrown.
|
|
80
|
+
*
|
|
81
|
+
* @param result The result to unwrap.
|
|
82
|
+
* @returns The error contained in the failed result.
|
|
83
|
+
* @throws An error if the result is a success.
|
|
84
|
+
*/
|
|
85
|
+
export function unwrapErr<T, E extends ErrorType<ErrorTag>>(
|
|
86
|
+
result: Result<T, E>,
|
|
87
|
+
) {
|
|
88
|
+
if (result.type === SuccessOrFailure.FAILURE) {
|
|
89
|
+
return result.error satisfies E;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
throw new Error(
|
|
93
|
+
"Can't unwrap the error from this result because it's a success",
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Checks if a result is a success.
|
|
99
|
+
* @param result The result to check.
|
|
100
|
+
* @returns True if the result is a success, false otherwise.
|
|
101
|
+
*/
|
|
102
|
+
export function isOk<T, E extends ErrorType<ErrorTag>>(
|
|
103
|
+
result: Result<T, E>,
|
|
104
|
+
): result is Ok<T> {
|
|
105
|
+
return result.type === SuccessOrFailure.SUCCESS;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Checks if a result is a failure.
|
|
110
|
+
* @param result The result to check.
|
|
111
|
+
* @returns True if the result is a failure, false otherwise.
|
|
112
|
+
*/
|
|
113
|
+
export function isErr<T, E extends ErrorType<ErrorTag>>(
|
|
114
|
+
result: Result<T, E>,
|
|
115
|
+
): result is Err<E> {
|
|
116
|
+
return result.type === SuccessOrFailure.FAILURE;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Maps a successful result to another value.
|
|
121
|
+
* If the result is a failure, it is returned unchanged.
|
|
122
|
+
*
|
|
123
|
+
* @param result The result to map.
|
|
124
|
+
* @param fn A function to transform the success value.
|
|
125
|
+
* @returns A new {@link Result} with the transformed value or the original error.
|
|
126
|
+
*/
|
|
127
|
+
export function map<T, U, E extends ErrorType<ErrorTag>>(
|
|
128
|
+
result: Result<T, E>,
|
|
129
|
+
fn: (value: T) => U,
|
|
130
|
+
): Result<U, E> {
|
|
131
|
+
if (isOk(result)) {
|
|
132
|
+
return ok(fn(result.value));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Maps a failed result's error to another error value.
|
|
140
|
+
* If the result is a success, it is returned unchanged.
|
|
141
|
+
*
|
|
142
|
+
* @param result The result to map.
|
|
143
|
+
* @param fn A function to transform the error value.
|
|
144
|
+
* @returns A new Result with the original value or transformed error.
|
|
145
|
+
*/
|
|
146
|
+
export function mapErr<
|
|
147
|
+
T,
|
|
148
|
+
E extends ErrorType<ErrorTag>,
|
|
149
|
+
F extends ErrorType<ErrorTag>,
|
|
150
|
+
>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {
|
|
151
|
+
if (isErr(result)) {
|
|
152
|
+
return err(fn(result.error));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return result;
|
|
156
|
+
}
|