feat: 优化 dataField 逻辑

This commit is contained in:
winixt 2021-07-29 11:24:44 +08:00
parent 7355a32572
commit 51c83c2df2
5 changed files with 26 additions and 22 deletions

View File

@ -46,7 +46,8 @@ export default async (ctx, next) => {
if (requestMap.get(ctx.key) && !ctx.config.mergeRequest) {
ctx.error = {
type: 'REPEAT',
msg: '重复请求'
msg: '重复请求',
config: ctx.config
};
return;
}

View File

@ -124,7 +124,6 @@ function createContext(userConfig) {
function getResponseCode(response) {
if (response) {
if (response._rawData) return response._rawData.code;
if (response.data) return response.data.code;
}
return null;
@ -181,7 +180,7 @@ export const request = (url, data, options = {}) => {
return currentRequestInstance.request(context).then(async () => {
if (!context.error) {
return context.config.useResonse ? context.response : context.response.data;
return context.config.useResonse ? context.response : context.filterData || context.response.data;
}
await handleRequestError(context);
return Promise.reject(context.error);

View File

@ -1,10 +1,10 @@
import { isObject } from './helpers';
// FEATURE: 后续支持 a.b.c
export default async (ctx, next) => {
const dataField = ctx.config.dataField ?? ctx.dataField;
if (ctx.response && isObject(ctx.response.data) && dataField) {
ctx.response._rawData = ctx.response.data;
ctx.response.data = ctx.response.data[dataField];
ctx.filterData = ctx.response.data[dataField];
}
await next();
};

View File

@ -7,8 +7,9 @@ export const request = {
console.log('500 error');
},
default(error) {
console.log('default error');
console.log(error);
const msg = error?.data?.msg || error?.msg;
console.log(msg);
}
}
};

View File

@ -23,10 +23,13 @@ export default {
console.log('click Icon');
};
// request('/api', null, {
// mergeRequest: true
// }).then((res) => {
// console.log(res);
// });
request('/api', null, {
}).then((res) => {
console.log(res);
});
// request('/api', null, {
// mergeRequest: true
// }).then((res) => {
@ -68,21 +71,21 @@ export default {
// });
// }, 3200);
request('/api', null, {
cache: true
}).then((res) => {
console.log(res);
});
request('/api', null, {
cache: true
}).then((res) => {
console.log(res);
});
request('/api', null, {
cache: true
}).then((res) => {
console.log(res);
});
// request('/api', null, {
// cache: true
// }).then((res) => {
// console.log(res);
// });
// request('/api', null, {
// cache: true
// }).then((res) => {
// console.log(res);
// });
// request('/api', null, {
// cache: true
// }).then((res) => {
// console.log(res);
// });
// request('/api', null, {
// // skipErrorHandler: [500]