@itwin/imodels-client-management
Version:
iModels API client wrapper for applications that manage iModels.
194 lines • 8.47 kB
JavaScript
export class IModelsApiUrlFormatter {
baseUrl;
_regexIgnoreCaseOption = "i";
_groupNames = {
iModelId: "iModelId",
changesetIdOrIndex: "changesetIdOrIndex",
namedVersionId: "namedVersionId",
userId: "userId",
};
_numericRegex = new RegExp("^\\d+$");
_changesetUrlRegex = new RegExp(`/iModels/(?<${this._groupNames.iModelId}>.*)/changesets/(?<${this._groupNames.changesetIdOrIndex}>[^/]*)`, this._regexIgnoreCaseOption);
_checkpointUrlRegex = new RegExp(`/iModels/(?<${this._groupNames.iModelId}>.*)/changesets/(?<${this._groupNames.changesetIdOrIndex}>.*)/checkpoint`, this._regexIgnoreCaseOption);
_namedVersionUrlRegex = new RegExp(`/iModels/(?<${this._groupNames.iModelId}>.*)/namedversions/(?<${this._groupNames.namedVersionId}>[^/]*)`, this._regexIgnoreCaseOption);
_userUrlRegex = new RegExp(`/iModels/(?<${this._groupNames.iModelId}>.*)/users/(?<${this._groupNames.userId}>[^/]*)`, this._regexIgnoreCaseOption);
_iModelUrlRegex = new RegExp(`/iModels/(?<${this._groupNames.iModelId}>[^/]*)`, this._regexIgnoreCaseOption);
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
getCreateIModelUrl() {
return this.baseUrl;
}
getCloneIModelUrl(params) {
return `${this.baseUrl}/${params.iModelId}/clone`;
}
getForkIModelUrl(params) {
return `${this.baseUrl}/${params.iModelId}/fork`;
}
getSingleIModelUrl(params) {
return `${this.baseUrl}/${params.iModelId}`;
}
getIModelListUrl(params) {
return `${this.baseUrl}${this.formQueryString({ ...params.urlParams })}`;
}
getSingleBriefcaseUrl(params) {
return `${this.baseUrl}/${params.iModelId}/briefcases/${params.briefcaseId}`;
}
getBriefcaseListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/briefcases${this.formQueryString({ ...params.urlParams })}`;
}
getSingleChangesetUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesets/${params.changesetId ?? params.changesetIndex}`;
}
getChangesetListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesets${this.formQueryString({ ...params.urlParams })}`;
}
getSingleChangesetExtendedDataUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesets/${params.changesetId ?? params.changesetIndex}/extendeddata`;
}
getChangesetExtendedDataListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesets/extendeddata${this.formQueryString({ ...params.urlParams })}`;
}
getSingleChangesetGroupUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesetgroups/${params.changesetGroupId}`;
}
getChangesetGroupListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/changesetgroups${this.formQueryString({ ...params.urlParams })}`;
}
parseChangesetUrl(url) {
const matchedGroups = this._changesetUrlRegex.exec(url).groups;
return {
iModelId: matchedGroups[this._groupNames.iModelId],
...this.parseChangesetIdOrIndex(matchedGroups[this._groupNames.changesetIdOrIndex]),
};
}
getSingleNamedVersionUrl(params) {
return `${this.baseUrl}/${params.iModelId}/namedversions/${params.namedVersionId}`;
}
getNamedVersionListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/namedversions${this.formQueryString({ ...params.urlParams })}`;
}
getCheckpointUrl(params) {
let parentEntityUrlPath;
if (params.namedVersionId)
parentEntityUrlPath = `namedversions/${params.namedVersionId}`;
else if (params.changesetId || params.changesetIndex != null)
parentEntityUrlPath = `changesets/${params.changesetId ?? params.changesetIndex}`;
else
parentEntityUrlPath = "briefcases";
return `${this.baseUrl}/${params.iModelId}/${parentEntityUrlPath}/checkpoint`;
}
getThumbnailUrl(params) {
return `${this.baseUrl}/${params.iModelId}/thumbnail${this.formQueryString({
...params.urlParams,
})}`;
}
getUserListUrl(params) {
return `${this.baseUrl}/${params.iModelId}/users${this.formQueryString({
...params.urlParams,
})}`;
}
getSingleUserUrl(params) {
return `${this.baseUrl}/${params.iModelId}/users/${params.userId}`;
}
getUserPermissionsUrl(params) {
return `${this.baseUrl}/${params.iModelId}/permissions`;
}
getCreateIModelOperationDetailsUrl(params) {
return `${this.baseUrl}/${params.iModelId}/operations/create`;
}
parseCheckpointUrl(url) {
const matchedGroups = this._checkpointUrlRegex.exec(url).groups;
return {
iModelId: matchedGroups[this._groupNames.iModelId],
...this.parseChangesetIdOrIndex(matchedGroups[this._groupNames.changesetIdOrIndex]),
};
}
parseNamedVersionUrl(url) {
const matchedGroups = this._namedVersionUrlRegex.exec(url).groups;
return {
iModelId: matchedGroups[this._groupNames.iModelId],
namedVersionId: matchedGroups[this._groupNames.namedVersionId],
};
}
parseUserUrl(url) {
const matchedGroups = this._userUrlRegex.exec(url).groups;
return {
iModelId: matchedGroups[this._groupNames.iModelId],
userId: matchedGroups[this._groupNames.userId],
};
}
parseIModelUrl(url) {
const matchedGroups = this._iModelUrlRegex.exec(url).groups;
return {
iModelId: matchedGroups[this._groupNames.iModelId],
};
}
formQueryString(urlParameters) {
let queryString = "";
for (const urlParameterKey in urlParameters) {
if (!Object.prototype.hasOwnProperty.call(urlParameters, urlParameterKey))
continue;
const urlParameterValue = urlParameters[urlParameterKey];
if (!this.shouldAppendToUrl(urlParameterValue))
continue;
queryString = this.appendToQueryString(queryString, urlParameterKey, urlParameterValue);
}
return queryString;
}
/**
* API could return Changeset urls that either contain id or index since both are valid identifiers
* so here we handle both scenarios. We assume if the value contains only digits and is shorter than 40
* symbols it is a numeric index, otherwise, it is a string id.
*/
parseChangesetIdOrIndex(changesetIdOrIndex) {
const containsOnlyDigits = this._numericRegex.test(changesetIdOrIndex);
if (containsOnlyDigits && changesetIdOrIndex.length < 40)
return {
changesetIndex: parseInt(changesetIdOrIndex, 10),
};
return {
changesetId: changesetIdOrIndex,
};
}
shouldAppendToUrl(urlParameterValue) {
if (urlParameterValue === null || urlParameterValue === undefined)
return false;
if (typeof urlParameterValue === "string" && !urlParameterValue.trim())
return false;
return true;
}
appendToQueryString(existingQueryString, parameterKey, parameterValue) {
const separator = existingQueryString.length === 0 ? "?" : "&";
return `${existingQueryString}${separator}${parameterKey}=${this.stringify(parameterValue)}`;
}
stringify(urlParameterValue) {
if (this.isSingleOrderBy(urlParameterValue)) {
return this.stringifyOrderByParameterValue([urlParameterValue]);
}
else if (this.isMultipleOrderBy(urlParameterValue)) {
return this.stringifyOrderByParameterValue(urlParameterValue);
}
return urlParameterValue.toString();
}
isSingleOrderBy(parameterValue) {
return parameterValue.property !== undefined;
}
isMultipleOrderBy(parameterValue) {
return (parameterValue?.[0]?.property !==
undefined);
}
stringifyOrderByParameterValue(orderByCriteria) {
let result = "";
for (let i = 0; i < orderByCriteria.length; i++) {
if (i !== 0)
result += ",";
const criterion = orderByCriteria[i];
result += criterion.property;
if (criterion.operator !== undefined)
result += ` ${criterion.operator}`;
}
return result;
}
}
//# sourceMappingURL=IModelsApiUrlFormatter.js.map