|
|
@@ -20,7 +20,7 @@ import axios, {
|
|
|
AxiosRequestConfig,
|
|
|
AxiosResponse,
|
|
|
AxiosResponseTransformer,
|
|
|
- CancelToken
|
|
|
+ InternalAxiosRequestConfig
|
|
|
} from 'axios';
|
|
|
import qs from 'qs';
|
|
|
|
|
|
@@ -48,6 +48,13 @@ export enum HttpMethod {
|
|
|
PATCH = 'patch'
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Options for API fetch functions.
|
|
|
+ *
|
|
|
+ * Note: This extends AxiosRequestConfig (not InternalAxiosRequestConfig) because it's the
|
|
|
+ * public API for users to pass options to axios requests. InternalAxiosRequestConfig is
|
|
|
+ * only used for the internal config object inside interceptors.
|
|
|
+ */
|
|
|
export interface ApiFetchOptions<T, E = AxiosError<DefaultApiResponse>> extends AxiosRequestConfig {
|
|
|
silenceErrors?: boolean;
|
|
|
ignoreSuccessErrors?: boolean;
|
|
|
@@ -71,7 +78,7 @@ const axiosInstance = axios.create({ withCredentials: true });
|
|
|
let baseUrl = (window as hueWindow).HUE_BASE_URL;
|
|
|
let bearerToken: string | undefined;
|
|
|
|
|
|
-axiosInstance.interceptors.request.use(config => {
|
|
|
+axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
|
if (baseUrl) {
|
|
|
config.baseURL = baseUrl;
|
|
|
}
|
|
|
@@ -194,9 +201,15 @@ const handleResponse = <T, E = unknown>(
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const getCancelToken = (): { cancelToken: CancelToken; cancel: () => void } => {
|
|
|
- const cancelTokenSource = axios.CancelToken.source();
|
|
|
- return { cancelToken: cancelTokenSource.token, cancel: cancelTokenSource.cancel };
|
|
|
+/**
|
|
|
+ * Creates an AbortController for cancelling axios requests.
|
|
|
+ * This replaces the deprecated CancelToken API with the standard AbortController API.
|
|
|
+ *
|
|
|
+ * @returns An object containing the AbortSignal and abort function
|
|
|
+ */
|
|
|
+const getAbortController = (): { signal: AbortSignal; abort: () => void } => {
|
|
|
+ const controller = new AbortController();
|
|
|
+ return { signal: controller.signal, abort: () => controller.abort() };
|
|
|
};
|
|
|
|
|
|
// Shared HTTP method for post, put, patch requests
|
|
|
@@ -207,13 +220,13 @@ export const sendApiRequest = <T, U = unknown, E = AxiosError>(
|
|
|
options?: ApiFetchOptions<T, E>
|
|
|
): CancellablePromise<T> =>
|
|
|
new CancellablePromise((resolve, reject, onCancel) => {
|
|
|
- const { cancelToken, cancel } = getCancelToken();
|
|
|
+ const { signal, abort } = getAbortController();
|
|
|
let completed = false;
|
|
|
|
|
|
const encodeData = options?.qsEncodeData == undefined || options?.qsEncodeData;
|
|
|
|
|
|
axiosInstance[method]<T & DefaultApiResponse>(url, encodeData ? qs.stringify(data) : data, {
|
|
|
- cancelToken,
|
|
|
+ signal,
|
|
|
...options
|
|
|
})
|
|
|
.then(response => {
|
|
|
@@ -235,7 +248,7 @@ export const sendApiRequest = <T, U = unknown, E = AxiosError>(
|
|
|
|
|
|
onCancel(() => {
|
|
|
if (!completed) {
|
|
|
- cancel();
|
|
|
+ abort();
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
@@ -264,12 +277,12 @@ export const get = <T, U = unknown, E = AxiosError<DefaultApiResponse>>(
|
|
|
options?: ApiFetchOptions<T, E>
|
|
|
): CancellablePromise<T> =>
|
|
|
new CancellablePromise((resolve, reject, onCancel) => {
|
|
|
- const { cancelToken, cancel } = getCancelToken();
|
|
|
+ const { signal, abort } = getAbortController();
|
|
|
let completed = false;
|
|
|
|
|
|
axiosInstance
|
|
|
.get<T & DefaultApiResponse>(url, {
|
|
|
- cancelToken,
|
|
|
+ signal,
|
|
|
params: data
|
|
|
})
|
|
|
.then(response => {
|
|
|
@@ -284,7 +297,7 @@ export const get = <T, U = unknown, E = AxiosError<DefaultApiResponse>>(
|
|
|
|
|
|
onCancel(() => {
|
|
|
if (!completed) {
|
|
|
- cancel();
|
|
|
+ abort();
|
|
|
}
|
|
|
});
|
|
|
});
|