Commit ccaa84c3058c7f7ff5ac9ce14ec1d951c87fa5a6
Committed by
GitHub
1 parent
357beaba
fix: axios type (#2678)
* fix: axios type * fix: axios type
Showing
4 changed files
with
35 additions
and
24 deletions
package.json
@@ -75,7 +75,7 @@ | @@ -75,7 +75,7 @@ | ||
75 | "@vueuse/shared": "^9.13.0", | 75 | "@vueuse/shared": "^9.13.0", |
76 | "@zxcvbn-ts/core": "^2.2.1", | 76 | "@zxcvbn-ts/core": "^2.2.1", |
77 | "ant-design-vue": "^3.2.17", | 77 | "ant-design-vue": "^3.2.17", |
78 | - "axios": "^1.3.4", | 78 | + "axios": "^1.3.5", |
79 | "codemirror": "^5.65.12", | 79 | "codemirror": "^5.65.12", |
80 | "cropperjs": "^1.5.13", | 80 | "cropperjs": "^1.5.13", |
81 | "crypto-js": "^4.1.1", | 81 | "crypto-js": "^4.1.1", |
pnpm-lock.yaml
@@ -32,8 +32,8 @@ importers: | @@ -32,8 +32,8 @@ importers: | ||
32 | specifier: ^3.2.17 | 32 | specifier: ^3.2.17 |
33 | version: 3.2.17(vue@3.2.47) | 33 | version: 3.2.17(vue@3.2.47) |
34 | axios: | 34 | axios: |
35 | - specifier: ^1.3.4 | ||
36 | - version: 1.3.4 | 35 | + specifier: ^1.3.5 |
36 | + version: 1.3.5 | ||
37 | codemirror: | 37 | codemirror: |
38 | specifier: ^5.65.12 | 38 | specifier: ^5.65.12 |
39 | version: 5.65.12 | 39 | version: 5.65.12 |
@@ -3021,8 +3021,8 @@ packages: | @@ -3021,8 +3021,8 @@ packages: | ||
3021 | - debug | 3021 | - debug |
3022 | dev: true | 3022 | dev: true |
3023 | 3023 | ||
3024 | - /axios@1.3.4: | ||
3025 | - resolution: {integrity: sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==} | 3024 | + /axios@1.3.5: |
3025 | + resolution: {integrity: sha512-glL/PvG/E+xCWwV8S6nCHcrfg1exGx7vxyUIivIA1iL7BIh6bePylCfVHwp6k13ao7SATxB6imau2kqY+I67kw==} | ||
3026 | dependencies: | 3026 | dependencies: |
3027 | follow-redirects: 1.15.2(debug@4.3.4) | 3027 | follow-redirects: 1.15.2(debug@4.3.4) |
3028 | form-data: 4.0.0 | 3028 | form-data: 4.0.0 |
src/utils/http/axios/Axios.ts
1 | -import type { AxiosRequestConfig, AxiosInstance, AxiosResponse, AxiosError } from 'axios'; | 1 | +import type { |
2 | + AxiosRequestConfig, | ||
3 | + AxiosInstance, | ||
4 | + AxiosResponse, | ||
5 | + AxiosError, | ||
6 | + InternalAxiosRequestConfig, | ||
7 | +} from 'axios'; | ||
2 | import type { RequestOptions, Result, UploadFileParams } from '/#/axios'; | 8 | import type { RequestOptions, Result, UploadFileParams } from '/#/axios'; |
3 | import type { CreateAxiosOptions } from './axiosTransform'; | 9 | import type { CreateAxiosOptions } from './axiosTransform'; |
4 | import axios from 'axios'; | 10 | import axios from 'axios'; |
@@ -63,7 +69,11 @@ export class VAxios { | @@ -63,7 +69,11 @@ export class VAxios { | ||
63 | * @description: Interceptor configuration 拦截器配置 | 69 | * @description: Interceptor configuration 拦截器配置 |
64 | */ | 70 | */ |
65 | private setupInterceptors() { | 71 | private setupInterceptors() { |
66 | - const transform = this.getTransform(); | 72 | + // const transform = this.getTransform(); |
73 | + const { | ||
74 | + axiosInstance, | ||
75 | + options: { transform }, | ||
76 | + } = this; | ||
67 | if (!transform) { | 77 | if (!transform) { |
68 | return; | 78 | return; |
69 | } | 79 | } |
@@ -77,16 +87,13 @@ export class VAxios { | @@ -77,16 +87,13 @@ export class VAxios { | ||
77 | const axiosCanceler = new AxiosCanceler(); | 87 | const axiosCanceler = new AxiosCanceler(); |
78 | 88 | ||
79 | // Request interceptor configuration processing | 89 | // Request interceptor configuration processing |
80 | - this.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => { | 90 | + this.axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => { |
81 | // If cancel repeat request is turned on, then cancel repeat request is prohibited | 91 | // If cancel repeat request is turned on, then cancel repeat request is prohibited |
82 | - // @ts-ignore | ||
83 | - const { ignoreCancelToken } = config.requestOptions; | ||
84 | - const ignoreCancel = | ||
85 | - ignoreCancelToken !== undefined | ||
86 | - ? ignoreCancelToken | ||
87 | - : this.options.requestOptions?.ignoreCancelToken; | ||
88 | - | ||
89 | - !ignoreCancel && axiosCanceler.addPending(config); | 92 | + const { requestOptions } = this.options; |
93 | + const ignoreCancelToken = requestOptions?.ignoreCancelToken ?? true; | ||
94 | + | ||
95 | + !ignoreCancelToken && axiosCanceler.addPending(config); | ||
96 | + | ||
90 | if (requestInterceptors && isFunction(requestInterceptors)) { | 97 | if (requestInterceptors && isFunction(requestInterceptors)) { |
91 | config = requestInterceptors(config, this.options); | 98 | config = requestInterceptors(config, this.options); |
92 | } | 99 | } |
@@ -111,8 +118,7 @@ export class VAxios { | @@ -111,8 +118,7 @@ export class VAxios { | ||
111 | responseInterceptorsCatch && | 118 | responseInterceptorsCatch && |
112 | isFunction(responseInterceptorsCatch) && | 119 | isFunction(responseInterceptorsCatch) && |
113 | this.axiosInstance.interceptors.response.use(undefined, (error) => { | 120 | this.axiosInstance.interceptors.response.use(undefined, (error) => { |
114 | - // @ts-ignore | ||
115 | - return responseInterceptorsCatch(this.axiosInstance, error); | 121 | + return responseInterceptorsCatch(axiosInstance, error); |
116 | }); | 122 | }); |
117 | } | 123 | } |
118 | 124 |
src/utils/http/axios/axiosTransform.ts
1 | /** | 1 | /** |
2 | * Data processing class, can be configured according to the project | 2 | * Data processing class, can be configured according to the project |
3 | */ | 3 | */ |
4 | -import type { AxiosRequestConfig, AxiosResponse } from 'axios'; | 4 | +import type { |
5 | + AxiosInstance, | ||
6 | + AxiosRequestConfig, | ||
7 | + AxiosResponse, | ||
8 | + InternalAxiosRequestConfig | ||
9 | +} from 'axios'; | ||
5 | import type { RequestOptions, Result } from '/#/axios'; | 10 | import type { RequestOptions, Result } from '/#/axios'; |
6 | 11 | ||
7 | export interface CreateAxiosOptions extends AxiosRequestConfig { | 12 | export interface CreateAxiosOptions extends AxiosRequestConfig { |
@@ -12,8 +17,8 @@ export interface CreateAxiosOptions extends AxiosRequestConfig { | @@ -12,8 +17,8 @@ export interface CreateAxiosOptions extends AxiosRequestConfig { | ||
12 | 17 | ||
13 | export abstract class AxiosTransform { | 18 | export abstract class AxiosTransform { |
14 | /** | 19 | /** |
15 | - * @description: Process configuration before request | ||
16 | - * @description: Process configuration before request | 20 | + * A function that is called before a request is sent. It can modify the request configuration as needed. |
21 | + * 在发送请求之前调用的函数。它可以根据需要修改请求配置。 | ||
17 | */ | 22 | */ |
18 | beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig; | 23 | beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig; |
19 | 24 | ||
@@ -31,9 +36,9 @@ export abstract class AxiosTransform { | @@ -31,9 +36,9 @@ export abstract class AxiosTransform { | ||
31 | * @description: 请求之前的拦截器 | 36 | * @description: 请求之前的拦截器 |
32 | */ | 37 | */ |
33 | requestInterceptors?: ( | 38 | requestInterceptors?: ( |
34 | - config: AxiosRequestConfig, | 39 | + config: InternalAxiosRequestConfig, |
35 | options: CreateAxiosOptions, | 40 | options: CreateAxiosOptions, |
36 | - ) => AxiosRequestConfig; | 41 | + ) => InternalAxiosRequestConfig; |
37 | 42 | ||
38 | /** | 43 | /** |
39 | * @description: 请求之后的拦截器 | 44 | * @description: 请求之后的拦截器 |
@@ -48,5 +53,5 @@ export abstract class AxiosTransform { | @@ -48,5 +53,5 @@ export abstract class AxiosTransform { | ||
48 | /** | 53 | /** |
49 | * @description: 请求之后的拦截器错误处理 | 54 | * @description: 请求之后的拦截器错误处理 |
50 | */ | 55 | */ |
51 | - responseInterceptorsCatch?: (axiosInstance: AxiosResponse, error: Error) => void; | 56 | + responseInterceptorsCatch?: (axiosInstance: AxiosInstance, error: Error) => void; |
52 | } | 57 | } |