55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
export interface EnvConfig {
|
|
PORT?: string;
|
|
PROXY_HOST?: string;
|
|
SERVER_API_URL?: string;
|
|
SITE_ID?: string;
|
|
CLAIM_NAME?: string;
|
|
ACCESS_TOKEN?: string;
|
|
REFRESH_TOKEN?: string;
|
|
AUTH_USER_ID?: string;
|
|
}
|
|
|
|
const defaultConfig: EnvConfig = {
|
|
PORT: "3000",
|
|
PROXY_HOST: "",
|
|
SERVER_API_URL: "",
|
|
SITE_ID: "CS",
|
|
CLAIM_NAME: "Authorization",
|
|
ACCESS_TOKEN: "access-token",
|
|
REFRESH_TOKEN: "refresh-token",
|
|
AUTH_USER_ID: "token-id",
|
|
};
|
|
|
|
let isInitialized = false;
|
|
let envConfig: EnvConfig = { ...defaultConfig };
|
|
|
|
export function initializeConfig(values: Partial<EnvConfig>): void {
|
|
if (isInitialized) {
|
|
console.warn("Configuration already initialized. Skipping.");
|
|
return;
|
|
}
|
|
|
|
const filteredValues = Object.entries(values).reduce((acc, [key, value]) => {
|
|
if (value !== undefined && value !== null) {
|
|
acc[key as keyof EnvConfig] = value;
|
|
}
|
|
return acc;
|
|
}, {} as Partial<EnvConfig>);
|
|
|
|
if (filteredValues.PORT && !filteredValues.PROXY_HOST) {
|
|
filteredValues.PROXY_HOST = `http://localhost:${filteredValues.PORT}`;
|
|
}
|
|
|
|
envConfig = { ...defaultConfig, ...filteredValues };
|
|
isInitialized = true;
|
|
}
|
|
|
|
export function getConfig(): EnvConfig {
|
|
if (!isInitialized) {
|
|
console.warn(
|
|
"Attempted to access config before initialization. Returning default values."
|
|
);
|
|
}
|
|
return envConfig;
|
|
}
|