forgeplus-react/src/indexEduplus2RequestProxy.js

53 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-16 14:12:11 +08:00
/*
新版很多接口只是改了接口名字参数和请求类型并没有修改只需要建立一个map来处理这个改动就行
*/
const normalRequestMap = {
}
// 有些url里面包含了可变的参数需要遍历一遍用正则找到对应的url
const paramRequestOldUrlArray = [
// /\/api\/v1\/careers\/(\w*)\/edit/i,
// /\/api\/v1\/games\/(\w*)\/rep_content/i,
// /api/v1/games/rwvl6htgoufi/entries
// /\/api\/v1\/games\/(\w*)\/entries/i,
// `/api/v1/games/${game.identifier}/choose_build`
/\/api\/v1\/games\/(\w*)\/choose_build/i
]
const paramRequestNewUrlArray = [
(matchResult) => {
const stageId = matchResult[1]
return `/tasks/${stageId}/choose_build.json`
},
]
export function requestProxy(config) {
// return config;
const url = config.url;
if (url.indexOf('.json') !== -1) { // 已经是新接口了
return config;
}
// TODO 为true的话会报错 Error: Network Error
config.withCredentials = false;
const oldUrlSplitPathArray = url.split('?');
let oldPath = oldUrlSplitPathArray[0]
let newPath, newUrl;
newPath = normalRequestMap[oldPath];
if (!newPath) { // 是带参的restful风格的url
paramRequestOldUrlArray.forEach((item, index) => {
const matchResult = oldPath.match(item);
if (matchResult) { // 找到了对应的restful api url
const newUrlGenerator = paramRequestNewUrlArray[index];
newPath = newUrlGenerator && newUrlGenerator(matchResult)
newUrl = `${newPath}?${oldUrlSplitPathArray[1]}`
config.url = newUrl
return config;
}
});
}
return config;
2021-04-01 19:03:56 +08:00
}