30 lines
694 B
TypeScript
30 lines
694 B
TypeScript
|
|
/**
|
|
* Safely retrieves environment variables in Vite environment.
|
|
* In Vite, variables must start with VITE_ to be exposed to the client.
|
|
*/
|
|
export const getEnvVar = (key: string): string => {
|
|
// 1. Try Vite / Modern Browser approach (import.meta.env)
|
|
try {
|
|
// @ts-ignore
|
|
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
|
// @ts-ignore
|
|
const val = import.meta.env[key];
|
|
if (val) return val;
|
|
}
|
|
} catch (e) {
|
|
// Ignore errors
|
|
}
|
|
|
|
// 2. Legacy/Fallback for some test environments
|
|
try {
|
|
if (typeof process !== 'undefined' && process.env) {
|
|
return process.env[key] || '';
|
|
}
|
|
} catch (e) {
|
|
// Ignore
|
|
}
|
|
|
|
return '';
|
|
};
|