74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
/**
|
|
* 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<Service.OtherBaseURLKey, string>;
|
|
try {
|
|
other = VITE_OTHER_SERVICE_BASE_URL ? 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: Service.SimpleServiceConfig = {
|
|
baseURL: VITE_SERVICE_BASE_URL,
|
|
other
|
|
};
|
|
|
|
const otherHttpKeys = Object.keys(httpConfig.other) as Service.OtherBaseURLKey[];
|
|
|
|
const otherConfig: Service.OtherServiceConfigItem[] = otherHttpKeys.map(key => {
|
|
return {
|
|
key,
|
|
baseURL: httpConfig.other[key],
|
|
proxyPattern: createProxyPattern(key)
|
|
};
|
|
});
|
|
|
|
const config: 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<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?: Service.OtherBaseURLKey) {
|
|
if (!key) {
|
|
return '/proxy-default';
|
|
}
|
|
|
|
return `/proxy-${key}`;
|
|
}
|