53 lines
1.8 KiB
JavaScript
53 lines
1.8 KiB
JavaScript
/*
|
||
新版很多接口只是改了接口名字,参数和请求类型并没有修改,只需要建立一个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;
|
||
} |