@itwin/insights-client
Version:
Insights client for the iTwin platform
137 lines • 6.85 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedGroupsClient = 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 NamedGroupsClient extends OperationsBase_1.OperationsBase {
static MAX_DISPLAY_NAME_LENGTH = 512;
constructor(basePath) {
super(basePath ?? OperationsBase_1.NAMED_GROUPS_BASE_PATH);
}
async createNamedGroup(accessToken, group) {
if (this.isNullOrWhitespace(group.query)) {
throw new Errors_1.RequiredError("query", "Required field query was null or undefined.");
}
if (this.isNullOrWhitespace(group.displayName)) {
throw new Errors_1.RequiredError("displayName", "Required field displayName cannot be empty or consist only of whitespace characters.");
}
if (this.isNullOrWhitespace(group.iTwinId)) {
throw new Errors_1.RequiredError("iTwinId", "Required field iTwinId was null or undefined.");
}
if (!this.isWithinMaxAllowedCharacters(group.displayName, NamedGroupsClient.MAX_DISPLAY_NAME_LENGTH)) {
throw new Errors_1.RequiredError("displayName", "Field displayName was invalid. It must be a string with a maximum length of 512 characters.");
}
group.metadata && this.validateMetadata(group.metadata);
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(group));
return (await this.fetchJSON(this.basePath, requestOptions)).group;
}
async deleteNamedGroup(accessToken, groupId) {
const url = this.constructUrl(groupId);
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async updateNamedGroup(accessToken, groupId, group) {
if (group.description === undefined && !group.displayName && !group.query && !group.metadata) {
throw new Errors_1.RequiredError("group", "At least one property must be provided for update.");
}
if (group.displayName) {
if (this.isNullOrWhitespace(group.displayName)) {
throw new Errors_1.RequiredError("displayName", "Field displayName cannot consist only of whitespace characters.");
}
if (!this.isWithinMaxAllowedCharacters(group.displayName, NamedGroupsClient.MAX_DISPLAY_NAME_LENGTH)) {
throw new Errors_1.RequiredError("displayName", "Field displayName was invalid. It must be a string with a maximum length of 512 characters.");
}
}
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(groupId);
const requestOptions = this.createRequest("PATCH", accessToken, JSON.stringify(group));
return (await this.fetchJSON(url, requestOptions)).group;
}
async getNamedGroup(accessToken, groupId) {
const url = this.constructUrl(groupId);
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).group;
}
async getNamedGroups(accessToken, iTwinId, 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(undefined, iTwinId, 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);
}
}
getNamedGroupsIterator(accessToken, iTwinId, 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(undefined, iTwinId, 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);
});
}
constructUrl(groupId, iTwinId, top) {
const url = new URL(this.basePath);
if (groupId) {
url.pathname += `/${encodeURIComponent(groupId)}`;
}
else {
if (!url.pathname.endsWith("/")) {
url.pathname += "/";
}
}
if (iTwinId) {
url.searchParams.append("iTwinId", iTwinId);
}
if (top && !groupId) {
url.searchParams.append("$top", top.toString());
}
return url.toString();
}
}
exports.NamedGroupsClient = NamedGroupsClient;
//# sourceMappingURL=NamedGroupsClient.js.map