refactor(projects): update service and proxy config
This commit is contained in:
parent
1ef1b6bda9
commit
8debfe7e95
@ -4,22 +4,13 @@ type ServiceEnv = Record<ServiceEnvType, ServiceEnvConfig>;
|
|||||||
/** 不同请求服务的环境配置 */
|
/** 不同请求服务的环境配置 */
|
||||||
const serviceEnv: ServiceEnv = {
|
const serviceEnv: ServiceEnv = {
|
||||||
dev: {
|
dev: {
|
||||||
url: 'http://localhost:8080',
|
url: 'http://localhost:8080'
|
||||||
urlPattern: '/url-pattern',
|
|
||||||
secondUrl: 'http://localhost:8081',
|
|
||||||
secondUrlPattern: '/second-url-pattern'
|
|
||||||
},
|
},
|
||||||
test: {
|
test: {
|
||||||
url: 'http://localhost:8080',
|
url: 'http://localhost:8080'
|
||||||
urlPattern: '/url-pattern',
|
|
||||||
secondUrl: 'http://localhost:8081',
|
|
||||||
secondUrlPattern: '/second-url-pattern'
|
|
||||||
},
|
},
|
||||||
prod: {
|
prod: {
|
||||||
url: 'http://localhost:8080',
|
url: 'http://localhost:8080'
|
||||||
urlPattern: '/url-pattern',
|
|
||||||
secondUrl: 'http://localhost:8081',
|
|
||||||
secondUrlPattern: '/second-url-pattern'
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,10 +18,13 @@ const serviceEnv: ServiceEnv = {
|
|||||||
* 获取当前环境模式下的请求服务的配置
|
* 获取当前环境模式下的请求服务的配置
|
||||||
* @param env 环境
|
* @param env 环境
|
||||||
*/
|
*/
|
||||||
export function getServiceEnvConfig(env: ImportMetaEnv) {
|
export function getServiceEnvConfig(env: ImportMetaEnv): ServiceEnvConfigWithProxyPattern {
|
||||||
const { VITE_SERVICE_ENV = 'dev' } = env;
|
const { VITE_SERVICE_ENV = 'dev' } = env;
|
||||||
|
|
||||||
const config = serviceEnv[VITE_SERVICE_ENV];
|
const config = serviceEnv[VITE_SERVICE_ENV];
|
||||||
|
|
||||||
return config;
|
return {
|
||||||
|
...config,
|
||||||
|
proxyPattern: 'proxy-pattern'
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,14 @@ import type { ProxyOptions } from 'vite';
|
|||||||
* @param isOpenProxy - 是否开启代理
|
* @param isOpenProxy - 是否开启代理
|
||||||
* @param envConfig - env环境配置
|
* @param envConfig - env环境配置
|
||||||
*/
|
*/
|
||||||
export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) {
|
export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfigWithProxyPattern) {
|
||||||
if (!isOpenProxy) return undefined;
|
if (!isOpenProxy) return undefined;
|
||||||
|
|
||||||
const proxy: Record<string, string | ProxyOptions> = {
|
const proxy: Record<string, string | ProxyOptions> = {
|
||||||
[envConfig.urlPattern]: {
|
[envConfig.proxyPattern]: {
|
||||||
target: envConfig.url,
|
target: envConfig.url,
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
rewrite: path => path.replace(new RegExp(`^${envConfig.urlPattern}`), '')
|
rewrite: path => path.replace(new RegExp(`^${envConfig.proxyPattern}`), '')
|
||||||
},
|
|
||||||
[envConfig.secondUrlPattern]: {
|
|
||||||
target: envConfig.secondUrl,
|
|
||||||
changeOrigin: true,
|
|
||||||
rewrite: path => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), '')
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
import { getServiceEnvConfig } from '~/.env-config';
|
import { getServiceEnvConfig } from '~/.env-config';
|
||||||
import { createRequest } from './request';
|
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';
|
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
|
||||||
|
|
||||||
export const request = createRequest({ baseURL: isHttpProxy ? urlPattern : url });
|
export const request = createRequest({ baseURL: isHttpProxy ? proxyPattern : url });
|
||||||
|
|
||||||
export const secondRequest = createRequest({ baseURL: isHttpProxy ? secondUrlPattern : secondUrl });
|
|
||||||
|
|
||||||
export const mockRequest = createRequest({ baseURL: '/mock' });
|
export const mockRequest = createRequest({ baseURL: '/mock' });
|
||||||
|
16
src/typings/env.d.ts
vendored
16
src/typings/env.d.ts
vendored
@ -10,12 +10,16 @@ type ServiceEnvType = 'dev' | 'test' | 'prod';
|
|||||||
interface ServiceEnvConfig {
|
interface ServiceEnvConfig {
|
||||||
/** 请求地址 */
|
/** 请求地址 */
|
||||||
url: string;
|
url: string;
|
||||||
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
}
|
||||||
urlPattern: '/url-pattern';
|
|
||||||
/** 另一个后端请求地址(有多个不同的后端服务时) */
|
interface ServiceEnvConfigWithProxyPattern extends ServiceEnvConfig {
|
||||||
secondUrl: string;
|
/**
|
||||||
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
* 匹配路径的正则字符串
|
||||||
secondUrlPattern: '/second-url-pattern';
|
* - 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用)
|
||||||
|
* - 和后端请求地址的前缀无关
|
||||||
|
* - 有多个后端请求实例时,需要创建不同的值
|
||||||
|
*/
|
||||||
|
proxyPattern: 'proxy-pattern';
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ImportMetaEnv {
|
interface ImportMetaEnv {
|
||||||
|
Loading…
Reference in New Issue
Block a user