11import axios , { AxiosError } from 'axios' ;
2- import type { AxiosResponse , CancelTokenSource , CreateAxiosDefaults , InternalAxiosRequestConfig } from 'axios' ;
2+ import type { AxiosResponse , CreateAxiosDefaults , InternalAxiosRequestConfig } from 'axios' ;
33import axiosRetry from 'axios-retry' ;
44import { nanoid } from '@sa/utils' ;
55import { createAxiosConfig , createDefaultOptions , createRetryOptions } from './options' ;
@@ -22,7 +22,7 @@ function createCommonRequest<ResponseData = any>(
2222 const axiosConf = createAxiosConfig ( axiosConfig ) ;
2323 const instance = axios . create ( axiosConf ) ;
2424
25- const cancelTokenSourceMap = new Map < string , CancelTokenSource > ( ) ;
25+ const abortControllerMap = new Map < string , AbortController > ( ) ;
2626
2727 // config axios retry
2828 const retryOptions = createRetryOptions ( axiosConf ) ;
@@ -35,10 +35,12 @@ function createCommonRequest<ResponseData = any>(
3535 const requestId = nanoid ( ) ;
3636 config . headers . set ( REQUEST_ID_KEY , requestId ) ;
3737
38- // config cancel token
39- const cancelTokenSource = axios . CancelToken . source ( ) ;
40- config . cancelToken = cancelTokenSource . token ;
41- cancelTokenSourceMap . set ( requestId , cancelTokenSource ) ;
38+ // config abort controller
39+ if ( ! config . signal ) {
40+ const abortController = new AbortController ( ) ;
41+ config . signal = abortController . signal ;
42+ abortControllerMap . set ( requestId , abortController ) ;
43+ }
4244
4345 // handle config by hook
4446 const handledConfig = opts . onRequest ?.( config ) || config ;
@@ -79,18 +81,18 @@ function createCommonRequest<ResponseData = any>(
7981 ) ;
8082
8183 function cancelRequest ( requestId : string ) {
82- const cancelTokenSource = cancelTokenSourceMap . get ( requestId ) ;
83- if ( cancelTokenSource ) {
84- cancelTokenSource . cancel ( ) ;
85- cancelTokenSourceMap . delete ( requestId ) ;
84+ const abortController = abortControllerMap . get ( requestId ) ;
85+ if ( abortController ) {
86+ abortController . abort ( ) ;
87+ abortControllerMap . delete ( requestId ) ;
8688 }
8789 }
8890
8991 function cancelAllRequest ( ) {
90- cancelTokenSourceMap . forEach ( cancelTokenSource => {
91- cancelTokenSource . cancel ( ) ;
92+ abortControllerMap . forEach ( abortController => {
93+ abortController . abort ( ) ;
9294 } ) ;
93- cancelTokenSourceMap . clear ( ) ;
95+ abortControllerMap . clear ( ) ;
9496 }
9597
9698 return {
0 commit comments