@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
138 lines (134 loc) • 4.07 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 2.0.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
'use strict';
const text = require('../tools/text.cjs');
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.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`.
*
* For batch calls the key tells you *which* command failed:
* - an **object / named-command batch** keys each error by the command label;
* - an **array-mode batch** keys each per-command error by its **numeric
* position** (`'0'`, `'1'`, … as a string), matching the command order you
* passed in. (#255 — previously these fell back to a random UUID.)
*
* An envelope-level soft error (not tied to one command) lands under the
* internal `'base-error'` key, and {@link Result.addErrors} (no explicit key)
* still uses generated UUIDs — for those, prefer {@link Result.getErrors} /
* {@link Result.getErrorMessages}. (#230)
*
* @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);
}
}
exports.Result = Result;
//# sourceMappingURL=result.cjs.map