@bitrix24/b24jssdk
Version:
Bitrix24 REST API JavaScript SDK
117 lines (114 loc) • 3.28 kB
JavaScript
/**
* @package @bitrix24/b24jssdk
* @version 1.0.3
* @copyright (c) 2026 Bitrix24
* @license MIT
* @see https://github.com/bitrix24/b24jssdk
* @see https://bitrix24.github.io/b24jssdk/
*/
import { ApiVersion } from '../types/b24.mjs';
import { ParseRow } from './interaction/batch/parse-row.mjs';
import { SdkError } from './sdk-error.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
class VersionManager {
static {
__name(this, "VersionManager");
}
#supportMethods;
constructor() {
this.#supportMethods = [
"/batch",
// done
"/scopes",
// done
"/rest.scope.list",
// done
"/rest.documentation.openapi",
"/documentation",
/** @see /settings/configs/event_log.php */
"/main.eventlog.list",
// done
"/main.eventlog.get",
// done
"/main.eventlog.tail",
// done
"/tasks.task.chat.message.send",
"/tasks.task.access.get",
"/tasks.task.file.attach",
"/tasks.task.update",
// done
"/tasks.task.delete",
"/tasks.task.add",
"/tasks.task.get"
// done
// @todo When API.v3 arrives - change in AuthOAuthManager.initIsAdmin()
// '/profile' // waite
// '/main.message.get' // waite
// '/main.chat.update' // waite
// '/main.chat.list' // waite
// '/main.user.list' // waite
];
}
static create() {
return new VersionManager();
}
/**
* List of supported API versions
* The highest version must be first
*/
getAllApiVersions() {
return [ApiVersion.v3, ApiVersion.v2];
}
isSupport(version, method) {
switch (version) {
case ApiVersion.v3:
return this.#v3Support(method);
case ApiVersion.v2:
return true;
}
return false;
}
#v3Support(method) {
return this.#supportMethods.includes(`/${method}`);
}
/**
* Automatically obtain the API version
*/
automaticallyObtainApiVersion(method) {
const version = this.getAllApiVersions().find((version2) => versionManager.isSupport(version2, method));
if (!version) {
throw new SdkError({
code: "JSSDK_VERSION_MANAGER_NOT_DETECT_FOR_METHOD",
description: `No API version found that supports method ${method}.`,
status: 500
});
}
return version;
}
/**
* Automatically obtain the API version for Batch
*
* @todo test methods
* `[['crm.item.get', { entityTypeId: 3, id: 1 }]]`
* `[{ method: 'crm.item.get', params: { entityTypeId: 3, id: 1 } }]`
* `{ cmd1: { method: 'crm.item.get', params: { entityTypeId: 3, id: 1 } }, cmd2: ['crm.item.get', { entityTypeId: 2, id: 2 }] }`
*/
automaticallyObtainApiVersionForBatch(calls) {
const commands = ParseRow.getMethodsFromCommands(calls);
let isAllSupportV3 = true;
for (const [_, method] of commands.entries()) {
if (!this.isSupport(ApiVersion.v3, method)) {
isAllSupportV3 = false;
break;
}
}
if (isAllSupportV3) {
return ApiVersion.v3;
}
return ApiVersion.v2;
}
}
const versionManager = VersionManager.create();
export { versionManager };
//# sourceMappingURL=version-manager.mjs.map