Skip to content

Commit c884b8b

Browse files
docs: modernize Axios examples to remove deprecated CancelToken (#2579)
- Remove deprecated Axios.CancelToken.source() usage (deprecated since Axios 0.22.0) - Remove promise.cancel() implementation (not needed in TanStack Query v4+) - Simplify custom instance implementations - Update documentation examples in guides and API reference - Update test mutators and sample code Modern Axios (1.x+) automatically supports AbortController via config.signal, and TanStack Query v4+ handles cancellation internally, making the custom cancel implementation unnecessary.
1 parent 4901a24 commit c884b8b

File tree

15 files changed

+15
-149
lines changed

15 files changed

+15
-149
lines changed

docs/src/pages/guides/custom-axios.mdx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,11 @@ export const customInstance = <T>(
3636
config: AxiosRequestConfig,
3737
options?: AxiosRequestConfig,
3838
): Promise<T> => {
39-
const source = Axios.CancelToken.source();
4039
const promise = AXIOS_INSTANCE({
4140
...config,
4241
...options,
43-
cancelToken: source.token,
4442
}).then(({ data }) => data);
4543

46-
// @ts-ignore
47-
promise.cancel = () => {
48-
source.cancel('Query was cancelled');
49-
};
50-
5144
return promise;
5245
};
5346

docs/src/pages/reference/configuration/output.mdx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -815,15 +815,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
815815
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
816816

817817
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
818-
const source = Axios.CancelToken.source();
819-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
820-
({ data }) => data,
821-
);
822-
823-
// @ts-ignore
824-
promise.cancel = () => {
825-
source.cancel('Query was cancelled by React Query');
826-
};
818+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
827819

828820
return promise;
829821
};
@@ -845,15 +837,7 @@ import config from '@config';
845837
export const AXIOS_INSTANCE = Axios.create({ baseURL: '', ...config });
846838

847839
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
848-
const source = Axios.CancelToken.source();
849-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
850-
({ data }) => data,
851-
);
852-
853-
// @ts-ignore
854-
promise.cancel = () => {
855-
source.cancel('Query was cancelled by React Query');
856-
};
840+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
857841

858842
return promise;
859843
};
@@ -917,20 +901,13 @@ export const useCustomInstance = <T>(): ((
917901
const token = useToken(); // Do what you want
918902

919903
return (config: AxiosRequestConfig) => {
920-
const source = Axios.CancelToken.source();
921904
const promise = AXIOS_INSTANCE({
922905
...config,
923906
headers: {
924-
Authorization: `Bearer ${token}`
925-
}
926-
cancelToken: source.token,
907+
Authorization: `Bearer ${token}`,
908+
},
927909
}).then(({ data }) => data);
928910

929-
// @ts-ignore
930-
promise.cancel = () => {
931-
source.cancel('Query was cancelled by React Query');
932-
};
933-
934911
return promise;
935912
};
936913
};

samples/react-app-with-swr/basic/src/api/mutator/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by React Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/basic/src/api/mutator/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosError, AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/form-data-mutator/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/form-data/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/form-url-encoded-mutator/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/form-url-encoded/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

samples/react-query/hook-mutator/use-custom-instance.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@ export const useCustomInstance = <T>(): ((
66
config: AxiosRequestConfig,
77
) => Promise<T>) => {
88
return (config: AxiosRequestConfig) => {
9-
const source = Axios.CancelToken.source();
10-
const promise = AXIOS_INSTANCE({
11-
...config,
12-
cancelToken: source.token,
13-
}).then(({ data }) => data);
14-
15-
// @ts-ignore
16-
promise.cancel = () => {
17-
source.cancel('Query was cancelled by React Query');
18-
};
9+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
1910

2011
return promise;
2112
};

samples/vue-query/vue-query-basic/src/api/mutator/custom-instance.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import Axios, { AxiosRequestConfig } from 'axios';
33
export const AXIOS_INSTANCE = Axios.create({ baseURL: '' });
44

55
export const customInstance = <T>(config: AxiosRequestConfig): Promise<T> => {
6-
const source = Axios.CancelToken.source();
7-
const promise = AXIOS_INSTANCE({ ...config, cancelToken: source.token }).then(
8-
({ data }) => data,
9-
);
10-
11-
// @ts-ignore
12-
promise.cancel = () => {
13-
source.cancel('Query was cancelled by Vue Query');
14-
};
6+
const promise = AXIOS_INSTANCE({ ...config }).then(({ data }) => data);
157

168
return promise;
179
};

0 commit comments

Comments
 (0)