@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
231 lines (227 loc) • 7.49 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/
*/
'use strict';
const sdkError = require('../core/sdk-error.cjs');
const abstractB24 = require('../core/abstract-b24.cjs');
const v2 = require('../core/http/v2.cjs');
const v3 = require('../core/http/v3.cjs');
const frame = require('./frame.cjs');
const auth = require('./auth.cjs');
const parent = require('./parent.cjs');
const options = require('./options.cjs');
const dialog = require('./dialog.cjs');
const slider = require('./slider.cjs');
const placement = require('./placement.cjs');
const authKeepAlive = require('./auth-keep-alive.cjs');
const controller = require('./message/controller.cjs');
const commands = require('./message/commands.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class B24Frame extends abstractB24.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$1) {
super();
this.#restrictionParams = options$1?.restrictionParams;
this.#keepAuthFresh = options$1?.keepAuthFresh;
this._httpOptions = options$1?.httpOptions ?? null;
this.#appFrame = new frame.AppFrame(queryParams);
this.#messageManager = new controller.MessageManager(this.#appFrame);
this.#messageManager.subscribe();
this.#authManager = new auth.AuthManager(
this.#appFrame,
this.#messageManager
);
this.#parentManager = new parent.ParentManager(this.#messageManager);
this.#optionsManager = new options.OptionsManager(this.#messageManager);
this.#dialogManager = new dialog.DialogManager(this.#messageManager);
this.#sliderManager = new slider.SliderManager(
this.#appFrame,
this.#messageManager
);
this.#placementManager = new placement.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(commands.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 v2.HttpV2(this.#authManager, this._getHttpOptions(), this.#restrictionParams);
this._httpV3 = new v3.HttpV3(this.#authManager, this._getHttpOptions(), this.#restrictionParams);
this._isInit = true;
this.#startKeepAuthFresh();
if (this.#isFirstRun) {
return this.#messageManager.send(commands.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.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.SdkError({ code: "JSSDK_FRAME_INSTALL_ALREADY_FINISHED", description: "Application was previously installed. You cannot call installFinish", status: 0 }));
}
return this.#messageManager.send(commands.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 ////
}
exports.B24Frame = B24Frame;
//# sourceMappingURL=b24.cjs.map