UNPKG

@itwin/insights-client

Version:

Insights client for the iTwin platform

133 lines 6.5 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { NAMED_GROUPS_BASE_PATH, OperationsBase } from "../../common/OperationsBase"; import { EntityListIteratorImpl } from "../../common/iterators/EntityListIteratorImpl"; import { getEntityCollectionPage } from "../../common/iterators/IteratorUtil"; import { RequiredError } from "../../common/Errors"; import { PreferReturn } from "../../common/Common"; export class NamedGroupsClient extends OperationsBase { static MAX_DISPLAY_NAME_LENGTH = 512; constructor(basePath) { super(basePath ?? NAMED_GROUPS_BASE_PATH); } async createNamedGroup(accessToken, group) { if (this.isNullOrWhitespace(group.query)) { throw new RequiredError("query", "Required field query was null or undefined."); } if (this.isNullOrWhitespace(group.displayName)) { throw new RequiredError("displayName", "Required field displayName cannot be empty or consist only of whitespace characters."); } if (this.isNullOrWhitespace(group.iTwinId)) { throw new RequiredError("iTwinId", "Required field iTwinId was null or undefined."); } if (!this.isWithinMaxAllowedCharacters(group.displayName, NamedGroupsClient.MAX_DISPLAY_NAME_LENGTH)) { throw new 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 RequiredError("group", "At least one property must be provided for update."); } if (group.displayName) { if (this.isNullOrWhitespace(group.displayName)) { throw new RequiredError("displayName", "Field displayName cannot consist only of whitespace characters."); } if (!this.isWithinMaxAllowedCharacters(group.displayName, NamedGroupsClient.MAX_DISPLAY_NAME_LENGTH)) { throw new 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 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 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 === PreferReturn.Representation) { return this.fetchJSON(url, request); } else { return this.fetchJSON(url, request); } } getNamedGroupsIterator(accessToken, iTwinId, preferReturn, top) { if (!this.topIsValid(top)) { throw new 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 === PreferReturn.Representation) { return new EntityListIteratorImpl(async () => this.fetchCollection(url, request)); } else { return new EntityListIteratorImpl(async () => this.fetchCollection(url, request)); } } async fetchCollection(url, request) { return 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 RequiredError(`metadata.key[${index}]`, "Key cannot be empty or consist only of whitespace characters."); } if (seenKeys.has(entry.key)) { throw new 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(); } } //# sourceMappingURL=NamedGroupsClient.js.map