UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

356 lines (353 loc) 11.1 kB
/** * @package @bitrix24/b24jssdk * @version 1.1.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { Type } from '../tools/type.mjs'; import { Result } from './result.mjs'; import { SdkError } from './sdk-error.mjs'; import { ApiVersion } from '../types/b24.mjs'; import { versionManager } from './version-manager.mjs'; import { ActionsManager } from './actions/manager.mjs'; import { ToolsManager } from './tools/manager.mjs'; import { LoggerFactory } from '../logger/logger-factory.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class AbstractB24 { static { __name(this, "AbstractB24"); } /** * Maximum length for batch response. * * @deprecated This const is deprecated and will be removed in version `2.0.0` * @removed 2.0.0 */ static batchSize = 50; _isInit = false; _httpV2 = null; _httpV3 = null; _logger; _actionsManager; _toolsManager; // region Init //// constructor() { this._isInit = false; this._logger = LoggerFactory.createNullLogger(); this._actionsManager = new ActionsManager(this); this._toolsManager = new ToolsManager(this); } /** * @inheritDoc */ get isInit() { return this._isInit; } async init() { this._isInit = true; return; } destroy() { } get actions() { this._ensureInitialized(); return this._actionsManager; } get tools() { this._ensureInitialized(); return this._toolsManager; } /** * Calls the Bitrix24 REST API method. * * @deprecated This method is deprecated and will be removed in version `2.0.0` * - for `restApi:v3` use {@link CallV3.make `b24.actions.v3.call.make(options)`} * - for `restApi:v2` use {@link CallV2.make `b24.actions.v2.call.make(options)`} * * @removed 2.0.0 * @memo Only for `restApi:v2` */ async callMethod(method, params, start) { LoggerFactory.forcedLog( this._logger, "warning", `The AbstractB24.callMethod() method is deprecated and will be removed in version 2.0.0. Use b24.actions.v3.call.make(options) or b24.actions.v2.call.make(options)`, { class: "AbstractB24", method: "callMethod", replacement: "b24.actions.v3.call.make(options) | b24.actions.v2.call.make(options)", removalVersion: "2.0.0", code: "JSSDK_CORE_DEPRECATED_METHOD" } ); params = { ...params }; if (!("start" in params && Number.isInteger(params.start)) && Number.isInteger(start)) { params.start = start; } return this._actionsManager.v2.call.make({ method, params }); } /** * Calls a Bitrix24 REST API list method to retrieve all data. * * @deprecated This method is deprecated and will be removed in version `2.0.0` * - for `restApi:v3` use {@link CallListV3.make `b24.actions.v3.callList.make(options)`} * - for `restApi:v2` use {@link CallListV2.make `b24.actions.v2.callList.make(options)`} * * @removed 2.0.0 * @memo Only for `restApi:v2` */ async callListMethod(method, params, progress, customKeyForResult) { LoggerFactory.forcedLog( this._logger, "warning", `The AbstractB24.callListMethod() method is deprecated and will be removed in version 2.0.0. Use b24.actions.v3.callList.make(options) or b24.actions.v2.callList.make(options)`, { class: "AbstractB24", method: "callListMethod", replacement: "b24.actions.v3.callList.make(options) | b24.actions.v2.callList.make(options)", removalVersion: "2.0.0", code: "JSSDK_CORE_DEPRECATED_METHOD" } ); const result = new Result(); if (Type.isFunction(progress) && null !== progress) { progress(0); } const sendParams = { ...params, start: 0 }; return this.actions.v2.call.make({ method, params: sendParams }).then(async (response) => { let list = []; let resultData; if (customKeyForResult) { resultData = response.getData().result[customKeyForResult]; } else { resultData = response.getData().result; } list = [...list, ...resultData]; if (response.isMore()) { let responseLoop = response; while (true) { responseLoop = await responseLoop.getNext(this.getHttpClient(ApiVersion.v2)); if (responseLoop === false) { break; } let resultData2 = void 0; if (customKeyForResult) { resultData2 = responseLoop.getData().result[customKeyForResult]; } else { resultData2 = responseLoop.getData().result; } list = [...list, ...resultData2]; if (progress) { const total = responseLoop.getTotal(); progress(total > 0 ? Math.round(100 * list.length / total) : 100); } } } result.setData(list); if (progress) { progress(100); } return result; }); } /** * Calls a Bitrix24 REST API list method and returns an async generator. * * @deprecated This method is deprecated and will be removed in version `2.0.0` * - for `restApi:v3` use {@link FetchListV3.make `b24.actions.v3.fetchList.make(options)`} * - for `restApi:v2` use {@link FetchListV2.make `b24.actions.v2.fetchList.make(options)`} * * @removed 2.0.0 * @memo Only for `restApi:v2` */ async *fetchListMethod(method, params, idKey, customKeyForResult) { LoggerFactory.forcedLog( this._logger, "warning", `The AbstractB24.fetchListMethod() method is deprecated and will be removed in version 2.0.0. Use b24.actions.v3.fetchList.make(options) or b24.actions.v2.fetchList.make(options)`, { class: "AbstractB24", method: "fetchListMethod", replacement: "b24.actions.v3.fetchList.make(options) | b24.actions.v2.fetchList.make(options)", removalVersion: "2.0.0", code: "JSSDK_CORE_DEPRECATED_METHOD" } ); const options = { method, params, idKey, customKeyForResult: customKeyForResult === null ? void 0 : customKeyForResult }; yield* this.actions.v2.fetchList.make(options); } /** * Executes a batch request to the Bitrix24 REST API. * * @deprecated This method is deprecated and will be removed in version `2.0.0` * - for `restApi:v3` use {@link BatchV3.make `b24.actions.v3.batch.make(options)`} * - for `restApi:v2` use {@link BatchV2.make `b24.actions.v2.batch.make(options)`} * * @removed 2.0.0 * @memo Only for `restApi:v2` */ async callBatch(calls, isHaltOnError, returnAjaxResult) { LoggerFactory.forcedLog( this._logger, "warning", `The AbstractB24.callBatch() method is deprecated and will be removed in version 2.0.0. Use b24.actions.v3.batch.make(options) or b24.actions.v2.batch.make(options)`, { class: "AbstractB24", method: "callBatch", replacement: "b24.actions.v3.batch.make(options) | b24.actions.v2.batch.make(options)", removalVersion: "2.0.0", code: "JSSDK_CORE_DEPRECATED_METHOD" } ); const callsTyped = calls; const options = { isHaltOnError: isHaltOnError ?? true, returnAjaxResult: returnAjaxResult ?? false }; return this.actions.v2.batch.make({ calls: callsTyped, options }); } /** * Executes a batch request to the Bitrix24 REST API with automatic chunking for any number of commands. * * @deprecated This method is deprecated and will be removed in version `2.0.0` * - for `restApi:v3` use {@link BatchByChunkV3.make `b24.actions.v3.batchByChunk.make(options)`} * - for `restApi:v2` use {@link BatchByChunkV2.make `b24.actions.v2.batchByChunk.make(options)`} * * @removed 2.0.0 * @memo Only for `restApi:v2` */ async callBatchByChunk(calls, isHaltOnError) { LoggerFactory.forcedLog( this._logger, "warning", `The AbstractB24.callBatchByChunk() method is deprecated and will be removed in version 2.0.0. Use b24.actions.v3.batchByChunk.make(options) or b24.actions.v2.batchByChunk.make(options)`, { class: "AbstractB24", method: "callBatchByChunk", replacement: "b24.actions.v3.batchByChunk.make(options) | b24.actions.v2.batchByChunk.make(options)", removalVersion: "2.0.0", code: "JSSDK_CORE_DEPRECATED_METHOD" } ); const callsTyped = calls; const options = { isHaltOnError, returnAjaxResult: false }; return this.actions.v2.batchByChunk.make({ calls: callsTyped, options }); } // endregion //// // region Tools //// /** * @inheritDoc */ getHttpClient(version) { this._ensureInitialized(); switch (version) { case ApiVersion.v3: if (null === this._httpV3) { throw new SdkError({ code: "JSSDK_CORE_B24_HTTP_V3_NOT_INIT", description: `HttpV3 not init`, status: 500 }); } return this._httpV3; case ApiVersion.v2: if (null === this._httpV2) { throw new SdkError({ code: "JSSDK_CORE_B24_HTTP_V2_NOT_INIT", description: `HttpV2 not init`, status: 500 }); } return this._httpV2; } throw new SdkError({ code: "JSSDK_CORE_B24_API_WRONG", description: `Wrong Api Version ${version}`, status: 500 }); } /** * @inheritDoc */ setHttpClient(version, client) { switch (version) { case ApiVersion.v3: this._httpV3 = client; return; case ApiVersion.v2: this._httpV2 = client; return; } throw new SdkError({ code: "JSSDK_CORE_B24_API_WRONG", description: `Wrong Api Version ${version}`, status: 500 }); } setLogger(logger) { this._logger = logger; this._actionsManager.setLogger(this._logger); this._toolsManager.setLogger(this._logger); versionManager.getAllApiVersions().forEach((version) => { this.getHttpClient(version).setLogger(this._logger); }); } getLogger() { return this._logger; } /** * @inheritDoc */ async setRestrictionManagerParams(params) { const promises = versionManager.getAllApiVersions().map( (version) => this.getHttpClient(version).setRestrictionManagerParams(params) ); await Promise.allSettled(promises); } /** * Returns settings for http connection * @protected */ _getHttpOptions() { return null; } /** * Generates an object not initialized error * @protected */ _ensureInitialized() { if (!this._isInit) { throw new SdkError({ code: "JSSDK_CORE_B24_NOT_INIT", description: `B24 not initialized`, status: 500 }); } } // endregion //// } export { AbstractB24 }; //# sourceMappingURL=abstract-b24.mjs.map