{"version":3,"file":"fetch-list.mjs","sources":["../../../../../src/core/actions/v2/fetch-list.ts"],"sourcesContent":["import type { ActionOptions } from '../abstract-action'\nimport type { TypeCallParams } from '../../../types/http'\nimport type { AjaxResult } from '../../http/ajax-result'\nimport { AbstractAction } from '../abstract-action'\nimport { SdkError } from '../../sdk-error'\n\nexport type ActionFetchListV2 = ActionOptions & {\n  method: string\n  params?: Omit<TypeCallParams, 'start'>\n  idKey?: string\n  customKeyForResult?: string\n  requestId?: string\n}\n\n/**\n * Calls a REST API list method and returns an async generator for efficient large data retrieval. `restApi:v2`\n *\n * @todo add docs\n */\nexport class FetchListV2 extends AbstractAction {\n  /**\n   * Calls a REST API list method and returns an async generator for efficient large data retrieval.\n   * Implements the fast algorithm for iterating over large datasets without loading all data into memory at once.\n   *\n   * @template T - The type of items in the returned arrays (default is `unknown`).\n   *\n   * @param {ActionFetchListV2} options - parameters for executing the request.\n   *     - `method: string` - The name of the REST API method that returns a list of data (for example: `crm.item.list`, `tasks.task.list`)\n   *     - `params?: Omit<TypeCallParams, 'start'>` - Request parameters, excluding the `start` parameter,\n   *         since the method is designed to obtain all data in one call.\n   *         Note: Use `filter`, `order`, and `select` to control the selection.\n   *     - `idKey?: string` - The name of the field containing the unique identifier of the element.\n   *         Default is 'ID' (uppercase). Alternatively, it can be 'id' (lowercase).\n   *         or another field, depending on the REST API data structure.\n   *     - `customKeyForResult?: string` - A custom key indicating that the response REST API will be\n   *        grouped by this field.\n   *        Example: `items` to group a list of CRM items.\n   *    - `requestId?: string` - Unique request identifier for tracking. Used for query deduplication and debugging.\n   *\n   * @returns {AsyncGenerator<T[]>} An async generator that yields chunks of data as arrays of type `T`.\n   *     Each iteration returns the next page/batch of results until all data is fetched.\n   *\n   * @example\n   * import { EnumCrmEntityTypeId, Text } from '@bitrix24/b24jssdk'\n   *\n   * interface CrmItem { id: number, title: string }\n   * const sixMonthAgo = new Date()\n   * sixMonthAgo.setMonth((new Date()).getMonth() - 6)\n   * sixMonthAgo.setHours(0, 0, 0)\n   * const generator = b24.actions.v2.fetchList.make<CrmItem>({\n   *   method: 'crm.item.list',\n   *   params: {\n   *     entityTypeId: EnumCrmEntityTypeId.company,\n   *     filter: {\n   *       '=%title': 'A%',\n   *       '>=createdTime': Text.toB24Format(sixMonthAgo) // created at least 6 months ago\n   *     },\n   *     select: ['id', 'title']\n   *   },\n   *   idKey: 'id',\n   *   customKeyForResult: 'items',\n   *   requestId: 'list-123'\n   * })\n   *\n   * for await (const chunk of generator) {\n   *   // Process chunk (e.g., save to database, analyze, etc.)\n   *   console.log(`Processing ${chunk.length} items`)\n   * }\n   *\n   * @see {@link https://apidocs.bitrix24.com/settings/performance/huge-data.html Bitrix24: Fast algorithm for large data}\n   */\n  public override async* make<T = unknown>(options: ActionFetchListV2): AsyncGenerator<T[]> {\n    const batchSize = 50\n\n    const idKey = options?.idKey ?? 'ID'\n    const customKeyForResult = options?.customKeyForResult ?? null\n    const params = options?.params ?? {}\n\n    const moreIdKey = `>${idKey}`\n    const requestParams: TypeCallParams = {\n      ...params,\n      order: { ...(params['order'] || {}), [idKey]: 'ASC' },\n      filter: { ...(params['filter'] || {}), [moreIdKey]: 0 },\n      start: -1\n    }\n\n    let isContinue = true\n\n    do {\n      const response: AjaxResult<T> = await this._b24.actions.v2.call.make<T>({\n        method: options.method,\n        params: requestParams,\n        requestId: options.requestId\n      })\n\n      if (!response.isSuccess) {\n        this._logger.error('fetchListMethod', {\n          method: options.method,\n          requestId: options.requestId,\n          messages: response.getErrorMessages()\n        })\n        throw new SdkError({\n          code: 'JSSDK_CORE_B24_FETCH_LIST_METHOD_API_V2',\n          description: `API Error: ${response.getErrorMessages().join('; ')}`,\n          status: 500\n        })\n      }\n      const responseData = response.getData()\n      if (!responseData) {\n        isContinue = false\n        break\n      }\n\n      let resultData: T[] = []\n      if (null === customKeyForResult) {\n        resultData = responseData.result as T[]\n      } else {\n        resultData = (responseData.result as any)[customKeyForResult] as T[]\n      }\n\n      if (resultData.length === 0) {\n        isContinue = false\n        break\n      }\n\n      yield resultData\n\n      if (resultData.length < batchSize) {\n        isContinue = false\n        break\n      }\n\n      // Update the filter for the next iteration\n      const lastItem = resultData[resultData.length - 1] as Record<string, any>\n      if (\n        lastItem\n        && typeof lastItem[idKey] !== 'undefined'\n      ) {\n        requestParams.filter[moreIdKey] = Number.parseInt(lastItem[idKey])\n      } else {\n        isContinue = false\n        break\n      }\n    } while (isContinue)\n  }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAmBO,MAAM,oBAAoB,cAAA,CAAe;AAAA,EAnBhD;AAmBgD,IAAA,MAAA,CAAA,IAAA,EAAA,aAAA,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,EAoD9C,OAAuB,KAAkB,OAAA,EAAiD;AACxF,IAAA,MAAM,SAAA,GAAY,EAAA;AAElB,IAAA,MAAM,KAAA,GAAQ,SAAS,KAAA,IAAS,IAAA;AAChC,IAAA,MAAM,kBAAA,GAAqB,SAAS,kBAAA,IAAsB,IAAA;AAC1D,IAAA,MAAM,MAAA,GAAS,OAAA,EAAS,MAAA,IAAU,EAAC;AAEnC,IAAA,MAAM,SAAA,GAAY,IAAI,KAAK,CAAA,CAAA;AAC3B,IAAA,MAAM,aAAA,GAAgC;AAAA,MACpC,GAAG,MAAA;AAAA,MACH,KAAA,EAAO,EAAE,GAAI,MAAA,CAAO,OAAO,CAAA,IAAK,EAAC,EAAI,CAAC,KAAK,GAAG,KAAA,EAAM;AAAA,MACpD,MAAA,EAAQ,EAAE,GAAI,MAAA,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