import axios from 'axios'; export interface IConfigItem { key: string; app: string; value: string; } interface IConfig { appName: string; getConfigs: () => Promise } // Run on server environment class ConfigBackend implements IConfig { appName: string; configServerURL: string; constructor(configServerURL: string, appName: string) { this.appName = appName; this.configServerURL = configServerURL; } async getConfigs() { try { const response = await axios.get(`${this.configServerURL}/configs`, { params: { app: this.appName } }); return response.data?.configs; } catch (error) { console.log('----------------------------------------------------------') console.error(error) //@ts-ignore process.exit(1) } }; } // Run on browser environment class Config implements IConfig { appName: string; constructor(appName: string) { this.appName = appName; } getConfigServerURL() { let configServer = ''; const hostname = window.location.hostname; // Check for wealth domain pattern const wealthRegex = /(?:.*-wealth-)(.+)\.binvest\.vn$/; const match = wealthRegex.exec(hostname); if (match) { const env = match[1]; configServer = `https://config-wealth-${env}.binvest.vn`; } else if (hostname.includes('local')) { configServer = 'http://localhost:8000' } else if(hostname.includes('pre-merge-on-prem')) { configServer = 'https://config-pre-merge-on-prem.fidt.vn' } else if(hostname.includes('dev-on-prem')) { configServer = 'https://config-dev-on-prem.fidt.vn' } else if(hostname.includes('uat-on-prem')) { configServer = 'https://config-uat-on-prem.fidt.vn' } else if (hostname.includes('dev')) { configServer = 'https://config-dev.fidt.vn' } else if(hostname.includes('pre-merge')) { configServer = 'https://config-pre-merge.fidt.vn' } else if(hostname.includes('uat')) { configServer = 'https://config-uat.fidt.vn' } else if (hostname.includes('training')) { configServer = 'https://config-training.fidt.vn'; } else { configServer = 'https://config.fidt.vn'; } return configServer; } async getConfigs() { try { const configServerURL = this.getConfigServerURL(); const response = await axios.get(`${configServerURL}/configs`, { params: { app: this.appName } }); return response.data?.configs; } catch (error) { console.log('----------------------------------------------------------') console.error(error) //@ts-ignore process.exit(1) } }; } function isServer() { return !(typeof window != 'undefined' && window.document); } export default (function getConfigClass() { return isServer() ? ConfigBackend : Config })();