{"version":3,"file":"batch-by-chunk.mjs","sources":["../../../../../src/core/actions/v2/batch-by-chunk.ts"],"sourcesContent":["import type { ActionOptions } from '../abstract-action'\nimport type {\n  BatchCommandsArrayUniversal,\n  BatchCommandsObjectUniversal, BatchCommandsUniversal\n} from '../../../types/http'\nimport type { IB24BatchOptions } from '../../../types/b24'\nimport { AbstractBatch } from '../abstract-batch'\nimport { ApiVersion } from '../../../types/b24'\nimport { Result } from '../../result'\n\nexport type ActionBatchByChunkV2 = ActionOptions & {\n  calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal\n  options?: Omit<IB24BatchOptions, 'returnAjaxResult'>\n}\n\n/**\n * Executes a batch request with automatic chunking for any number of commands. `restApi:v2`\n *\n * @todo add docs\n */\nexport class BatchByChunkV2 extends AbstractBatch {\n  /**\n   * Executes a batch request with automatic chunking for any number of commands.\n   * Unlike `BatchV2`, which is limited to 50 commands, this method automatically splits\n   * a large set of commands into multiple batches and executes them sequentially.\n   *\n   * @template T - The data type returned by commands (default: `unknown`)\n   *\n   * @param {ActionBatchByChunkV2} options - parameters for executing the request.\n   *     - `calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal` - 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   *        - Note: Named commands are not supported as they are difficult to process when chunking.\n   *     - `options?: Omit<IB24BatchOptions, 'returnAjaxResult'>` - 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   *\n   * @returns {Promise<Result<T[]>>} A promise that is resolved by the result of executing all commands.\n   *\n   * @example\n   * import { EnumCrmEntityTypeId, Text } from '@bitrix24/b24jssdk'\n   *\n   * interface Contact { id: number, name: string }\n   * const commands = Array.from({ length: 150 }, (_, i) =>\n   *   ['crm.item.get', { entityTypeId: EnumCrmEntityTypeId.contact, id: i + 1 }]\n   * )\n   *\n   * const response = await b24.actions.v2.batchByChunk.make<{ item: Contact }>({\n   *   calls: commands,\n   *   options: {\n   *     isHaltOnError: false,\n   *     requestId: 'batch-by-chunk-123'\n   *   }\n   * })\n   *\n   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   *\n   * const resultData = response.getData()\n   * const items: Contact[] = []\n   * resultData.forEach((chunkRow) => {\n   *   items.push(chunkRow.item)\n   * })\n   * console.log(`Successfully retrieved ${items.length} items`)\n   *\n   * @tip For very large command sets, consider using server-side task queues instead of bulk batch requests.\n   */\n  public override async make<T = unknown>(options: ActionBatchByChunkV2): Promise<Result<T[]>> {\n    const batchSize = 50\n\n    const opts = {\n      ...options.options,\n      returnAjaxResult: false,\n      apiVersion: ApiVersion.v2\n    }\n\n    // callBatchByChunk\n    const result = new Result<T[]>()\n\n    const dataResult: T[] = []\n    const chunks = this.chunkArray(options.calls as BatchCommandsUniversal, batchSize) as BatchCommandsArrayUniversal[] | BatchCommandsObjectUniversal[]\n\n    for (const chunkRequest of chunks) {\n      const response = await this._b24.getHttpClient(ApiVersion.v2).batch<T[]>(chunkRequest, opts)\n\n      if (!response.isSuccess) {\n        this._addBatchErrorsIfAny(response, result)\n      }\n\n      for (const [_index, data] of response.getData()!.result!) {\n        // @memo Add only success rows\n        if (data.isSuccess) {\n          dataResult.push(data.getData()!.result as T)\n        }\n      }\n    }\n\n    return result.setData(dataResult)\n  }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAoBO,MAAM,uBAAuB,aAAA,CAAc;AAAA,EApBlD;AAoBkD,IAAA,MAAA,CAAA,IAAA,EAAA,gBAAA,CAAA;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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