{"version":3,"file":"version-manager.mjs","sources":["../../../src/core/version-manager.ts"],"sourcesContent":["import { ApiVersion } from '../types/b24'\nimport type {\n  BatchCommandsArrayUniversal,\n  BatchCommandsObjectUniversal,\n  BatchNamedCommandsUniversal\n} from '../types/http'\nimport { ParseRow } from './interaction/batch/parse-row'\nimport { SdkError } from './sdk-error'\n\n/**\n * Decides which REST API version (v2 or v3) a method is routed through:\n * v3 only for methods on the published allowlist (`#supportMethods`); everything\n * else falls back to v2.\n */\nclass VersionManager {\n  #supportMethods: string[]\n\n  constructor() {\n    /**\n     * Mirrors the methods published under Bitrix24 REST v3 (per the module pages\n     * of apidocs rest-v3). Absences are intentional — a module exposes only what\n     * is published (e.g. `timeman` is read-only in v3: no `record.get` / `add` /\n     * `update`). Entries WITHOUT a `// done` marker are routed but not yet verified\n     * end-to-end against a live portal in this SDK.\n     *\n     * Source of truth: https://{portal}/rest/api/{user_id}/{webhook}/documentation\n     * @see https://apidocs.bitrix24.com/api-reference/rest-v3/index.html#openapi\n     */\n    this.#supportMethods = [\n      // --- infrastructure ---\n      '/batch', // done\n      '/scopes', // done\n      '/rest.scope.list', // done\n      '/rest.documentation.openapi',\n      '/documentation',\n\n      // --- main ---\n      /** @see /settings/configs/event_log.php */\n      '/main.eventlog.list', // done\n      '/main.eventlog.get', // done\n      '/main.eventlog.tail', // done\n      '/main.eventlog.field.list',\n      '/main.eventlog.field.get',\n\n      // --- mail (rest-v3) ---\n      '/mail.mailbox.list',\n      '/mail.mailbox.get',\n      '/mail.mailbox.senders',\n      '/mail.mailbox.field.list',\n      '/mail.mailbox.field.get',\n      '/mail.message.list',\n      '/mail.message.get',\n      '/mail.message.thread',\n      '/mail.message.send',\n      '/mail.message.reply',\n      '/mail.message.forward',\n      '/mail.message.movetofolder',\n      '/mail.message.createcrmactivity',\n      '/mail.message.removecrmactivity',\n      '/mail.message.createtask',\n      '/mail.message.createcalendarevent',\n      '/mail.message.createchat',\n      '/mail.message.createfeedpost',\n      '/mail.message.field.list',\n      '/mail.message.field.get',\n      '/mail.recipient.listcontacts',\n      '/mail.recipient.listemployees',\n      '/mail.recipient.field.list',\n      '/mail.recipient.field.get',\n\n      // --- humanresources (rest-v3) ---\n      '/humanresources.node.add',\n      '/humanresources.node.edit',\n      '/humanresources.node.get',\n      '/humanresources.node.list',\n      '/humanresources.node.search',\n      '/humanresources.node.children',\n      '/humanresources.node.count',\n      '/humanresources.node.move',\n      '/humanresources.node.field.list',\n      '/humanresources.node.field.get',\n      '/humanresources.node.member.add',\n      '/humanresources.node.member.set',\n      '/humanresources.node.member.move',\n      '/humanresources.node.member.remove',\n      '/humanresources.node.communication.edit',\n      '/humanresources.node.communication.list',\n      '/humanresources.employee.search',\n      '/humanresources.employee.subordinates',\n      '/humanresources.employee.count',\n      '/humanresources.employee.multidepartment',\n      '/humanresources.employee.field.list',\n      '/humanresources.employee.field.get',\n\n      // --- tasks (rest-v3) ---\n      '/tasks.task.add',\n      '/tasks.task.get', // done\n      '/tasks.task.update', // done\n      '/tasks.task.delete',\n      '/tasks.task.access.get',\n      '/tasks.task.file.attach',\n      '/tasks.task.chat.message.send',\n      '/tasks.task.result.add',\n      '/tasks.task.result.addfromchatmessage',\n      '/tasks.task.result.update',\n      '/tasks.task.result.list',\n      '/tasks.task.result.delete',\n      '/tasks.task.field.list',\n      '/tasks.task.field.get',\n      '/tasks.task.access.field.list',\n      '/tasks.task.access.field.get',\n      '/tasks.task.file.field.list',\n      '/tasks.task.file.field.get',\n      '/tasks.task.chat.message.field.list',\n      '/tasks.task.chat.message.field.get',\n\n      // --- timeman (rest-v3) — read-only in v3 (no record.get / add / update / delete) ---\n      '/timeman.record.list',\n      '/timeman.record.field.list',\n      '/timeman.record.field.get'\n\n      // Cross-module methods are cross-referenced from the rest-v3 pages above but\n      // belong to modules not actualized here — add them when those modules land:\n      //   user.get                                          — user module (seen on humanresources + timeman)\n      //   im.message.update / im.message.delete / im.dialog.messages.get  — im module (seen on tasks)\n      //   disk.storage.uploadfile / disk.folder.uploadfile /\n      //   disk.storage.getchildren / disk.folder.getchildren            — disk module (seen on tasks)\n      //\n      // @todo When API.v3 arrives - change in AuthOAuthManager.initIsAdmin()\n      // '/profile' // wait\n      // '/main.message.get' // wait\n      // '/main.chat.update' // wait\n      // '/main.chat.list' // wait\n      // '/main.user.list' // wait\n    ]\n  }\n\n  static create(): VersionManager {\n    return new VersionManager()\n  }\n\n  /**\n   * List of supported API versions\n   * The highest version must be first\n   */\n  public getAllApiVersions(): ApiVersion[] {\n    return [ApiVersion.v3, ApiVersion.v2]\n  }\n\n  public isSupport(version: ApiVersion, method: string): boolean {\n    switch (version) {\n      case ApiVersion.v3:\n        return this.#v3Support(method)\n      case ApiVersion.v2:\n        return true\n    }\n\n    return false\n  }\n\n  #v3Support(method: string): boolean {\n    return this.#supportMethods.includes(`/${method}`)\n  }\n\n  /**\n   * Automatically obtain the API version\n   */\n  public automaticallyObtainApiVersion(method: string): ApiVersion {\n    const version = this.getAllApiVersions().find(version => versionManager.isSupport(version, method))\n    if (!version) {\n      throw new SdkError({\n        code: 'JSSDK_VERSION_MANAGER_NOT_DETECT_FOR_METHOD',\n        description: `No API version found that supports method ${method}.`,\n        status: 500\n      })\n    }\n\n    return version\n  }\n\n  /**\n   * Automatically obtain the API version for Batch\n   *\n   * @todo test methods\n   *   `[['crm.item.get', { entityTypeId: 3, id: 1 }]]`\n   *   `[{ method: 'crm.item.get', params: { entityTypeId: 3, id: 1 } }]`\n   *   `{ cmd1: { method: 'crm.item.get', params: { entityTypeId: 3, id: 1 } }, cmd2: ['crm.item.get', { entityTypeId: 2, id: 2 }] }`\n   */\n  public automaticallyObtainApiVersionForBatch(\n    calls: BatchCommandsArrayUniversal | BatchCommandsObjectUniversal | BatchNamedCommandsUniversal\n  ): ApiVersion {\n    const commands = ParseRow.getMethodsFromCommands(calls)\n\n    let isAllSupportV3 = true\n    for (const [_, method] of commands.entries()) {\n      if (!this.isSupport(ApiVersion.v3, method)) {\n        isAllSupportV3 = false\n        break\n      }\n    }\n\n    if (isAllSupportV3) {\n      return ApiVersion.v3\n    }\n    return ApiVersion.v2\n  }\n}\n\nexport const versionManager = VersionManager.create()\n"],"names":["version"],"mappings":";;;;;;;;;;;;;;AAcA,MAAM,cAAA,CAAe;AAAA,EAdrB;AAcqB,IAAA,MAAA,CAAA,IAAA,EAAA,gBAAA,CAAA;AAAA;AAAA,EACnB,eAAA;AAAA,EAEA,WAAA,GAAc;AAWZ,IAAA,IAAA,CAAK,eAAA,GAAkB;AAAA;AAAA,MAErB,QAAA;AAAA;AAAA,MACA,SAAA;AAAA;AAAA,MACA,kBAAA;AAAA;AAAA,MACA,6BAAA;AAAA,MACA,gBAAA;AAAA;AAAA;AAAA,MAIA,qBAAA;AAAA;AAAA,MACA,oBAAA;AAAA;AAAA,MACA,qBAAA;AAAA;AAAA,MACA,2BAAA;AAAA,MACA,0BAAA;AAAA;AAAA,MAGA,oBAAA;AAAA,MACA,mBAAA;AAAA,MACA,uBAAA;AAAA,MACA,0BAAA;AAAA,MACA,yBAAA;AAAA,MACA,oBAAA;AAAA,MACA,mBAAA;AAAA,MACA,sBAAA;AAAA,MACA,oBAAA;AAAA,MACA,qBAAA;AAAA,MACA,uBAAA;AAAA,MACA,4BAAA;AAAA,MACA,iCAAA;AAAA,MACA,iCAAA;AAAA,MACA,0BAAA;AAAA,MACA,mCAAA;AAAA,MACA,0BAAA;AAAA,MACA,8BAAA;AAAA,MACA,0BAAA;AAAA,MACA,yBAAA;AAAA,MACA,8BAAA;AAAA,MACA,+BAAA;AAAA,MACA,4BAAA;AAAA,MACA,2BAAA;AAAA;AAAA,MAGA,0BAAA;AAAA,MACA,2BAAA;AAAA,MACA,0BAAA;AAAA,MACA,2BAAA;AAAA,MACA,6BAAA;AAAA,MACA,+BAAA;AAAA,MACA,4BAAA;AAAA,MACA,2BAAA;AAAA,MACA,iCAAA;AAAA,MACA,gCAAA;AAAA,MACA,iCAAA;AAAA,MACA,iCAAA;AAAA,MACA,kCAAA;AAAA,MAC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