Skip to content

Commit

Permalink
feat(axios): use defHttp like axios
Browse files Browse the repository at this point in the history
当非GET请求并且同时存在data和params,不再忽略data。使defHttp的用法习惯接近axios原生配置

fixed: #850
  • Loading branch information
mynetfan committed Jul 3, 2021
1 parent 8819af0 commit 49f39de
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/utils/http/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const transform: AxiosTransform = {
switch (code) {
case ResultEnum.TIMEOUT:
timeoutMsg = t('sys.api.timeoutMessage');
break;
default:
if (message) {
timeoutMsg = message;
Expand Down Expand Up @@ -90,6 +91,8 @@ const transform: AxiosTransform = {
config.url = `${apiUrl}${config.url}`;
}
const params = config.params || {};
const data = config.data || false;
formatDate && data && !isString(data) && formatRequestDate(data);
if (config.method?.toUpperCase() === RequestEnum.GET) {
if (!isString(params)) {
// 给 get 请求加上时间戳参数,避免从缓存中拿数据。
Expand All @@ -102,10 +105,19 @@ const transform: AxiosTransform = {
} else {
if (!isString(params)) {
formatDate && formatRequestDate(params);
config.data = params;
config.params = undefined;
if (Reflect.has(config, 'data') && config.data && Object.keys(config.data).length > 0) {
config.data = data;
config.params = params;
} else {
// 非GET请求如果没有提供data,则将params视为data
config.data = params;
config.params = undefined;
}
if (joinParamsToUrl) {
config.url = setObjToUrlParams(config.url as string, config.data);
config.url = setObjToUrlParams(
config.url as string,
Object.assign({}, config.params, config.data)
);
}
} else {
// 兼容restful风格
Expand Down

0 comments on commit 49f39de

Please sign in to comment.