refactor: switch to using abort controller, and allow signal to be overridden

This commit is contained in:
Derrick Hammer 2024-03-21 16:04:20 -04:00
parent 6bdc43dd85
commit b6f335bc09
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 3 additions and 3 deletions

View File

@ -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>;