refactor(projects): update service and proxy config

This commit is contained in:
Soybean 2023-03-07 07:52:05 +08:00
parent 1ef1b6bda9
commit 8debfe7e95
4 changed files with 23 additions and 32 deletions

View File

@ -4,22 +4,13 @@ type ServiceEnv = Record<ServiceEnvType, ServiceEnvConfig>;
/** 不同请求服务的环境配置 */
const serviceEnv: ServiceEnv = {
dev: {
url: 'http://localhost:8080',
urlPattern: '/url-pattern',
secondUrl: 'http://localhost:8081',
secondUrlPattern: '/second-url-pattern'
url: 'http://localhost:8080'
},
test: {
url: 'http://localhost:8080',
urlPattern: '/url-pattern',
secondUrl: 'http://localhost:8081',
secondUrlPattern: '/second-url-pattern'
url: 'http://localhost:8080'
},
prod: {
url: 'http://localhost:8080',
urlPattern: '/url-pattern',
secondUrl: 'http://localhost:8081',
secondUrlPattern: '/second-url-pattern'
url: 'http://localhost:8080'
}
};
@ -27,10 +18,13 @@ const serviceEnv: ServiceEnv = {
*
* @param env
*/
export function getServiceEnvConfig(env: ImportMetaEnv) {
export function getServiceEnvConfig(env: ImportMetaEnv): ServiceEnvConfigWithProxyPattern {
const { VITE_SERVICE_ENV = 'dev' } = env;
const config = serviceEnv[VITE_SERVICE_ENV];
return config;
return {
...config,
proxyPattern: 'proxy-pattern'
};
}

View File

@ -5,19 +5,14 @@ import type { ProxyOptions } from 'vite';
* @param isOpenProxy -
* @param envConfig - env环境配置
*/
export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) {
export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfigWithProxyPattern) {
if (!isOpenProxy) return undefined;
const proxy: Record<string, string | ProxyOptions> = {
[envConfig.urlPattern]: {
[envConfig.proxyPattern]: {
target: envConfig.url,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${envConfig.urlPattern}`), '')
},
[envConfig.secondUrlPattern]: {
target: envConfig.secondUrl,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), '')
rewrite: path => path.replace(new RegExp(`^${envConfig.proxyPattern}`), '')
}
};

View File

@ -1,12 +1,10 @@
import { getServiceEnvConfig } from '~/.env-config';
import { createRequest } from './request';
const { url, urlPattern, secondUrl, secondUrlPattern } = getServiceEnvConfig(import.meta.env);
const { url, proxyPattern } = getServiceEnvConfig(import.meta.env);
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url });
export const secondRequest = createRequest({ baseURL: isHttpProxy ? secondUrlPattern : secondUrl });
export const request = createRequest({ baseURL: isHttpProxy ? proxyPattern : url });
export const mockRequest = createRequest({ baseURL: '/mock' });

16
src/typings/env.d.ts vendored
View File

@ -10,12 +10,16 @@ type ServiceEnvType = 'dev' | 'test' | 'prod';
interface ServiceEnvConfig {
/** 请求地址 */
url: string;
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
urlPattern: '/url-pattern';
/** 另一个后端请求地址(有多个不同的后端服务时) */
secondUrl: string;
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
secondUrlPattern: '/second-url-pattern';
}
interface ServiceEnvConfigWithProxyPattern extends ServiceEnvConfig {
/**
*
* - ( / + , /)
* -
* -
*/
proxyPattern: 'proxy-pattern';
}
interface ImportMetaEnv {