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) { if (requestMap.get(ctx.key) && !ctx.config.mergeRequest) {
ctx.error = { ctx.error = {
type: 'REPEAT', type: 'REPEAT',
msg: '重复请求' msg: '重复请求',
config: ctx.config
}; };
return; return;
} }

View File

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

View File

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

View File

@ -7,8 +7,9 @@ export const request = {
console.log('500 error'); console.log('500 error');
}, },
default(error) { default(error) {
console.log('default error');
console.log(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'); console.log('click Icon');
}; };
// request('/api', null, { // request('/api', null, {
// mergeRequest: true
// }).then((res) => { // }).then((res) => {
// console.log(res); // console.log(res);
// }); // });
request('/api', null, {
}).then((res) => {
console.log(res);
});
// request('/api', null, { // request('/api', null, {
// mergeRequest: true // mergeRequest: true
// }).then((res) => { // }).then((res) => {
@ -68,21 +71,21 @@ export default {
// }); // });
// }, 3200); // }, 3200);
request('/api', null, { // request('/api', null, {
cache: true // cache: true
}).then((res) => { // }).then((res) => {
console.log(res); // console.log(res);
}); // });
request('/api', null, { // request('/api', null, {
cache: true // cache: true
}).then((res) => { // }).then((res) => {
console.log(res); // console.log(res);
}); // });
request('/api', null, { // request('/api', null, {
cache: true // cache: true
}).then((res) => { // }).then((res) => {
console.log(res); // console.log(res);
}); // });
// request('/api', null, { // request('/api', null, {
// // skipErrorHandler: [500] // // skipErrorHandler: [500]