{"version":3,"file":"call-list.cjs","sources":["../../../../../src/core/actions/v3/call-list.ts"],"sourcesContent":["import type { ActionOptions } from '../abstract-action'\nimport type { TypeCallParams } from '../../../types/http'\nimport { AbstractAction } from '../abstract-action'\nimport { Result } from '../../result'\nimport { keysetPaginate, KeysetPaginationError } from './_keyset-paginate'\n\nexport type ActionCallListV3 = ActionOptions & {\n  method: string\n  params?: Omit<TypeCallParams, 'pagination' | 'order'>\n  idKey?: string\n  cursorIdKey?: string\n  customKeyForResult: string\n  requestId?: string\n  limit?: number\n}\n\n/**\n * Fast data retrieval without counting the total number of records. `restApi:v3`\n *\n * @todo add docs\n */\nexport class CallListV3 extends AbstractAction {\n  /**\n   * Fast data retrieval without counting the total number of records.\n   *\n   * @template T - The type of the elements of the returned array (default is `unknown`).\n   *\n   * @param {ActionCallListV3} 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' | 'order'>` - Request parameters, excluding the `pagination` and `order` parameters,\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 id field as it appears in each RESPONSE item; its value\n   *         drives the cursor. Default is 'id'. Set it to match the id field the method returns.\n   *     - `cursorIdKey?: string` - The field name used in the REQUEST for `order` and the\n   *         `[field, '>', n]` page filter. Defaults to `idKey`. Set it only when the sortable /\n   *         filterable field name differs from the response field name (e.g. an uppercase request\n   *         field but a lowercase response id): pass `idKey: 'id', cursorIdKey: 'ID'`.\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 {Promise<Result<T[]>>} A promise that resolves to the result of an REST API call.\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 response = await b24.actions.v3.callList.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   * if (!response.isSuccess) {\n   *   throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n   * }\n   * const list = response.getData()\n   * console.log(`Result: ${list?.length}`) // Number of items received\n   */\n  public override async make<T = unknown>(options: ActionCallListV3): Promise<Result<T[]>> {\n    const batchSize = options?.limit ?? 50\n    const result: Result<T[]> = new Result()\n\n    const idKey = options?.idKey ?? 'id'\n    const cursorIdKey = options?.cursorIdKey ?? idKey\n    const customKeyForResult = options?.customKeyForResult ?? null\n    const params = options?.params ?? {}\n\n    // Warn and strip user-provided `order` — cursor pagination requires ordering by cursorIdKey only\n    if ('order' in params && params['order']) {\n      this._logger.warning('callList.make: user-provided `order` parameter is ignored because cursor-based pagination requires ordering by cursorIdKey. Use `filter` to narrow results instead.')\n    }\n\n    const { order: _ignoredOrder, ...restParams } = params as TypeCallParams\n    const requestParams: TypeCallParams = {\n      ...restParams,\n      order: { [cursorIdKey]: 'ASC' },\n      filter: [...(params['filter'] || [])],\n      pagination: { page: 0, limit: batchSize }\n    }\n\n    const allItems: T[] = []\n    try {\n      for await (const page of keysetPaginate<T>(this._b24, this._logger, {\n        method: options.method,\n        requestId: options.requestId,\n        customKeyForResult,\n        initialCursor: 0,\n        // Emulated keyset: append the `[cursorIdKey, '>', cursor]` page filter.\n        buildParams: cursor => ({ ...requestParams, filter: [...requestParams.filter, [cursorIdKey, '>', cursor]] }),\n        // Advance by the numeric id read from the last item via `idKey`. A\n        // non-numeric value (almost always an `idKey` that doesn't match the\n        // response field — e.g. sorting by `ID` while the response carries a\n        // lowercase `id`) stops the walk instead of silently truncating.\n        readNextCursor: (lastItem) => {\n          const value = Number.parseInt(lastItem[idKey], 10)\n          return Number.isFinite(value) ? value : null\n        },\n        noCursorWarning: `callList.make: pagination stops here — no numeric id could be read from the returned items via idKey \"${idKey}\". Make sure idKey matches the id field in the response; if the sortable field name differs from it, also set cursorIdKey (e.g. idKey: 'id', cursorIdKey: 'ID').`,\n        errorLabel: 'callFastListMethod'\n      })) {\n        for (const item of page) {\n          allItems.push(item)\n        }\n      }\n    } catch (error) {\n      if (error instanceof KeysetPaginationError) {\n        for (const [index, err] of error.errors) {\n          result.addError(err, index)\n        }\n      } else {\n        throw error\n      }\n    }\n\n    return result.setData(allItems)\n  }\n}\n"],"names":["AbstractAction","result","Result","keysetPaginate","KeysetPaginationError"],"mappings":";;;;;;;;;;;;;;;;AAqBO,MAAM,mBAAmBA,6BAAA,CAAe;AAAA,EArB/C;AAqB+C,IAAA,MAAA,CAAA,IAAA,EAAA,YAAA,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,EAmD7C,MAAsB,KAAkB,OAAA,EAAiD;AACvF,IAAA,MAAM,SAAA,GAAY,SAAS,KAAA,IAAS,EAAA;AACpC,IAAA,MAAMC,QAAA,GAAsB,IAAIC,aAAA,EAAO;AAEvC,IAAA,MAAM,KAAA,GAAQ,SAAS,KAAA,IAAS,IAAA;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