ruoyi-plus-soybean/build/config/proxy.ts

38 lines
974 B
TypeScript
Raw Normal View History

import type { ProxyOptions } from 'vite';
import { createServiceConfig } from '../../src/utils/service';
/**
2023-12-14 21:45:29 +08:00
* Set http proxy
*
* @param env - The current env
* @param enable - If enable http proxy
*/
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y';
2023-11-17 08:45:00 +08:00
if (!isEnableHttpProxy) return undefined;
2024-08-16 16:35:27 +08:00
const { baseURL, proxyPattern, ws, other } = createServiceConfig(env);
2023-11-17 08:45:00 +08:00
2024-08-16 16:35:27 +08:00
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, ws, proxyPattern });
2023-11-17 08:45:00 +08:00
other.forEach(item => {
Object.assign(proxy, createProxyItem(item));
});
return proxy;
}
2023-11-17 08:45:00 +08:00
function createProxyItem(item: App.Service.ServiceConfigItem) {
const proxy: Record<string, ProxyOptions> = {};
2023-11-17 08:45:00 +08:00
proxy[item.proxyPattern] = {
target: item.baseURL,
changeOrigin: true,
2024-08-16 16:33:11 +08:00
ws: item.ws,
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), '')
};
2023-11-17 08:45:00 +08:00
return proxy;
}