axios restfull 风格参数 匹配
程序员文章站
2022-07-15 15:42:05
...
axios 用于restfull 风格参数 匹配(JS)
/**
* 匹配URL 参数
* 用于restfull 风格参数 匹配
* 注意:URL中参数名称要和map 匹配
* eg:
* URL = "https://test.cn/game/${a}/user/${b}/"
* map = { a: 1, b: 909 }
*
* @param {*} api game/${a}/user/${b}/
* @param {*} map { a: 1, b: 909 }
* @returns
*/
const patternURLParams = (api = '', map = {}) => {
try {
var regex = /\${(.*?)}/g
var patternRes = api.match(regex)
if (null != patternRes && patternRes.length > 0) {
for (let i = 0; i < patternRes.length; i++) {
var cStr = patternRes[i]
var key = cStr.replace("${", "").replace("}", "")
api = api.replace(cStr, map[key])
}
}
} catch (e) {
console.log(e)
}
return api
}