UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

127 lines (124 loc) 5.54 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.3 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { AbstractBatch } from '../abstract-batch.mjs'; import { ApiVersion } from '../../../types/b24.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class BatchV2 extends AbstractBatch { static { __name(this, "BatchV2"); } /** * Executes a batch request to the Bitrix24 REST API with a maximum number of commands of no more than 50. * Allows you to execute multiple requests in a single API call, significantly improving performance. * * @template T - The data type returned by batch query commands (default is `unknown`) * * @param {ActionBatchV2} options - parameters for executing the request. * - `calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal | BatchNamedCommandsUniversal` - Commands to execute in a batch. * Supports several formats: * 1. Array of tuples: `[['method1', params1], ['method2', params2], ...]` * 2. Array of objects: `[{ method: 'method1', params: params1 }, { method: 'method2', params: params2 }, ...]` * 3. An object with named commands: `{ cmd1: { method: 'method1', params: params1 }, cmd2: ['method2', params2], ...}` * - `options?: IB24BatchOptions` - Additional options for executing a batch request. * - `isHaltOnError?: boolean` - Whether to stop execution on the first error (default: true) * - `requestId?: string` - Unique request identifier for tracking. Used for query deduplication and debugging (default: undefined) * - `returnAjaxResult?: boolean` - Whether to return an AjaxResult object instead of data (default: false) * * @returns {Promise<CallBatchResult<T>>} A promise that is resolved by the result of executing a batch request: * - On success: a `Result` object with the command execution results * - The structure of the results depends on the format of the `calls` input data: * - For an array of commands, an array of results in the same order * - For named commands, an object with keys corresponding to the command names * * @example * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk' * * interface Contact { id: number, name: string } * const response = await b24.actions.v2.batch.make<{ item: Contact }>({ * calls: [ * ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 }], * ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 2 }], * ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 3 }] * ], * options: { * isHaltOnError: true, * returnAjaxResult: true, * requestId: 'batch-123' * } * }) * if (!response.isSuccess) { * throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`) * } * * const resultData = (response as Result<AjaxResult<{ item: Contact }>[]>).getData() * resultData.forEach((resultRow, index) => { * if (resultRow.isSuccess) { * console.log(`Item ${index + 1}:`, resultRow.getData().result.item) * } * }) * * @example * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk' * * const response = await b24.actions.v2.batch.make({ * calls: [ * { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 } }, * { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 2 } } * ], * options: { * isHaltOnError: true, * returnAjaxResult: true, * requestId: 'batch-123' * } * }) * if (!response.isSuccess) { * throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`) * } * * @example * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk' * * interface Contact { id: number, name: string } * interface Deal { id: number, title: string } * const response = await b24.actions.v2.batch.make<{ item: Contact } | { item: Deal }>({ * calls: { * Contact: { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 } }, * Deal: ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.deal, id: 2 }] * }, * options: { * isHaltOnError: true, * returnAjaxResult: true, * requestId: 'batch-123' * } * }) * if (!response.isSuccess) { * throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`) * } * * const results = response.getData() as Record<string, AjaxResult<{ item: Contact } | { item: Deal }>> * console.log('Contact:', results.Contact.getData().result.item as Contact) * console.log('Deal:', results.Deal.getData().result.item as Deal) * * @warning The maximum number of commands in one batch request is 50. * @note A batch request executes faster than sequential single calls, * but if one command fails, the entire batch may fail * (depending on API settings and options). */ async make(options) { const opts = { ...options.options, apiVersion: ApiVersion.v2 }; const response = await this._b24.getHttpClient(ApiVersion.v2).batch(options.calls, opts); return this._processBatchResponse(response, options.calls, opts); } } export { BatchV2 }; //# sourceMappingURL=batch.mjs.map