UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

118 lines (114 loc) 3.73 kB
/** * @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 abstractB24 = require('../core/abstract-b24.cjs'); const v2 = require('../core/http/v2.cjs'); const v3 = require('../core/http/v3.cjs'); const auth = require('./auth.cjs'); const versionManager = require('../core/version-manager.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class B24Hook extends abstractB24.AbstractB24 { static { __name(this, "B24Hook"); } #authHookManager; // region Init //// constructor(b24HookParams, options) { super(); this.#authHookManager = new auth.AuthHookManager( b24HookParams ); const warningText = "The B24Hook object is intended exclusively for use on the server.\nA webhook contains a secret access key, which MUST NOT be used in client-side code (browser, mobile app)."; this._httpV2 = new v2.HttpV2(this.#authHookManager, this._getHttpOptions(), options?.restrictionParams); this._httpV2.setClientSideWarning(true, warningText); this._httpV3 = new v3.HttpV3(this.#authHookManager, this._getHttpOptions(), options?.restrictionParams); this._httpV3.setClientSideWarning(true, warningText); this._isInit = true; } // endregion //// get auth() { return this.#authHookManager; } // region Core //// /** * Disables warning about client-side query execution */ offClientSideWarning() { versionManager.versionManager.getAllApiVersions().forEach((version) => { this.getHttpClient(version).setClientSideWarning(false, ""); }); } // endregion //// // region Get //// /** * @inheritDoc */ getTargetOrigin() { this._ensureInitialized(); return this.#authHookManager.getTargetOrigin(); } /** * @inheritDoc */ getTargetOriginWithPath() { this._ensureInitialized(); return this.#authHookManager.getTargetOriginWithPath(); } // endregion //// // region Tools //// /** * Init Webhook from url * - ver2 `https://your_domain.bitrix24.com/rest/{id}/{webhook}` * - ver3 `https://your_domain.bitrix24.com/rest/api/{id}/{webhook}` * * @todo docs */ static fromWebhookUrl(url, options) { if (!url.trim()) { throw new Error("Webhook URL cannot be empty"); } let parsedUrl; try { parsedUrl = new URL(url.replace("/rest/api", "/rest")); } catch { throw new Error("Invalid webhook URL format"); } if (parsedUrl.protocol !== "https:") { throw new Error("Webhook requires HTTPS protocol"); } const pathParts = parsedUrl.pathname.split("/").filter(Boolean); const isValidFormat = ( // Format: /rest/{id}/{webhook} pathParts.length === 3 && pathParts[0] === "rest" || pathParts.length === 4 && pathParts[0] === "rest" && pathParts[1] === "api" ); if (!isValidFormat) { throw new Error("Webhook URL must follow format: /rest/<userId>/<secret> or /rest/api/<userId>/<secret>"); } const userIdIndex = pathParts[1] === "api" ? 2 : 1; const secretIndex = pathParts[1] === "api" ? 3 : 2; const userIdStr = pathParts[userIdIndex]; const secret = pathParts[secretIndex]; if (!/^\d+$/.test(userIdStr)) { throw new Error("User ID must be numeric in webhook URL"); } const userId = Number.parseInt(userIdStr, 10); return new B24Hook( { b24Url: parsedUrl.origin, userId, secret }, options ); } // endregion //// } exports.B24Hook = B24Hook; //# sourceMappingURL=b24.cjs.map