UNPKG

@asposecloud/aspose-tasks-cloud

Version:
295 lines 12.1 kB
"use strict"; /** * * Copyright (c) 2025 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 }); exports.invokeApiMethod = invokeApiMethod; exports.addQueryParameterToUrl = addQueryParameterToUrl; const axios_1 = require("axios"); const FormData = require("form-data"); const querystring = require("querystring"); const https = require("https"); const url_1 = require("url"); const objectSerializer_1 = require("./objectSerializer"); /** * Invoke api method * @param requestOptions request parameters * @param configuration api configuration * @param notApplyAuthToRequest if set to true, auth is not applied to request */ function invokeApiMethod(requestOptions, configuration, notApplyAuthToRequest, postData) { return __awaiter(this, void 0, void 0, function* () { try { return yield invokeApiMethodInternal(requestOptions, configuration, notApplyAuthToRequest, postData); } catch (e) { if (e instanceof NeedRepeatException) { return yield invokeApiMethodInternal(requestOptions, configuration, notApplyAuthToRequest, postData); } throw e; } }); } /** * 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; } /** * Internal implementation using axios * @param requestOptions * @param configuration * @param notApplyAuthToRequest * @param postData */ function invokeApiMethodInternal(requestOptions, configuration, notApplyAuthToRequest, postData) { return __awaiter(this, void 0, void 0, function* () { const auth = configuration.authentication; if (!notApplyAuthToRequest && auth && typeof auth.applyToRequest === "function") { yield auth.applyToRequest(requestOptions, configuration); } const rawUrl = String(requestOptions.uri || requestOptions.url || ""); let urlObj = null; try { urlObj = rawUrl ? new url_1.URL(rawUrl) : null; } catch (_a) { urlObj = null; } const axiosConfig = { method: (requestOptions.method || "GET").toLowerCase(), url: rawUrl, validateStatus: () => true, headers: {}, params: requestOptions.qs || undefined, }; const defaultHeaders = { "User-Agent": "tasks nodejs sdk", "x-aspose-client": "nodejs sdk", "x-aspose-client-version": "25.12", }; axiosConfig.headers = Object.assign({}, defaultHeaders, requestOptions.headers || {}); if (requestOptions.form) { axiosConfig.data = querystring.stringify(requestOptions.form); axiosConfig.headers["Content-Type"] = "application/x-www-form-urlencoded"; } else if (requestOptions.formData) { const form = new FormData(); const formData = requestOptions.formData || {}; for (const k of Object.keys(formData)) { const val = formData[k]; if (val && typeof val === "object" && ("value" in val || "options" in val)) { const value = val.value; const options = val.options; form.append(k, value, options); } else if (Buffer.isBuffer(val)) { const filename = (requestOptions && requestOptions.fileName) || k || "file"; form.append(k, val, { filename }); } else if (val && typeof val === "object" && typeof val.pipe === "function") { const stream = val; let options = undefined; if (stream.path && typeof stream.path === "string") { try { const parts = stream.path.split(/[\/\\]/); options = { filename: parts[parts.length - 1] || k }; } catch (_b) { options = undefined; } } if (options) form.append(k, stream, options); else form.append(k, stream); } else { form.append(k, val); } } axiosConfig.headers = Object.assign({}, axiosConfig.headers, form.getHeaders ? form.getHeaders() : {}); try { const length = yield new Promise((resolve, _) => { form.getLength((err, len) => { if (err) { return resolve(-1); } return resolve(len); }); }); if (length >= 0) { axiosConfig.headers["Content-Length"] = length; } } catch (_c) { } axiosConfig.data = form; } else if (postData) { axiosConfig.data = postData; axiosConfig.headers["Content-Type"] = "application/octet-stream"; axiosConfig.headers["Content-Length"] = Buffer.byteLength(postData); } else if (requestOptions.body !== undefined) { if (requestOptions.json) { axiosConfig.data = requestOptions.body; axiosConfig.headers["Content-Type"] = "application/json"; } else { axiosConfig.data = requestOptions.body; } } if (requestOptions.encoding === null || requestOptions.isResponseFile) { axiosConfig.responseType = "arraybuffer"; } else if (requestOptions.json) { axiosConfig.responseType = "json"; } else { axiosConfig.responseType = "text"; } if (requestOptions.json) { axiosConfig.headers["Accept"] = "application/json"; } if (urlObj) { try { axiosConfig.httpsAgent = new https.Agent({ minVersion: "TLSv1", }); } catch (_d) { } } let axiosResponse; try { axiosResponse = yield axios_1.default.request(axiosConfig); } catch (err) { if (err && err.code === "EPROTO") { const host = urlObj ? urlObj.hostname : axiosConfig.url; err.message = `${err.message} (while connecting to ${host}). Possible causes: SNI mismatch, outdated OpenSSL/Node, or corporate TLS interception. Try: 'openssl s_client -connect ${host}:443 -servername ${host}' and check Node/OpenSSL versions.`; } throw err; } if (axiosResponse && axiosResponse.status === 401 && !notApplyAuthToRequest) { if (auth && typeof auth.handle401response === "function") { yield auth.handle401response(configuration); throw new NeedRepeatException(); } } const rawData = axiosResponse.data; let bodyContent = rawData; if (axiosResponse.config && axiosResponse.config.responseType === "arraybuffer" && rawData) { try { bodyContent = Buffer.from(rawData); if (requestOptions.json) { try { const text = bodyContent.toString("utf8"); bodyContent = JSON.parse(text); } catch (_e) { } } } catch (_f) { bodyContent = rawData; } } else if (axiosResponse.config && axiosResponse.config.responseType === "json") { bodyContent = rawData; } else if (axiosResponse.config && axiosResponse.config.responseType === "text" && typeof rawData === "string") { bodyContent = rawData; } else { bodyContent = rawData; } const wrappedResponse = { body: bodyContent, status: axiosResponse.status, statusCode: axiosResponse.status, statusMessage: axiosResponse.statusText, headers: axiosResponse.headers, text: typeof bodyContent === "string" ? bodyContent : undefined, axiosResponse: axiosResponse }; if (axiosResponse.status >= 200 && axiosResponse.status <= 299) { return Promise.resolve(wrappedResponse); } try { let parsedErrorBody = bodyContent; if (Buffer.isBuffer(parsedErrorBody)) { try { parsedErrorBody = JSON.parse(parsedErrorBody.toString("utf8")); } catch (_g) { } } const result = objectSerializer_1.ObjectSerializer.deserialize(parsedErrorBody, "AsposeResponse"); return Promise.reject({ message: result && result.message ? result.message : axiosResponse.statusText, code: axiosResponse.status, response: wrappedResponse }); } catch (error) { return Promise.reject({ message: "Error while parse server error: " + error, code: axiosResponse.status }); } }); } /** * Exception, indicating necessity of request repeat */ class NeedRepeatException extends Error { } //# sourceMappingURL=requestHelper.js.map