ant design pro 修改request请求方法,修改为符合实际业务接口请求的数据处理方式
ant design pro中对fetch进行了封装处理,写在utils/request.js,但是与我们通常的接口格式的处理并不适配,我们业务出现的错误代码并不会直接放在网络层,而是放在json中,所以需要做一些修改
原本写法
在ant design pro的项目中,原本的请求处理中可以看出,项目里把所有错误的情况都放在了网络请求错误码中,各种错误状态都在响应头status code中展现,所以原本的utils/request.js中的方法封装是这么写的
//这里的写的是响应头的status code如果不是200-300之间,直接抛出错误
const checkStatus = response => {
if (response.status >= 200 && response.status < 300) {
return response;
}
const errortext = codeMessage[response.status] || response.statusText;
notification.error({
message: `请求错误 ${
response.status}: ${
response.url}`,
description: errortext,
});
const error = new Error(errortext);
error.name = response.status;
error.response = response;
throw error;
};
... //中间省略不解释
//fetch封装
return fetch(url, newOptions)
.then(checkStatus)
.then(response => cachedSave(response, hashcode))
.then(response => {