Commit c41fa75265beb00f629dcda808957cb58b905bc2

Authored by Vben
1 parent 5cc9488b

feat: axios supports form-data format requests

CHANGELOG.zh_CN.md
1 ## Wip 1 ## Wip
2 2
  3 +### ✨ Features
  4 +
  5 +- axios 支持 form-data 格式请求
  6 +
3 ### ⚡ Performance Improvements 7 ### ⚡ Performance Improvements
4 8
5 - 登录界面动画优化 9 - 登录界面动画优化
package.json
@@ -59,6 +59,7 @@ @@ -59,6 +59,7 @@
59 "@types/mockjs": "^1.0.3", 59 "@types/mockjs": "^1.0.3",
60 "@types/nprogress": "^0.2.0", 60 "@types/nprogress": "^0.2.0",
61 "@types/qrcode": "^1.4.0", 61 "@types/qrcode": "^1.4.0",
  62 + "@types/qs": "^6.9.5",
62 "@types/rollup-plugin-visualizer": "^2.6.0", 63 "@types/rollup-plugin-visualizer": "^2.6.0",
63 "@types/sortablejs": "^1.10.6", 64 "@types/sortablejs": "^1.10.6",
64 "@types/yargs": "^16.0.0", 65 "@types/yargs": "^16.0.0",
@@ -101,7 +102,7 @@ @@ -101,7 +102,7 @@
101 "vite-plugin-mock": "^2.1.5", 102 "vite-plugin-mock": "^2.1.5",
102 "vite-plugin-purge-icons": "^0.7.0", 103 "vite-plugin-purge-icons": "^0.7.0",
103 "vite-plugin-pwa": "^0.5.3", 104 "vite-plugin-pwa": "^0.5.3",
104 - "vite-plugin-style-import": "^0.7.4", 105 + "vite-plugin-style-import": "^0.7.5",
105 "vite-plugin-theme": "^0.4.8", 106 "vite-plugin-theme": "^0.4.8",
106 "vite-plugin-windicss": "0.4.12", 107 "vite-plugin-windicss": "0.4.12",
107 "vue-eslint-parser": "^7.5.0", 108 "vue-eslint-parser": "^7.5.0",
src/utils/http/axios/Axios.ts
@@ -8,6 +8,8 @@ import { cloneDeep } from 'lodash-es'; @@ -8,6 +8,8 @@ import { cloneDeep } from 'lodash-es';
8 import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types'; 8 import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types';
9 import { errorResult } from './const'; 9 import { errorResult } from './const';
10 import { ContentTypeEnum } from '/@/enums/httpEnum'; 10 import { ContentTypeEnum } from '/@/enums/httpEnum';
  11 +import qs from 'qs';
  12 +import { RequestEnum } from '../../../enums/httpEnum';
11 13
12 export * from './axiosTransform'; 14 export * from './axiosTransform';
13 15
@@ -144,6 +146,25 @@ export class VAxios { @@ -144,6 +146,25 @@ export class VAxios {
144 }); 146 });
145 } 147 }
146 148
  149 + // support form-data
  150 + supportFormData(config: AxiosRequestConfig) {
  151 + const headers = this.options?.headers;
  152 + const contentType = headers?.['Content-Type'] || headers?.['content-type'];
  153 +
  154 + if (
  155 + contentType !== ContentTypeEnum.FORM_URLENCODED ||
  156 + !Reflect.has(config, 'data') ||
  157 + config.method?.toUpperCase() === RequestEnum.GET
  158 + ) {
  159 + return config;
  160 + }
  161 +
  162 + return {
  163 + ...config,
  164 + data: qs.stringify(config.data),
  165 + };
  166 + }
  167 +
