mirror of
				https://github.com/soybeanjs/soybean-admin.git
				synced 2025-11-04 15:53:43 +08:00 
			
		
		
		
	This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
			
		||||
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 { nanoid } from '@sa/utils';
 | 
			
		||||
import { createAxiosConfig, createDefaultOptions, createRetryOptions } from './options';
 | 
			
		||||
@@ -22,7 +22,7 @@ function createCommonRequest<ResponseData = any>(
 | 
			
		||||
  const axiosConf = createAxiosConfig(axiosConfig);
 | 
			
		||||
  const instance = axios.create(axiosConf);
 | 
			
		||||
 | 
			
		||||
  const cancelTokenSourceMap = new Map<string, CancelTokenSource>();
 | 
			
		||||
  const abortControllerMap = new Map<string, AbortController>();
 | 
			
		||||
 | 
			
		||||
  // config axios retry
 | 
			
		||||
  const retryOptions = createRetryOptions(axiosConf);
 | 
			
		||||
@@ -35,10 +35,12 @@ function createCommonRequest<ResponseData = any>(
 | 
			
		||||
    const requestId = nanoid();
 | 
			
		||||
    config.headers.set(REQUEST_ID_KEY, requestId);
 | 
			
		||||
 | 
			
		||||
    // config cancel token
 | 
			
		||||
    const cancelTokenSource = axios.CancelToken.source();
 | 
			
		||||
    config.cancelToken = cancelTokenSource.token;
 | 
			
		||||
    cancelTokenSourceMap.set(requestId, cancelTokenSource);
 | 
			
		||||
    // config abort controller
 | 
			
		||||
    if (!config.signal) {
 | 
			
		||||
      const abortController = new AbortController();
 | 
			
		||||
      config.signal = abortController.signal;
 | 
			
		||||
      abortControllerMap.set(requestId, abortController);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // handle config by hook
 | 
			
		||||
    const handledConfig = opts.onRequest?.(config) || config;
 | 
			
		||||
@@ -79,18 +81,18 @@ function createCommonRequest<ResponseData = any>(
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  function cancelRequest(requestId: string) {
 | 
			
		||||
    const cancelTokenSource = cancelTokenSourceMap.get(requestId);
 | 
			
		||||
    if (cancelTokenSource) {
 | 
			
		||||
      cancelTokenSource.cancel();
 | 
			
		||||
      cancelTokenSourceMap.delete(requestId);
 | 
			
		||||
    const abortController = abortControllerMap.get(requestId);
 | 
			
		||||
    if (abortController) {
 | 
			
		||||
      abortController.abort();
 | 
			
		||||
      abortControllerMap.delete(requestId);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  function cancelAllRequest() {
 | 
			
		||||
    cancelTokenSourceMap.forEach(cancelTokenSource => {
 | 
			
		||||
      cancelTokenSource.cancel();
 | 
			
		||||
    abortControllerMap.forEach(abortController => {
 | 
			
		||||
      abortController.abort();
 | 
			
		||||
    });
 | 
			
		||||
    cancelTokenSourceMap.clear();
 | 
			
		||||
    abortControllerMap.clear();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,19 @@ export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<Axi
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
  /**
 | 
			
		||||
   * cancel all request
 | 
			
		||||
   *
 | 
			
		||||
   * if the request provide abort controller sign from config, it will not collect in the abort controller map
 | 
			
		||||
   */
 | 
			
		||||
  cancelAllRequest: () => void;
 | 
			
		||||
  /** you can set custom state in the request instance */
 | 
			
		||||
  state: T;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								pnpm-lock.yaml
									
									
									
										generated
									
									
									
								
							@@ -191,7 +191,7 @@ importers:
 | 
			
		||||
        specifier: workspace:*
 | 
			
		||||
        version: link:../axios
 | 
			
		||||
      '@sa/utils':
 | 
			
		||||
        specifier: workspace:^
 | 
			
		||||
        specifier: workspace:*
 | 
			
		||||
        version: link:../utils
 | 
			
		||||
 | 
			
		||||
  packages/materials:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user