I found that maxRedirects
-option only works in Node.js, as stated in this GitHub-issue here.
I then tried the beforeRedirect
-option, which I found on the AxiosRequestConfig
-type definitions. It didn't work either, probably due to the same reasons as maxRedirects
didn't.
What I ended up doing is using interceptors
like this:
import axios, { isAxiosError } from "axios";
import { hasOwnProperties, hasOwnProperty } from "utils";
//...
const axiosInstance = axios.create();
axiosInstance.interceptors.response.use(
// Any status codes that lie within the range of 2xx (default), or do pass custom `validateStatus()` cause this function to trigger
function (response) {
if (
hasOwnProperty(response, "request") &&
hasOwnProperties(response.request, ["requestURL", "responseURL"]) &&
response.request.requestURL !== response.request.responseURL
) {
// do your stuff
}
return response;
},
// Any status codes that falls outside the range of 2xx cause (default), or do not pass custom `validateStatus()` this function to trigger
function (error) {
if (
isAxiosError(error) &&
error.response &&
hasOwnProperties(error.response.request, ["requestURL", "responseURL"]) &&
error.response.request.requestURL !== error.response.request.responseURL
) {
// do your stuff
}
}
);