147 request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { 168 request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
148 let conf: AxiosRequestConfig = cloneDeep(config); 169 let conf: AxiosRequestConfig = cloneDeep(config);
149 const transform = this.getTransform(); 170 const transform = this.getTransform();
@@ -156,6 +177,8 @@ export class VAxios { @@ -156,6 +177,8 @@ export class VAxios {
156 if (beforeRequestHook && isFunction(beforeRequestHook)) { 177 if (beforeRequestHook && isFunction(beforeRequestHook)) {
157 conf = beforeRequestHook(conf, opt); 178 conf = beforeRequestHook(conf, opt);
158 } 179 }
  180 +
  181 + conf = this.supportFormData(conf);
159 return new Promise((resolve, reject) => { 182 return new Promise((resolve, reject) => {
160 this.axiosInstance 183 this.axiosInstance
161 .request<any, AxiosResponse<Result>>(conf) 184 .request<any, AxiosResponse<Result>>(conf)
windi.config.ts
@@ -9,13 +9,13 @@ export default defineConfig({ @@ -9,13 +9,13 @@ export default defineConfig({
9 theme: { 9 theme: {
10 extend: { 10 extend: {
11 colors, 11 colors,
12 - },  
13 - screens: {  
14 - sm: '576px',  
15 - md: '768px',  
16 - lg: '992px',  
17 - xl: '1200px',  
18 - '2xl': '1600px', 12 + screens: {
  13 + sm: '576px',
  14 + md: '768px',
  15 + lg: '992px',
  16 + xl: '1200px',
  17 + '2xl': '1600px',
  18 + },
19 }, 19 },
20 }, 20 },
21 }); 21 });
yarn.lock
@@ -1446,6 +1446,11 @@ @@ -1446,6 +1446,11 @@
1446 dependencies: 1446 dependencies:
1447 "@types/node" "*" 1447 "@types/node" "*"
1448 1448
  1449 +"@types/qs@^6.9.5":
  1450 + version "6.9.5"
  1451 + resolved "https://registry.npmjs.org/@types/qs/-/qs-6.9.5.tgz#434711bdd49eb5ee69d90c1d67c354a9a8ecb18b"
  1452 + integrity sha512-/JHkVHtx/REVG0VVToGRGH2+23hsYLHdyG+GrvoUGlGAd0ErauXDyvHtRI/7H7mzLm+tBCKA7pfcpkQ1lf58iQ==
  1453 +
1449 "@types/resolve@1.17.1": 1454 "@types/resolve@1.17.1":
1450 version "1.17.1" 1455 version "1.17.1"
1451 resolved "https://registry.npmjs.org/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6" 1456 resolved "https://registry.npmjs.org/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
@@ -3567,6 +3572,11 @@ es-module-lexer@^0.3.26: @@ -3567,6 +3572,11 @@ es-module-lexer@^0.3.26:
3567 resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.3.26.tgz#7b507044e97d5b03b01d4392c74ffeb9c177a83b" 3572 resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.3.26.tgz#7b507044e97d5b03b01d4392c74ffeb9c177a83b"
3568 integrity sha512-Va0Q/xqtrss45hWzP8CZJwzGSZJjDM5/MJRE3IXXnUCcVLElR9BRaE9F62BopysASyc4nM3uwhSW7FFB9nlWAA== 3573 integrity sha512-Va0Q/xqtrss45hWzP8CZJwzGSZJjDM5/MJRE3IXXnUCcVLElR9BRaE9F62BopysASyc4nM3uwhSW7FFB9nlWAA==
3569 3574
  3575 +es-module-lexer@^0.4.0:
  3576 + version "0.4.0"
  3577 + resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.4.0.tgz#21f4181cc8b7eee06855f1c59e6087c7bc4f77b0"
  3578 + integrity sha512-iuEGihqqhKWFgh72Q/Jtch7V2t/ft8w8IPP2aEN8ArYKO+IWyo6hsi96hCdgyeEDQIV3InhYQ9BlwUFPGXrbEQ==
  3579 +
3570 es-to-primitive@^1.2.1: 3580 es-to-primitive@^1.2.1:
3571 version "1.2.1" 3581 version "1.2.1"
3572 resolved "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a" 3582 resolved "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a"
@@ -8930,15 +8940,15 @@ vite-plugin-pwa@^0.5.3: @@ -8930,15 +8940,15 @@ vite-plugin-pwa@^0.5.3:
8930 pretty-bytes "^5.5.0" 8940 pretty-bytes "^5.5.0"
8931 workbox-build "^6.1.0" 8941 workbox-build "^6.1.0"
8932 8942
8933 -vite-plugin-style-import@^0.7.4:  
8934 - version "0.7.4"  
8935 - resolved "https://registry.npmjs.org/vite-plugin-style-import/-/vite-plugin-style-import-0.7.4.tgz#999d8930db67ff0b3786bca25187cc1dc734befa"  
8936 - integrity sha512-a9f44QXEz7D/YLmykkK1Oif9IOOsqfHZRjsaNBwpWdcF7zfL4OE93Z8Xm++Qm+jUuHLuo9BGcfDAwY7+ObxRkQ== 8943 +vite-plugin-style-import@^0.7.5:
  8944 + version "0.7.5"
  8945 + resolved "https://registry.npmjs.org/vite-plugin-style-import/-/vite-plugin-style-import-0.7.5.tgz#da0455fd79e273767e84ead66e96b82a10cc891c"
  8946 + integrity sha512-0jdP+fnt/duEmpS6gaI5yfLNUNF2KIDcQIzWAH4w1R+fwK98Zt3F+UZprIQAlreRCD+WDLPJJ/M4ECeqKzCtUQ==
8937 dependencies: 8947 dependencies:
8938 "@rollup/pluginutils" "^4.1.0" 8948 "@rollup/pluginutils" "^4.1.0"
8939 change-case "^4.1.2" 8949 change-case "^4.1.2"
8940 debug "^4.3.2" 8950 debug "^4.3.2"
8941 - es-module-lexer "^0.3.26" 8951 + es-module-lexer "^0.4.0"
8942 magic-string "^0.25.7" 8952 magic-string "^0.25.7"
8943 8953
8944 vite-plugin-theme@^0.4.8: 8954 vite-plugin-theme@^0.4.8: