@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
139 lines (133 loc) • 5.01 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/
*/
;
const qs = require('qs-esm');
const interfaceStrategy = require('../interface-strategy.cjs');
const sdkError = require('../../../../sdk-error.cjs');
const ajaxResult = require('../../../../http/ajax-result.cjs');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
const n = Object.create(null);
if (e) {
for (const k in e) {
n[k] = e[k];
}
}
n.default = e;
return n;
}
const qs__namespace = /*#__PURE__*/_interopNamespace(qs);
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class AbstractProcessingV2 extends interfaceStrategy.AbstractProcessing {
static {
__name(this, "AbstractProcessingV2");
}
_buildRow(command) {
return `${command.method}?${qs__namespace.stringify(command.query || {})}`;
}
buildCommands(commands) {
if (commands.length < 1) {
throw new sdkError.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 ////
// Soft-error guard lives in AbstractProcessing.prepareItems (#228); this is the
// success-only path for apiVer2.
async _prepareItemsSuccess(commands, responseHelper, results) {
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.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.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 ////
// Soft-error guard lives in AbstractProcessing.handleResults (#228); this is the
// success-only path for apiVer2.
async _handleResultsSuccess(_commands, results, responseHelper, 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 ////
}
exports.AbstractProcessingV2 = AbstractProcessingV2;
//# sourceMappingURL=abstract-processing.cjs.map