UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

180 lines (177 loc) • 5.08 kB
/** * @package @bitrix24/b24jssdk * @version 3.0.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { SdkError } from './sdk-error.mjs'; import { ApiVersion } from '../types/b24.mjs'; import { versionManager } from './version-manager.mjs'; import { warnOnNonPromiseLogger } from '../logger/assert-logger-shape.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"); } _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; } // 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) { warnOnNonPromiseLogger(logger, "B24 client"); 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 */ /** * Applies the parameters to every API version's client. * * `Promise.all`, not `allSettled`: a rejection here means the policy was not * applied, and swallowing it made the call resolve while the client kept * running the old configuration — or, when a malformed limiter block reached * a sub-limiter, while it ran on nothing at all. A caller must be able to see * that their policy did not take. (#479) * * All versions receive the same object, so a value read from one client is * written to all of them. */ async setRestrictionManagerParams(params) { const promises = versionManager.getAllApiVersions().map( (version) => this.getHttpClient(version).setRestrictionManagerParams(params) ); await Promise.all(promises); } /** * Axios config handed to both transports at construction. * * `null` by default: the transports already choose sensible defaults, and an * entry point that takes no such option from its caller has nothing to add. * The entry points that do accept one assign this before building the clients. * * Whatever lands here is filtered against the allowlist in `pickHttpOptions` * and then spread **after** the transport's own defaults, so a caller wins on * any key the allowlist accepts — including `adapter`, which is the reason * this channel is open to callers at all. A key outside it is dropped at * construction and its name logged. */ _httpOptions = null; /** * Settings for the http connection, read once per transport at construction. * * @protected */ _getHttpOptions() { return this._httpOptions; } /** * 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