@itwin/insights-client
Version:
Insights client for the iTwin platform
126 lines • 6.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GroupsClient = 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 OperationsBase_1 = require("../../common/OperationsBase");
const EntityListIteratorImpl_1 = require("../../common/iterators/EntityListIteratorImpl");
const IteratorUtil_1 = require("../../common/iterators/IteratorUtil");
const Errors_1 = require("../../common/Errors");
const Common_1 = require("../../common/Common");
class GroupsClient extends OperationsBase_1.OperationsBase {
constructor(basePath) {
super(basePath ?? OperationsBase_1.GROUPING_AND_MAPPING_BASE_PATH);
this._baseUrl = `${this.basePath}/datasources/imodel-mappings`;
}
async createGroup(accessToken, mappingId, group) {
if (!this.isSimpleIdentifier(group.groupName)) {
throw new Errors_1.RequiredError("groupName", "Field groupName was invalid.");
}
if (this.isNullOrWhitespace(group.query)) {
throw new Errors_1.RequiredError("query", "Required field query was null or undefined.");
}
group.metadata && this.validateMetadata(group.metadata);
const url = this.constructUrl(mappingId);
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(group));
return (await this.fetchJSON(url, requestOptions)).group;
}
async deleteGroup(accessToken, mappingId, groupId) {
const url = this.constructUrl(mappingId, groupId);
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async updateGroup(accessToken, mappingId, groupId, group) {
if (!group.groupName && group.description === undefined && !group.query && !group.metadata) {
throw new Errors_1.RequiredError("group", "All properties of group were missing.");
}
if (group.groupName && !this.isSimpleIdentifier(group.groupName)) {
throw new Errors_1.RequiredError("groupName", "Field groupName was invalid.");
}
if (group.query && this.isNullOrWhitespace(group.query)) {
throw new Errors_1.RequiredError("query", "Field query cannot consist only of whitespace characters.");
}
group.metadata && this.validateMetadata(group.metadata);
const url = this.constructUrl(mappingId, groupId);
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(group));
return (await this.fetchJSON(url, requestOptions)).group;
}
async getGroup(accessToken, mappingId, groupId) {
const url = this.constructUrl(mappingId, groupId);
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).group;
}
async getGroups(accessToken, mappingId, preferReturn, top) {
if (top !== undefined && !this.topIsValid(top)) {
throw new Errors_1.RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
const url = this.constructUrl(mappingId, undefined, top);
const request = this.createRequest("GET", accessToken, undefined, preferReturn);
if (preferReturn === Common_1.PreferReturn.Representation) {
return this.fetchJSON(url, request);
}
else {
return this.fetchJSON(url, request);
}
}
getGroupsIterator(accessToken, mappingId, preferReturn, top) {
if (!this.topIsValid(top)) {
throw new Errors_1.RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
const url = this.constructUrl(mappingId, undefined, top);
const request = this.createRequest("GET", accessToken, undefined, preferReturn);
if (preferReturn === Common_1.PreferReturn.Representation) {
return new EntityListIteratorImpl_1.EntityListIteratorImpl(async () => this.fetchCollection(url, request));
}
else {
return new EntityListIteratorImpl_1.EntityListIteratorImpl(async () => this.fetchCollection(url, request));
}
}
async fetchCollection(url, request) {
return (0, IteratorUtil_1.getEntityCollectionPage)(url, async (nextUrl) => {
const response = await this.fetchJSON(nextUrl, request);
return {
values: response.groups,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
});
}
/**
* Validates metadata entries.
* @param entries
*/
validateMetadata(entries) {
const seenKeys = new Set();
entries.forEach((entry, index) => {
if (this.isNullOrWhitespace(entry.key)) {
throw new Errors_1.RequiredError(`metadata.key[${index}]`, "Key cannot be empty or consist only of whitespace characters.");
}
if (seenKeys.has(entry.key)) {
throw new Errors_1.RequiredError(`metadata.key[${index}]`, `Duplicate key found: ${entry.key}`);
}
seenKeys.add(entry.key);
});
}
/**
* Construct group endpoint with provided params.
* @param mappingId Mapping Id.
* @param groupId Group Id.
* @param top Optional top number.
* @returns url endpoint.
*/
constructUrl(mappingId, groupId, top) {
let url = `${this._baseUrl}/${encodeURIComponent(mappingId)}/groups`;
if (groupId) {
url += `/${encodeURIComponent(groupId)}`;
}
else if (top) {
url += `?$top=${top}`;
}
return url;
}
}
exports.GroupsClient = GroupsClient;
//# sourceMappingURL=GroupsClient.js.map