From dea2890d473d5c367d387e4a03807a83357add1d Mon Sep 17 00:00:00 2001 From: Ray Lee Date: Fri, 2 Feb 2024 19:53:52 -0500 Subject: [PATCH] Allow overriding headers and maxRedirects, and don't fail on redirects. --- package-lock.json | 2 +- package.json | 2 +- src/agent.js | 20 +++++++++++++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ce370f..a3af2bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.8", "license": "ECL-2.0", "dependencies": { - "axios": "^1.3.4", + "axios": "^1.6.7", "form-data": "^2.3.2", "lodash": "^4.17.19", "qs": "^6.9.4", diff --git a/package.json b/package.json index 10d106a..6f155bb 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "webpack-cli": "^5.0.1" }, "dependencies": { - "axios": "^1.3.4", + "axios": "^1.6.7", "form-data": "^2.3.2", "lodash": "^4.17.19", "qs": "^6.9.4", diff --git a/src/agent.js b/src/agent.js index db685c2..cf55fa5 100644 --- a/src/agent.js +++ b/src/agent.js @@ -101,6 +101,7 @@ function getConfig(requestConfig) { const { resource: url, url: baseURL, + maxRedirects, params, responseType, } = requestConfig; @@ -117,12 +118,12 @@ function getConfig(requestConfig) { data = requestConfig.data; } - const headers = {}; + const computedHeaders = {}; let auth = null; if (config.hasOption(requestConfig, 'token')) { - headers.Authorization = `Bearer ${requestConfig.token}`; + computedHeaders.Authorization = `Bearer ${requestConfig.token}`; } else if (requestConfig.username || requestConfig.password) { auth = { username: requestConfig.username, @@ -134,25 +135,31 @@ function getConfig(requestConfig) { const { type } = requestConfig; if (type === MIME_TYPE_FORM || type === MIME_TYPE_JSON) { - headers['Content-Type'] = `${type};charset=utf-8`; + computedHeaders['Content-Type'] = `${type};charset=utf-8`; } else if (type === MIME_TYPE_MULTIPART_FORM_DATA) { if (data && data.getHeaders) { // If we're in node, using form-data, axios won't natively know what to do (it just sees a // stream). We need to manually set the content type header to have the correct mime type // and boundary, using the getHeaders method. If we're in a browser, using the built-in // FormData, the header will be set automatically, so we don't have to do anything. - headers['Content-Type'] = data.getHeaders()['content-type']; + computedHeaders['Content-Type'] = data.getHeaders()['content-type']; } } else { - headers['Content-Type'] = type; + computedHeaders['Content-Type'] = type; } } + const headers = { + ...computedHeaders, + ...requestConfig.headers, + }; + return { url, method, baseURL, headers, + maxRedirects, params, data, auth, @@ -160,6 +167,9 @@ function getConfig(requestConfig) { paramsSerializer: { serialize: (obj) => qs.stringify(obj, { arrayFormat: 'repeat' }), }, + validateStatus: (status) => ( + status >= 200 && status < 400 + ), }; }