UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

1 lines • 6.57 kB
{"version":3,"file":"batch.mjs","sources":["../../../../../src/core/actions/v2/batch.ts"],"sourcesContent":["import type {\n BatchResultByIndex,\n BatchResultByIndexDetailed,\n BatchResultByName,\n BatchResultByNameDetailed,\n CallBatchResult,\n IB24BatchOptions\n} 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'\n\nexport type ActionBatchV2 = {\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:v2`\n * Allows you to execute multiple requests in a single API call, significantly improving performance.\n *\n * Sends up to 50 commands in a single v2 batch HTTP call and returns their results together.\n * Supports array, object, and named-command formats. Unlike `BatchByChunkV2`, it does not\n * split large command sets automatically — callers must ensure the command count stays within\n * the 50-command limit.\n */\nexport class BatchV2 extends AbstractBatch {\n /**\n * Executes a batch request to the Bitrix24 REST API: up to 50 commands in one\n * HTTP call, in array, object or named-command form. `restApi:v2`\n *\n * **The argument reference, the three `calls` formats, the options table and\n * worked examples live on the [batch page](https://bitrix24.github.io/b24jssdk/docs/working-with-the-rest-api/batch-rest-api-ver2/).**\n * Deliberately not repeated here: that copy is compiled on every CI run and\n * this one would not be — no pass type-checks a JSDoc `@example` (#420).\n *\n * What matters while editing this file:\n * - **50 commands maximum.** This action does not split; `BatchByChunkV2`\n * is the one that does.\n * - **`getData()` has no `result` envelope here.** It returns the keyed map\n * or array directly — only `call.make` returns `{ result, time }` (#425).\n * - **Flags live in `options`.** At the top level they are dropped, which is\n * why `_warnMisplacedOptions` runs first (#426).\n *\n * @template T - The data type returned by batch query commands (default `unknown`)\n * @param {ActionBatchV2} options - `calls` plus an optional `options` bag; see\n * {@link ActionBatchV2} and {@link IB24BatchOptions} for the members.\n * @returns {Promise<CallBatchResult<T>>} results in the shape of the input:\n * an array for array input, an object keyed by command name for named input.\n * With `options.returnAjaxResult` each entry is an `AjaxResult` rather than\n * the raw payload.\n *\n * @example\n * import type { AjaxResult } from '@bitrix24/b24jssdk'\n *\n * interface Contact { id: number, name: string }\n * const response = await b24.actions.v2.batch.make<{ item: Contact }>({\n * calls: {\n * first: ['crm.item.get', { entityTypeId: 3, id: 1 }],\n * second: ['crm.item.get', { entityTypeId: 3, id: 2 }]\n * },\n * options: { isHaltOnError: false, returnAjaxResult: true, requestId: 'batch-123' }\n * })\n * if (!response.isSuccess) {\n * throw new Error(`Problem: ${response.getErrorMessages().join('; ')}`)\n * }\n * const data = response.getData()! as Record<string, AjaxResult<{ item: Contact }>>\n * console.log(data['first']!.getData()!.result.item)\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 /**\n * Overloads, not decoration. The shape of a batch answer is decided by the\n * arguments — a named record of commands answers by name, an array answers by\n * index, and `returnAjaxResult` decides whether each entry is the payload or\n * the `AjaxResult` wrapping it. None of that is visible on the returned value,\n * so without these a caller has the union and no way to narrow it, and has to\n * cast at exactly the point the types are worth having (#518).\n *\n * `T` is **one command's payload** in every overload.\n *\n * The last signature keeps a dynamic `returnAjaxResult` working: when the flag\n * is a `boolean` the compiler cannot read as a literal, the union is still the\n * honest answer.\n */\n public async make<T = unknown>(options: {\n calls: BatchNamedCommandsUniversal\n options: IB24BatchOptions & { returnAjaxResult: true }\n }): Promise<BatchResultByNameDetailed<T>>\n\n public async make<T = unknown>(options: {\n calls: BatchNamedCommandsUniversal\n options?: IB24BatchOptions & { returnAjaxResult?: false }\n }): Promise<BatchResultByName<T>>\n\n public async make<T = unknown>(options: {\n calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal\n options: IB24BatchOptions & { returnAjaxResult: true }\n }): Promise<BatchResultByIndexDetailed<T>>\n\n public async make<T = unknown>(options: {\n calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal\n options?: IB24BatchOptions & { returnAjaxResult?: false }\n }): Promise<BatchResultByIndex<T>>\n\n public async make<T = unknown>(options: ActionBatchV2): Promise<CallBatchResult<T>>\n\n public async make<T = unknown>(options: ActionBatchV2): Promise<CallBatchResult<T>> {\n this._warnMisplacedOptions(\n options,\n ['isHaltOnError', 'returnAjaxResult', 'requestId'],\n 'options'\n )\n\n const opts = {\n ...options.options,\n apiVersion: ApiVersion.v2\n }\n\n const response = await this._b24.getHttpClient(ApiVersion.v2).batch<T>(options.calls, opts)\n\n return this._processBatchResponse<T>(response, options.calls, opts)\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AA8BO,MAAM,gBAAgB,aAAA,CAAc;AAAA,EA9B3C;AA8B2C,IAAA,MAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAAA;AAAA,EAoFzC,MAAa,KAAkB,OAAA,EAAqD;AAClF,IAAA,IAAA,CAAK,qBAAA;AAAA,MACH,OAAA;AAAA,MACA,CAAC,eAAA,EAAiB,kBAAA,EAAoB,WAAW,CAAA;AAAA,MACjD;AAAA,KACF;AAEA,IAAA,MAAM,IAAA,GAAO;AAAA,MACX,GAAG,OAAA,CAAQ,OAAA;AAAA,MACX,YAAY,UAAA,CAAW;AAAA,KACzB;AAEA,IAAA,MAAM,QAAA,GAAW,MAAM,IAAA,CAAK,IAAA,CAAK,aAAA,CAAc,UAAA,CAAW,EAAE,CAAA,CAAE,KAAA,CAAS,OAAA,CAAQ,KAAA,EAAO,IAAI,CAAA;AAE1F,IAAA,OAAO,IAAA,CAAK,qBAAA,CAAyB,QAAA,EAAU,OAAA,CAAQ,OAAO,IAAI,CAAA;AAAA,EACpE;AACF;;;;"}