UNPKG

cloudcms-cli

Version:
165 lines (149 loc) 4.27 kB
var axios = require("axios"); const { HttpsProxyAgent } = require("https-proxy-agent"); const { HttpProxyAgent } = require("http-proxy-agent"); const { getProxyForUrl } = require("proxy-from-env"); // checks HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables // determines whether a proxy should be configured (otherwise null) // sets proxy settings onto options var applyAgent = function(url, config) { // checks HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables // determines whether a proxy should be configured (otherwise null) var proxy = getProxyForUrl(url); if (proxy) { // bind in http proxy if (url.startsWith("https:")) { config.proxy = false; config.httpsAgent = new HttpsProxyAgent(proxy); } else if (url.startsWith("http:")) { config.proxy = false; config.httpAgent = new HttpProxyAgent(proxy); } } }; /** * Incoming config: * * { * "url": "", * "method": "", * "headers": {}, * "qs": {}, * "data": "" | {}, * "json": {} * } * * Callback is (err, response). * * Where response is the Axios response object. * * @param config * @param callback */ module.exports = function(config, callback) { // request config - https://github.com/request/request#requestoptions-callback // axios config - https://www.npmjs.com/package/axios if (!callback) { callback = function(err, response, data) { // nothing }; } var requestConfig = {}; requestConfig.url = config.uri || config.url; requestConfig.method = config.method || "get"; requestConfig.headers = {}; if (!config) { config = {}; } if (!config.headers) { config.headers = {}; } for (var k in config.headers) { var v = config.headers[k]; if (v) { requestConfig.headers[k.trim().toLowerCase()] = v; } } // support for FormData headers // copy form data headers if (config.data && config.data.getHeaders) { var formDataHeaders = config.data.getHeaders(); for (var k in formDataHeaders) { var v = formDataHeaders[k]; requestConfig.headers[k] = v; } } if (config.qs) { requestConfig.params = config.qs; } if (typeof(config.json) !== "undefined") { if (config.json === true && config.body) { requestConfig.data = config.body; } else if (config.json === true) { // allow content type to be assigned only } else { requestConfig.data = config.json; } if (!requestConfig.headers["content-type"]) { requestConfig.headers["content-type"] = "application/json"; } } else if (config.data) { requestConfig.data = config.data; if (!requestConfig.headers["content-type"]) { if (!requestConfig.data) { if (requestConfig.data.getHeaders) { // assume this is a FormData and skip } else if (typeof(requestConfig.data) === "object") { // send as json requestConfig.headers["content-type"] = "application/json"; } } } } if (config.responseType) { requestConfig.responseType = config.responseType; } // apply basic auth if (config.auth) { if (config.auth.user) { config.auth.username = config.auth.user; delete config.auth.user; } if (config.auth.pass) { config.auth.password = config.auth.pass; delete config.auth.pass; } requestConfig.auth = config.auth; } // apply agent settings (support for http/https proxy and agents) applyAgent(requestConfig.url, requestConfig); // run the request return axios.request(requestConfig).then(function(response) { callback(null, response, response.data); }, function(error) { callback(error); }); };