groupdocs-conversion-cloud
Version:
GroupDocs.Conversion Cloud SDK for Node.js
136 lines (135 loc) • 5.74 kB
JavaScript
;
/*
* The MIT License (MIT)
*
* Copyright (c) Aspose Pty Ltd
*
* 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 });
exports.invokeApiMethod = invokeApiMethod;
exports.addQueryParameterToUrl = addQueryParameterToUrl;
const axios_1 = require("axios");
const package_version_1 = require("./package_version");
/**
* Invoke api method
* @param requestOptions Request parameters
* @param config Configuration
* @param skipAuth If enabled, auth is not applied to request
*/
function invokeApiMethod(requestOptions, config, skipAuth) {
return __awaiter(this, void 0, void 0, function* () {
return yield invokeApiMethodInternal(requestOptions, config, skipAuth);
});
}
/**
* 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) {
if (url.indexOf("{" + parameterName + "}") >= 0) {
url = url.replace("{" + parameterName + "}", String(parameterValue));
}
else {
queryParameters[parameterName] = String(parameterValue);
}
}
else {
url = url.replace("/{" + parameterName + "}", "");
}
return url;
}
/**
* Invoke api method
* @param requestOptions request parameters
* @param config Configuration
* @param skipAuth If enabled, auth is not applied to request
*/
function invokeApiMethodInternal(requestOptions, config, skipAuth) {
return __awaiter(this, void 0, void 0, function* () {
// if (config.debugging === true) {
// requestDebug(request, (type, data) => {
// const toLog = {};
// toLog[type] = data;
// console.log(JSON.stringify(toLog, undefined, 2));
// });
// }
if (!requestOptions.headers) {
requestOptions.headers = {};
}
requestOptions.headers["x-groupdocs-client"] = "node.js sdk";
requestOptions.headers["x-groupdocs-client-version"] = package_version_1.PackageVersion;
const auth = config.authentication;
if (!skipAuth) {
yield auth.applyToRequest(requestOptions, config);
}
return new Promise((resolve, reject) => {
(0, axios_1.default)(requestOptions)
.then((response) => {
if (response.status >= 200 && response.status <= 299) {
resolve(response);
}
else {
reject({ message: response.data, code: response.status });
}
})
.catch((error) => {
try {
const response = error.response;
let bodyContent = response.data;
if (bodyContent instanceof Buffer) {
bodyContent = JSON.parse(bodyContent.toString("utf8"));
}
if (bodyContent.error) {
if (bodyContent.error.message) {
reject({ message: bodyContent.error.message, code: bodyContent.error.code });
}
else {
reject({ message: bodyContent.error, code: response.status });
}
}
else {
if (bodyContent.message) {
reject({ message: bodyContent.message, code: bodyContent.code });
}
else {
reject({ message: bodyContent, code: response.status });
}
}
}
catch (error) {
reject({ message: "Error while parse server error: " + error });
}
});
});
});
}