server.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Disable the following rule because this file is the intended place to declare
  2. // and load all env variables.
  3. /* eslint-disable n/no-process-env */
  4. // Disable the following rule because variables in this file are only loaded at
  5. // runtime and do not influence the build outputs, thus they need not be
  6. // declared to turbo for it to be able to cache build outputs correctly.
  7. /* eslint-disable turbo/no-undeclared-env-vars */
  8. import "server-only";
  9. /**
  10. * Throw if the env var `key` is not set (at either runtime or build time).
  11. */
  12. const demand = (key: string): string => {
  13. const value = process.env[key];
  14. if (value === undefined || value === "") {
  15. throw new MissingEnvironmentError(key);
  16. } else {
  17. return value;
  18. }
  19. };
  20. /**
  21. * Indicates that this server is the live customer-facing production server.
  22. */
  23. export const IS_PRODUCTION_SERVER = process.env.VERCEL_ENV === "production";
  24. /**
  25. * Throw if the env var `key` is not set in the live customer-facing production
  26. * server, but allow it to be unset in any other environment.
  27. */
  28. const demandInProduction = IS_PRODUCTION_SERVER
  29. ? demand
  30. : (key: string) => process.env[key];
  31. export const GOOGLE_ANALYTICS_ID = demandInProduction("GOOGLE_ANALYTICS_ID");
  32. export const AMPLITUDE_API_KEY = demandInProduction("AMPLITUDE_API_KEY");
  33. class MissingEnvironmentError extends Error {
  34. constructor(name: string) {
  35. super(`Missing environment variable: ${name}!`);
  36. this.name = "MissingEnvironmentError";
  37. }
  38. }