@itwin/imodels-client-management
Version:
iModels API client wrapper for applications that manage iModels.
156 lines • 8.09 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { EntityListIteratorImpl, OperationsBase, } from "../../base/internal";
import { PreferReturn, } from "../../base/types";
import { getUser } from "../SharedFunctions";
export class NamedVersionOperations extends OperationsBase {
_iModelsClient;
constructor(options, _iModelsClient) {
super(options);
this._iModelsClient = _iModelsClient;
}
/**
* Gets Named Versions of a specific iModel. This method returns Named Versions in their minimal representation. The
* returned iterator internally queries entities in pages. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-named-versions/ Get iModel Named Versions}
* operation from iModels API.
* @param {GetNamedVersionListParams} params parameters for this operation. See {@link GetNamedVersionListParams}.
* @returns {EntityListIterator<MinimalNamedVersion>} iterator for Named Version list. See {@link EntityListIterator},
* {@link MinimalNamedVersion}.
*/
getMinimalList(params) {
return new EntityListIteratorImpl(async () => this.getEntityCollectionPage({
authorization: params.authorization,
url: this._options.urlFormatter.getNamedVersionListUrl({
iModelId: params.iModelId,
urlParams: params.urlParams,
}),
preferReturn: PreferReturn.Minimal,
entityCollectionAccessor: (response) => response.body.namedVersions,
headers: params.headers,
}));
}
/**
* Gets Named Versions of a specific iModel. This method returns Named Versions in their full representation. The
* returned iterator internally queries entities in pages. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-named-versions/
* Get iModel Named Versions} operation from iModels API.
* @param {GetNamedVersionListParams} params parameters for this operation. See {@link GetNamedVersionListParams}.
* @returns {EntityListIterator<NamedVersion>} iterator for Named Version list. See {@link EntityListIterator},
* {@link NamedVersion}.
*/
getRepresentationList(params) {
const entityCollectionAccessor = (response) => {
const namedVersions = response.body.namedVersions;
const mappedNamedVersions = namedVersions.map((namedVersion) => this.appendRelatedEntityCallbacks(params.authorization, namedVersion, params.headers));
return mappedNamedVersions;
};
return new EntityListIteratorImpl(async () => this.getEntityCollectionPage({
authorization: params.authorization,
url: this._options.urlFormatter.getNamedVersionListUrl({
iModelId: params.iModelId,
urlParams: params.urlParams,
}),
preferReturn: PreferReturn.Representation,
entityCollectionAccessor,
headers: params.headers,
}));
}
/**
* Gets a single Named Version by its id. This method returns a Named Version in its full representation. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/get-imodel-named-version-details/
* Get iModel Named Version} operation from iModels API.
* @param {GetSingleNamedVersionParams} params parameters for this operation. See {@link GetSingleNamedVersionParams}.
* @returns {Promise<NamedVersion>} a Named Version with specified id. See {@link NamedVersion}.
*/
async getSingle(params) {
const response = await this.sendGetRequest({
authorization: params.authorization,
url: this._options.urlFormatter.getSingleNamedVersionUrl({
iModelId: params.iModelId,
namedVersionId: params.namedVersionId,
}),
headers: params.headers,
});
const result = this.appendRelatedEntityCallbacks(params.authorization, response.body.namedVersion, params.headers);
return result;
}
/**
* Creates a Named Version with specified properties. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/create-imodel-named-version/
* Create iModel Named Version} operation from iModels API.
* @param {CreateNamedVersionParams} params parameters for this operation. See {@link CreateNamedVersionParams}.
* @returns {Promise<NamedVersion>} newly created Named Version. See {@link NamedVersion}.
*/
async create(params) {
const createNamedVersionBody = this.getCreateNamedVersionRequestBody(params.namedVersionProperties);
const createNamedVersionResponse = await this.sendPostRequest({
authorization: params.authorization,
url: this._options.urlFormatter.getNamedVersionListUrl({
iModelId: params.iModelId,
}),
body: createNamedVersionBody,
headers: params.headers,
});
const result = this.appendRelatedEntityCallbacks(params.authorization, createNamedVersionResponse.body.namedVersion, params.headers);
return result;
}
/**
* Updates Named Version with specified properties. Wraps the
* {@link https://developer.bentley.com/apis/imodels-v2/operations/update-imodel-named-version/
* Update iModel Named Version} operation from iModels API.
* @param {UpdateNamedVersionParams} params parameters for this operation. See {@link UpdateNamedVersionParams}.
* @returns {Promise<NamedVersion>} updated Named Version. See {@link NamedVersion}.
*/
async update(params) {
const updateNamedVersionBody = this.getUpdateNamedVersionRequestBody(params.namedVersionProperties);
const updateNamedVersionResponse = await this.sendPatchRequest({
authorization: params.authorization,
url: this._options.urlFormatter.getSingleNamedVersionUrl({
iModelId: params.iModelId,
namedVersionId: params.namedVersionId,
}),
body: updateNamedVersionBody,
headers: params.headers,
});
const result = this.appendRelatedEntityCallbacks(params.authorization, updateNamedVersionResponse.body.namedVersion, params.headers);
return result;
}
getCreateNamedVersionRequestBody(namedVersionProperties) {
return {
name: namedVersionProperties.name,
description: namedVersionProperties.description,
changesetId: namedVersionProperties.changesetId,
};
}
getUpdateNamedVersionRequestBody(namedVersionProperties) {
return {
name: namedVersionProperties.name,
description: namedVersionProperties.description,
state: namedVersionProperties.state,
};
}
appendRelatedEntityCallbacks(authorization, namedVersion, headers) {
const getCreator = async () => getUser(authorization, this._iModelsClient.users, this._options.urlFormatter, namedVersion._links.creator?.href, headers);
const getChangeset = async () => this.getChangeset(authorization, namedVersion._links.changeset?.href, headers);
const result = {
...namedVersion,
getCreator,
getChangeset,
};
return result;
}
async getChangeset(authorization, changesetLink, headers) {
if (!changesetLink)
return undefined;
const entityIds = this._options.urlFormatter.parseChangesetUrl(changesetLink);
return this._iModelsClient.changesets.getSingle({
authorization,
...entityIds,
headers,
});
}
}
//# sourceMappingURL=NamedVersionOperations.js.map