mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Adding support for beforeRedirect config option (#3852)
* Adding support for beforeRedirect config option * Adding tests for beforeRedirect * Update README.md Co-authored-by: Prabodh Meshram <prabodh.meshram7@gmail.com> * fix types Co-authored-by: Prabodh Meshram <prabodh.meshram7@gmail.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -414,7 +414,18 @@ These are the available config options for making requests. Only the `url` is re
|
||||
|
||||
// `maxRedirects` defines the maximum number of redirects to follow in node.js.
|
||||
// If set to 0, no redirects will be followed.
|
||||
maxRedirects: 5, // default
|
||||
maxRedirects: 21, // default
|
||||
|
||||
// `beforeRedirect` defines a function that will be called before redirect.
|
||||
// Use this to adjust the request options upon redirecting,
|
||||
// to inspect the latest response headers,
|
||||
// or to cancel the request by throwing an error
|
||||
// If maxRedirects is set to 0, `beforeRedirect` is not used.
|
||||
beforeRedirect: (options, { headers }) => {
|
||||
if (options.hostname === "example.com") {
|
||||
options.auth = "user:password";
|
||||
}
|
||||
};
|
||||
|
||||
// `socketPath` defines a UNIX Socket to be used in node.js.
|
||||
// e.g. '/var/run/docker.sock' to send requests to the docker daemon.
|
||||
|
||||
Vendored
+1
-1
@@ -1,5 +1,4 @@
|
||||
// TypeScript Version: 3.0
|
||||
|
||||
export type AxiosRequestHeaders = Record<string, string | number | boolean>;
|
||||
|
||||
export type AxiosResponseHeaders = Record<string, string> & {
|
||||
@@ -98,6 +97,7 @@ export interface AxiosRequestConfig<D = any> {
|
||||
validateStatus?: ((status: number) => boolean) | null;
|
||||
maxBodyLength?: number;
|
||||
maxRedirects?: number;
|
||||
beforeRedirect?: (options: Record<string, any>, responseDetails: {headers: Record<string, string>}) => void;
|
||||
socketPath?: string | null;
|
||||
httpAgent?: any;
|
||||
httpsAgent?: any;
|
||||
|
||||
@@ -227,6 +227,9 @@ module.exports = function httpAdapter(config) {
|
||||
if (config.maxRedirects) {
|
||||
options.maxRedirects = config.maxRedirects;
|
||||
}
|
||||
if (config.beforeRedirect) {
|
||||
options.beforeRedirect = config.beforeRedirect;
|
||||
}
|
||||
transport = isHttpsProxy ? httpsFollow : httpFollow;
|
||||
}
|
||||
|
||||
@@ -326,7 +329,7 @@ module.exports = function httpAdapter(config) {
|
||||
|
||||
// Handle errors
|
||||
req.on('error', function handleRequestError(err) {
|
||||
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
|
||||
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') reject(err);
|
||||
reject(enhanceError(err, config, null, req));
|
||||
});
|
||||
|
||||
|
||||
@@ -80,6 +80,7 @@ module.exports = function mergeConfig(config1, config2) {
|
||||
'decompress': defaultToConfig2,
|
||||
'maxContentLength': defaultToConfig2,
|
||||
'maxBodyLength': defaultToConfig2,
|
||||
'beforeRedirect': defaultToConfig2,
|
||||
'transport': defaultToConfig2,
|
||||
'httpAgent': defaultToConfig2,
|
||||
'httpsAgent': defaultToConfig2,
|
||||
|
||||
@@ -241,6 +241,26 @@ describe('supports http with nodejs', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should support beforeRedirect', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
res.setHeader('Location', '/foo');
|
||||
res.statusCode = 302;
|
||||
res.end();
|
||||
}).listen(4444, function () {
|
||||
axios.get('http://localhost:4444/', {
|
||||
maxRedirects: 3,
|
||||
beforeRedirect: function (options) {
|
||||
if (options.path === '/foo') {
|
||||
throw new Error('path not allowed');
|
||||
}
|
||||
}
|
||||
}).catch(function (error) {
|
||||
assert.equal(error.toJSON().message, 'path not allowed')
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should preserve the HTTP verb on redirect', function (done) {
|
||||
server = http.createServer(function (req, res) {
|
||||
if (req.method.toLowerCase() !== "head") {
|
||||
|
||||
Reference in New Issue
Block a user