{"version":3,"file":"b24.mjs","sources":["../../../src/hook/b24.ts"],"sourcesContent":["import { SdkError } from '../core/sdk-error'\nimport type { AuthActions, B24HookParams } from '../types/auth'\nimport type { RestrictionParams } from '../types/limiters'\nimport type { TypeB24, ApiVersion } from '../types/b24'\nimport { AbstractB24 } from '../core/abstract-b24'\nimport { HttpV2 } from '../core/http/v2'\nimport { HttpV3 } from '../core/http/v3'\nimport { AuthHookManager } from './auth'\nimport { versionManager } from '../core/version-manager'\n\n/**\n * Server-side Bitrix24 client based on an inbound webhook URL.\n *\n * Use this class to make REST API calls from a backend service using a\n * pre-configured webhook. The webhook URL embeds a secret access key and\n * therefore **must never be used in browser or mobile code** — instantiating\n * `B24Hook` automatically enables a client-side warning for every HTTP call.\n *\n * @example\n * ```ts\n * const b24 = B24Hook.fromWebhookUrl('https://your_domain.bitrix24.com/rest/1/abc123xyz/')\n * const result = await b24.actions.v2.call.make({ method: 'user.current' })\n * ```\n *\n * @link https://bitrix24.github.io/b24jssdk/docs/hook/\n */\nexport class B24Hook extends AbstractB24 implements TypeB24 {\n  readonly #authHookManager: AuthHookManager\n\n  // region Init ////\n  constructor(\n    b24HookParams: B24HookParams,\n    options?: {\n      restrictionParams?: Partial<RestrictionParams>\n    }\n  ) {\n    super()\n\n    this.#authHookManager = new AuthHookManager(\n      b24HookParams\n    )\n\n    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).'\n\n    this._httpV2 = new HttpV2(this.#authHookManager, this._getHttpOptions(), options?.restrictionParams)\n    this._httpV2.setClientSideWarning(true, warningText)\n    this._httpV3 = new HttpV3(this.#authHookManager, this._getHttpOptions(), options?.restrictionParams)\n    this._httpV3.setClientSideWarning(true, warningText)\n\n    this._isInit = true\n  }\n  // endregion ////\n\n  override get auth(): AuthActions {\n    return this.#authHookManager\n  }\n\n  // region Core ////\n  /**\n   * Disables warning about client-side query execution\n   */\n  public offClientSideWarning(): void {\n    versionManager.getAllApiVersions().forEach((version) => {\n      this.getHttpClient(version).setClientSideWarning(false, '')\n    })\n  }\n\n  // endregion ////\n\n  // region Get ////\n  /**\n   * @inheritDoc\n   */\n  public override getTargetOrigin(): string {\n    this._ensureInitialized()\n    return this.#authHookManager.getTargetOrigin()\n  }\n\n  /**\n   * @inheritDoc\n   */\n  public override getTargetOriginWithPath(): Map<ApiVersion, string> {\n    this._ensureInitialized()\n    return this.#authHookManager.getTargetOriginWithPath()\n  }\n\n  // endregion ////\n\n  // region Tools ////\n  /**\n   * Creates a `B24Hook` instance from a webhook URL.\n   *\n   * Accepts both REST API v2 and v3 webhook formats:\n   *   - v2: `https://your_domain.bitrix24.com/rest/{userId}/{secret}`\n   *   - v3: `https://your_domain.bitrix24.com/rest/api/{userId}/{secret}`\n   *\n   * Validates that the URL uses HTTPS, has the correct path structure, and\n   * contains a numeric user ID. Throws a descriptive `Error` on any violation\n   * without echoing the URL (which contains the secret).\n   *\n   * @param url - Full webhook URL as shown in the Bitrix24 admin panel.\n   * @param options - Optional restriction parameters (rate limits, etc.).\n   * @returns A ready-to-use `B24Hook` instance.\n   * @throws {SdkError} If the URL is empty (`JSSDK_HOOK_URL_EMPTY`), unparseable\n   *     (`JSSDK_HOOK_URL_INVALID`), not HTTPS (`JSSDK_HOOK_URL_NOT_HTTPS`),\n   *     malformed (`JSSDK_HOOK_URL_MALFORMED`), or the userId segment is not\n   *     numeric (`JSSDK_HOOK_URL_USER_ID_NOT_NUMERIC`).\n   */\n  public static fromWebhookUrl(\n    url: string,\n    options?: { restrictionParams?: Partial<RestrictionParams> }\n  ): B24Hook {\n    if (!url.trim()) {\n      throw new SdkError({ code: 'JSSDK_HOOK_URL_EMPTY', description: 'Webhook URL cannot be empty', status: 0 })\n    }\n\n    let parsedUrl: URL\n\n    try {\n      parsedUrl = new URL(url.replace('/rest/api', '/rest'))\n    } catch {\n      // Don't echo the URL — it carries the webhook secret in its path (#43).\n      throw new SdkError({ code: 'JSSDK_HOOK_URL_INVALID', description: 'Invalid webhook URL format', status: 0 })\n    }\n\n    if (parsedUrl.protocol !== 'https:') {\n      throw new SdkError({ code: 'JSSDK_HOOK_URL_NOT_HTTPS', description: 'Webhook requires HTTPS protocol', status: 0 })\n    }\n\n    const pathParts = parsedUrl.pathname.split('/').filter(Boolean)\n    const isValidFormat = (\n      // Format: /rest/{id}/{webhook}\n      (pathParts.length === 3 && pathParts[0] === 'rest')\n      // Format: /rest/api/{id}/{webhook}\n      || (pathParts.length === 4 && pathParts[0] === 'rest' && pathParts[1] === 'api')\n    )\n\n    if (!isValidFormat) {\n      throw new SdkError({ code: 'JSSDK_HOOK_URL_MALFORMED', description: 'Webhook URL must follow format: /rest/<userId>/<secret> or /rest/api/<userId>/<secret>', status: 0 })\n    }\n\n    // Determine the position of userId and secret depending on the format\n    const userIdIndex = pathParts[1] === 'api' ? 2 : 1\n    const secretIndex = pathParts[1] === 'api' ? 3 : 2\n\n    const userIdStr = pathParts[userIdIndex]!\n    const secret = pathParts[secretIndex]!\n\n    if (!/^\\d+$/.test(userIdStr)) {\n      // Don't echo the segment — in a transposed URL it could be the secret (#43).\n      throw new SdkError({ code: 'JSSDK_HOOK_URL_USER_ID_NOT_NUMERIC', description: 'User ID must be numeric in webhook URL', status: 0 })\n    }\n    const userId = Number.parseInt(userIdStr, 10)\n\n    return new B24Hook(\n      {\n        b24Url: parsedUrl.origin,\n        userId,\n        secret\n      },\n      options\n    )\n  }\n  // endregion ////\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AA0BO,MAAM,gBAAgB,WAAA,CAA+B;AAAA,EA1B5D;AA0B4D,IAAA,MAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAAA;AAAA,EACjD,gBAAA;AAAA;AAAA,EAGT,WAAA,CACE,eACA,OAAA,EAGA;AACA,IAAA,KAAA,EAAM;AAEN,IAAA,IAAA,CAAK,mBAAmB,IAAI,eAAA;AAAA,MAC1B;AAAA,KACF;AAEA,IAAA,MAAM,WAAA,GAAc,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