mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
refactor(projects): refactor service env config
This commit is contained in:
@ -1,14 +1,13 @@
|
||||
import { BACKEND_ERROR_CODE, createFlatRequest, createRequest } from '@sa/axios';
|
||||
import { localStg } from '@/utils/storage';
|
||||
import { createProxyPattern, createServiceConfig } from '~/env.config';
|
||||
|
||||
const { baseURL, otherBaseURL } = createServiceConfig(import.meta.env);
|
||||
import { getServiceBaseURL } from '@/utils/service';
|
||||
|
||||
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y';
|
||||
const { baseURL, otherBaseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);
|
||||
|
||||
export const request = createFlatRequest<App.Service.Response>(
|
||||
{
|
||||
baseURL: isHttpProxy ? createProxyPattern() : baseURL,
|
||||
baseURL,
|
||||
headers: {
|
||||
apifoxToken: 'XL299LiMEDZ0H5h3A29PxwQXdMJqWyY2'
|
||||
}
|
||||
@ -53,7 +52,7 @@ export const request = createFlatRequest<App.Service.Response>(
|
||||
|
||||
export const demoRequest = createRequest<App.Service.DemoResponse>(
|
||||
{
|
||||
baseURL: isHttpProxy ? createProxyPattern('demo') : otherBaseURL.demo
|
||||
baseURL: otherBaseURL.demo
|
||||
},
|
||||
{
|
||||
async onRequest(config) {
|
||||
|
25
src/typings/app.d.ts
vendored
25
src/typings/app.d.ts
vendored
@ -600,22 +600,29 @@ declare namespace App {
|
||||
|
||||
/** Service namespace */
|
||||
namespace Service {
|
||||
/** The backend service env type */
|
||||
type EnvType = 'dev' | 'test' | 'prod';
|
||||
|
||||
/** Other baseURL key */
|
||||
type OtherBaseURLKey = 'demo';
|
||||
|
||||
/** The backend service config */
|
||||
interface ServiceConfig<T extends OtherBaseURLKey = OtherBaseURLKey> {
|
||||
interface ServiceConfigItem {
|
||||
/** The backend service base url */
|
||||
baseURL: string;
|
||||
/** Other backend service base url map */
|
||||
otherBaseURL: Record<T, string>;
|
||||
/** The proxy pattern of the backend service base url */
|
||||
proxyPattern: string;
|
||||
}
|
||||
|
||||
/** The backend service config map */
|
||||
type ServiceConfigMap = Record<EnvType, ServiceConfig>;
|
||||
interface OtherServiceConfigItem extends ServiceConfigItem {
|
||||
key: OtherBaseURLKey;
|
||||
}
|
||||
|
||||
/** The backend service config */
|
||||
interface ServiceConfig extends ServiceConfigItem {
|
||||
/** Other backend service config */
|
||||
other: OtherServiceConfigItem[];
|
||||
}
|
||||
|
||||
interface SimpleServiceConfig extends Pick<ServiceConfigItem, 'baseURL'> {
|
||||
other: Record<OtherBaseURLKey, string>;
|
||||
}
|
||||
|
||||
/** The backend service response data */
|
||||
type Response<T = unknown> = {
|
||||
|
10
src/typings/env.d.ts
vendored
10
src/typings/env.d.ts
vendored
@ -25,14 +25,20 @@ declare namespace Env {
|
||||
* This prefix is start with the icon prefix
|
||||
*/
|
||||
readonly VITE_ICON_LOCAL_PREFIX: 'local-icon';
|
||||
/** backend service base url */
|
||||
readonly VITE_SERVICE_BASE_URL: string;
|
||||
/**
|
||||
* other backend service base url
|
||||
*
|
||||
* the value is a json
|
||||
*/
|
||||
readonly VITE_OTHER_SERVICE_BASE_URL: string;
|
||||
/**
|
||||
* Whether to enable the http proxy
|
||||
*
|
||||
* Only valid in the development environment
|
||||
*/
|
||||
readonly VITE_HTTP_PROXY?: CommonType.YesOrNo;
|
||||
/** The back service env */
|
||||
readonly VITE_SERVICE_ENV?: App.Service.EnvType;
|
||||
/**
|
||||
* The auth route mode
|
||||
*
|
||||
|
73
src/utils/service.ts
Normal file
73
src/utils/service.ts
Normal file
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Create service config by current env
|
||||
*
|
||||
* @param env The current env
|
||||
*/
|
||||
export function createServiceConfig(env: Env.ImportMeta) {
|
||||
const { VITE_SERVICE_BASE_URL, VITE_OTHER_SERVICE_BASE_URL } = env;
|
||||
|
||||
let other = {} as Record<App.Service.OtherBaseURLKey, string>;
|
||||
try {
|
||||
other = JSON.parse(VITE_OTHER_SERVICE_BASE_URL);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid JSON string');
|
||||
}
|
||||
|
||||
const httpConfig: App.Service.SimpleServiceConfig = {
|
||||
baseURL: VITE_SERVICE_BASE_URL,
|
||||
other
|
||||
};
|
||||
|
||||
const otherHttpKeys = Object.keys(httpConfig.other) as App.Service.OtherBaseURLKey[];
|
||||
|
||||
const otherConfig: App.Service.OtherServiceConfigItem[] = otherHttpKeys.map(key => {
|
||||
return {
|
||||
key,
|
||||
baseURL: httpConfig.other[key],
|
||||
proxyPattern: createProxyPattern(key)
|
||||
};
|
||||
});
|
||||
|
||||
const config: App.Service.ServiceConfig = {
|
||||
baseURL: httpConfig.baseURL,
|
||||
proxyPattern: createProxyPattern(),
|
||||
other: otherConfig
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* get backend service base url
|
||||
*
|
||||
* @param env - the current env
|
||||
* @param isProxy - if use proxy
|
||||
*/
|
||||
export function getServiceBaseURL(env: Env.ImportMeta, isProxy: boolean) {
|
||||
const { baseURL, other } = createServiceConfig(env);
|
||||
|
||||
const otherBaseURL = {} as Record<App.Service.OtherBaseURLKey, string>;
|
||||
|
||||
other.forEach(item => {
|
||||
otherBaseURL[item.key] = isProxy ? item.proxyPattern : item.baseURL;
|
||||
});
|
||||
|
||||
return {
|
||||
baseURL: isProxy ? createProxyPattern() : baseURL,
|
||||
otherBaseURL
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get proxy pattern of backend service base url
|
||||
*
|
||||
* @param key If not set, will use the default key
|
||||
*/
|
||||
function createProxyPattern(key?: App.Service.OtherBaseURLKey) {
|
||||
if (!key) {
|
||||
return '/proxy-default';
|
||||
}
|
||||
|
||||
return `/proxy-${key}`;
|
||||
}
|
Reference in New Issue
Block a user