{"version":3,"file":"batch.mjs","sources":["../../../../../src/core/actions/v2/batch.ts"],"sourcesContent":["import type { ActionOptions } from '../abstract-action'\nimport type { CallBatchResult, IB24BatchOptions } from '../../../types/b24'\nimport type {\n  BatchCommandsArrayUniversal,\n  BatchCommandsObjectUniversal,\n  BatchNamedCommandsUniversal\n} from '../../../types/http'\nimport { AbstractBatch } from '../abstract-batch'\nimport { ApiVersion } from '../../../types/b24'\n\nexport type ActionBatchV2 = ActionOptions & {\n  calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal | BatchNamedCommandsUniversal\n  options?: IB24BatchOptions\n}\n\n/**\n * Executes a batch request to the Bitrix24 REST API with a maximum number of commands of no more than 50. `restApi:v2`\n * Allows you to execute multiple requests in a single API call, significantly improving performance.\n *\n * @todo add docs\n */\nexport class BatchV2 extends AbstractBatch {\n  /**\n   * Executes a batch request to the Bitrix24 REST API with a maximum number of commands of no more than 50.\n   * Allows you to execute multiple requests in a single API call, significantly improving performance.\n   *\n   * @template T - The data type returned by batch query commands (default is `unknown`)\n   *\n   * @param {ActionBatchV2} options - parameters for executing the request.\n   *     - `calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal | BatchNamedCommandsUniversal` - Commands to execute in a batch.\n   *        Supports several formats:\n   *        1. Array of tuples: `[['method1', params1], ['method2', params2], ...]`\n   *        2. Array of objects: `[{ method: 'method1', params: params1 }, { method: 'method2', params: params2 }, ...]`\n   *        3. An object with named commands: `{ cmd1: { method: 'method1', params: params1 }, cmd2: ['method2', params2], ...}`\n   *     - `options?: IB24BatchOptions` - Additional options for executing a batch request.\n   *        - `isHaltOnError?: boolean` - Whether to stop execution on the first error (default: true)\n   *        - `requestId?: string` - Unique request identifier for tracking. Used for query deduplication and debugging (default: undefined)\n   *        - `returnAjaxResult?: boolean` - Whether to return an AjaxResult object instead of data (default: false)\n   *\n   * @returns {Promise<CallBatchResult<T>>} A promise that is resolved by the result of executing a batch request:\n   *     - On success: a `Result` object with the command execution results\n   *     - The structure of the results depends on the format of the `calls` input data:\n   *          - For an array of commands, an array of results in the same order\n   *          - For named commands, an object with keys corresponding to the command names\n   *\n   * @example\n   * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk'\n   *\n   * interface Contact { id: number, name: string }\n   * const response = await b24.actions.v2.batch.make<{ item: Contact }>({\n   *   calls: [\n   *     ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 }],\n   *     ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 2 }],\n   *     ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: 3 }]\n   *   ],\n   *   options: {\n   *     isHaltOnError: true,\n   *     returnAjaxResult: true,\n   *     requestId: 'batch-123'\n   *   }\n   * })\n   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   *\n   * const resultData = (response as Result<AjaxResult<{ item: Contact }>[]>).getData()\n   * resultData.forEach((resultRow, index) => {\n   *   if (resultRow.isSuccess) {\n   *    console.log(`Item ${index + 1}:`, resultRow.getData().result.item)\n   *   }\n   * })\n   *\n   * @example\n   * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk'\n   *\n   * const response = await b24.actions.v2.batch.make({\n   *   calls: [\n   *     { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 } },\n   *     { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 2 } }\n   *   ],\n   *   options: {\n   *     isHaltOnError: true,\n   *     returnAjaxResult: true,\n   *     requestId: 'batch-123'\n   *   }\n   * })\n   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   *\n   * @example\n   * import { EnumCrmEntityTypeId } from '@bitrix24/b24jssdk'\n   *\n   * interface Contact { id: number, name: string }\n   * interface Deal { id: number, title: string }\n   * const response = await b24.actions.v2.batch.make<{ item: Contact } | { item: Deal }>({\n   *   calls: {\n   *     Contact: { method: 'crm.item.get', params: { entityTypeId: EnumCrmEntityTypeId.contact, id: 1 } },\n   *     Deal: ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.deal, id: 2 }]\n   *   },\n   *   options: {\n   *     isHaltOnError: true,\n   *     returnAjaxResult: true,\n   *     requestId: 'batch-123'\n   *   }\n   * })\n   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   *\n   * const results = response.getData() as Record<string, AjaxResult<{ item: Contact } | { item: Deal }>>\n   * console.log('Contact:', results.Contact.getData().result.item as Contact)\n   * console.log('Deal:', results.Deal.getData().result.item as Deal)\n   *\n   * @warning The maximum number of commands in one batch request is 50.\n   * @note A batch request executes faster than sequential single calls,\n   *     but if one command fails, the entire batch may fail\n   *     (depending on API settings and options).\n   */\n  public override async make<T = unknown>(options: ActionBatchV2): Promise<CallBatchResult<T>> {\n    const opts = {\n      ...options.options,\n      apiVersion: ApiVersion.v2\n    }\n\n    const response = await this._b24.getHttpClient(ApiVersion.v2).batch<T>(options.calls, opts)\n\n    return this._processBatchResponse<T>(response, options.calls, opts)\n  }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAqBO,MAAM,gBAAgB,aAAA,CAAc;AAAA,EArB3C;AAqB2C,IAAA,MAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkGzC,MAAsB,KAAkB,OAAA,EAAqD;AAC3F,IAAA,MAAM,IAAA,GAAO;AAAA,MACX,GAAG,OAAA,CAAQ,OAAA;AAAA,MACX,YAAY,UAAA,CAAW;AAAA,KACzB;AAEA,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,IAAA,CAAK,aAAA,CAAc,UAAA,CAAW,EAAE,CAAA,CAAE,KAAA,CAAS,OAAA,CAAQ,KAAA,EAAO,IAAI,CAAA;AAE1F,IAAA,OAAO,IAAA,CAAK,qBAAA,CAAyB,QAAA,EAAU,OAAA,CAAQ,OAAO,IAAI,CAAA;AAAA,EACpE;AACF;;;;"}