@itwin/insights-client
Version:
Insights client for the iTwin platform
107 lines • 5.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertiesClient = void 0;
const OperationsBase_1 = require("../../common/OperationsBase");
const Errors_1 = require("../../common/Errors");
const EntityListIteratorImpl_1 = require("../../common/iterators/EntityListIteratorImpl");
const IteratorUtil_1 = require("../../common/iterators/IteratorUtil");
class PropertiesClient extends OperationsBase_1.OperationsBase {
_baseUrl = `${this.basePath}/datasources/imodel-mappings`;
constructor(basePath) {
super(basePath ?? OperationsBase_1.GROUPING_AND_MAPPING_BASE_PATH);
}
async createProperty(accessToken, mappingId, groupId, property) {
if (!this.isSimpleIdentifier(property.propertyName)) {
throw new Errors_1.RequiredError("propertyName", "Field propertyName was invalid.");
}
if (property.ecProperties)
for (const ecProperty of property.ecProperties) {
if (!this.isValidECProperty(ecProperty)) {
throw new Errors_1.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 Errors_1.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 Errors_1.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_1.EntityListIteratorImpl(async () => (0, IteratorUtil_1.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 Errors_1.RequiredError("propertyName", "Field propertyName was invalid.");
}
if (propertyUpdate.dataType === undefined) {
throw new Errors_1.RequiredError("dataType", "Required field dataType was null or undefined.");
}
if (propertyUpdate.ecProperties)
for (const ecProperty of propertyUpdate.ecProperties) {
if (!this.isValidECProperty(ecProperty)) {
throw new Errors_1.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;
}
}
exports.PropertiesClient = PropertiesClient;
//# sourceMappingURL=PropertiesClient.js.map