Skip to content

Commit

Permalink
refactor(projects): update service and proxy config
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Mar 6, 2023
1 parent 1ef1b6b commit 8debfe7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
22 changes: 8 additions & 14 deletions .env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@ 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'
}
};

/**
* 获取当前环境模式下的请求服务的配置
* @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'
};
}
11 changes: 3 additions & 8 deletions build/config/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`), '')
}
};

Expand Down
6 changes: 2 additions & 4 deletions src/service/request/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 6 deletions src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

1 comment on commit 8debfe7

@vercel
Copy link

@vercel vercel bot commented on 8debfe7 Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.