@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
177 lines (174 loc) • 5.01 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.5
* @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,
next: "next" in payload ? payload.next : void 0,
total: "total" in payload ? payload.total : void 0,
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
*/
hasMore() {
return this.isMore();
}
isMore() {
if (!this.isSuccess) {
return false;
}
const payload = this._data;
const nextValue = "next" in payload ? payload.next : void 0;
return Type.isNumber(nextValue);
}
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
*
* @todo !fix api version
*/
async fetchNext(http) {
const data = await this.getNext(http);
if (data === false) {
return null;
}
return data;
}
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