This commit is contained in:
parent
06971f3935
commit
527fd79df2
@ -1,5 +1,5 @@
|
|||||||
import axios, { AxiosError } from 'axios';
|
import axios, { AxiosError } from 'axios';
|
||||||
import type { AxiosResponse, CancelTokenSource, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
|
import type { AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
|
||||||
import axiosRetry from 'axios-retry';
|
import axiosRetry from 'axios-retry';
|
||||||
import { nanoid } from '@sa/utils';
|
import { nanoid } from '@sa/utils';
|
||||||
import { createAxiosConfig, createDefaultOptions, createRetryOptions } from './options';
|
import { createAxiosConfig, createDefaultOptions, createRetryOptions } from './options';
|
||||||
@ -22,7 +22,7 @@ function createCommonRequest<ResponseData = any>(
|
|||||||
const axiosConf = createAxiosConfig(axiosConfig);
|
const axiosConf = createAxiosConfig(axiosConfig);
|
||||||
const instance = axios.create(axiosConf);
|
const instance = axios.create(axiosConf);
|
||||||
|
|
||||||
const cancelTokenSourceMap = new Map<string, CancelTokenSource>();
|
const abortControllerMap = new Map<string, AbortController>();
|
||||||
|
|
||||||
// config axios retry
|
// config axios retry
|
||||||
const retryOptions = createRetryOptions(axiosConf);
|
const retryOptions = createRetryOptions(axiosConf);
|
||||||
@ -35,10 +35,12 @@ function createCommonRequest<ResponseData = any>(
|
|||||||
const requestId = nanoid();
|
const requestId = nanoid();
|
||||||
config.headers.set(REQUEST_ID_KEY, requestId);
|
config.headers.set(REQUEST_ID_KEY, requestId);
|
||||||
|
|
||||||
// config cancel token
|
// config abort controller
|
||||||
const cancelTokenSource = axios.CancelToken.source();
|
if (!config.signal) {
|
||||||
config.cancelToken = cancelTokenSource.token;
|
const abortController = new AbortController();
|
||||||
cancelTokenSourceMap.set(requestId, cancelTokenSource);
|
config.signal = abortController.signal;
|
||||||
|
abortControllerMap.set(requestId, abortController);
|
||||||
|
}
|
||||||
|
|
||||||
// handle config by hook
|
// handle config by hook
|
||||||
const handledConfig = opts.onRequest?.(config) || config;
|
const handledConfig = opts.onRequest?.(config) || config;
|
||||||
@ -79,18 +81,18 @@ function createCommonRequest<ResponseData = any>(
|
|||||||
);
|
);
|
||||||
|
|
||||||
function cancelRequest(requestId: string) {
|
function cancelRequest(requestId: string) {
|
||||||
const cancelTokenSource = cancelTokenSourceMap.get(requestId);
|
const abortController = abortControllerMap.get(requestId);
|
||||||
if (cancelTokenSource) {
|
if (abortController) {
|
||||||
cancelTokenSource.cancel();
|
abortController.abort();
|
||||||
cancelTokenSourceMap.delete(requestId);
|
abortControllerMap.delete(requestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cancelAllRequest() {
|
function cancelAllRequest() {
|
||||||
cancelTokenSourceMap.forEach(cancelTokenSource => {
|
abortControllerMap.forEach(abortController => {
|
||||||
cancelTokenSource.cancel();
|
abortController.abort();
|
||||||
});
|
});
|
||||||
cancelTokenSourceMap.clear();
|
abortControllerMap.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -69,7 +69,19 @@ export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<Axi
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface RequestInstanceCommon<T> {
|
export interface RequestInstanceCommon<T> {
|
||||||
|
/**
|
||||||
|
* cancel the request by request id
|
||||||
|
*
|
||||||
|
* if the request provide abort controller sign from config, it will not collect in the abort controller map
|
||||||
|
*
|
||||||
|
* @param requestId
|
||||||
|
*/
|
||||||
cancelRequest: (requestId: string) => void;
|
cancelRequest: (requestId: string) => void;
|
||||||
|
/**
|
||||||
|
* cancel all request
|
||||||
|
*
|
||||||
|
* if the request provide abort controller sign from config, it will not collect in the abort controller map
|
||||||
|
*/
|
||||||
cancelAllRequest: () => void;
|
cancelAllRequest: () => void;
|
||||||
/** you can set custom state in the request instance */
|
/** you can set custom state in the request instance */
|
||||||
state: T;
|
state: T;
|
||||||
|
@ -191,7 +191,7 @@ importers:
|
|||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../axios
|
version: link:../axios
|
||||||
'@sa/utils':
|
'@sa/utils':
|
||||||
specifier: workspace:^
|
specifier: workspace:*
|
||||||
version: link:../utils
|
version: link:../utils
|
||||||
|
|
||||||
packages/materials:
|
packages/materials:
|
||||||
|
Loading…
Reference in New Issue
Block a user