@adobe/aio-commerce-lib-core 0.6.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ import util from "util";
2
+ import { cyanBright, dim, whiteBright, yellowBright } from "ansis";
3
+ import { getDotPath } from "valibot";
4
+
5
+ //#region source/error/base-error.ts
6
+ /**
7
+ * Base class for all the errors in the AIO Commerce SDK.
8
+ * @example
9
+ * ```ts
10
+ * class ValidationError extends CommerceSdkErrorBase {
11
+ * constructor(message: string, options: ValidationErrorOptions) {
12
+ * super(message, options);
13
+ * }
14
+ * }
15
+ *
16
+ * const err = new ValidationError("Invalid value", {
17
+ * tag: "ValidationError",
18
+ * field: "name",
19
+ * value: "John Doe",
20
+ * });
21
+ *
22
+ * console.log(err.toJSON());
23
+ * ```
24
+ */
25
+ var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
26
+ /**
27
+ * Constructs a new CommerceSdkErrorBase instance. This is an abstract class so you
28
+ * should not instantiate it directly. Only invoke this constructor from a subclass.
29
+ *
30
+ * @param message - A human-friendly description of the error.
31
+ * @param options - Optional error options (additional information).
32
+ */
33
+ constructor(message, options) {
34
+ const { traceId, ...baseOptions } = options ?? {};
35
+ super(message, baseOptions);
36
+ Object.setPrototypeOf(this, new.target.prototype);
37
+ if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
38
+ this.name = new.target.name || "CommerceSdkError";
39
+ this.traceId = traceId;
40
+ }
41
+ /**
42
+ * Checks if the error is any CommerceSdkErrorBase instance.
43
+ * @example
44
+ * ```ts
45
+ * class ValidationError extends CommerceSdkErrorBase {}
46
+ * const err = new ValidationError("Invalid", {});
47
+ *
48
+ * CommerceSdkErrorBase.isSdkError(err); // true
49
+ * ValidationError.isSdkError(err); // true
50
+ * CommerceSdkErrorBase.isSdkError(new Error("Regular")); // false
51
+ * ```
52
+ */
53
+ static isSdkError(error) {
54
+ return error instanceof CommerceSdkErrorBase;
55
+ }
56
+ /** Returns the full stack trace of the error and its causes. */
57
+ get fullStack() {
58
+ let out = this.stack ?? "";
59
+ let cause = this.cause;
60
+ while (cause instanceof Error) {
61
+ out += `\nCaused by: ${cause.stack ?? cause.message}`;
62
+ cause = cause.cause;
63
+ }
64
+ return out;
65
+ }
66
+ /** Returns the root cause of the error. */
67
+ get rootCause() {
68
+ let cause = this.cause;
69
+ while (cause) if (typeof cause === "object" && cause !== null && "cause" in cause) cause = cause.cause;
70
+ else break;
71
+ return cause;
72
+ }
73
+ /** Converts the error to a JSON-like representation. */
74
+ toJSON() {
75
+ return {
76
+ name: this.name,
77
+ message: this.message,
78
+ stack: this.fullStack,
79
+ cause: this.cause,
80
+ traceId: this.traceId
81
+ };
82
+ }
83
+ /**
84
+ * Returns a pretty string representation of the error.
85
+ * @param inspect - Whether to inspect the error (returns a more detailed string, useful for debugging).
86
+ */
87
+ toString(inspect = true) {
88
+ if (inspect) return util.inspect(this, {
89
+ depth: null,
90
+ colors: true,
91
+ sorted: true,
92
+ maxStringLength: Number.POSITIVE_INFINITY
93
+ });
94
+ return super.toString();
95
+ }
96
+ };
97
+
98
+ //#endregion
99
+ //#region source/error/validation-error.ts
100
+ const LAST_RETURN_CHAR = "└── ";
101
+ const RETURN_CHAR = "├── ";
102
+ /** Maps issue kinds to their corresponding error titles for display purposes. */
103
+ const ISSUE_KIND_TO_ERROR_TITLE = {
104
+ schema: "Schema validation error",
105
+ transformation: "Transformation error",
106
+ validation: "Input error"
107
+ };
108
+ function getReturnChar(isLastItem, withColor) {
109
+ const char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR;
110
+ return withColor ? cyanBright(char) : char;
111
+ }
112
+ function getKindText(kind, withColor) {
113
+ const text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind";
114
+ return withColor ? yellowBright(text) : text;
115
+ }
116
+ function getPathText(dotPath, withColor) {
117
+ if (!dotPath) return "";
118
+ return withColor ? `${cyanBright(dotPath)}${whiteBright(dim(" →"))}` : `${dotPath} →`;
119
+ }
120
+ function issueToDisplay(issues, withColor = true) {
121
+ return (issues?.map((issue, index) => {
122
+ const returnChar = getReturnChar(index === issues.length - 1, withColor);
123
+ const kindText = getKindText(issue.kind, withColor);
124
+ const path = getPathText(getDotPath(issue), withColor);
125
+ const message = withColor ? whiteBright(issue.message) : issue.message;
126
+ return `${returnChar} ${`${kindText} ${withColor ? whiteBright("at") : "at"} ${path} ${message}`}`;
127
+ }))?.join("\n") ?? "";
128
+ }
129
+ function displayValidationError(error, withColor = true) {
130
+ const display = issueToDisplay(error.issues, withColor);
131
+ return `${withColor ? whiteBright(error.message) : error.message}\n${display}`;
132
+ }
133
+ /**
134
+ * Represents a validation error in the Commerce SDK.
135
+ * This error should be thrown when an input does not conform to the expected schema.
136
+ * It contains a list of issues that describe the validation errors.
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * const error = new CommerceSdkValidationError("Invalid input", {
141
+ * // `someIssues` is in scope, returned by some `valibot` operation.
142
+ * issues: someIssues
143
+ * });
144
+ *
145
+ * console.log(error.display());
146
+ * ```
147
+ */
148
+ var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
149
+ /**
150
+ * Constructs a new {@link CommerceSdkValidationError} instance.
151
+ *
152
+ * @param message - A human-friendly description of the validation error.
153
+ * @param options - Options for the validation error, including the issues that caused the error.
154
+ */
155
+ constructor(message, { issues, ...options }) {
156
+ super(message, options);
157
+ this.issues = issues;
158
+ }
159
+ /**
160
+ * Returns a pretty string (and colored) representation of the validation error.
161
+ * @param withColor Whether to use color in the output.
162
+ */
163
+ display(withColor = true) {
164
+ return displayValidationError(this, withColor);
165
+ }
166
+ };
167
+
168
+ //#endregion
169
+ export { CommerceSdkErrorBase as n, CommerceSdkValidationError as t };
package/package.json CHANGED
@@ -2,13 +2,13 @@
2
2
  "name": "@adobe/aio-commerce-lib-core",
