@adobe/aio-commerce-lib-core
Version:
Core utilities for AIO Commerce SDK Libraries
99 lines (98 loc) • 4.65 kB
JavaScript
/**
* @license
*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
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));
const util = __toESM(require("util")), ansis = __toESM(require("ansis")), valibot = __toESM(require("valibot"));
var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error {
traceId;
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: Infinity
}) : 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 ? LAST_RETURN_CHAR : RETURN_CHAR;
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) {
let lines = 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, at = withColor ? (0, ansis.whiteBright)("at") : "at", issueLine = `${kindText} ${at} ${path} ${message}`;
return `${returnChar} ${issueLine}`;
});
return lines?.join("\n") ?? "";
}
function displayValidationError(error, withColor = !0) {
let display = issueToDisplay(error.issues, withColor), message = withColor ? (0, ansis.whiteBright)(error.message) : error.message;
return `${message}\n${display}`;
}
var CommerceSdkValidationError = class extends CommerceSdkErrorBase {
issues;
constructor(message, { issues,...options }) {
super(message, options), this.issues = issues;
}
display(withColor = !0) {
return displayValidationError(this, withColor);
}
};
exports.CommerceSdkErrorBase = CommerceSdkErrorBase, exports.CommerceSdkValidationError = CommerceSdkValidationError;