Commit c41fa75265beb00f629dcda808957cb58b905bc2

Authored by Vben
1 parent 5cc9488b

feat: axios supports form-data format requests

CHANGELOG.zh_CN.md
1 1 ## Wip
2 2  
  3 +### ✨ Features
  4 +
  5 +- axios 支持 form-data 格式请求
  6 +
3 7 ### ⚡ Performance Improvements
4 8  
5 9 - 登录界面动画优化
... ...
package.json
... ... @@ -59,6 +59,7 @@
59 59 "@types/mockjs": "^1.0.3",
60 60 "@types/nprogress": "^0.2.0",
61 61 "@types/qrcode": "^1.4.0",
  62 + "@types/qs": "^6.9.5",
62 63 "@types/rollup-plugin-visualizer": "^2.6.0",
63 64 "@types/sortablejs": "^1.10.6",
64 65 "@types/yargs": "^16.0.0",
... ... @@ -101,7 +102,7 @@
101 102 "vite-plugin-mock": "^2.1.5",
102 103 "vite-plugin-purge-icons": "^0.7.0",
103 104 "vite-plugin-pwa": "^0.5.3",
104   - "vite-plugin-style-import": "^0.7.4",
  105 + "vite-plugin-style-import": "^0.7.5",
105 106 "vite-plugin-theme": "^0.4.8",
106 107 "vite-plugin-windicss": "0.4.12",
107 108 "vue-eslint-parser": "^7.5.0",
... ...
src/utils/http/axios/Axios.ts
... ... @@ -8,6 +8,8 @@ import { cloneDeep } from 'lodash-es';
8 8 import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types';
9 9 import { errorResult } from './const';
10 10 import { ContentTypeEnum } from '/@/enums/httpEnum';
  11 +import qs from 'qs';
  12 +import { RequestEnum } from '../../../enums/httpEnum';
11 13  
12 14 export * from './axiosTransform';
13 15  
... ... @@ -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 168 request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
148 169 let conf: AxiosRequestConfig = cloneDeep(config);
149 170 const transform = this.getTransform();
... ... @@ -156,6 +177,8 @@ export class VAxios {
156 177 if (beforeRequestHook && isFunction(beforeRequestHook)) {
157 178 conf = beforeRequestHook(conf, opt);
158 179 }
  180 +
  181 + conf = this.supportFormData(conf);
159 182 return new Promise((resolve, reject) => {
160 183 this.axiosInstance
161 184 .request<any, AxiosResponse<Result>>(conf)
... ...
windi.config.ts
... ... @@ -9,13 +9,13 @@ export default defineConfig({
9 9 theme: {
10 10 extend: {
11 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 1446 dependencies:
1447 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 1454 "@types/resolve@1.17.1":
1450 1455 version "1.17.1"
1451 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 3572 resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.3.26.tgz#7b507044e97d5b03b01d4392c74ffeb9c177a83b"
3568 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 3580 es-to-primitive@^1.2.1:
3571 3581 version "1.2.1"
3572 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 8940 pretty-bytes "^5.5.0"
8931 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 8947 dependencies:
8938 8948 "@rollup/pluginutils" "^4.1.0"
8939 8949 change-case "^4.1.2"
8940 8950 debug "^4.3.2"
8941   - es-module-lexer "^0.3.26"
  8951 + es-module-lexer "^0.4.0"
8942 8952 magic-string "^0.25.7"
8943 8953  
8944 8954 vite-plugin-theme@^0.4.8:
... ...