UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

102 lines (99 loc) 2.48 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.4 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { Text } from '../tools/text.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class Result { static { __name(this, "Result"); } _errors; _data; constructor(data) { this._errors = /* @__PURE__ */ new Map(); this._data = data ?? null; } get isSuccess() { return this._errors.size === 0; } get errors() { return this._errors; } setData(data) { this._data = data; return this; } getData() { return this._data; } addError(error, key) { const errorKey = key ?? Text.getUuidRfc4122(); const errorObj = typeof error === "string" ? new Error(error) : error; this._errors.set(errorKey, errorObj); return this; } addErrors(errors) { for (const error of errors) { this.addError(error); } return this; } getErrors() { return this._errors.values(); } hasError(key) { return this._errors.has(key); } /** * Retrieves an array of error messages from the collected errors. * * @returns An array of strings representing the error messages. Each string * contains the message of a corresponding error object. */ getErrorMessages() { return Array.from(this._errors.values(), (e) => e.message); } /** * Converts the Result object to a string. * * @returns {string} Returns a string representation of the result operation */ toString() { const status = this.isSuccess ? "success" : "failure"; const data = this.safeStringify(this._data); return this.isSuccess ? `Result(${status}): ${data}` : `Result(${status}): ${data} Errors: ${this.getErrorMessages().join(", ")}`; } safeStringify(data) { try { return JSON.stringify(data, this.replacer, 2); } catch { return "[Unable to serialize data]"; } } replacer(_, value) { if (value instanceof Error) { return { name: value.name, message: value.message, stack: value.stack }; } return value; } // Static constructors static ok(data) { return new Result(data); } static fail(error, key) { return new Result().addError(error, key); } } export { Result }; //# sourceMappingURL=result.mjs.map