@itwin/insights-client
Version:
Insights client for the iTwin platform
103 lines • 5 kB
JavaScript
import { GROUPING_AND_MAPPING_BASE_PATH, OperationsBase } from "../../common/OperationsBase";
import { RequiredError } from "../../common/Errors";
import { EntityListIteratorImpl } from "../../common/iterators/EntityListIteratorImpl";
import { getEntityCollectionPage } from "../../common/iterators/IteratorUtil";
export class PropertiesClient extends OperationsBase {
constructor(basePath) {
super(basePath ?? GROUPING_AND_MAPPING_BASE_PATH);
this._baseUrl = `${this.basePath}/datasources/imodel-mappings`;
}
async createProperty(accessToken, mappingId, groupId, property) {
if (!this.isSimpleIdentifier(property.propertyName)) {
throw new RequiredError("propertyName", "Field propertyName was invalid.");
}
if (property.ecProperties)
for (const ecProperty of property.ecProperties) {
if (!this.isValidECProperty(ecProperty)) {
throw new RequiredError("ecProperties", "Field ecProperties was invalid.");
}
}
const url = this.constructUrl(mappingId, groupId);
const requestOptions = this.createRequest("POST", accessToken, JSON.stringify(property));
return (await this.fetchJSON(url, requestOptions)).property;
}
async deleteProperty(accessToken, mappingId, groupId, propertyId) {
const url = this.constructUrl(mappingId, groupId, propertyId);
const requestOptions = this.createRequest("DELETE", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getProperty(accessToken, mappingId, groupId, propertyId) {
const url = this.constructUrl(mappingId, groupId, propertyId);
const requestOptions = this.createRequest("GET", accessToken);
return (await this.fetchJSON(url, requestOptions)).property;
}
async getProperties(accessToken, mappingId, groupId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
const url = this.constructUrl(mappingId, groupId, undefined, top);
const requestOptions = this.createRequest("GET", accessToken);
const response = await this.fetchJSON(url, requestOptions);
return response;
}
getPropertiesIterator(accessToken, mappingId, groupId, top) {
if (!this.topIsValid(top)) {
throw new RequiredError("top", "Parameter top was outside of the valid range [1-1000].");
}
const url = this.constructUrl(mappingId, groupId, undefined, top);
const requestOptions = this.createRequest("GET", accessToken);
return new EntityListIteratorImpl(async () => getEntityCollectionPage(url, async (nextUrl) => {
const response = await this.fetchJSON(nextUrl, requestOptions);
return {
values: response.properties,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: response._links,
};
}));
}
async updateProperty(accessToken, mappingId, groupId, propertyId, propertyUpdate) {
if (!this.isSimpleIdentifier(propertyUpdate.propertyName)) {
throw new RequiredError("propertyName", "Field propertyName was invalid.");
}
if (propertyUpdate.dataType === undefined) {
throw new RequiredError("dataType", "Required field dataType was null or undefined.");
}
if (propertyUpdate.ecProperties)
for (const ecProperty of propertyUpdate.ecProperties) {
if (!this.isValidECProperty(ecProperty)) {
throw new RequiredError("ecProperties", "Field ecProperties was invalid.");
}
}
const url = this.constructUrl(mappingId, groupId, propertyId);
const requestOptions = this.createRequest("PUT", accessToken, JSON.stringify(propertyUpdate));
return (await this.fetchJSON(url, requestOptions)).property;
}
/**
* checks if given ECProperty is valid
* @param {ECPropertyReference} prop
*/
isValidECProperty(prop) {
return !this.isNullOrWhitespace(prop.ecSchemaName) &&
!this.isNullOrWhitespace(prop.ecClassName) &&
!this.isNullOrWhitespace(prop.ecPropertyName);
}
/**
* Constructs the endpoint with the provided params
* @param mappingId Mapping Id.
* @param groupId Group Id.
* @param propertyId Optional group Id.
* @param top Optional top number.
* @returns url endpoint.
*/
constructUrl(mappingId, groupId, propertyId, top) {
let url = `${this._baseUrl}/${encodeURIComponent(mappingId)}/groups/${encodeURIComponent(groupId)}/properties`;
if (propertyId) {
url += `/${encodeURIComponent(propertyId)}`;
}
else if (top) {
url += `?$top=${top}`;
}
return url;
}
}
//# sourceMappingURL=PropertiesClient.js.map