gtsoft-snail-job-admin/build/config/proxy.ts

37 lines
949 B
TypeScript
Raw Normal View History

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