UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

129 lines (126 loc) 3.59 kB
/** * @package @bitrix24/b24jssdk * @version 1.3.0 * @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); } /** * Retrieves all errors as a plain object (a snapshot copy) keyed by their * identifier, preserving which request produced each error. Unlike * {@link Result.getErrors}, the keys are not discarded — useful for batch * calls with `isHaltOnError: false`. * * Keys are meaningful only when the error was added with an explicit key * (e.g. an object / named-command batch). Array-mode batches and * {@link Result.addErrors} fall back to generated UUID keys. * * @returns {Record<string, Error>} A map of error key to Error object. */ getErrorsByKey() { return Object.fromEntries(this._errors); } /** * Retrieves all error messages as a plain object (a snapshot copy) keyed by * their identifier. Unlike {@link Result.getErrorMessages}, the keys are * preserved. See {@link Result.getErrorsByKey} for when keys are meaningful. * * @returns {Record<string, string>} A map of error key to error message. */ getErrorMessagesByKey() { return Object.fromEntries( Array.from(this._errors, ([key, error]) => [key, error.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