forked from chansee97/nova-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.ts
32 lines (30 loc) · 808 Bytes
/
proxy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { ProxyOptions } from 'vite'
import { mapEntries } from 'radash'
export function generateProxyPattern(envConfig: Record<string, string>) {
return mapEntries(envConfig, (key, value) => {
return [
key,
{
value,
proxy: `/proxy-${key}`,
},
]
})
}
/**
* @description: 生成vite代理字段
* @param {*} envConfig - 环境变量配置
*/
export function createViteProxy(envConfig: Record<string, string>) {
const proxyMap = generateProxyPattern(envConfig)
return mapEntries(proxyMap, (key, value) => {
return [
value.proxy,
{
target: value.value,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${value.proxy}`), ''),
},
]
}) as Record<string, string | ProxyOptions>
}