add a constant file to read the env variables
This commit is contained in:
parent
7c5ab7c65f
commit
4007efcc57
4 changed files with 37 additions and 17 deletions
20
ui/lib/constants.ts
Normal file
20
ui/lib/constants.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
export const ENV = {
|
||||
WS_URL: process.env.NEXT_PUBLIC_WS_URL,
|
||||
API_URL: process.env.NEXT_PUBLIC_API_URL,
|
||||
} as const;
|
||||
|
||||
export type ENV = typeof ENV;
|
||||
|
||||
// Type guard function
|
||||
export function assertEnvVariables(ENV: ENV): asserts ENV is Required<ENV> {
|
||||
const missingVariables = Object.entries(ENV).filter(([_, value]) => value === undefined);
|
||||
if (missingVariables.length > 0) {
|
||||
throw new Error(`Missing environment variables: ${missingVariables.map(([key]) => key).join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Call this function early in your app's initialization
|
||||
assertEnvVariables(ENV);
|
||||
|
||||
// After assertion, we can safely use ENV
|
||||
export const VALIDATED_ENV: Required<ENV> = ENV as Required<ENV>;
|
Loading…
Add table
Add a link
Reference in a new issue