partey_workadventure/front/src/Connexion/AxiosUtils.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import axios from "axios";
import * as rax from "retry-axios";
2021-12-04 18:30:12 +01:00
import { errorStore } from "../Stores/ErrorStore";
2022-01-21 17:06:03 +01:00
import LL from "../i18n/i18n-svelte";
import { get } from "svelte/store";
/**
* This instance of Axios will retry in case of an issue and display an error message as a HTML overlay.
*/
export const axiosWithRetry = axios.create();
axiosWithRetry.defaults.raxConfig = {
instance: axiosWithRetry,
retry: Infinity,
noResponseRetries: Infinity,
maxRetryAfter: 60_000,
statusCodesToRetry: [
[100, 199],
[429, 429],
[501, 599],
],
// You can detect when a retry is happening, and figure out how many
// retry attempts have been made
2021-12-04 18:30:12 +01:00
onRetryAttempt: (err) => {
const cfg = rax.getConfig(err);
2021-12-04 18:30:12 +01:00
console.log(err);
console.log(cfg);
console.log(`Retry attempt #${cfg?.currentRetryAttempt} on URL '${err.config.url}'`);
2022-01-21 17:06:03 +01:00
errorStore.addErrorMessage(get(LL).error.connectionRetry.unableConnect(), {
closable: false,
id: "axios_retry",
});
},
};
2021-12-04 18:30:12 +01:00
axiosWithRetry.interceptors.response.use((res) => {
if (res.status < 400) {
errorStore.clearMessageById("axios_retry");
}
return res;
});
const interceptorId = rax.attach(axiosWithRetry);