@itwin/access-control-client
Version:
Access control client for the iTwin platform
85 lines • 3.07 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseClient = void 0;
const axios_1 = require("axios");
class BaseClient {
constructor(url) {
this._baseUrl = "https://api.bentley.com/accesscontrol/itwins";
if (url !== undefined) {
this._baseUrl = url;
}
else {
const urlPrefix = process.env.IMJS_URL_PREFIX;
if (urlPrefix) {
const baseUrl = new URL(this._baseUrl);
baseUrl.hostname = urlPrefix + baseUrl.hostname;
this._baseUrl = baseUrl.href;
}
}
}
/**
* Sends a basic API request
* @param accessTokenString The client access token string
* @param method The method type of the request (ex. GET, POST, DELETE, etc)
* @param url The url of the request
*/
async sendGenericAPIRequest(accessToken, method, url, data, property, additionalHeaders) {
const requestOptions = this.getRequestOptions(accessToken, method, url, data, additionalHeaders);
try {
const response = await (0, axios_1.default)(requestOptions);
return {
status: response.status,
data: response.data.error || response.data === "" ? undefined : property ? response.data[property] : response.data,
error: response.data.error,
};
}
catch (err) {
return {
status: 500,
error: {
code: "InternalServerError",
message: "An internal exception happened while calling iTwins Service",
},
};
}
}
/**
* Build the request methods, headers, and other options
* @param accessTokenString The client access token string
*/
getRequestOptions(accessTokenString, method, url, data, additionalHeaders) {
return {
method,
url,
data,
headers: {
"authorization": accessTokenString,
"content-type": "application/json",
"accept": "application/vnd.bentley.itwin-platform.v2+json",
...additionalHeaders,
},
validateStatus(status) {
return status < 500; // Resolve only if the status code is less than 500
},
};
}
/**
* Build a query to be appended to a URL
* @param queryArg Object container queryable properties
* @returns query string with AccessControlQueryArg applied, which should be appended to a url
*/
getQueryString(queryArg) {
let queryString = "";
if (queryArg.top) {
queryString += `&$top=${queryArg.top}`;
}
if (queryArg.skip) {
queryString += `&$skip=${queryArg.skip}`;
}
// trim & from start of string
queryString.replace(/^&+/, "");
return queryString;
}
}
exports.BaseClient = BaseClient;
//# sourceMappingURL=BaseClient.js.map
;