Skip to content

Commit

Permalink
Allow overriding headers and maxRedirects, and don't fail on redirects.
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-lee committed Feb 3, 2024
1 parent d65a9c5 commit dea2890
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 15 additions & 5 deletions src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function getConfig(requestConfig) {
const {
resource: url,
url: baseURL,
maxRedirects,
params,
responseType,
} = requestConfig;
Expand All @@ -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,
Expand All @@ -134,32 +135,41 @@ 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,
responseType,
paramsSerializer: {
serialize: (obj) => qs.stringify(obj, { arrayFormat: 'repeat' }),
},
validateStatus: (status) => (
status >= 200 && status < 400
),
};
}

Expand Down

0 comments on commit dea2890

Please sign in to comment.