zmiany w obsłudze kluczy api i zabezpieczeń
This commit is contained in:
@@ -1,27 +1,28 @@
|
||||
|
||||
/**
|
||||
* Safely retrieves environment variables in both Vite (browser) and Node environments.
|
||||
* Prevents "ReferenceError: process is not defined" crashes in production builds.
|
||||
* 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 && import.meta.env[key]) {
|
||||
if (typeof import.meta !== 'undefined' && import.meta.env) {
|
||||
// @ts-ignore
|
||||
return import.meta.env[key];
|
||||
const val = import.meta.env[key];
|
||||
if (val) return val;
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore errors if import.meta is not supported
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
// 2. Try Node / Webpack / Polyfilled approach (process.env)
|
||||
// 2. Legacy/Fallback for some test environments
|
||||
try {
|
||||
if (typeof process !== 'undefined' && process.env && process.env[key]) {
|
||||
if (typeof process !== 'undefined' && process.env) {
|
||||
return process.env[key] || '';
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore errors if process is not defined
|
||||
// Ignore
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
Reference in New Issue
Block a user