@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
217 lines (214 loc) • 7.49 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.1.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { Type } from '../../tools/type.mjs';
import { Text } from '../../tools/text.mjs';
import { Result } from '../result.mjs';
import { AjaxError } from './ajax-error.mjs';
import { ApiVersion } from '../../types/b24.mjs';
import { SdkError } from '../sdk-error.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class AjaxResult extends Result {
static {
__name(this, "AjaxResult");
}
_status;
_query;
_data;
constructor(options) {
super();
this._data = options.answer ? Object.freeze(options.answer) : void 0;
this._query = Object.freeze(structuredClone(options.query));
this._status = options.status;
this.#processErrors();
}
get isSuccess() {
return this.#getIsSuccess();
}
/**
* @todo test this predicate
*/
#getIsSuccess() {
return this._errors.size === 0;
}
getData() {
if (!this.isSuccess) {
return void 0;
}
const payload = this._data;
return Object.freeze({
result: payload.result,
time: payload.time
});
}
/**
* If the response contains error data, we'll restore it to an error.
*
* @todo make single function
* @see AbstractHttp._convertAxiosErrorToAjaxError()
*/
#processErrors() {
if (this._data && typeof this._data === "object" && "error" in this._data) {
const responseData = this._data;
if (responseData.error && typeof responseData.error === "object" && "code" in responseData.error) {
const errorCode = responseData.error.code;
let errorDescription = responseData.error.message.trimEnd();
if (responseData.error.validation) {
if (errorDescription.length > 0) {
if (!errorDescription.endsWith(".")) {
errorDescription += `.`;
}
errorDescription += ` `;
}
responseData.error.validation.forEach((row) => {
errorDescription += `${row?.message || JSON.stringify(row)}`;
});
}
this.addError(this.#createAjaxError({
code: errorCode,
description: errorDescription,
status: this._status
}), "base-error");
} else if (responseData.error && typeof responseData.error === "string") {
const errorCode = responseData.error !== "0" ? responseData.error : "JSSDK_RESPONSE_ERROR";
const errorDescription = responseData?.error_description ?? "Some error in response";
this.addError(this.#createAjaxError({
code: errorCode,
description: errorDescription,
status: this._status
}), "base-error");
}
}
}
#createAjaxError(errorData) {
return new AjaxError({
code: errorData.code,
description: errorData.description,
status: errorData.status,
requestInfo: {
method: this._query.method,
params: this._query.params,
requestId: this._query.requestId
}
});
}
/**
* Alias for isMore
*
* @deprecated Will be removed in `2.0.0`. Tied to the `restApi:v2` envelope
* field `next`, which `restApi:v3` does not return. Use the SDK's list
* helpers — they hide pagination entirely:
* - `restApi:v2`: {@link CallListV2.make `b24.actions.v2.callList.make`} or {@link FetchListV2.make `b24.actions.v2.fetchList.make`}
* - `restApi:v3`: {@link CallListV3.make `b24.actions.v3.callList.make`} or {@link FetchListV3.make `b24.actions.v3.fetchList.make`}
*
* @removed 2.0.0
*/
hasMore() {
return this.isMore();
}
/**
* @deprecated Will be removed in `2.0.0`. Tied to the `restApi:v2` envelope
* field `next`, which `restApi:v3` does not return. Use the SDK's list
* helpers — they hide pagination entirely:
* - `restApi:v2`: {@link CallListV2.make `b24.actions.v2.callList.make`} or {@link FetchListV2.make `b24.actions.v2.fetchList.make`}
* - `restApi:v3`: {@link CallListV3.make `b24.actions.v3.callList.make`} or {@link FetchListV3.make `b24.actions.v3.fetchList.make`}
*
* @removed 2.0.0
*/
isMore() {
if (!this.isSuccess) {
return false;
}
const payload = this._data;
const nextValue = "next" in payload ? payload.next : void 0;
return Type.isNumber(nextValue);
}
/**
* @deprecated Will be removed in `2.0.0`. Tied to the `restApi:v2` envelope
* field `total`, which `restApi:v3` does not return. For `restApi:v3` the
* SDK exposes element counts via the `aggregate` action (`count` /
* `countDistinct`); for `restApi:v2` use the list helpers, which iterate
* without exposing `total`.
*
* @removed 2.0.0
*/
getTotal() {
if (!this.isSuccess) {
return 0;
}
const payload = this._data;
const totalValue = "total" in payload ? payload.total : void 0;
return Text.toInteger(totalValue);
}
getStatus() {
return this._status;
}
getQuery() {
return this._query;
}
/**
* Alias for getNext
* @param http
*
* @deprecated Will be removed in `2.0.0`. `restApi:v3` does not support
* `getNext()` (the v2 envelope field `next` does not exist). Use the SDK's
* list helpers instead — they hide pagination entirely:
* - `restApi:v2`: {@link CallListV2.make `b24.actions.v2.callList.make`} or {@link FetchListV2.make `b24.actions.v2.fetchList.make`}
* - `restApi:v3`: {@link CallListV3.make `b24.actions.v3.callList.make`} or {@link FetchListV3.make `b24.actions.v3.fetchList.make`}
*
* @removed 2.0.0
*/
async fetchNext(http) {
const data = await this.getNext(http);
if (data === false) {
return null;
}
return data;
}
/**
* @deprecated Will be removed in `2.0.0`. Throws on `restApi:v3` because the
* v2 envelope field `next` is not part of the v3 protocol. Use the SDK's
* list helpers instead — they hide pagination entirely:
* - `restApi:v2`: {@link CallListV2.make `b24.actions.v2.callList.make`} or {@link FetchListV2.make `b24.actions.v2.fetchList.make`}
* - `restApi:v3`: {@link CallListV3.make `b24.actions.v3.callList.make`} or {@link FetchListV3.make `b24.actions.v3.fetchList.make`}
*
* @throws {SdkError} `JSSDK_CORE_METHOD_NOT_SUPPORT_IN_API_V3` when called against a `restApi:v3` HTTP client. This throw is preserved until `2.0.0`.
* @removed 2.0.0
*/
async getNext(http) {
if (http.apiVersion === ApiVersion.v3) {
throw new SdkError({
code: "JSSDK_CORE_METHOD_NOT_SUPPORT_IN_API_V3",
description: `restApi:v3 not support method getNext`,
status: 500
});
}
if (!this.isSuccess || !this.isMore()) {
return false;
}
const nextPageQuery = this.#buildNextPageQuery();
return http.call(
nextPageQuery.method,
nextPageQuery.params
);
}
#buildNextPageQuery() {
const result = { ...this._query };
const payload = this._data;
const nextValue = "next" in payload ? payload.next : void 0;
result.params.start = Text.toInteger(Text.toInteger(nextValue));
return result;
}
// Immutable API
setData() {
throw new ReferenceError("AjaxResult does not allow data modification");
}
}
export { AjaxResult };
//# sourceMappingURL=ajax-result.mjs.map