UNPKG

@itwin/imodels-access-frontend

Version:

Interoperability package between iModels API and iTwin.js library for frontend.

105 lines 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FrontendIModelsAccess = void 0; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const core_bentley_1 = require("@itwin/core-bentley"); const core_frontend_1 = require("@itwin/core-frontend"); const imodels_access_common_1 = require("@itwin/imodels-access-common"); const imodels_client_management_1 = require("@itwin/imodels-client-management"); class FrontendIModelsAccess { _emptyChangeset = { index: imodels_access_common_1.Constants.ChangeSet0.index, id: imodels_access_common_1.Constants.ChangeSet0.id, }; _iModelsClient; constructor(iModelsClient) { this._iModelsClient = iModelsClient ?? new imodels_client_management_1.IModelsClient(); } async getChangesetFromId(arg) { const getSingleChangesetParams = { ...this.getIModelScopedOperationParams(arg), changesetId: arg.changeSetId, }; const changeset = await (0, imodels_access_common_1.handleAPIErrors)(async () => this._iModelsClient.changesets.getSingle(getSingleChangesetParams)); if (!changeset) core_bentley_1.ITwinError.throwError({ iTwinErrorId: { key: imodels_client_management_1.IModelsErrorCode.ChangesetNotFound, scope: imodels_client_management_1.IModelsErrorScope, }, message: `Changeset ${arg.changeSetId} not found`, }); return { index: changeset.index, id: changeset.id }; } async getLatestChangeset(arg) { const latestChangeset = await (0, imodels_access_common_1.getLatestMinimalChangesetIfExists)(this._iModelsClient, this.getIModelScopedOperationParams(arg)); if (!latestChangeset) return this._emptyChangeset; return { index: latestChangeset.index, id: latestChangeset.id }; } async getChangesetFromVersion(arg) { const version = arg.version; if (version.isFirst) return this._emptyChangeset; const namedVersionChangesetId = version.getAsOfChangeSet(); if (namedVersionChangesetId) return this.getChangesetFromId({ ...arg, changeSetId: namedVersionChangesetId, }); const namedVersionName = version.getName(); if (namedVersionName) return this.getChangesetFromNamedVersion({ ...arg, versionName: namedVersionName, }); return this.getLatestChangeset(arg); } async getChangesetFromNamedVersion(arg) { if (!arg.versionName) return this.getChangesetFromLatestNamedVersion(arg); return (0, imodels_access_common_1.getNamedVersionChangeset)(this._iModelsClient, this.getIModelScopedOperationParams(arg), arg.versionName); } getIModelScopedOperationParams(arg) { const authorizationCallback = arg.accessToken ? () => Promise.resolve(imodels_access_common_1.AccessTokenAdapter.toAuthorization(arg.accessToken)) : imodels_access_common_1.AccessTokenAdapter.toAuthorizationCallback(() => core_frontend_1.IModelApp.getAccessToken()); return { authorization: authorizationCallback, iModelId: arg.iModelId, }; } async getChangesetFromLatestNamedVersion(arg) { const getNamedVersionListParams = { ...this.getIModelScopedOperationParams(arg), urlParams: { $top: 1, $orderBy: { property: imodels_client_management_1.NamedVersionOrderByProperty.ChangesetIndex, operator: imodels_client_management_1.OrderByOperator.Descending, }, }, }; const namedVersionsIterator = this._iModelsClient.namedVersions.getMinimalList(getNamedVersionListParams); const namedVersions = await (0, imodels_access_common_1.handleAPIErrors)(async () => (0, imodels_client_management_1.take)(namedVersionsIterator, 1)); if (namedVersions.length === 0 || !namedVersions[0].changesetIndex || !namedVersions[0].changesetId) core_bentley_1.ITwinError.throwError({ iTwinErrorId: { key: imodels_client_management_1.IModelsErrorCode.NamedVersionNotFound, scope: imodels_client_management_1.IModelsErrorScope, }, message: "No named versions found", }); return { index: namedVersions[0].changesetIndex, id: namedVersions[0].changesetId, }; } } exports.FrontendIModelsAccess = FrontendIModelsAccess; //# sourceMappingURL=FrontendIModelsAccess.js.map