@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
122 lines (119 loc) • 4.52 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 * as qs from 'qs-esm';
import { AbstractProcessing } from '../interface-strategy.mjs';
import { SdkError } from '../../../../sdk-error.mjs';
import { AjaxResult } from '../../../../http/ajax-result.mjs';
import { Result } from '../../../../result.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class AbstractProcessingV2 extends AbstractProcessing {
static {
__name(this, "AbstractProcessingV2");
}
_buildRow(command) {
return `${command.method}?${qs.stringify(command.query || {})}`;
}
buildCommands(commands) {
if (commands.length < 1) {
throw new SdkError({
code: "JSSDK_INTERACTION_BATCH_STRATEGY_V2_EMPTY_COMMANDS",
description: "commands not set",
status: 500
});
}
const firstCommand = commands[0];
const asObject = typeof firstCommand.as === "string" && firstCommand.as.length > 0;
if (asObject) {
const result2 = {};
for (const command of commands) {
result2[command.as] = this._buildRow(command);
}
return result2;
}
const result = [];
for (const command of commands) {
result.push(this._buildRow(command));
}
return result;
}
// region prepareItems ////
async prepareItems(commands, responseHelper) {
const results = /* @__PURE__ */ new Map();
for (const [index, command] of commands.entries()) {
await this._processResponseItem(
command,
// @memo for apiVer2 in this pace we get objectIndex from `command.as` OR array `index` from `commands[]`
command.as ?? index,
responseHelper,
results
);
}
return results;
}
async _processResponseItem(command, index, responseHelper, results) {
const responseResult = responseHelper.response.getData().result;
const resultData = this._getBatchResultByIndex(responseResult.result, index);
const resultError = this._getBatchResultByIndex(responseResult.result_error, index);
if (typeof resultData !== "undefined" || typeof resultError !== "undefined") {
const methodName = command.method;
const resultTime = this._getBatchResultByIndex(responseResult.result_time, index);
if (typeof resultTime !== "undefined") {
await responseHelper.restrictionManager.updateStats(responseHelper.requestId, `batch::${methodName}`, resultTime);
}
const result = new AjaxResult({
answer: {
error: resultError ? typeof resultError === "string" ? resultError : resultError.error : void 0,
error_description: resultError ? typeof resultError === "string" ? void 0 : resultError.error_description : void 0,
result: resultData ?? {},
total: Number.parseInt(this._getBatchResultByIndex(responseResult.result_total, index) || "0"),
next: Number.parseInt(this._getBatchResultByIndex(responseResult.result_next, index) || "0"),
time: resultTime
},
query: {
method: methodName,
params: command.query || {},
requestId: responseHelper.requestId
},
status: responseHelper.response.getStatus()
});
results.set(index, result);
return;
}
if (responseHelper.parallelDefaultValue) {
throw new SdkError({
code: "JSSDK_INTERACTION_BATCH_STRATEGY_V2_EMPTY_COMMAND_RESPONSE",
description: `There were difficulties parsing the response for batch { index: ${index}, method: ${command.method} }`,
status: 500
});
}
}
// endregion ////
// region handleResults ////
async handleResults(_commands, results, responseHelper) {
const result = new Result();
const dataResult = /* @__PURE__ */ new Map();
for (const [index, data] of results) {
if (data.getStatus() !== 200 || !data.isSuccess) {
const ajaxError = this._createErrorFromAjaxResult(data);
this._processResponseError(result, ajaxError, `${index}`);
dataResult.set(index, data);
}
dataResult.set(index, data);
}
result.setData({
result: dataResult,
time: responseHelper.response.getData().time
});
return result;
}
// endregion ////
}
export { AbstractProcessingV2 };
//# sourceMappingURL=abstract-processing.mjs.map