refactor: switch to using abort controller, and allow signal to be overridden
This commit is contained in:
parent
6bdc43dd85
commit
b6f335bc09
|
@ -9,7 +9,7 @@ export const customInstance = <T>(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
options?: AxiosRequestConfig,
|
options?: AxiosRequestConfig,
|
||||||
): CancelablePromise<T> => {
|
): CancelablePromise<T> => {
|
||||||
const source = Axios.CancelToken.source();
|
const abort = new AbortController();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Hack to ensure that the data is passed to the request as an option.
|
Hack to ensure that the data is passed to the request as an option.
|
||||||
|
@ -22,9 +22,9 @@ export const customInstance = <T>(
|
||||||
|
|
||||||
const instance = Axios.create({ baseURL: options?.baseURL });
|
const instance = Axios.create({ baseURL: options?.baseURL });
|
||||||
const promise = instance({
|
const promise = instance({
|
||||||
|
signal: abort.signal,
|
||||||
...config,
|
...config,
|
||||||
...options,
|
...options,
|
||||||
cancelToken: source.token,
|
|
||||||
})
|
})
|
||||||
.then(({ data }) => data)
|
.then(({ data }) => data)
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
@ -39,7 +39,7 @@ export const customInstance = <T>(
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
promise.cancel = () => {
|
promise.cancel = () => {
|
||||||
source.cancel("Query was cancelled");
|
abort.abort("Query was cancelled");
|
||||||
};
|
};
|
||||||
|
|
||||||
return promise as CancelablePromise<T>;
|
return promise as CancelablePromise<T>;
|
||||||
|
|
Loading…
Reference in New Issue