From b5604456596d2cf478b03e521445b04a0a14ae0f Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sun, 3 Mar 2024 18:37:28 +0000 Subject: [PATCH] Return null values where appr and handle the env being blank --- src/setup/config.ts | 58 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/setup/config.ts b/src/setup/config.ts index f081e673..26eb814e 100644 --- a/src/setup/config.ts +++ b/src/setup/config.ts @@ -31,10 +31,10 @@ export interface RuntimeConfig { DONATION_LINK: string; DISCORD_LINK: string; DMCA_EMAIL: string | null; - TMDB_READ_API_KEY: string; + TMDB_READ_API_KEY: string | null; NORMAL_ROUTER: boolean; PROXY_URLS: string[]; - BACKEND_URL: string; + BACKEND_URL: string | null; DISALLOWED_IDS: string[]; TURNSTILE_KEY: string | null; CDN_REPLACEMENTS: Array; @@ -66,48 +66,48 @@ const env: Record = { HAS_ONBOARDING: import.meta.env.VITE_HAS_ONBOARDING, }; -// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) -function getKeyValue(key: keyof Config): string | undefined { - let windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`]; - if ( - windowValue !== null && - windowValue !== undefined && - windowValue.length === 0 - ) - windowValue = undefined; - return env[key] ?? windowValue ?? undefined; +function coerceUndefined(value: string | null | undefined): string | undefined { + if (value == null) return undefined; + if (value.length === 0) return undefined; + return value; } -function getKey(key: keyof Config, defaultString?: string): string { - return getKeyValue(key)?.toString() ?? defaultString ?? ""; +// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) +function getKeyValue(key: keyof Config): string | undefined { + const windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`]; + + return coerceUndefined(env[key]) ?? coerceUndefined(windowValue) ?? undefined; +} + +function getKey(key: keyof Config): string | null; +function getKey(key: keyof Config, defaultString: string): string; +function getKey(key: keyof Config, defaultString?: string): string | null { + return getKeyValue(key)?.toString() ?? defaultString ?? null; } export function conf(): RuntimeConfig { - const dmcaEmail = getKey("DMCA_EMAIL"); - const chromeExtension = getKey("ONBOARDING_CHROME_EXTENSION_INSTALL_LINK"); - const firefoxExtension = getKey("ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK"); - const proxyInstallLink = getKey("ONBOARDING_PROXY_INSTALL_LINK"); - const turnstileKey = getKey("TURNSTILE_KEY"); return { APP_VERSION, GITHUB_LINK, DONATION_LINK, DISCORD_LINK, - DMCA_EMAIL: dmcaEmail.length > 0 ? dmcaEmail : null, - ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: - chromeExtension.length > 0 ? chromeExtension : null, - ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: - firefoxExtension.length > 0 ? firefoxExtension : null, - ONBOARDING_PROXY_INSTALL_LINK: - proxyInstallLink.length > 0 ? proxyInstallLink : null, + DMCA_EMAIL: getKey("DMCA_EMAIL"), + ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: getKey( + "ONBOARDING_CHROME_EXTENSION_INSTALL_LINK", + ), + ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: getKey( + "ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK", + ), + ONBOARDING_PROXY_INSTALL_LINK: getKey("ONBOARDING_PROXY_INSTALL_LINK"), BACKEND_URL: getKey("BACKEND_URL", BACKEND_URL), TMDB_READ_API_KEY: getKey("TMDB_READ_API_KEY"), - PROXY_URLS: getKey("CORS_PROXY_URL") + PROXY_URLS: getKey("CORS_PROXY_URL", "") .split(",") - .map((v) => v.trim()), + .map((v) => v.trim()) + .filter((v) => v.length > 0), NORMAL_ROUTER: getKey("NORMAL_ROUTER", "false") === "true", HAS_ONBOARDING: getKey("HAS_ONBOARDING", "false") === "true", - TURNSTILE_KEY: turnstileKey.length > 0 ? turnstileKey : null, + TURNSTILE_KEY: getKey("TURNSTILE_KEY"), DISALLOWED_IDS: getKey("DISALLOWED_IDS", "") .split(",") .map((v) => v.trim())