Return null values where appr and handle the env being blank

This commit is contained in:
William Oldham 2024-03-03 18:37:28 +00:00
parent e555354e17
commit b560445659

View File

@ -31,10 +31,10 @@ export interface RuntimeConfig {
DONATION_LINK: string; DONATION_LINK: string;
DISCORD_LINK: string; DISCORD_LINK: string;
DMCA_EMAIL: string | null; DMCA_EMAIL: string | null;
TMDB_READ_API_KEY: string; TMDB_READ_API_KEY: string | null;
NORMAL_ROUTER: boolean; NORMAL_ROUTER: boolean;
PROXY_URLS: string[]; PROXY_URLS: string[];
BACKEND_URL: string; BACKEND_URL: string | null;
DISALLOWED_IDS: string[]; DISALLOWED_IDS: string[];
TURNSTILE_KEY: string | null; TURNSTILE_KEY: string | null;
CDN_REPLACEMENTS: Array<string[]>; CDN_REPLACEMENTS: Array<string[]>;
@ -66,48 +66,48 @@ const env: Record<keyof Config, undefined | string> = {
HAS_ONBOARDING: import.meta.env.VITE_HAS_ONBOARDING, HAS_ONBOARDING: import.meta.env.VITE_HAS_ONBOARDING,
}; };
// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js) function coerceUndefined(value: string | null | undefined): string | undefined {
function getKeyValue(key: keyof Config): string | undefined { if (value == null) return undefined;
let windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`]; if (value.length === 0) return undefined;
if ( return value;
windowValue !== null &&
windowValue !== undefined &&
windowValue.length === 0
)
windowValue = undefined;
return env[key] ?? windowValue ?? undefined;
} }
function getKey(key: keyof Config, defaultString?: string): string { // loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js)
return getKeyValue(key)?.toString() ?? defaultString ?? ""; 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 { 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 { return {
APP_VERSION, APP_VERSION,
GITHUB_LINK, GITHUB_LINK,
DONATION_LINK, DONATION_LINK,
DISCORD_LINK, DISCORD_LINK,
DMCA_EMAIL: dmcaEmail.length > 0 ? dmcaEmail : null, DMCA_EMAIL: getKey("DMCA_EMAIL"),
ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: ONBOARDING_CHROME_EXTENSION_INSTALL_LINK: getKey(
chromeExtension.length > 0 ? chromeExtension : null, "ONBOARDING_CHROME_EXTENSION_INSTALL_LINK",
ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: ),
firefoxExtension.length > 0 ? firefoxExtension : null, ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK: getKey(
ONBOARDING_PROXY_INSTALL_LINK: "ONBOARDING_FIREFOX_EXTENSION_INSTALL_LINK",
proxyInstallLink.length > 0 ? proxyInstallLink : null, ),
ONBOARDING_PROXY_INSTALL_LINK: getKey("ONBOARDING_PROXY_INSTALL_LINK"),
BACKEND_URL: getKey("BACKEND_URL", BACKEND_URL), BACKEND_URL: getKey("BACKEND_URL", BACKEND_URL),
TMDB_READ_API_KEY: getKey("TMDB_READ_API_KEY"), TMDB_READ_API_KEY: getKey("TMDB_READ_API_KEY"),
PROXY_URLS: getKey("CORS_PROXY_URL") PROXY_URLS: getKey("CORS_PROXY_URL", "")
.split(",") .split(",")
.map((v) => v.trim()), .map((v) => v.trim())
.filter((v) => v.length > 0),
NORMAL_ROUTER: getKey("NORMAL_ROUTER", "false") === "true", NORMAL_ROUTER: getKey("NORMAL_ROUTER", "false") === "true",
HAS_ONBOARDING: getKey("HAS_ONBOARDING", "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", "") DISALLOWED_IDS: getKey("DISALLOWED_IDS", "")
.split(",") .split(",")
.map((v) => v.trim()) .map((v) => v.trim())