movie-web/src/setup/config.ts

105 lines
3.2 KiB
TypeScript
Raw Normal View History

import {
APP_VERSION,
BACKEND_URL,
DISCORD_LINK,
DONATION_LINK,
GITHUB_LINK,
} from "./constants";
2022-12-27 16:44:36 +01:00
2022-12-29 18:25:57 +01:00
interface Config {
2022-12-27 16:44:36 +01:00
APP_VERSION: string;
GITHUB_LINK: string;
DONATION_LINK: string;
2022-12-27 16:44:36 +01:00
DISCORD_LINK: string;
DMCA_EMAIL: string;
2023-06-23 21:58:33 +02:00
TMDB_READ_API_KEY: string;
2022-12-27 16:44:36 +01:00
CORS_PROXY_URL: string;
NORMAL_ROUTER: boolean;
2023-10-31 21:08:09 +01:00
BACKEND_URL: string;
2023-12-01 14:34:52 +01:00
DISALLOWED_IDS: string;
2023-12-19 20:41:56 +01:00
TURNSTILE_KEY: string;
2023-12-27 23:39:32 +01:00
CDN_REPLACEMENTS: string;
2024-01-16 20:28:33 +01:00
HAS_ONBOARDING: string;
2022-12-27 16:44:36 +01:00
}
2023-02-22 21:41:13 +01:00
export interface RuntimeConfig {
APP_VERSION: string;
GITHUB_LINK: string;
DONATION_LINK: string;
2023-02-22 21:41:13 +01:00
DISCORD_LINK: string;
DMCA_EMAIL: string | null;
2023-06-23 21:58:33 +02:00
TMDB_READ_API_KEY: string;
2023-02-22 21:41:13 +01:00
NORMAL_ROUTER: boolean;
PROXY_URLS: string[];
2023-10-31 21:08:09 +01:00
BACKEND_URL: string;
2023-12-01 14:34:52 +01:00
DISALLOWED_IDS: string[];
2023-12-19 20:41:56 +01:00
TURNSTILE_KEY: string | null;
2023-12-27 23:39:32 +01:00
CDN_REPLACEMENTS: Array<string[]>;
2024-01-16 20:28:33 +01:00
HAS_ONBOARDING: boolean;
2022-12-29 18:25:57 +01:00
}
2022-12-27 16:44:36 +01:00
const env: Record<keyof Config, undefined | string> = {
2023-06-23 21:58:33 +02:00
TMDB_READ_API_KEY: import.meta.env.VITE_TMDB_READ_API_KEY,
2022-12-27 16:44:36 +01:00
APP_VERSION: undefined,
GITHUB_LINK: undefined,
DONATION_LINK: undefined,
2022-12-27 16:44:36 +01:00
DISCORD_LINK: undefined,
DMCA_EMAIL: import.meta.env.VITE_DMCA_EMAIL,
2022-12-27 16:44:36 +01:00
CORS_PROXY_URL: import.meta.env.VITE_CORS_PROXY_URL,
NORMAL_ROUTER: import.meta.env.VITE_NORMAL_ROUTER,
2023-10-31 21:08:09 +01:00
BACKEND_URL: import.meta.env.VITE_BACKEND_URL,
2023-12-01 14:34:52 +01:00
DISALLOWED_IDS: import.meta.env.VITE_DISALLOWED_IDS,
2023-12-19 20:41:56 +01:00
TURNSTILE_KEY: import.meta.env.VITE_TURNSTILE_KEY,
2023-12-27 23:39:32 +01:00
CDN_REPLACEMENTS: import.meta.env.VITE_CDN_REPLACEMENTS,
2024-01-16 20:28:33 +01:00
HAS_ONBOARDING: import.meta.env.VITE_HAS_ONBOARDING,
2022-12-27 16:44:36 +01:00
};
// loads from different locations, in order: environment (VITE_{KEY}), window (public/config.js)
2023-06-23 21:58:33 +02:00
function getKeyValue(key: keyof Config): string | undefined {
2022-12-27 16:44:36 +01:00
let windowValue = (window as any)?.__CONFIG__?.[`VITE_${key}`];
2023-12-12 22:53:35 +01:00
if (
windowValue !== null &&
windowValue !== undefined &&
windowValue.length === 0
)
2022-12-27 16:44:36 +01:00
windowValue = undefined;
2023-06-23 21:58:33 +02:00
return env[key] ?? windowValue ?? undefined;
}
function getKey(key: keyof Config, defaultString?: string): string {
return getKeyValue(key)?.toString() ?? defaultString ?? "";
2023-06-23 21:58:33 +02:00
}
2022-12-27 16:44:36 +01:00
2022-12-29 18:25:57 +01:00
export function conf(): RuntimeConfig {
const dmcaEmail = getKey("DMCA_EMAIL");
2023-12-19 20:41:56 +01:00
const turnstileKey = getKey("TURNSTILE_KEY");
2022-12-27 16:44:36 +01:00
return {
APP_VERSION,
GITHUB_LINK,
DONATION_LINK,
2022-12-27 16:44:36 +01:00
DISCORD_LINK,
DMCA_EMAIL: dmcaEmail.length > 0 ? dmcaEmail : null,
BACKEND_URL: getKey("BACKEND_URL", BACKEND_URL),
2023-06-23 21:58:33 +02:00
TMDB_READ_API_KEY: getKey("TMDB_READ_API_KEY"),
2023-02-22 21:41:13 +01:00
PROXY_URLS: getKey("CORS_PROXY_URL")
.split(",")
.map((v) => v.trim()),
2023-02-22 21:54:02 +01:00
NORMAL_ROUTER: getKey("NORMAL_ROUTER", "false") === "true",
2024-01-16 20:28:33 +01:00
HAS_ONBOARDING: getKey("HAS_ONBOARDING", "false") === "true",
2023-12-19 20:41:56 +01:00
TURNSTILE_KEY: turnstileKey.length > 0 ? turnstileKey : null,
2023-12-01 14:34:52 +01:00
DISALLOWED_IDS: getKey("DISALLOWED_IDS", "")
.split(",")
2023-12-01 23:23:06 +01:00
.map((v) => v.trim())
.filter((v) => v.length > 0), // Should be comma-seperated and contain the media type and ID, formatted like so: movie-753342,movie-753342,movie-753342
2023-12-27 23:39:32 +01:00
CDN_REPLACEMENTS: getKey("CDN_REPLACEMENTS", "")
.split(",")
.map((v) =>
v
.split(":")
.map((s) => s.trim())
.filter((s) => s.length > 0),
)
2023-12-28 00:00:14 +01:00
.filter((v) => v.length === 2), // The format is <beforeA>:<afterA>,<beforeB>:<afterB>
2022-12-27 16:44:36 +01:00
};
}