3
3
  "type": "module",
4
4
  "author": "Adobe Inc.",
5
- "version": "0.6.1",
5
+ "version": "1.0.0",
6
6
  "private": false,
7
7
  "engines": {
8
8
  "node": ">=20 <=24"
9
9
  },
10
10
  "license": "Apache-2.0",
11
- "description": "Core utilities for AIO Commerce SDK Libraries",
11
+ "description": "Core utilities for Adobe Commerce SDK libraries",
12
12
  "keywords": [
13
13
  "aio",
14
14
  "adobe-io",
@@ -17,7 +17,7 @@
17
17
  "adobe-commerce-sdk",
18
18
  "aio-commerce-sdk",
19
19
  "aio-commerce-lib-core",
20
- "aio-commerce-sdk-lib-core"
20
+ "app-builder"
21
21
  ],
22
22
  "bugs": {
23
23
  "url": "https://github.com/adobe/aio-commerce-sdk/issues"
@@ -53,10 +53,12 @@
53
53
  "dependencies": {
54
54
  "ansis": "^4.1.0",
55
55
  "camelcase": "^8.0.0",
56
+ "type-fest": "^5.0.0",
56
57
  "valibot": "^1.1.0"
57
58
  },
58
59
  "devDependencies": {
59
- "@aio-commerce-sdk/config-tsdown": "1.0.0",
60
+ "typescript": "^5.9.2",
61
+ "@aio-commerce-sdk/config-tsdown": "1.0.1",
60
62
  "@aio-commerce-sdk/config-typedoc": "1.0.0",
61
63
  "@aio-commerce-sdk/config-typescript": "1.0.0",
62
64
  "@aio-commerce-sdk/config-vitest": "1.0.0"
@@ -1,2 +0,0 @@
1
- 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)=>{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,{get:(k=>from[k]).bind(null,key),enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable});return to},__toESM=(mod,isNodeMode,target)=>(target=mod==null?{}:__create(__getProtoOf(mod)),__copyProps(isNodeMode||!mod||!mod.__esModule?__defProp(target,`default`,{value:mod,enumerable:!0}):target,mod));let util=require(`util`);util=__toESM(util);let ansis=require(`ansis`),valibot=require(`valibot`);var CommerceSdkErrorBase=class CommerceSdkErrorBase extends Error{constructor(message,options){let{traceId,...baseOptions}=options??{};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}static isSdkError(error){return error instanceof CommerceSdkErrorBase}get fullStack(){let out=this.stack??``,cause=this.cause;for(;cause instanceof Error;)out+=`\nCaused by: ${cause.stack??cause.message}`,cause=cause.cause;return out}get rootCause(){let cause=this.cause;for(;cause&&typeof cause==`object`&&cause&&`cause`in cause;)cause=cause.cause;return cause}toJSON(){return{name:this.name,message:this.message,stack:this.fullStack,cause:this.cause,traceId:this.traceId}}toString(inspect=!0){return inspect?util.default.inspect(this,{depth:null,colors:!0,sorted:!0,maxStringLength:1/0}):super.toString()}};const LAST_RETURN_CHAR=`└── `,RETURN_CHAR=`├── `,ISSUE_KIND_TO_ERROR_TITLE={schema:`Schema validation error`,transformation:`Transformation error`,validation:`Input error`};function getReturnChar(isLastItem,withColor){let char=isLastItem?`└── `:`├── `;return withColor?(0,ansis.cyanBright)(char):char}function getKindText(kind,withColor){let text=ISSUE_KIND_TO_ERROR_TITLE[kind]??`Unknown issue kind`;return withColor?(0,ansis.yellowBright)(text):text}function getPathText(dotPath,withColor){return dotPath?withColor?`${(0,ansis.cyanBright)(dotPath)}${(0,ansis.whiteBright)((0,ansis.dim)(` →`))}`:`${dotPath} →`:``}function issueToDisplay(issues,withColor=!0){return(issues?.map((issue,index)=>{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;return`${returnChar} ${`${kindText} ${withColor?(0,ansis.whiteBright)(`at`):`at`} ${path} ${message}`}`}))?.join(`
2
- `)??``}function displayValidationError(error,withColor=!0){let display=issueToDisplay(error.issues,withColor);return`${withColor?(0,ansis.whiteBright)(error.message):error.message}\n${display}`}var CommerceSdkValidationError=class extends CommerceSdkErrorBase{constructor(message,{issues,...options}){super(message,options),this.issues=issues}display(withColor=!0){return displayValidationError(this,withColor)}};Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return CommerceSdkErrorBase}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return __toESM}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return CommerceSdkValidationError}});
@@ -1,2 +0,0 @@
1
- import util from"util";import{cyanBright,dim,whiteBright,yellowBright}from"ansis";import{getDotPath}from"valibot";var CommerceSdkErrorBase=class CommerceSdkErrorBase extends Error{constructor(message,options){let{traceId,...baseOptions}=options??{};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}static isSdkError(error){return error instanceof CommerceSdkErrorBase}get fullStack(){let out=this.stack??``,cause=this.cause;for(;cause instanceof Error;)out+=`\nCaused by: ${cause.stack??cause.message}`,cause=cause.cause;return out}get rootCause(){let cause=this.cause;for(;cause&&typeof cause==`object`&&cause&&`cause`in cause;)cause=cause.cause;return cause}toJSON(){return{name:this.name,message:this.message,stack:this.fullStack,cause:this.cause,traceId:this.traceId}}toString(inspect=!0){return inspect?util.inspect(this,{depth:null,colors:!0,sorted:!0,maxStringLength:1/0}):super.toString()}};const ISSUE_KIND_TO_ERROR_TITLE={schema:`Schema validation error`,transformation:`Transformation error`,validation:`Input error`};function getReturnChar(isLastItem,withColor){let char=isLastItem?`└── `:`├── `;return withColor?cyanBright(char):char}function getKindText(kind,withColor){let text=ISSUE_KIND_TO_ERROR_TITLE[kind]??`Unknown issue kind`;return withColor?yellowBright(text):text}function getPathText(dotPath,withColor){return dotPath?withColor?`${cyanBright(dotPath)}${whiteBright(dim(` →`))}`:`${dotPath} →`:``}function issueToDisplay(issues,withColor=!0){return(issues?.map((issue,index)=>{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;return`${returnChar} ${`${kindText} ${withColor?whiteBright(`at`):`at`} ${path} ${message}`}`}))?.join(`
2
- `)??``}function displayValidationError(error,withColor=!0){let display=issueToDisplay(error.issues,withColor);return`${withColor?whiteBright(error.message):error.message}\n${display}`}var CommerceSdkValidationError=class extends CommerceSdkErrorBase{constructor(message,{issues,...options}){super(message,options),this.issues=issues}display(withColor=!0){return displayValidationError(this,withColor)}};export{CommerceSdkErrorBase as n,CommerceSdkValidationError as t};