UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

81 lines (78 loc) • 2.9 kB
/** * @package @bitrix24/b24jssdk * @version 3.0.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { SdkError } from '../../sdk-error.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class ParseRow { static { __name(this, "ParseRow"); } static getBatchCommand(row, options) { if (row) { if (typeof row === "object" && "method" in row && typeof row.method === "string") { return { method: row.method, // `?? {}` because `params` is optional on both command shapes while // `query` is not optional to the portal: it reads every top-level entry // of a v3 batch body as a command and rejects the WHOLE batch with // `INVALIDSELECTEXCEPTION` when one of them has no `query` — measured, // and one bad entry is enough to take the others down with it. // `JSON.stringify` drops an `undefined` value, so leaving it undefined // is not "sending an empty query", it is sending no key at all. query: row.params ?? {}, as: row.as ?? options.asDefaultValue, parallel: row.parallel ?? options.parallelDefaultValue }; } if (Array.isArray(row) && row.length > 0 && typeof row[0] === "string") { return { method: row[0], // Same reason as above — `['rest.scope.list']` is a valid tuple. query: row[1] ?? {}, as: options.asDefaultValue, parallel: options.parallelDefaultValue }; } } const { parallelDefaultValue, asDefaultValue } = options; const rowShape = row && "object" === typeof row ? { keys: Object.keys(row).join(", "), ...Array.isArray(row) ? { length: row.length } : { method: "string" === typeof row.method ? row.method : void 0 } } : { type: typeof row }; throw new SdkError({ code: "JSSDK_INTERACTION_BATCH_ROW_FAIL", description: `There were difficulties parsing the command for batch. ${JSON.stringify({ row: rowShape, options: { parallelDefaultValue, asDefaultValue } })}`, status: 500 }); } static getMethodsFromCommands(calls) { const result = []; const optsFake = { parallelDefaultValue: false }; if (Array.isArray(calls)) { calls.forEach((row) => { const command = ParseRow.getBatchCommand(row, optsFake); result.push(command.method); }); } else { Object.entries(calls).forEach(([index, row]) => { const command = ParseRow.getBatchCommand(row, { ...optsFake, asDefaultValue: index }); result.push(command.method); }); } return result; } } export { ParseRow }; //# sourceMappingURL=parse-row.mjs.map