@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
182 lines (178 loc) • 5.24 kB
JavaScript
/**
* @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/
*/
;
const sdkError = require('./sdk-error.cjs');
const b24 = require('../types/b24.cjs');
const versionManager = require('./version-manager.cjs');
const assertLoggerShape = require('../logger/assert-logger-shape.cjs');
const manager = require('./actions/manager.cjs');
const manager$1 = require('./tools/manager.cjs');
const loggerFactory = require('../logger/logger-factory.cjs');
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.LoggerFactory.createNullLogger();
this._actionsManager = new manager.ActionsManager(this);
this._toolsManager = new manager$1.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 b24.ApiVersion.v3:
if (null === this._httpV3) {
throw new sdkError.SdkError({
code: "JSSDK_CORE_B24_HTTP_V3_NOT_INIT",
description: `HttpV3 not init`,
status: 500
});
}
return this._httpV3;
case b24.ApiVersion.v2:
if (null === this._httpV2) {
throw new sdkError.SdkError({
code: "JSSDK_CORE_B24_HTTP_V2_NOT_INIT",
description: `HttpV2 not init`,
status: 500
});
}
return this._httpV2;
}
throw new sdkError.SdkError({
code: "JSSDK_CORE_B24_API_WRONG",
description: `Wrong Api Version ${version}`,
status: 500
});
}
/**
* @inheritDoc
*/
setHttpClient(version, client) {
switch (version) {
case b24.ApiVersion.v3:
this._httpV3 = client;
return;
case b24.ApiVersion.v2:
this._httpV2 = client;
return;
}
throw new sdkError.SdkError({
code: "JSSDK_CORE_B24_API_WRONG",
description: `Wrong Api Version ${version}`,
status: 500
});
}
setLogger(logger) {
assertLoggerShape.warnOnNonPromiseLogger(logger, "B24 client");
this._logger = logger;
this._actionsManager.setLogger(this._logger);
this._toolsManager.setLogger(this._logger);
versionManager.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.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.SdkError({
code: "JSSDK_CORE_B24_NOT_INIT",
description: `B24 not initialized`,
status: 500
});
}
}
// endregion ////
}
exports.AbstractB24 = AbstractB24;
//# sourceMappingURL=abstract-b24.cjs.map