@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
1 lines • 7.75 kB
Source Map (JSON)
{"version":3,"file":"fetch-list.mjs","sources":["../../../../../src/core/actions/v3/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 ActionFetchListV3 = ActionOptions & {\n method: string\n params?: Omit<TypeCallParams, 'pagination'>\n idKey?: string\n customKeyForResult: string\n requestId?: string\n limit?: number\n}\n\n/**\n * Calls a REST API list method and returns an async generator for efficient large data retrieval. `restApi:v3`\n *\n * @todo add docs\n */\nexport class FetchListV3 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 {ActionFetchListV3} 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, 'pagination'>` - Request parameters, excluding the `pagination` 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'. Alternatively, it can be 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 * - `limit?: number` - How many records to retrieve at a time. Default is `50`. Maximum is `1000`.\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 { Text } from '@bitrix24/b24jssdk'\n *\n * interface MainEventLogItem { id: number, userId: number }\n * const sixMonthAgo = new Date()\n * sixMonthAgo.setMonth((new Date()).getMonth() - 6)\n * sixMonthAgo.setHours(0, 0, 0)\n * const generator = b24.actions.v3.fetchList.make<MainEventLogItem>({\n * method: 'main.eventlog.list',\n * params: {\n * filter: [\n * ['timestampX', '>=', Text.toB24Format(sixMonthAgo)] // created at least 6 months ago\n * ],\n * select: ['id', 'userId']\n * },\n * idKey: 'id',\n * customKeyForResult: 'items',\n * requestId: 'eventlog-123',\n * limit: 60\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 public override async* make<T = unknown>(options: ActionFetchListV3): AsyncGenerator<T[]> {\n const batchSize = options?.limit ?? 50\n\n const idKey = options?.idKey ?? 'id'\n const customKeyForResult = options?.customKeyForResult ?? null\n const params = options?.params ?? {}\n\n const requestParams: TypeCallParams = {\n ...params,\n order: { ...(params['order'] || {}), [idKey]: 'ASC' },\n filter: [...(params['filter'] || [])],\n pagination: { page: 0, limit: batchSize }\n }\n\n let isContinue = true\n let nextId = 0\n do {\n const sendParams = { ...requestParams }\n sendParams.filter.push([idKey, '>', nextId])\n\n const response: AjaxResult<T> = await this._b24.actions.v3.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_V3',\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 const resultData: T[] = (responseData.result as any)[customKeyForResult] as T[]\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] !== '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