@adobe/aio-commerce-lib-core 0.3.0 → 0.4.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/CHANGELOG.md +6 -0
- package/README.md +1 -128
- package/dist/cjs/error/index.cjs +98 -0
- package/dist/cjs/error/index.d.cts +133 -0
- package/dist/cjs/index.cjs +13 -8
- package/dist/cjs/index.d.cts +1 -2
- package/dist/es/error/index.d.ts +133 -0
- package/dist/es/error/index.js +90 -0
- package/dist/es/index.d.ts +1 -2
- package/dist/es/index.js +13 -3
- package/package.json +21 -25
- package/dist/cjs/error-DkqwTy7o.cjs +0 -225
- package/dist/cjs/index-Bde1DS_G.d.cts +0 -45
- package/dist/cjs/lib/error/index.cjs +0 -4
- package/dist/cjs/lib/error/index.d.cts +0 -2
- package/dist/es/chunk-Cl8Af3a2.js +0 -11
- package/dist/es/error-CPCTGRuV.js +0 -180
- package/dist/es/index-C9-P5X0V.d.ts +0 -43
- package/dist/es/lib/error/index.d.ts +0 -2
- package/dist/es/lib/error/index.js +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @adobe/aio-commerce-lib-core
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#46](https://github.com/adobe/aio-commerce-sdk/pull/46) [`4b75585`](https://github.com/adobe/aio-commerce-sdk/commit/4b75585c0d27bd472de3277be5ddaf6a977664de) Thanks [@iivvaannxx](https://github.com/iivvaannxx)! - Removed namespaced exports for index module
|
|
8
|
+
|
|
3
9
|
## 0.3.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -11,136 +11,9 @@ This package provides core utilities used across the Adobe Commerce SDK.
|
|
|
11
11
|
pnpm install @adobe/aio-commerce-lib-core
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
## Overview
|
|
15
|
-
|
|
16
|
-
This package provides core utilities for the Adobe Commerce SDK libraries.
|
|
17
|
-
|
|
18
|
-
- **Error Handling**: Custom error classes with enhanced debugging capabilities
|
|
19
|
-
- **Validation Errors**: Pretty-printable validation errors.
|
|
20
|
-
|
|
21
14
|
## Usage
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
The library provides custom error classes for consistent error handling across the SDK.
|
|
26
|
-
|
|
27
|
-
#### CommerceSdkErrorBase
|
|
28
|
-
|
|
29
|
-
Base class for all SDK errors with enhanced debugging capabilities:
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
32
|
-
import { CommerceSdkErrorBase } from "@adobe/aio-commerce-lib-core/error";
|
|
33
|
-
|
|
34
|
-
// Create a custom error class
|
|
35
|
-
class MyCustomError extends CommerceSdkErrorBase {
|
|
36
|
-
constructor(message: string, options?: { traceId?: string }) {
|
|
37
|
-
super(message, options || {});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Use the error
|
|
42
|
-
const error = new MyCustomError("Something went wrong", {
|
|
43
|
-
traceId: "trace-123",
|
|
44
|
-
cause: originalError,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Check if error is an SDK error
|
|
48
|
-
if (CommerceSdkErrorBase.isSdkError(error)) {
|
|
49
|
-
console.log(error.fullStack); // Full stack trace including causes
|
|
50
|
-
console.log(error.rootCause); // Get the root cause
|
|
51
|
-
console.log(error.toJSON()); // JSON representation
|
|
52
|
-
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
#### CommerceSdkValidationError
|
|
56
|
-
|
|
57
|
-
Specialized error for validation failures with pretty-printing support:
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
import { CommerceSdkValidationError } from "@adobe/aio-commerce-lib-core/error";
|
|
61
|
-
import { safeParse } from "valibot";
|
|
62
|
-
|
|
63
|
-
// When validation fails (`schema` and `data` are in scope somewhere)
|
|
64
|
-
const result = safeParse(schema, data);
|
|
65
|
-
if (!result.success) {
|
|
66
|
-
const error = new CommerceSdkValidationError("Invalid configuration", {
|
|
67
|
-
issues: result.issues,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Pretty-print the validation errors
|
|
71
|
-
console.error(error.display());
|
|
72
|
-
|
|
73
|
-
// Output:
|
|
74
|
-
// Invalid configuration
|
|
75
|
-
// ├── Schema validation error at clientId → Expected a non-empty string value
|
|
76
|
-
// ├── Schema validation error at email → Expected a valid email format
|
|
77
|
-
// └── Input error at age → Expected a number greater than 18
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Working with Validation Errors
|
|
82
|
-
|
|
83
|
-
The validation error display shows a tree structure of all validation issues:
|
|
84
|
-
|
|
85
|
-
```typescript
|
|
86
|
-
try {
|
|
87
|
-
// Some validation logic that throws CommerceSdkValidationError
|
|
88
|
-
validateConfig(params);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
if (error instanceof CommerceSdkValidationError) {
|
|
91
|
-
console.error(error.display()); // with colors (default)
|
|
92
|
-
console.error(error.display(false)); // without colors (for logs)
|
|
93
|
-
|
|
94
|
-
// Access raw valibot issues
|
|
95
|
-
error.issues.forEach((issue) => {
|
|
96
|
-
console.log(`Issue at ${issue.path}: ${issue.message}`);
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
## Best Practices
|
|
103
|
-
|
|
104
|
-
1. **Extend base error class** for custom errors:
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
class ApiError extends CommerceSdkErrorBase {
|
|
108
|
-
constructor(
|
|
109
|
-
message: string,
|
|
110
|
-
public statusCode: number,
|
|
111
|
-
options?: CommerceSdkErrorBaseOptions,
|
|
112
|
-
) {
|
|
113
|
-
super(message, options);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
2. **Use validation errors** for anything related to validation:
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
import { safeParse } from "valibot";
|
|
122
|
-
|
|
123
|
-
function validateParams(params: unknown) {
|
|
124
|
-
const result = safeParse(schema, params);
|
|
125
|
-
if (!result.success) {
|
|
126
|
-
throw new CommerceSdkValidationError("Invalid parameters", {
|
|
127
|
-
issues: result.issues,
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
return result.output;
|
|
131
|
-
}
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
3. **Chain errors properly** using the `cause` option:
|
|
135
|
-
```typescript
|
|
136
|
-
try {
|
|
137
|
-
await riskyOperation();
|
|
138
|
-
} catch (error) {
|
|
139
|
-
throw new MyCustomError("High-level operation failed", {
|
|
140
|
-
cause: error,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
```
|
|
16
|
+
See the [Usage Guide](./docs/usage.md) for more information.
|
|
144
17
|
|
|
145
18
|
## Contributing
|
|
146
19
|
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
var __create = Object.create, __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __getOwnPropNames = Object.getOwnPropertyNames, __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty, __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from == "object" || typeof from == "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) key = keys[i], !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
return to;
|
|
20
|
+
}, __toESM = (mod, isNodeMode, target) => (target = mod == null ? {} : __create(__getProtoOf(mod)), __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: !0
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
const util = __toESM(require("util")), ansis = __toESM(require("ansis")), valibot = __toESM(require("valibot"));
|
|
25
|
+
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
|
|
26
|
+
traceId;
|
|
27
|
+
constructor(message, options) {
|
|
28
|
+
let { traceId,...baseOptions } = options ?? {};
|
|
29
|
+
super(message, baseOptions), Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace && Error.captureStackTrace(this, new.target), this.name = new.target.name || "CommerceSdkError", this.traceId = traceId;
|
|
30
|
+
}
|
|
31
|
+
static isSdkError(error) {
|
|
32
|
+
return error instanceof CommerceSdkErrorBase;
|
|
33
|
+
}
|
|
34
|
+
get fullStack() {
|
|
35
|
+
let out = this.stack ?? "", cause = this.cause;
|
|
36
|
+
for (; cause instanceof Error;) out += `\nCaused by: ${cause.stack ?? cause.message}`, cause = cause.cause;
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
get rootCause() {
|
|
40
|
+
let cause = this.cause;
|
|
41
|
+
for (; cause && typeof cause == "object" && cause && "cause" in cause;) cause = cause.cause;
|
|
42
|
+
return cause;
|
|
43
|
+
}
|
|
44
|
+
toJSON() {
|
|
45
|
+
return {
|
|
46
|
+
name: this.name,
|
|
47
|
+
message: this.message,
|
|
48
|
+
stack: this.fullStack,
|
|
49
|
+
cause: this.cause,
|
|
50
|
+
traceId: this.traceId
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
toString(inspect = !0) {
|
|
54
|
+
return inspect ? util.default.inspect(this, {
|
|
55
|
+
depth: null,
|
|
56
|
+
colors: !0,
|
|
57
|
+
sorted: !0,
|
|
58
|
+
maxStringLength: Infinity
|
|
59
|
+
}) : super.toString();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const LAST_RETURN_CHAR = "└── ", RETURN_CHAR = "├── ", ISSUE_KIND_TO_ERROR_TITLE = {
|
|
63
|
+
schema: "Schema validation error",
|
|
64
|
+
transformation: "Transformation error",
|
|
65
|
+
validation: "Input error"
|
|
66
|
+
};
|
|
67
|
+
function getReturnChar(isLastItem, withColor) {
|
|
68
|
+
let char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
|
|
69
|
+
return withColor ? (0, ansis.cyanBright)(char) : char;
|
|
70
|
+
}
|
|
71
|
+
function getKindText(kind, withColor) {
|
|
72
|
+
let text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
|
|
73
|
+
return withColor ? (0, ansis.yellowBright)(text) : text;
|
|
74
|
+
}
|
|
75
|
+
function getPathText(dotPath, withColor) {
|
|
76
|
+
return dotPath ? withColor ? `${(0, ansis.cyanBright)(dotPath)}${(0, ansis.whiteBright)((0, ansis.dim)(" →"))}` : `${dotPath} →` : "";
|
|
77
|
+
}
|
|
78
|
+
function issueToDisplay(issues, withColor = !0) {
|
|
79
|
+
let lines = issues?.map((issue, index) => {
|
|
80
|
+
let returnChar = getReturnChar(index === issues.length - 1, withColor), kindText = getKindText(issue.kind, withColor), path = getPathText((0, valibot.getDotPath)(issue), withColor), message = withColor ? (0, ansis.whiteBright)(issue.message) : issue.message, at = withColor ? (0, ansis.whiteBright)("at") : "at", issueLine = `${kindText} ${at} ${path} ${message}`;
|
|
81
|
+
return `${returnChar} ${issueLine}`;
|
|
82
|
+
});
|
|
83
|
+
return lines?.join("\n") ?? "";
|
|
84
|
+
}
|
|
85
|
+
function displayValidationError(error, withColor = !0) {
|
|
86
|
+
let display = issueToDisplay(error.issues, withColor), message = withColor ? (0, ansis.whiteBright)(error.message) : error.message;
|
|
87
|
+
return `${message}\n${display}`;
|
|
88
|
+
}
|
|
89
|
+
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
|
|
90
|
+
issues;
|
|
91
|
+
constructor(message, { issues,...options }) {
|
|
92
|
+
super(message, options), this.issues = issues;
|
|
93
|
+
}
|
|
94
|
+
display(withColor = !0) {
|
|
95
|
+
return displayValidationError(this, withColor);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.CommerceSdkErrorBase = CommerceSdkErrorBase, exports.CommerceSdkValidationError = CommerceSdkValidationError;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { GenericIssue } from "valibot";
|
|
16
|
+
|
|
17
|
+
//#region source/error/base-error.d.ts
|
|
18
|
+
/** Defines the base options for {@link CommerceSdkErrorBase}. */
|
|
19
|
+
type CommerceSdkErrorBaseOptions = ErrorOptions & {
|
|
20
|
+
traceId?: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Helper type to define custom error options.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* type ValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
27
|
+
* field: string;
|
|
28
|
+
* value: unknown;
|
|
29
|
+
* }>;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
type CommerceSdkErrorOptions<T extends Record<string, unknown> = Record<string, unknown>> = CommerceSdkErrorBaseOptions & T;
|
|
33
|
+
/**
|
|
34
|
+
* Base class for all the errors in the AIO Commerce SDK.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* class ValidationError extends CommerceSdkErrorBase {
|
|
38
|
+
* constructor(message: string, options: ValidationErrorOptions) {
|
|
39
|
+
* super(message, options);
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const err = new ValidationError("Invalid value", {
|
|
44
|
+
* tag: "ValidationError",
|
|
45
|
+
* field: "name",
|
|
46
|
+
* value: "John Doe",
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log(err.toJSON());
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare abstract class CommerceSdkErrorBase extends Error {
|
|
53
|
+
readonly traceId?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Constructs a new CommerceSdkErrorBase instance. This is an abstract class so you
|
|
56
|
+
* should not instantiate it directly. Only invoke this constructor from a subclass.
|
|
57
|
+
*
|
|
58
|
+
* @param message - A human-friendly description of the error.
|
|
59
|
+
* @param options - Optional error options (additional information).
|
|
60
|
+
*/
|
|
61
|
+
constructor(message: string, options?: CommerceSdkErrorBaseOptions);
|
|
62
|
+
/**
|
|
63
|
+
* Checks if the error is any CommerceSdkErrorBase instance.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* class ValidationError extends CommerceSdkErrorBase {}
|
|
67
|
+
* const err = new ValidationError("Invalid", {});
|
|
68
|
+
*
|
|
69
|
+
* CommerceSdkErrorBase.isSdkError(err); // true
|
|
70
|
+
* ValidationError.isSdkError(err); // true
|
|
71
|
+
* CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
static isSdkError(error: unknown): error is CommerceSdkErrorBase;
|
|
75
|
+
/** Returns the full stack trace of the error and its causes. */
|
|
76
|
+
get fullStack(): string;
|
|
77
|
+
/** Returns the root cause of the error. */
|
|
78
|
+
get rootCause(): unknown;
|
|
79
|
+
/** Converts the error to a JSON-like representation. */
|
|
80
|
+
toJSON(): {
|
|
81
|
+
name: string;
|
|
82
|
+
message: string;
|
|
83
|
+
stack: string;
|
|
84
|
+
cause: unknown;
|
|
85
|
+
traceId: string | undefined;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Returns a pretty string representation of the error.
|
|
89
|
+
* @param inspect - Whether to inspect the error (returns a more detailed string, useful for debugging).
|
|
90
|
+
*/
|
|
91
|
+
toString(inspect?: boolean): string;
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region source/error/validation-error.d.ts
|
|
95
|
+
/** Defines the base options for {@link CommerceSdkValidationError}. */
|
|
96
|
+
type CommerceSdkValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
97
|
+
issues: GenericIssue[];
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Represents a validation error in the Commerce SDK.
|
|
101
|
+
* This error should be thrown when an input does not conform to the expected schema.
|
|
102
|
+
* It contains a list of issues that describe the validation errors.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const error = new CommerceSdkValidationError("Invalid input", {
|
|
107
|
+
* // `someIssues` is in scope, returned by some `valibot` operation.
|
|
108
|
+
* issues: someIssues
|
|
109
|
+
* });
|
|
110
|
+
*
|
|
111
|
+
* console.log(error.display());
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare class CommerceSdkValidationError extends CommerceSdkErrorBase {
|
|
115
|
+
readonly issues: GenericIssue[];
|
|
116
|
+
/**
|
|
117
|
+
* Constructs a new {@link CommerceSdkValidationError} instance.
|
|
118
|
+
*
|
|
119
|
+
* @param message - A human-friendly description of the validation error.
|
|
120
|
+
* @param options - Options for the validation error, including the issues that caused the error.
|
|
121
|
+
*/
|
|
122
|
+
constructor(message: string, {
|
|
123
|
+
issues,
|
|
124
|
+
...options
|
|
125
|
+
}: CommerceSdkValidationErrorOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Returns a pretty string (and colored) representation of the validation error.
|
|
128
|
+
* @param withColor Whether to use color in the output.
|
|
129
|
+
*/
|
|
130
|
+
display(withColor?: boolean): string;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { index_d_exports as Error };
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { GenericIssue } from "valibot";
|
|
16
|
+
|
|
17
|
+
//#region source/error/base-error.d.ts
|
|
18
|
+
/** Defines the base options for {@link CommerceSdkErrorBase}. */
|
|
19
|
+
type CommerceSdkErrorBaseOptions = ErrorOptions & {
|
|
20
|
+
traceId?: string;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Helper type to define custom error options.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* type ValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
27
|
+
* field: string;
|
|
28
|
+
* value: unknown;
|
|
29
|
+
* }>;
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
type CommerceSdkErrorOptions<T extends Record<string, unknown> = Record<string, unknown>> = CommerceSdkErrorBaseOptions & T;
|
|
33
|
+
/**
|
|
34
|
+
* Base class for all the errors in the AIO Commerce SDK.
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* class ValidationError extends CommerceSdkErrorBase {
|
|
38
|
+
* constructor(message: string, options: ValidationErrorOptions) {
|
|
39
|
+
* super(message, options);
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const err = new ValidationError("Invalid value", {
|
|
44
|
+
* tag: "ValidationError",
|
|
45
|
+
* field: "name",
|
|
46
|
+
* value: "John Doe",
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log(err.toJSON());
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare abstract class CommerceSdkErrorBase extends Error {
|
|
53
|
+
readonly traceId?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Constructs a new CommerceSdkErrorBase instance. This is an abstract class so you
|
|
56
|
+
* should not instantiate it directly. Only invoke this constructor from a subclass.
|
|
57
|
+
*
|
|
58
|
+
* @param message - A human-friendly description of the error.
|
|
59
|
+
* @param options - Optional error options (additional information).
|
|
60
|
+
*/
|
|
61
|
+
constructor(message: string, options?: CommerceSdkErrorBaseOptions);
|
|
62
|
+
/**
|
|
63
|
+
* Checks if the error is any CommerceSdkErrorBase instance.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* class ValidationError extends CommerceSdkErrorBase {}
|
|
67
|
+
* const err = new ValidationError("Invalid", {});
|
|
68
|
+
*
|
|
69
|
+
* CommerceSdkErrorBase.isSdkError(err); // true
|
|
70
|
+
* ValidationError.isSdkError(err); // true
|
|
71
|
+
* CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
static isSdkError(error: unknown): error is CommerceSdkErrorBase;
|
|
75
|
+
/** Returns the full stack trace of the error and its causes. */
|
|
76
|
+
get fullStack(): string;
|
|
77
|
+
/** Returns the root cause of the error. */
|
|
78
|
+
get rootCause(): unknown;
|
|
79
|
+
/** Converts the error to a JSON-like representation. */
|
|
80
|
+
toJSON(): {
|
|
81
|
+
name: string;
|
|
82
|
+
message: string;
|
|
83
|
+
stack: string;
|
|
84
|
+
cause: unknown;
|
|
85
|
+
traceId: string | undefined;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Returns a pretty string representation of the error.
|
|
89
|
+
* @param inspect - Whether to inspect the error (returns a more detailed string, useful for debugging).
|
|
90
|
+
*/
|
|
91
|
+
toString(inspect?: boolean): string;
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region source/error/validation-error.d.ts
|
|
95
|
+
/** Defines the base options for {@link CommerceSdkValidationError}. */
|
|
96
|
+
type CommerceSdkValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
97
|
+
issues: GenericIssue[];
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Represents a validation error in the Commerce SDK.
|
|
101
|
+
* This error should be thrown when an input does not conform to the expected schema.
|
|
102
|
+
* It contains a list of issues that describe the validation errors.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const error = new CommerceSdkValidationError("Invalid input", {
|
|
107
|
+
* // `someIssues` is in scope, returned by some `valibot` operation.
|
|
108
|
+
* issues: someIssues
|
|
109
|
+
* });
|
|
110
|
+
*
|
|
111
|
+
* console.log(error.display());
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare class CommerceSdkValidationError extends CommerceSdkErrorBase {
|
|
115
|
+
readonly issues: GenericIssue[];
|
|
116
|
+
/**
|
|
117
|
+
* Constructs a new {@link CommerceSdkValidationError} instance.
|
|
118
|
+
*
|
|
119
|
+
* @param message - A human-friendly description of the validation error.
|
|
120
|
+
* @param options - Options for the validation error, including the issues that caused the error.
|
|
121
|
+
*/
|
|
122
|
+
constructor(message: string, {
|
|
123
|
+
issues,
|
|
124
|
+
...options
|
|
125
|
+
}: CommerceSdkValidationErrorOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Returns a pretty string (and colored) representation of the validation error.
|
|
128
|
+
* @param withColor Whether to use color in the output.
|
|
129
|
+
*/
|
|
130
|
+
display(withColor?: boolean): string;
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
import util from "util";
|
|
15
|
+
import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
|
|
16
|
+
import { getDotPath } from "valibot";
|
|
17
|
+
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
|
|
18
|
+
traceId;
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
let { traceId,...baseOptions } = options ?? {};
|
|
21
|
+
super(message, baseOptions), Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace && Error.captureStackTrace(this, new.target), this.name = new.target.name || "CommerceSdkError", this.traceId = traceId;
|
|
22
|
+
}
|
|
23
|
+
static isSdkError(error) {
|
|
24
|
+
return error instanceof CommerceSdkErrorBase;
|
|
25
|
+
}
|
|
26
|
+
get fullStack() {
|
|
27
|
+
let out = this.stack ?? "", cause = this.cause;
|
|
28
|
+
for (; cause instanceof Error;) out += `\nCaused by: ${cause.stack ?? cause.message}`, cause = cause.cause;
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
get rootCause() {
|
|
32
|
+
let cause = this.cause;
|
|
33
|
+
for (; cause && typeof cause == "object" && cause && "cause" in cause;) cause = cause.cause;
|
|
34
|
+
return cause;
|
|
35
|
+
}
|
|
36
|
+
toJSON() {
|
|
37
|
+
return {
|
|
38
|
+
name: this.name,
|
|
39
|
+
message: this.message,
|
|
40
|
+
stack: this.fullStack,
|
|
41
|
+
cause: this.cause,
|
|
42
|
+
traceId: this.traceId
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
toString(inspect = !0) {
|
|
46
|
+
return inspect ? util.inspect(this, {
|
|
47
|
+
depth: null,
|
|
48
|
+
colors: !0,
|
|
49
|
+
sorted: !0,
|
|
50
|
+
maxStringLength: Infinity
|
|
51
|
+
}) : super.toString();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const LAST_RETURN_CHAR = "└── ", RETURN_CHAR = "├── ", ISSUE_KIND_TO_ERROR_TITLE = {
|
|
55
|
+
schema: "Schema validation error",
|
|
56
|
+
transformation: "Transformation error",
|
|
57
|
+
validation: "Input error"
|
|
58
|
+
};
|
|
59
|
+
function getReturnChar(isLastItem, withColor) {
|
|
60
|
+
let char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
|
|
61
|
+
return withColor ? cyanBright(char) : char;
|
|
62
|
+
}
|
|
63
|
+
function getKindText(kind, withColor) {
|
|
64
|
+
let text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
|
|
65
|
+
return withColor ? yellowBright(text) : text;
|
|
66
|
+
}
|
|
67
|
+
function getPathText(dotPath, withColor) {
|
|
68
|
+
return dotPath ? withColor ? `${cyanBright(dotPath)}${whiteBright(dim(" →"))}` : `${dotPath} →` : "";
|
|
69
|
+
}
|
|
70
|
+
function issueToDisplay(issues, withColor = !0) {
|
|
71
|
+
let lines = issues?.map((issue, index) => {
|
|
72
|
+
let returnChar = getReturnChar(index === issues.length - 1, withColor), kindText = getKindText(issue.kind, withColor), path = getPathText(getDotPath(issue), withColor), message = withColor ? whiteBright(issue.message) : issue.message, at = withColor ? whiteBright("at") : "at", issueLine = `${kindText} ${at} ${path} ${message}`;
|
|
73
|
+
return `${returnChar} ${issueLine}`;
|
|
74
|
+
});
|
|
75
|
+
return lines?.join("\n") ?? "";
|
|
76
|
+
}
|
|
77
|
+
function displayValidationError(error, withColor = !0) {
|
|
78
|
+
let display = issueToDisplay(error.issues, withColor), message = withColor ? whiteBright(error.message) : error.message;
|
|
79
|
+
return `${message}\n${display}`;
|
|
80
|
+
}
|
|
81
|
+
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
|
|
82
|
+
issues;
|
|
83
|
+
constructor(message, { issues,...options }) {
|
|
84
|
+
super(message, options), this.issues = issues;
|
|
85
|
+
}
|
|
86
|
+
display(withColor = !0) {
|
|
87
|
+
return displayValidationError(this, withColor);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
package/dist/es/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { index_d_exports as Error };
|
|
1
|
+
export { };
|
package/dist/es/index.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/aio-commerce-lib-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.0",
|
|
5
|
-
"private": false,
|
|
6
4
|
"author": "Adobe Inc.",
|
|
5
|
+
"version": "0.4.0",
|
|
6
|
+
"private": false,
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": ">=
|
|
8
|
+
"node": ">=20 <=24"
|
|
9
9
|
},
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"description": "Core utilities for AIO Commerce SDK Libraries",
|
|
@@ -24,27 +24,17 @@
|
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
|
-
"url": "https://github.com/adobe/aio-commerce-sdk.git",
|
|
27
|
+
"url": "git+https://github.com/adobe/aio-commerce-sdk.git",
|
|
28
28
|
"directory": "packages/aio-commerce-lib-core"
|
|
29
29
|
},
|
|
30
30
|
"main": "./dist/cjs/index.cjs",
|
|
31
31
|
"module": "./dist/es/index.js",
|
|
32
32
|
"types": "./dist/cjs/index.d.cts",
|
|
33
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
34
|
"./*": {
|
|
45
35
|
"import": {
|
|
46
|
-
"types": "./dist/es
|
|
47
|
-
"default": "./dist/es
|
|
36
|
+
"types": "./dist/es/*/index.d.ts",
|
|
37
|
+
"default": "./dist/es/*/index.js"
|
|
48
38
|
},
|
|
49
39
|
"require": {
|
|
50
40
|
"types": "./dist/cjs/lib/*/index.d.cts",
|
|
@@ -64,20 +54,26 @@
|
|
|
64
54
|
"valibot": "^1.1.0"
|
|
65
55
|
},
|
|
66
56
|
"devDependencies": {
|
|
67
|
-
"vitest": "^2.0.0",
|
|
68
57
|
"@aio-commerce-sdk/config-tsdown": "1.0.0",
|
|
69
|
-
"@aio-commerce-sdk/config-
|
|
58
|
+
"@aio-commerce-sdk/config-typedoc": "1.0.0",
|
|
59
|
+
"@aio-commerce-sdk/config-typescript": "1.0.0",
|
|
60
|
+
"@aio-commerce-sdk/config-vitest": "1.0.0"
|
|
70
61
|
},
|
|
71
62
|
"sideEffects": false,
|
|
72
63
|
"scripts": {
|
|
73
64
|
"build": "tsdown",
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
65
|
+
"docs": "typedoc && prettier --write '**/*.md'",
|
|
66
|
+
"assist": "biome check --formatter-enabled=false --linter-enabled=false --assist-enabled=true --no-errors-on-unmatched",
|
|
67
|
+
"assist:apply": "biome check --write --formatter-enabled=false --linter-enabled=false --assist-enabled=true --no-errors-on-unmatched",
|
|
68
|
+
"check:ci": "biome ci --formatter-enabled=true --linter-enabled=true --assist-enabled=true --no-errors-on-unmatched",
|
|
69
|
+
"format": "biome format --write --no-errors-on-unmatched",
|
|
70
|
+
"format:markdown": "prettier --no-error-on-unmatched-pattern --write '**/*.md' \"!**/{CODE_OF_CONDUCT.md,COPYRIGHT,LICENSE,SECURITY.md,CONTRIBUTING.md}\"",
|
|
71
|
+
"format:check": "biome format --no-errors-on-unmatched",
|
|
72
|
+
"lint": "biome lint --no-errors-on-unmatched",
|
|
73
|
+
"lint:fix": "biome lint --write --no-errors-on-unmatched",
|
|
79
74
|
"typecheck": "tsc --noEmit && echo '✅ No type errors found.'",
|
|
80
|
-
"test": "vitest run",
|
|
81
|
-
"test:watch": "vitest --
|
|
75
|
+
"test": "vitest run --coverage",
|
|
76
|
+
"test:watch": "vitest --coverage",
|
|
77
|
+
"test:ui": "vitest --ui"
|
|
82
78
|
}
|
|
83
79
|
}
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
//#region rolldown:runtime
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all) __defProp(target, name, {
|
|
10
|
-
get: all[name],
|
|
11
|
-
enumerable: true
|
|
12
|
-
});
|
|
13
|
-
};
|
|
14
|
-
var __copyProps = (to, from, except, desc) => {
|
|
15
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
16
|
-
key = keys[i];
|
|
17
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
18
|
-
get: ((k) => from[k]).bind(null, key),
|
|
19
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
return to;
|
|
23
|
-
};
|
|
24
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
25
|
-
value: mod,
|
|
26
|
-
enumerable: true
|
|
27
|
-
}) : target, mod));
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
|
-
const node_util = __toESM(require("node:util"));
|
|
31
|
-
const ansis = __toESM(require("ansis"));
|
|
32
|
-
const valibot = __toESM(require("valibot"));
|
|
33
|
-
|
|
34
|
-
//#region source/lib/error/base-error.ts
|
|
35
|
-
/**
|
|
36
|
-
* Base class for all AioCommerceSdk errors.
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* class ValidationError extends CommerceSdkErrorBase {
|
|
40
|
-
* constructor(message: string, options: ValidationErrorOptions) {
|
|
41
|
-
* super(message, options);
|
|
42
|
-
* }
|
|
43
|
-
* }
|
|
44
|
-
*
|
|
45
|
-
* const err = new ValidationError("Invalid value", {
|
|
46
|
-
* tag: "ValidationError",
|
|
47
|
-
* field: "name",
|
|
48
|
-
* value: "John Doe",
|
|
49
|
-
* });
|
|
50
|
-
*
|
|
51
|
-
* console.log(err.toJSON());
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
|
|
55
|
-
traceId;
|
|
56
|
-
/**
|
|
57
|
-
* Constructs a new CommerceSdkErrorBase instance.
|
|
58
|
-
*
|
|
59
|
-
* @param message - A human-friendly description of the error.
|
|
60
|
-
* @param traceId - An optional trace ID for tracking the error in logs.
|
|
61
|
-
* @param options - Required error options.
|
|
62
|
-
*/
|
|
63
|
-
constructor(message, { traceId,...options }) {
|
|
64
|
-
super(message, options);
|
|
65
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
66
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
67
|
-
this.name = new.target.name || "CommerceSdkError";
|
|
68
|
-
this.traceId = traceId;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Checks if the error is any CommerceSdkErrorBase instance.
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* class ValidationError extends CommerceSdkErrorBase {}
|
|
75
|
-
* const err = new ValidationError("Invalid", {});
|
|
76
|
-
*
|
|
77
|
-
* CommerceSdkErrorBase.isSdkError(err); // true
|
|
78
|
-
* ValidationError.isSdkError(err); // true
|
|
79
|
-
* CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
static isSdkError(error) {
|
|
83
|
-
return error instanceof CommerceSdkErrorBase;
|
|
84
|
-
}
|
|
85
|
-
/** Returns the full stack trace of the error and its causes. */
|
|
86
|
-
get fullStack() {
|
|
87
|
-
let out = this.stack ?? "";
|
|
88
|
-
let cause = this.cause;
|
|
89
|
-
while (cause instanceof Error) {
|
|
90
|
-
out += `\nCaused by: ${cause.stack ?? cause.message}`;
|
|
91
|
-
cause = cause.cause;
|
|
92
|
-
}
|
|
93
|
-
return out;
|
|
94
|
-
}
|
|
95
|
-
/** Returns the root cause of the error. */
|
|
96
|
-
get rootCause() {
|
|
97
|
-
let cause = this.cause;
|
|
98
|
-
while (cause) if (typeof cause === "object" && cause !== null && "cause" in cause) cause = cause.cause;
|
|
99
|
-
else break;
|
|
100
|
-
return cause;
|
|
101
|
-
}
|
|
102
|
-
/** Converts the error to a JSON-like representation. */
|
|
103
|
-
toJSON() {
|
|
104
|
-
return {
|
|
105
|
-
name: this.name,
|
|
106
|
-
message: this.message,
|
|
107
|
-
stack: this.fullStack,
|
|
108
|
-
cause: this.cause,
|
|
109
|
-
traceId: this.traceId
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
/** Returns a pretty string representation of the error. */
|
|
113
|
-
toString(inspect = true) {
|
|
114
|
-
if (inspect) return node_util.default.inspect(this, {
|
|
115
|
-
depth: null,
|
|
116
|
-
colors: true,
|
|
117
|
-
sorted: true,
|
|
118
|
-
maxStringLength: Number.POSITIVE_INFINITY
|
|
119
|
-
});
|
|
120
|
-
return super.toString();
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
//#endregion
|
|
125
|
-
//#region source/lib/error/validation-error.ts
|
|
126
|
-
const LAST_RETURN_CHAR = "└── ";
|
|
127
|
-
const RETURN_CHAR = "├── ";
|
|
128
|
-
/** Maps issue kinds to their corresponding error titles for display purposes. */
|
|
129
|
-
const ISSUE_KIND_TO_ERROR_TITLE = {
|
|
130
|
-
schema: "Schema validation error",
|
|
131
|
-
transformation: "Transformation error",
|
|
132
|
-
validation: "Input error"
|
|
133
|
-
};
|
|
134
|
-
function getReturnChar(isLastItem, withColor) {
|
|
135
|
-
const char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
|
|
136
|
-
return withColor ? (0, ansis.cyanBright)(char) : char;
|
|
137
|
-
}
|
|
138
|
-
function getKindText(kind, withColor) {
|
|
139
|
-
const text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
|
|
140
|
-
return withColor ? (0, ansis.yellowBright)(text) : text;
|
|
141
|
-
}
|
|
142
|
-
function getPathText(dotPath, withColor) {
|
|
143
|
-
if (!dotPath) return "";
|
|
144
|
-
return withColor ? `${(0, ansis.cyanBright)(dotPath)}${(0, ansis.whiteBright)((0, ansis.dim)(" →"))}` : `${dotPath} →`;
|
|
145
|
-
}
|
|
146
|
-
function issueToDisplay(issues, withColor = true) {
|
|
147
|
-
const lines = issues?.map((issue, index) => {
|
|
148
|
-
const returnChar = getReturnChar(index === issues.length - 1, withColor);
|
|
149
|
-
const kindText = getKindText(issue.kind, withColor);
|
|
150
|
-
const path = getPathText((0, valibot.getDotPath)(issue), withColor);
|
|
151
|
-
const message = withColor ? (0, ansis.whiteBright)(issue.message) : issue.message;
|
|
152
|
-
const at = withColor ? (0, ansis.whiteBright)("at") : "at";
|
|
153
|
-
const issueLine = `${kindText} ${at} ${path} ${message}`;
|
|
154
|
-
return `${returnChar} ${issueLine}`;
|
|
155
|
-
});
|
|
156
|
-
return lines?.join("\n") ?? "";
|
|
157
|
-
}
|
|
158
|
-
function displayValidationError(error, withColor = true) {
|
|
159
|
-
const display = issueToDisplay(error.issues, withColor);
|
|
160
|
-
const message = withColor ? (0, ansis.whiteBright)(error.message) : error.message;
|
|
161
|
-
return `${message}\n${display}`;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Represents a validation error in the Commerce SDK.
|
|
165
|
-
* This error is thrown when the input does not conform to the expected schema.
|
|
166
|
-
* It contains a list of issues that describe the validation errors.
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* const error = new CommerceSdkValidationError("Invalid input", {
|
|
171
|
-
* // `someIssues` is in scope, returned by some `valibot` operation.
|
|
172
|
-
* issues: someIssues
|
|
173
|
-
* });
|
|
174
|
-
*
|
|
175
|
-
* console.log(error.display());
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
|
|
179
|
-
issues;
|
|
180
|
-
/**
|
|
181
|
-
* Constructs a new {@link CommerceSdkValidationError} instance.
|
|
182
|
-
*
|
|
183
|
-
* @param message - A human-friendly description of the validation error.
|
|
184
|
-
* @param options - Options for the validation error, including the issues that caused the error.
|
|
185
|
-
*/
|
|
186
|
-
constructor(message, { issues,...options }) {
|
|
187
|
-
super(message, options);
|
|
188
|
-
this.issues = issues;
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Returns a pretty string (and colored) representation of the validation error.
|
|
192
|
-
* @param withColor Whether to use color in the output.
|
|
193
|
-
*/
|
|
194
|
-
display(withColor = true) {
|
|
195
|
-
return displayValidationError(this, withColor);
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
//#endregion
|
|
200
|
-
//#region source/lib/error/index.ts
|
|
201
|
-
var error_exports = {};
|
|
202
|
-
__export(error_exports, {
|
|
203
|
-
CommerceSdkErrorBase: () => CommerceSdkErrorBase,
|
|
204
|
-
CommerceSdkValidationError: () => CommerceSdkValidationError
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
//#endregion
|
|
208
|
-
Object.defineProperty(exports, 'CommerceSdkErrorBase', {
|
|
209
|
-
enumerable: true,
|
|
210
|
-
get: function () {
|
|
211
|
-
return CommerceSdkErrorBase;
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
Object.defineProperty(exports, 'CommerceSdkValidationError', {
|
|
215
|
-
enumerable: true,
|
|
216
|
-
get: function () {
|
|
217
|
-
return CommerceSdkValidationError;
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
Object.defineProperty(exports, 'error_exports', {
|
|
221
|
-
enumerable: true,
|
|
222
|
-
get: function () {
|
|
223
|
-
return error_exports;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { GenericIssue } from "valibot";
|
|
2
|
-
|
|
3
|
-
//#region rolldown:runtime
|
|
4
|
-
//#endregion
|
|
5
|
-
//#region source/lib/error/base-error.d.ts
|
|
6
|
-
type CommerceSdkErrorBaseOptions = ErrorOptions & {
|
|
7
|
-
traceId?: string;
|
|
8
|
-
};
|
|
9
|
-
type CommerceSdkErrorOptions<T extends Record<string, unknown> = Record<string, unknown>> = CommerceSdkErrorBaseOptions & T;
|
|
10
|
-
declare abstract class CommerceSdkErrorBase extends Error {
|
|
11
|
-
readonly traceId?: string;
|
|
12
|
-
constructor(message: string, {
|
|
13
|
-
traceId,
|
|
14
|
-
...options
|
|
15
|
-
}: CommerceSdkErrorBaseOptions);
|
|
16
|
-
static isSdkError(error: unknown): error is CommerceSdkErrorBase;
|
|
17
|
-
get fullStack(): string;
|
|
18
|
-
get rootCause(): unknown;
|
|
19
|
-
toJSON(): {
|
|
20
|
-
name: string;
|
|
21
|
-
message: string;
|
|
22
|
-
stack: string;
|
|
23
|
-
cause: unknown;
|
|
24
|
-
traceId: string | undefined;
|
|
25
|
-
};
|
|
26
|
-
toString(inspect?: boolean): string;
|
|
27
|
-
}
|
|
28
|
-
//#endregion
|
|
29
|
-
//#region source/lib/error/validation-error.d.ts
|
|
30
|
-
type CommerceSdkValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
31
|
-
issues: GenericIssue[];
|
|
32
|
-
}>;
|
|
33
|
-
declare class CommerceSdkValidationError extends CommerceSdkErrorBase {
|
|
34
|
-
readonly issues: GenericIssue[];
|
|
35
|
-
constructor(message: string, {
|
|
36
|
-
issues,
|
|
37
|
-
...options
|
|
38
|
-
}: CommerceSdkValidationErrorOptions);
|
|
39
|
-
display(withColor?: boolean): string;
|
|
40
|
-
}
|
|
41
|
-
declare namespace index_d_exports {
|
|
42
|
-
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
|
43
|
-
}
|
|
44
|
-
//#endregion
|
|
45
|
-
export { CommerceSdkErrorBase, CommerceSdkValidationError, index_d_exports };
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { __export } from "./chunk-Cl8Af3a2.js";
|
|
2
|
-
import util from "node:util";
|
|
3
|
-
import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
|
|
4
|
-
import { getDotPath } from "valibot";
|
|
5
|
-
|
|
6
|
-
//#region source/lib/error/base-error.ts
|
|
7
|
-
/**
|
|
8
|
-
* Base class for all AioCommerceSdk errors.
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* class ValidationError extends CommerceSdkErrorBase {
|
|
12
|
-
* constructor(message: string, options: ValidationErrorOptions) {
|
|
13
|
-
* super(message, options);
|
|
14
|
-
* }
|
|
15
|
-
* }
|
|
16
|
-
*
|
|
17
|
-
* const err = new ValidationError("Invalid value", {
|
|
18
|
-
* tag: "ValidationError",
|
|
19
|
-
* field: "name",
|
|
20
|
-
* value: "John Doe",
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* console.log(err.toJSON());
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
|
|
27
|
-
traceId;
|
|
28
|
-
/**
|
|
29
|
-
* Constructs a new CommerceSdkErrorBase instance.
|
|
30
|
-
*
|
|
31
|
-
* @param message - A human-friendly description of the error.
|
|
32
|
-
* @param traceId - An optional trace ID for tracking the error in logs.
|
|
33
|
-
* @param options - Required error options.
|
|
34
|
-
*/
|
|
35
|
-
constructor(message, { traceId,...options }) {
|
|
36
|
-
super(message, options);
|
|
37
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
38
|
-
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
39
|
-
this.name = new.target.name || "CommerceSdkError";
|
|
40
|
-
this.traceId = traceId;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Checks if the error is any CommerceSdkErrorBase instance.
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* class ValidationError extends CommerceSdkErrorBase {}
|
|
47
|
-
* const err = new ValidationError("Invalid", {});
|
|
48
|
-
*
|
|
49
|
-
* CommerceSdkErrorBase.isSdkError(err); // true
|
|
50
|
-
* ValidationError.isSdkError(err); // true
|
|
51
|
-
* CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
static isSdkError(error) {
|
|
55
|
-
return error instanceof CommerceSdkErrorBase;
|
|
56
|
-
}
|
|
57
|
-
/** Returns the full stack trace of the error and its causes. */
|
|
58
|
-
get fullStack() {
|
|
59
|
-
let out = this.stack ?? "";
|
|
60
|
-
let cause = this.cause;
|
|
61
|
-
while (cause instanceof Error) {
|
|
62
|
-
out += `\nCaused by: ${cause.stack ?? cause.message}`;
|
|
63
|
-
cause = cause.cause;
|
|
64
|
-
}
|
|
65
|
-
return out;
|
|
66
|
-
}
|
|
67
|
-
/** Returns the root cause of the error. */
|
|
68
|
-
get rootCause() {
|
|
69
|
-
let cause = this.cause;
|
|
70
|
-
while (cause) if (typeof cause === "object" && cause !== null && "cause" in cause) cause = cause.cause;
|
|
71
|
-
else break;
|
|
72
|
-
return cause;
|
|
73
|
-
}
|
|
74
|
-
/** Converts the error to a JSON-like representation. */
|
|
75
|
-
toJSON() {
|
|
76
|
-
return {
|
|
77
|
-
name: this.name,
|
|
78
|
-
message: this.message,
|
|
79
|
-
stack: this.fullStack,
|
|
80
|
-
cause: this.cause,
|
|
81
|
-
traceId: this.traceId
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
/** Returns a pretty string representation of the error. */
|
|
85
|
-
toString(inspect = true) {
|
|
86
|
-
if (inspect) return util.inspect(this, {
|
|
87
|
-
depth: null,
|
|
88
|
-
colors: true,
|
|
89
|
-
sorted: true,
|
|
90
|
-
maxStringLength: Number.POSITIVE_INFINITY
|
|
91
|
-
});
|
|
92
|
-
return super.toString();
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
//#endregion
|
|
97
|
-
//#region source/lib/error/validation-error.ts
|
|
98
|
-
const LAST_RETURN_CHAR = "└── ";
|
|
99
|
-
const RETURN_CHAR = "├── ";
|
|
100
|
-
/** Maps issue kinds to their corresponding error titles for display purposes. */
|
|
101
|
-
const ISSUE_KIND_TO_ERROR_TITLE = {
|
|
102
|
-
schema: "Schema validation error",
|
|
103
|
-
transformation: "Transformation error",
|
|
104
|
-
validation: "Input error"
|
|
105
|
-
};
|
|
106
|
-
function getReturnChar(isLastItem, withColor) {
|
|
107
|
-
const char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
|
|
108
|
-
return withColor ? cyanBright(char) : char;
|
|
109
|
-
}
|
|
110
|
-
function getKindText(kind, withColor) {
|
|
111
|
-
const text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
|
|
112
|
-
return withColor ? yellowBright(text) : text;
|
|
113
|
-
}
|
|
114
|
-
function getPathText(dotPath, withColor) {
|
|
115
|
-
if (!dotPath) return "";
|
|
116
|
-
return withColor ? `${cyanBright(dotPath)}${whiteBright(dim(" →"))}` : `${dotPath} →`;
|
|
117
|
-
}
|
|
118
|
-
function issueToDisplay(issues, withColor = true) {
|
|
119
|
-
const lines = issues?.map((issue, index) => {
|
|
120
|
-
const returnChar = getReturnChar(index === issues.length - 1, withColor);
|
|
121
|
-
const kindText = getKindText(issue.kind, withColor);
|
|
122
|
-
const path = getPathText(getDotPath(issue), withColor);
|
|
123
|
-
const message = withColor ? whiteBright(issue.message) : issue.message;
|
|
124
|
-
const at = withColor ? whiteBright("at") : "at";
|
|
125
|
-
const issueLine = `${kindText} ${at} ${path} ${message}`;
|
|
126
|
-
return `${returnChar} ${issueLine}`;
|
|
127
|
-
});
|
|
128
|
-
return lines?.join("\n") ?? "";
|
|
129
|
-
}
|
|
130
|
-
function displayValidationError(error, withColor = true) {
|
|
131
|
-
const display = issueToDisplay(error.issues, withColor);
|
|
132
|
-
const message = withColor ? whiteBright(error.message) : error.message;
|
|
133
|
-
return `${message}\n${display}`;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Represents a validation error in the Commerce SDK.
|
|
137
|
-
* This error is thrown when the input does not conform to the expected schema.
|
|
138
|
-
* It contains a list of issues that describe the validation errors.
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```ts
|
|
142
|
-
* const error = new CommerceSdkValidationError("Invalid input", {
|
|
143
|
-
* // `someIssues` is in scope, returned by some `valibot` operation.
|
|
144
|
-
* issues: someIssues
|
|
145
|
-
* });
|
|
146
|
-
*
|
|
147
|
-
* console.log(error.display());
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
|
-
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
|
|
151
|
-
issues;
|
|
152
|
-
/**
|
|
153
|
-
* Constructs a new {@link CommerceSdkValidationError} instance.
|
|
154
|
-
*
|
|
155
|
-
* @param message - A human-friendly description of the validation error.
|
|
156
|
-
* @param options - Options for the validation error, including the issues that caused the error.
|
|
157
|
-
*/
|
|
158
|
-
constructor(message, { issues,...options }) {
|
|
159
|
-
super(message, options);
|
|
160
|
-
this.issues = issues;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Returns a pretty string (and colored) representation of the validation error.
|
|
164
|
-
* @param withColor Whether to use color in the output.
|
|
165
|
-
*/
|
|
166
|
-
display(withColor = true) {
|
|
167
|
-
return displayValidationError(this, withColor);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
//#endregion
|
|
172
|
-
//#region source/lib/error/index.ts
|
|
173
|
-
var error_exports = {};
|
|
174
|
-
__export(error_exports, {
|
|
175
|
-
CommerceSdkErrorBase: () => CommerceSdkErrorBase,
|
|
176
|
-
CommerceSdkValidationError: () => CommerceSdkValidationError
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
//#endregion
|
|
180
|
-
export { CommerceSdkErrorBase, CommerceSdkValidationError, error_exports };
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { GenericIssue } from "valibot";
|
|
2
|
-
|
|
3
|
-
//#region source/lib/error/base-error.d.ts
|
|
4
|
-
type CommerceSdkErrorBaseOptions = ErrorOptions & {
|
|
5
|
-
traceId?: string;
|
|
6
|
-
};
|
|
7
|
-
type CommerceSdkErrorOptions<T extends Record<string, unknown> = Record<string, unknown>> = CommerceSdkErrorBaseOptions & T;
|
|
8
|
-
declare abstract class CommerceSdkErrorBase extends Error {
|
|
9
|
-
readonly traceId?: string;
|
|
10
|
-
constructor(message: string, {
|
|
11
|
-
traceId,
|
|
12
|
-
...options
|
|
13
|
-
}: CommerceSdkErrorBaseOptions);
|
|
14
|
-
static isSdkError(error: unknown): error is CommerceSdkErrorBase;
|
|
15
|
-
get fullStack(): string;
|
|
16
|
-
get rootCause(): unknown;
|
|
17
|
-
toJSON(): {
|
|
18
|
-
name: string;
|
|
19
|
-
message: string;
|
|
20
|
-
stack: string;
|
|
21
|
-
cause: unknown;
|
|
22
|
-
traceId: string | undefined;
|
|
23
|
-
};
|
|
24
|
-
toString(inspect?: boolean): string;
|
|
25
|
-
}
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region source/lib/error/validation-error.d.ts
|
|
28
|
-
type CommerceSdkValidationErrorOptions = CommerceSdkErrorOptions<{
|
|
29
|
-
issues: GenericIssue[];
|
|
30
|
-
}>;
|
|
31
|
-
declare class CommerceSdkValidationError extends CommerceSdkErrorBase {
|
|
32
|
-
readonly issues: GenericIssue[];
|
|
33
|
-
constructor(message: string, {
|
|
34
|
-
issues,
|
|
35
|
-
...options
|
|
36
|
-
}: CommerceSdkValidationErrorOptions);
|
|
37
|
-
display(withColor?: boolean): string;
|
|
38
|
-
}
|
|
39
|
-
declare namespace index_d_exports {
|
|
40
|
-
export { CommerceSdkErrorBase, CommerceSdkValidationError };
|
|
41
|
-
}
|
|
42
|
-
//#endregion
|
|
43
|
-
export { CommerceSdkErrorBase, CommerceSdkValidationError, index_d_exports };
|