2023-09-11 13:10:46 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
import axios, { AxiosRequestConfig, AxiosError } from 'axios';
|
2023-09-18 12:55:51 -04:00
|
|
|
/* TODO: fix dependency cycle */
|
2023-09-11 13:10:46 -04:00
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
|
import { refreshToken } from './data-service';
|
|
|
|
|
import { setTokenHeader } from './headers-helpers';
|
|
|
|
|
|
|
|
|
|
let isRefreshing = false;
|
|
|
|
|
let failedQueue: { resolve: (value?: any) => void; reject: (reason?: any) => void }[] = [];
|
|
|
|
|
|
|
|
|
|
const processQueue = (error: AxiosError | null, token: string | null = null) => {
|
|
|
|
|
failedQueue.forEach((prom) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
prom.reject(error);
|
|
|
|
|
} else {
|
|
|
|
|
prom.resolve(token);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
failedQueue = [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
axios.interceptors.response.use(
|
|
|
|
|
(response) => response,
|
2023-10-17 08:34:14 -04:00
|
|
|
async (error) => {
|
2023-09-11 13:10:46 -04:00
|
|
|
const originalRequest = error.config;
|
2023-10-21 12:39:08 -04:00
|
|
|
|
2023-09-11 13:10:46 -04:00
|
|
|
if (error.response.status === 401 && !originalRequest._retry) {
|
2023-10-21 12:39:08 -04:00
|
|
|
originalRequest._retry = true;
|
|
|
|
|
|
2023-09-11 13:10:46 -04:00
|
|
|
if (isRefreshing) {
|
2023-10-17 08:34:14 -04:00
|
|
|
try {
|
2023-10-21 12:39:08 -04:00
|
|
|
const token = await new Promise((resolve, reject) => {
|
2023-10-17 08:34:14 -04:00
|
|
|
failedQueue.push({ resolve, reject });
|
2023-09-11 13:10:46 -04:00
|
|
|
});
|
2023-10-17 08:34:14 -04:00
|
|
|
originalRequest.headers['Authorization'] = 'Bearer ' + token;
|
|
|
|
|
return await axios(originalRequest);
|
|
|
|
|
} catch (err) {
|
2023-10-21 12:39:08 -04:00
|
|
|
return Promise.reject(err);
|
2023-10-17 08:34:14 -04:00
|
|
|
}
|
2023-09-11 13:10:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isRefreshing = true;
|
|
|
|
|
|
2023-10-21 12:39:08 -04:00
|
|
|
try {
|
|
|
|
|
const { token } = await refreshToken(
|
|
|
|
|
// Handle edge case where we get a blank screen if the initial 401 error is from a refresh token request
|
|
|
|
|
originalRequest.url?.includes('api/auth/refresh') ? true : false,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
originalRequest.headers['Authorization'] = 'Bearer ' + token;
|
|
|
|
|
setTokenHeader(token);
|
|
|
|
|
window.dispatchEvent(new CustomEvent('tokenUpdated', { detail: token }));
|
|
|
|
|
processQueue(null, token);
|
|
|
|
|
return await axios(originalRequest);
|
|
|
|
|
} else {
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
processQueue(err as AxiosError, null);
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
|
} finally {
|
|
|
|
|
isRefreshing = false;
|
|
|
|
|
}
|
2023-09-11 13:10:46 -04:00
|
|
|
}
|
2023-10-21 12:39:08 -04:00
|
|
|
|
2023-09-11 13:10:46 -04:00
|
|
|
return Promise.reject(error);
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-07-30 10:37:25 -04:00
|
|
|
|
|
|
|
|
async function _get<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
const response = await axios.get(url, { ...options });
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _post(url: string, data?: any) {
|
|
|
|
|
const response = await axios.post(url, JSON.stringify(data), {
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _postMultiPart(url: string, formData: FormData, options?: AxiosRequestConfig) {
|
|
|
|
|
const response = await axios.post(url, formData, {
|
|
|
|
|
...options,
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _put(url: string, data?: any) {
|
|
|
|
|
const response = await axios.put(url, JSON.stringify(data), {
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _delete<T>(url: string): Promise<T> {
|
|
|
|
|
const response = await axios.delete(url);
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _deleteWithOptions<T>(url: string, options?: AxiosRequestConfig): Promise<T> {
|
|
|
|
|
const response = await axios.delete(url, { ...options });
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function _patch(url: string, data?: any) {
|
|
|
|
|
const response = await axios.patch(url, JSON.stringify(data), {
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
get: _get,
|
|
|
|
|
post: _post,
|
|
|
|
|
postMultiPart: _postMultiPart,
|
|
|
|
|
put: _put,
|
|
|
|
|
delete: _delete,
|
|
|
|
|
deleteWithOptions: _deleteWithOptions,
|
|
|
|
|
patch: _patch,
|
|
|
|
|
};
|