You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
921 B
38 lines
921 B
/** |
|
* @program: kicc-ui |
|
* @description: 用于解析 .env.development 代理配置 |
|
* @author: wangxiang4 |
|
* @create: 2022/4/5 |
|
*/ |
|
|
|
import type { ProxyOptions } from 'vite'; |
|
|
|
type ProxyItem = [string, string] |
|
|
|
type ProxyList = ProxyItem[] |
|
|
|
type ProxyTargetList = Record<string, ProxyOptions & { rewrite: (path: string) => string }> |
|
|
|
const httpsRE = /^https:\/\//; |
|
|
|
/** |
|
* 生成代理 |
|
* @param list |
|
*/ |
|
export function createProxy(list: ProxyList = []) { |
|
const ret: ProxyTargetList = {}; |
|
for (const [prefix, target] of list) { |
|
const isHttps = httpsRE.test(target); |
|
|
|
// 选型配置参考: https://github.com/http-party/node-http-proxy#options |
|
ret[prefix] = { |
|
target: target, |
|
changeOrigin: true, |
|
ws: true, |
|
rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''), |
|
// https is require secure=false |
|
...(isHttps ? { secure: false } : {}) |
|
}; |
|
} |
|
return ret; |
|
}
|
|
|