@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
206 lines (202 loc) • 4.86 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 2.0.0
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
'use strict';
const abstractHelper = require('./abstract-helper.cjs');
const b24Helper = require('../types/b24-helper.cjs');
const type = require('../tools/type.cjs');
const text = require('../tools/text.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class OptionsManager extends abstractHelper.AbstractHelper {
static {
__name(this, "OptionsManager");
}
_data;
_type;
// region static ////
static getSupportTypes() {
return [
b24Helper.TypeOption.NotSet,
b24Helper.TypeOption.JsonArray,
b24Helper.TypeOption.JsonObject,
b24Helper.TypeOption.FloatVal,
b24Helper.TypeOption.IntegerVal,
b24Helper.TypeOption.BoolYN,
b24Helper.TypeOption.StringVal
];
}
static prepareArrayList(list) {
if (type.Type.isArray(list)) {
return list;
}
if (type.Type.isObject(list)) {
return Object.values(list);
}
return [];
}
// endregion ////
// region Init ////
constructor(b24, type) {
super(b24);
this._type = type;
this._data = /* @__PURE__ */ new Map();
}
get data() {
return this._data;
}
reset() {
this.data.clear();
}
/**
* @inheritDoc
*/
async initData(data) {
this.reset();
if (type.Type.isObject(data)) {
for (const [key, value] of Object.entries(data)) {
this.data.set(key, value);
}
}
}
// endregion ////
// region Get ////
getJsonArray(key, defValue = []) {
if (!this.data.has(key)) {
return defValue;
}
let data = this.data.get(key);
try {
data = JSON.parse(data);
if (!type.Type.isArray(data) && !type.Type.isObject(data)) {
data = defValue;
}
} catch (error) {
this.getLogger().error("Failed JSON parse", { error });
data = defValue;
}
return OptionsManager.prepareArrayList(data);
}
getJsonObject(key, defValue = {}) {
if (!this.data.has(key)) {
return defValue;
}
let data = this.data.get(key);
try {
data = JSON.parse(data);
} catch (error) {
this.getLogger().error("Failed JSON parse", { error });
data = defValue;
}
if (!type.Type.isObject(data)) {
data = defValue;
}
return data;
}
getFloat(key, defValue = 0) {
if (!this.data.has(key)) {
return defValue;
}
return text.Text.toNumber(this.data.get(key));
}
getInteger(key, defValue = 0) {
if (!this.data.has(key)) {
return defValue;
}
return text.Text.toInteger(this.data.get(key));
}
getBoolYN(key, defValue = true) {
if (!this.data.has(key)) {
return defValue;
}
return text.Text.toBoolean(this.data.get(key));
}
getBoolNY(key, defValue = false) {
if (!this.data.has(key)) {
return defValue;
}
return text.Text.toBoolean(this.data.get(key));
}
getString(key, defValue = "") {
if (!this.data.has(key)) {
return defValue;
}
return this.data.get(key).toString();
}
getDate(key, defValue = null) {
if (!this.data.has(key)) {
return defValue;
}
try {
const result = text.Text.toDateTime(this.data.get(key).toString());
if (result.isValid) {
return result;
} else {
return defValue;
}
} catch {
return defValue;
}
}
// endregion ////
// region Tools ////
encode(value) {
return JSON.stringify(value);
}
decode(data, defaultValue) {
try {
if (data.length > 0) {
return JSON.parse(data);
}
return defaultValue;
} catch (error) {
this.getLogger().error("Failed JSON parse", { error });
}
return defaultValue;
}
// endregion ////
// region Save ////
getMethodSave() {
switch (this._type) {
case "app":
return "app.option.set";
case "user":
return "user.option.set";
}
}
async save(options, optionsPull, requestId) {
const calls = [];
calls.push({
method: this.getMethodSave(),
params: {
options
}
});
if (type.Type.isObject(optionsPull)) {
calls.push({
method: "pull.application.event.add",
params: {
COMMAND: optionsPull?.command,
PARAMS: optionsPull?.params,
MODULE_ID: optionsPull?.moduleId
}
});
}
return this._b24.actions.v2.batch.make({
calls,
options: {
isHaltOnError: true,
returnAjaxResult: false,
requestId
}
});
}
// endregion ////
}
exports.OptionsManager = OptionsManager;
//# sourceMappingURL=options-manager.cjs.map