@asposecloud/aspose-tasks-cloud
Version:
Aspose.Tasks Cloud SDK for Node.js
169 lines • 7.09 kB
JavaScript
/**
*
* Copyright (c) 2021 Aspose.Tasks Cloud
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const objectSerializer_1 = require("./objectSerializer");
const superagent = require("superagent");
/**
* Invoke api method
* @param requestOptions request parameters
* @param confguration api configuration
* @param notApplyAuthToRequest if setted to true, auth is not applied to request
*/
function invokeApiMethod(requestOptions, confguration, notApplyAuthToRequest, postData) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield invokeApiMethodInternal(requestOptions, confguration, notApplyAuthToRequest, postData);
}
catch (e) {
if (e instanceof NeedRepeatException) {
return yield invokeApiMethodInternal(requestOptions, confguration, notApplyAuthToRequest, postData);
}
throw e;
}
});
}
exports.invokeApiMethod = invokeApiMethod;
/**
* Add parameter to query
* @param url url
* @param queryParameters queryParameters
* @param parameterName parameterName
* @param parameterValue parameterValue
*/
function addQueryParameterToUrl(url, queryParameters, parameterName, parameterValue) {
if (parameterValue !== undefined) {
let valueAsString;
if (parameterValue instanceof Date) {
valueAsString = parameterValue.toISOString();
}
else {
valueAsString = String(parameterValue);
}
if (url.indexOf("{" + parameterName + "}") >= 0) {
url = url.replace("{" + parameterName + "}", valueAsString);
}
else {
queryParameters[parameterName] = valueAsString;
}
}
else {
url = url.replace("/{" + parameterName + "}", "");
}
return url;
}
exports.addQueryParameterToUrl = addQueryParameterToUrl;
/**
* Invoke api method
* @param requestOptions request parameters
* @param confguration api configuration
* @param notApplyAuthToRequest if setted to true, auth is not applied to request
*/
function invokeApiMethodInternal(requestOptions, confguration, notApplyAuthToRequest, postData) {
return __awaiter(this, void 0, void 0, function* () {
let sa = superagent(requestOptions.method, requestOptions["uri"]);
const auth = confguration.authentication;
if (!notApplyAuthToRequest) {
yield auth.applyToRequest(requestOptions, confguration);
}
if (requestOptions.form) {
sa.type('form');
sa.send(requestOptions.form);
}
else if (postData) {
sa.type('application/octet-stream');
sa.set("Content-Length", postData.length);
sa.send(postData);
}
else {
sa.type('application/json');
if (requestOptions.body) {
sa.send(requestOptions.body);
}
}
// query params
sa.query(requestOptions.qs);
//headers
sa.set("User-Agent", "tasks nodejs sdk");
sa.set("x-aspose-client", "nodejs sdk");
sa.set("x-aspose-client-version", "25.8");
if (!requestOptions.headers) {
requestOptions.headers = {};
}
if (!notApplyAuthToRequest) {
for (let [propName, propValue] of Object.entries(requestOptions.headers)) {
if (propValue != undefined && propValue != null) {
sa.set(propName, propValue);
}
}
}
if (requestOptions.json) {
sa.accept('json');
}
else {
sa.responseType('blob');
}
return new Promise((resolve, reject) => {
sa.catch((err) => __awaiter(this, void 0, void 0, function* () {
if (err.status === 401 && !notApplyAuthToRequest) {
yield auth.handle401response(confguration);
reject(new NeedRepeatException());
}
else {
reject(err);
}
}));
sa.then((response) => __awaiter(this, void 0, void 0, function* () {
if (response.status >= 200 && response.status <= 299) {
resolve(response);
}
else {
try {
let bodyContent = response.body;
if (bodyContent instanceof Buffer) {
bodyContent = JSON.parse(bodyContent.toString("utf8"));
}
const result = objectSerializer_1.ObjectSerializer.deserialize(bodyContent, "AsposeResponse");
reject({ message: result.message, code: response.status });
}
catch (error) {
reject({ message: "Error while parse server error: " + error });
}
}
}));
});
});
}
/**
* Exception, indicating necessity of request repeat
*/
class NeedRepeatException extends Error {
}
//# sourceMappingURL=requestHelper.js.map
;