@itwin/insights-client
Version:
Insights client for the iTwin platform
126 lines • 5.7 kB
JavaScript
import { RequiredError } from "../../common/Errors";
import { EntityListIteratorImpl } from "../../common/iterators/EntityListIteratorImpl";
import { getEntityCollectionPage } from "../../common/iterators/IteratorUtil";
import { OperationsBase, REPORTING_BASE_PATH } from "../../common/OperationsBase";
import { XMLParser } from "fast-xml-parser";
export class ODataClient extends OperationsBase {
constructor(basePath) {
super(basePath ?? REPORTING_BASE_PATH);
}
async getODataReport(accessToken, reportId) {
const url = `${this.basePath}/odata/${encodeURIComponent(reportId)}`;
const requestOptions = this.createRequest("GET", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getODataReportEntityPage(accessToken, reportId, odataItem, sequence) {
const segments = odataItem.url.split("/"); // region, manifestId, entityType
if (segments.length !== 3) {
throw new RequiredError("odataItem", "Parameter odataItem item was invalid.");
}
const url = `${this.basePath}/odata/${encodeURIComponent(reportId)}/${odataItem.url}?sequence=${encodeURIComponent(sequence)}`;
const requestOptions = this.createRequest("GET", accessToken);
return this.fetchJSON(url, requestOptions);
}
async getODataReportEntities(accessToken, reportId, odataItem) {
const segments = odataItem.url.split("/"); // region, manifestId, entityType
if (segments.length !== 3) {
throw new RequiredError("odataItem", "Parameter odataItem item was invalid.");
}
const reportData = [];
const oDataReportEntitiesIt = this.getODataReportEntitiesIterator(accessToken, reportId, odataItem);
for await (const oDataReportEntity of oDataReportEntitiesIt) {
reportData.push(oDataReportEntity);
}
return reportData;
}
getODataReportEntitiesIterator(accessToken, reportId, odataItem) {
const segments = odataItem.url.split("/"); // region, manifestId, entityType
if (segments.length !== 3) {
throw new RequiredError("odataItem", "Parameter odataItem item was invalid.");
}
const url = `${this.basePath}/odata/${encodeURIComponent(reportId)}/${odataItem.url}`;
const request = this.createRequest("GET", accessToken);
return new EntityListIteratorImpl(async () => getEntityCollectionPage(url, async (nextUrl) => {
const response = await this.fetchJSON(nextUrl, request);
const link = {
self: {
href: nextUrl,
},
};
if (response["@odata.nextLink"]) {
link.next = {
href: response["@odata.nextLink"],
};
}
return {
values: response.value,
// eslint-disable-next-line @typescript-eslint/naming-convention
_links: link,
};
}));
}
async getODataReportMetadata(accessToken, reportId) {
const url = `${this.basePath}/odata/${encodeURIComponent(reportId)}/$metadata`;
const requestOptions = this.createRequest("GET", accessToken);
const response = await this.fetchData(url, requestOptions);
return this.parseXML(response);
}
async parseXML(response) {
const options = {
ignoreAttributes: false,
attributeNamePrefix: "",
transformTagName: (tagName) => tagName.charAt(0).toLowerCase() + tagName.slice(1),
};
const parser = new XMLParser(options);
const text = await response.text();
const parsedXML = parser.parse(text);
if (!parsedXML["edmx:Edmx"]["edmx:DataServices"].hasOwnProperty("schema") || !(parsedXML["edmx:Edmx"]["edmx:DataServices"].schema instanceof Array)) {
return [];
}
const schemas = parsedXML["edmx:Edmx"]["edmx:DataServices"].schema;
const defaultSchema = schemas.find((s) => s.Namespace === "Default");
if (defaultSchema === undefined) {
return [];
}
const entitySet = this.makeArray(defaultSchema.entityContainer.entitySet);
const result = [];
for (const entity of entitySet) {
result.push(this.getEntitySetByName(entity, schemas));
}
return result;
}
getEntitySetByName(entity, schemas) {
const table = {
name: entity.Name,
columns: [],
annotations: [],
};
const identifiers = entity.EntityType.split(".");
const entityTypes = [];
schemas.filter((schema) => schema.Namespace === identifiers[0])
.forEach((schema) => entityTypes.push(...this.makeArray(schema.entityType)));
const targetEntity = entityTypes.find((entityType) => entityType.Name === identifiers[1]);
const properties = this.makeArray(targetEntity.property);
properties.forEach((property) => {
table.columns.push({
name: property.Name,
type: property.Type,
});
});
const annotationNodes = entity.annotation;
if (annotationNodes) {
const annotations = this.makeArray(annotationNodes);
annotations.forEach((annotation) => {
table.annotations.push({
term: annotation.Term,
stringValue: annotation.String,
});
});
}
return table;
}
makeArray(entity) {
return entity instanceof Array ? entity : [entity];
}
}
//# sourceMappingURL=ODataClient.js.map