How to invoke the cancelRequest func in the flatRequest? #774
Answered
by
honghuangdc
hanzhenfang
asked this question in
Q&A
-
function createCommonRequest<ResponseData = any>(
axiosConfig?: CreateAxiosDefaults,
options?: Partial<RequestOption<ResponseData>>
) {
const opts = createDefaultOptions<ResponseData>(options)
const axiosConf = createAxiosConfig(axiosConfig)
const abordControllerMap = new Map<string, AbortController>()
const instance = axios.create(axiosConf)
const retryOpts = createRetryOptions(axiosConf)
axiosRetry(instance, retryOpts)
instance.interceptors.request.use((conf) => {
const requestId = nanoid()
const config: InternalAxiosRequestConfig = { ...conf }
config.headers.set(REQUEST_ID_KEY, requestId)
if (!config.signal) {
const aborter = new AbortController()
config.signal = aborter.signal
abordControllerMap.set(requestId, aborter)
}
// 用户可能在请求开始前修改配置文件
const ultimateConf = opts.onRequest(conf) || conf
return ultimateConf
})
instance.interceptors.response.use(
async (response) => {
const responseType: ResponseType = (response.config?.responseType as ResponseType) || 'json'
if (responseType !== 'json' || opts.isBackendSuccess(response)) {
return Promise.resolve(response)
}
const fail = await opts.onBackEndFail(response, instance)
if (fail) return fail
const backendError = new AxiosError<ResponseData>(
'the backend request error',
BACKEND_ERROR_CODE,
response.config,
response.request,
response
)
await opts.onError(backendError)
return Promise.reject(backendError)
},
async (error: AxiosError<ResponseData>) => {
await opts.onError(error)
return Promise.reject(error)
}
) |
Beta Was this translation helpful? Give feedback.
Answered by
honghuangdc
Jun 26, 2025
Replies: 3 comments 3 replies
-
Then I create the flatRequest using the below function.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
My problem is: I don't know how to get the requestId out of the function. |
Beta Was this translation helpful? Give feedback.
0 replies
-
just use AbortController normally |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Azir-11
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just use AbortController normally
https://developer.mozilla.org/en-US/docs/Web/API/AbortController