UNPKG

@itwin/insights-client

Version:

Insights client for the iTwin platform

122 lines 5.77 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { GROUPING_AND_MAPPING_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 GroupsClient extends OperationsBase { constructor(basePath) { super(basePath ?? GROUPING_AND_MAPPING_BASE_PATH); this._baseUrl = `${this.basePath}/datasources/imodel-mappings`; } async createGroup(accessToken, mappingId, group) { if (!this.isSimpleIdentifier(group.groupName)) { throw new RequiredError("groupName", "Field groupName was invalid."); } if (this.isNullOrWhitespace(group.query)) { throw new 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 RequiredError("group", "All properties of group were missing."); } if (group.groupName && !this.isSimpleIdentifier(group.groupName)) { throw new RequiredError("groupName", "Field groupName was invalid."); } 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(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 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 === PreferReturn.Representation) { return this.fetchJSON(url, request); } else { return this.fetchJSON(url, request); } } getGroupsIterator(accessToken, mappingId, preferReturn, top) { if (!this.topIsValid(top)) { throw new 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 === 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); }); } /** * 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; } } //# sourceMappingURL=GroupsClient.js.map