udpate API client
This commit is contained in:
1 parent
30631d6ff8
commit
8d6bc7bc0b
3 files changed
+44
-25
No files matched your search
@@ -98,7 +98,7 @@ const UptimeMonitorsPage = () => {
|
|||||||
} = useGet<MonitorsWithChecksResponse>(
|
} = useGet<MonitorsWithChecksResponse>(
|
||||||
monitorsWithChecksUrl,
|
monitorsWithChecksUrl,
|
||||||
{},
|
{},
|
||||||
{ refreshInterval: 10000 }
|
{ refreshInterval: 5000 }
|
||||||
);
|
);
|
||||||
|
|
||||||
const { monitors: monitorsWithChecks, summary, count } = monitorsWithChecksData ?? {};
|
const { monitors: monitorsWithChecks, summary, count } = monitorsWithChecksData ?? {};
|
||||||
|
|||||||
@@ -1,38 +1,54 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import type { AxiosError } from "axios";
|
import type { AxiosError } from "axios";
|
||||||
import type { AxiosRequestConfig, AxiosResponse } from "axios";
|
import type { AxiosRequestConfig, AxiosResponse } from "axios";
|
||||||
import { store } from "@/store";
|
|
||||||
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
||||||
|
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: BASE_URL,
|
baseURL: BASE_URL,
|
||||||
});
|
});
|
||||||
|
|
||||||
api.interceptors.request.use(
|
type StoreType = {
|
||||||
(config) => {
|
getState: () => { auth?: { authToken?: string } };
|
||||||
const authToken = store.getState().auth?.authToken ?? "";
|
|
||||||
|
|
||||||
config.headers.set("Authorization", `Bearer ${authToken}`);
|
|
||||||
if (!config.headers.has("Accept-Language")) {
|
|
||||||
config.headers.set("Accept-Language", "en");
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const onSuccess = (response: AxiosResponse) => response;
|
|
||||||
const onError = (error: AxiosError) => {
|
|
||||||
if (error.response?.status === 401) {
|
|
||||||
window.location.href = "/login";
|
|
||||||
}
|
|
||||||
return Promise.reject(error);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
api.interceptors.response.use(onSuccess, onError);
|
let storeInstance: StoreType | null = null;
|
||||||
|
let interceptorsInitialized = false;
|
||||||
|
|
||||||
|
export const initApiClient = (store: StoreType): void => {
|
||||||
|
storeInstance = store;
|
||||||
|
|
||||||
|
if (interceptorsInitialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
interceptorsInitialized = true;
|
||||||
|
|
||||||
|
api.interceptors.request.use(
|
||||||
|
(config) => {
|
||||||
|
const authToken = storeInstance?.getState()?.auth?.authToken ?? "";
|
||||||
|
|
||||||
|
config.headers.set("Authorization", `Bearer ${authToken}`);
|
||||||
|
if (!config.headers.has("Accept-Language")) {
|
||||||
|
config.headers.set("Accept-Language", "en");
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const onSuccess = (response: AxiosResponse) => response;
|
||||||
|
const onError = (error: AxiosError) => {
|
||||||
|
if (error.response?.status === 401) {
|
||||||
|
window.location.href = "/login";
|
||||||
|
}
|
||||||
|
return Promise.reject(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
api.interceptors.response.use(onSuccess, onError);
|
||||||
|
};
|
||||||
|
|
||||||
export const get = <T>(
|
export const get = <T>(
|
||||||
url: string,
|
url: string,
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import NetworkServiceProvider from "./Utils/NetworkServiceProvider.jsx";
|
|||||||
import { networkService } from "./Utils/NetworkService";
|
import { networkService } from "./Utils/NetworkService";
|
||||||
export { networkService };
|
export { networkService };
|
||||||
import I18nLoader from "./Components/v1/I18nLoader";
|
import I18nLoader from "./Components/v1/I18nLoader";
|
||||||
|
import { initApiClient } from "./Utils/ApiClient";
|
||||||
|
|
||||||
|
initApiClient(store);
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root")).render(
|
ReactDOM.createRoot(document.getElementById("root")).render(
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
|
|||||||
Reference in new issue
Block a user