admin-vben/build/vite/proxy.ts

35 lines
780 B
TypeScript
Raw Normal View History

2023-03-18 13:10:54 +00:00
/**
* Used to parse the .env.development proxy configuration
*/
import type { ProxyOptions } from 'vite'
type ProxyItem = [string, string]
type ProxyList = ProxyItem[]
type ProxyTargetList = Record<string, ProxyOptions>
const httpsRE = /^https:\/\//
/**
* Generate proxy
* @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] = {
2023-07-29 10:46:43 +00:00
target,
2023-03-18 13:10:54 +00:00
changeOrigin: true,
ws: true,
2023-07-29 10:46:43 +00:00
rewrite: path => path.replace(new RegExp(`^${prefix}`), ''),
2023-03-18 13:10:54 +00:00
// https is require secure=false
2023-07-29 10:46:43 +00:00
...(isHttps ? { secure: false } : {}),
2023-03-18 13:10:54 +00:00
}
}
return ret
}