UNPKG

@itwin/imodels-access-frontend

Version:

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

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