@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
229 lines (226 loc) • 7.38 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/
*/
import { SdkError } from '../core/sdk-error.mjs';
import { AbstractB24 } from '../core/abstract-b24.mjs';
import { HttpV2 } from '../core/http/v2.mjs';
import { HttpV3 } from '../core/http/v3.mjs';
import { AppFrame } from './frame.mjs';
import { AuthManager } from './auth.mjs';
import { ParentManager } from './parent.mjs';
import { OptionsManager } from './options.mjs';
import { DialogManager } from './dialog.mjs';
import { SliderManager } from './slider.mjs';
import { PlacementManager } from './placement.mjs';
import { AuthKeepAlive } from './auth-keep-alive.mjs';
import { MessageManager } from './message/controller.mjs';
import { MessageCommands } from './message/commands.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class B24Frame extends AbstractB24 {
static {
__name(this, "B24Frame");
}
#isInstallMode = false;
#isFirstRun = false;
#appFrame;
#messageManager;
#authManager;
#parentManager;
#optionsManager;
#dialogManager;
#sliderManager;
#placementManager;
#restrictionParams;
#keepAuthFresh;
#authKeepAlive = null;
// region Init ////
constructor(queryParams, options) {
super();
this.#restrictionParams = options?.restrictionParams;
this.#keepAuthFresh = options?.keepAuthFresh;
this._httpOptions = options?.httpOptions ?? null;
this.#appFrame = new AppFrame(queryParams);
this.#messageManager = new MessageManager(this.#appFrame);
this.#messageManager.subscribe();
this.#authManager = new AuthManager(
this.#appFrame,
this.#messageManager
);
this.#parentManager = new ParentManager(this.#messageManager);
this.#optionsManager = new OptionsManager(this.#messageManager);
this.#dialogManager = new DialogManager(this.#messageManager);
this.#sliderManager = new SliderManager(
this.#appFrame,
this.#messageManager
);
this.#placementManager = new PlacementManager(this.#messageManager);
this._isInit = false;
}
setLogger(logger) {
super.setLogger(logger);
this.#messageManager.setLogger(this.getLogger());
}
get isFirstRun() {
this._ensureInitialized();
return this.#isFirstRun;
}
get isInstallMode() {
this._ensureInitialized();
return this.#isInstallMode;
}
get parent() {
this._ensureInitialized();
return this.#parentManager;
}
get auth() {
this._ensureInitialized();
return this.#authManager;
}
get slider() {
this._ensureInitialized();
return this.#sliderManager;
}
get placement() {
this._ensureInitialized();
return this.#placementManager;
}
get options() {
this._ensureInitialized();
return this.#optionsManager;
}
get dialog() {
this._ensureInitialized();
return this.#dialogManager;
}
async init() {
const data = await this.#messageManager.send(MessageCommands.getInitData, {});
this.getLogger().debug("init data", {
PLACEMENT: data.PLACEMENT,
LANG: data.LANG,
INSTALL: data.INSTALL,
IS_ADMIN: data.IS_ADMIN,
FIRST_RUN: data.FIRST_RUN
}).catch(() => {
});
this.#appFrame.initData(data);
this.#authManager.initData(data);
this.#placementManager.initData(data);
this.#optionsManager.initData(data);
this.#isInstallMode = data.INSTALL;
this.#isFirstRun = data.FIRST_RUN;
this._httpV2 = new HttpV2(this.#authManager, this._getHttpOptions(), this.#restrictionParams);
this._httpV3 = new HttpV3(this.#authManager, this._getHttpOptions(), this.#restrictionParams);
this._isInit = true;
this.#startKeepAuthFresh();
if (this.#isFirstRun) {
return this.#messageManager.send(MessageCommands.setInstall, { install: true });
}
return Promise.resolve();
}
/**
* Destructor.
* Removes an event subscription
*/
destroy() {
this.#authKeepAlive?.stop();
this.#authKeepAlive = null;
this.#messageManager.unsubscribe();
super.destroy();
}
/**
* Starts the token keep-alive when the caller asked for it (#532).
*
* Called from `init()` — before that there is no token to keep alive — and
* torn down in `destroy()`.
*/
#startKeepAuthFresh() {
if (!this.#keepAuthFresh || this.#authKeepAlive !== null) {
return;
}
this.#authKeepAlive = new AuthKeepAlive(
this.#authManager,
() => this.getLogger(),
this.#keepAuthFresh === true ? void 0 : this.#keepAuthFresh
);
this.#authKeepAlive.start();
}
// endregion ////
// region Core ////
/**
* Signals that the installer or application setup has finished running.
*
* Until this lands the portal treats the application as half-installed and
* delivers no events to it, silently — so the install flow must always reach
* it. The portal reloads the page in response, which makes it the last
* statement of that flow.
*
* It has **two** failure modes, and they carry different codes:
*
* - outside install mode — `JSSDK_FRAME_INSTALL_ALREADY_FINISHED`, rejected
* locally with `status: 0`, nothing sent to the parent window. Guard with
* `isInstallMode` rather than catching it.
* - before `init()` has resolved — `JSSDK_CORE_B24_NOT_INIT`, thrown by the
* `isInstallMode` getter this method reads. A caller discriminating on the
* code above falls straight through it. Reachable only by constructing
* `B24Frame` directly; `initializeB24Frame()` awaits `init()` for you.
*
* Both are pinned in `test/integration/frame/install-finish-guard.unit.spec.ts`,
* because the first one's code and message are quoted verbatim by three
* documentation pages (#379).
*
* @example
* if ($b24.isInstallMode) {
* // provision fields, register placements, seed options…
* await $b24.installFinish()
* }
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/system-functions/bx24-install-finish.html
*/
async installFinish() {
if (!this.isInstallMode) {
return Promise.reject(new SdkError({ code: "JSSDK_FRAME_INSTALL_ALREADY_FINISHED", description: "Application was previously installed. You cannot call installFinish", status: 0 }));
}
return this.#messageManager.send(MessageCommands.setInstallFinish, {});
}
// endregion ////
// region Get ////
/**
* @inheritDoc
*/
getTargetOrigin() {
this._ensureInitialized();
return this.#authManager.getTargetOrigin();
}
/**
* @inheritDoc
*/
getTargetOriginWithPath() {
this._ensureInitialized();
return this.#authManager.getTargetOriginWithPath();
}
/**
* Returns the sid of the application relative to the parent window like this `9c33468728e1d2c8c97562475edfd96`
*/
getAppSid() {
this._ensureInitialized();
return this.#appFrame.getAppSid();
}
/**
* Returns the localization of the B24 interface
*
* @link https://apidocs.bitrix24.com/sdk/bx24-js-sdk/additional-functions/bx24-get-lang.html
*/
getLang() {
this._ensureInitialized();
return this.#appFrame.getLang();
}
// endregion ////
}
export { B24Frame };
//# sourceMappingURL=b24.mjs.map