Skip to content

Commit c41fa75

Browse files
committed
feat: axios supports form-data format requests
1 parent 5cc9488 commit c41fa75

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

CHANGELOG.zh_CN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Wip
22

3+
### ✨ Features
4+
5+
- axios 支持 form-data 格式请求
6+
37
### ⚡ Performance Improvements
48

59
- 登录界面动画优化

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@types/mockjs": "^1.0.3",
6060
"@types/nprogress": "^0.2.0",
6161
"@types/qrcode": "^1.4.0",
62+
"@types/qs": "^6.9.5",
6263
"@types/rollup-plugin-visualizer": "^2.6.0",
6364
"@types/sortablejs": "^1.10.6",
6465
"@types/yargs": "^16.0.0",
@@ -101,7 +102,7 @@
101102
"vite-plugin-mock": "^2.1.5",
102103
"vite-plugin-purge-icons": "^0.7.0",
103104
"vite-plugin-pwa": "^0.5.3",
104-
"vite-plugin-style-import": "^0.7.4",
105+
"vite-plugin-style-import": "^0.7.5",
105106
"vite-plugin-theme": "^0.4.8",
106107
"vite-plugin-windicss": "0.4.12",
107108
"vue-eslint-parser": "^7.5.0",

src/utils/http/axios/Axios.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { cloneDeep } from 'lodash-es';
88
import type { RequestOptions, CreateAxiosOptions, Result, UploadFileParams } from './types';
99
import { errorResult } from './const';
1010
import { ContentTypeEnum } from '/@/enums/httpEnum';
11+
import qs from 'qs';
12+
import { RequestEnum } from '../../../enums/httpEnum';
1113

1214
export * from './axiosTransform';
1315

@@ -144,6 +146,25 @@ export class VAxios {
144146
});
145147
}
146148

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+
147168
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
148169
let conf: AxiosRequestConfig = cloneDeep(config);
149170
const transform = this.getTransform();
@@ -156,6 +177,8 @@ export class VAxios {
156177
if (beforeRequestHook && isFunction(beforeRequestHook)) {
157178
conf = beforeRequestHook(conf, opt);
158179
}
180+
181+
conf = this.supportFormData(conf);
159182
return new Promise((resolve, reject) => {
160183
this.axiosInstance
161184
.request<any, AxiosResponse<Result>>(conf)

windi.config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ export default defineConfig({
99
theme: {
1010
extend: {
1111
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+
},
1919
},
2020
},
2121
});

yarn.lock

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,11 @@
14461446
dependencies:
14471447
"@types/node" "*"
14481448

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+
14491454
"@types/resolve@1.17.1":
14501455
version "1.17.1"
14511456
resolved "https://registry.npmjs.org/@types/resolve/-/resolve-1.17.1.tgz#3afd6ad8967c77e4376c598a82ddd58f46ec45d6"
@@ -3567,6 +3572,11 @@ es-module-lexer@^0.3.26:
35673572
resolved "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-0.3.26.tgz#7b507044e97d5b03b01d4392c74ffeb9c177a83b"
35683573
integrity sha512-Va0Q/xqtrss45hWzP8CZJwzGSZJjDM5/MJRE3IXXnUCcVLElR9BRaE9F62BopysASyc4nM3uwhSW7FFB9nlWAA==
35693574

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+
35703580
es-to-primitive@^1.2.1:
35713581
version "1.2.1"
35723582
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:
89308940
pretty-bytes "^5.5.0"
89318941
workbox-build "^6.1.0"
89328942

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==
89378947
dependencies:
89388948
"@rollup/pluginutils" "^4.1.0"
89398949
change-case "^4.1.2"
89408950
debug "^4.3.2"
8941-
es-module-lexer "^0.3.26"
8951+
es-module-lexer "^0.4.0"
89428952
magic-string "^0.25.7"
89438953

89448954
vite-plugin-theme@^0.4.8:

0 commit comments

Comments
 (0)