{"version":3,"file":"batch.mjs","sources":["../../../../../src/core/actions/v3/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'\nimport { versionManager } from '../../version-manager'\nimport { SdkError } from '../../sdk-error'\n\nexport type ActionBatchV3 = 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:v3`\n * Allows you to execute multiple requests in a single API call, significantly improving performance.\n *\n * @todo add docs\n */\nexport class BatchV3 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 {ActionBatchV3} 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   * interface TaskItem { id: number, title: string }\n   * const response = await b24.actions.v3.batch.make<{ item: TaskItem }>({\n   *   calls: [\n   *     ['tasks.task.get', { id: 1, select: ['id', 'title'] }],\n   *     ['tasks.task.get', { id: 2, select: ['id', 'title'] }],\n   *     ['tasks.task.get', { id: 3, select: ['id', 'title'] }]\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: TaskItem }>[]>).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   * const response = await b24.actions.v3.batch.make({\n   *   calls: [\n   *     { method: 'tasks.task.get', params: { id: 1, select: ['id', 'title'] } },\n   *     { method: 'tasks.task.get', params: { id: 2, select: ['id', 'title'] } }\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   * interface TaskItem { id: number, title: string }\n   * interface MainEventLogItem { id: number, userId: number }\n   * const response = await b24.actions.v3.batch.make<{ item: TaskItem } | { items: MainEventLogItem[] }>({\n   *   calls: {\n   *     Task: { method: 'tasks.task.get', params: { id: 1, select: ['id', 'title'] } },\n   *     MainEventLog: ['main.eventlog.list', { select: ['id', 'userId'], pagination: { limit: 5 } }]\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: TaskItem } | { items: MainEventLogItem[] }>>\n   * console.log('Task:', results.Task.getData().result.item as TaskItem)\n   * console.log('MainEventLog:', results.MainEventLog.getData().result.items as MainEventLogItem[])\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: ActionBatchV3): Promise<CallBatchResult<T>> {\n    const opts = {\n      ...options.options,\n      apiVersion: ApiVersion.v3\n    }\n\n    if (versionManager.automaticallyObtainApiVersionForBatch(options.calls) !== opts.apiVersion) {\n      throw new SdkError({\n        code: 'JSSDK_CORE_METHOD_NOT_SUPPORT_IN_API_V3',\n        description: `restApi:v3 not support some methods in calls: ${JSON.stringify(options.calls)}`,\n        status: 500\n      })\n    }\n\n    const response = await this._b24.getHttpClient(ApiVersion.v3).batch<T>(options.calls, opts)\n\n    return this._processBatchResponse<T>(response, options.calls, opts)\n  }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAuBO,MAAM,gBAAgB,aAAA,CAAc;AAAA,EAvB3C;AAuB2C,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;AA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