Created
May 13, 2021 16:22
-
-
Save gugadev/91f464d65a58a92dcb175a642bd57b20 to your computer and use it in GitHub Desktop.
Utilitary for get environment variables in CRA.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Environment { | |
static string(varName: string, defaultValue = ""): string { | |
const value = process.env[`REACT_APP_${varName}`]; | |
return value ?? defaultValue; | |
} | |
static int(varName: string, defaultValue = 0): number { | |
const value = process.env[`REACT_APP_${varName}`]; | |
if (value) { | |
return Number.parseInt(value, 10); | |
} | |
return defaultValue; | |
} | |
static float(varName: string, defaultValue = 0.0): number { | |
const value = process.env[`REACT_APP_${varName}`]; | |
if (value) { | |
return Number.parseFloat(value); | |
} | |
return defaultValue; | |
} | |
static bool(varName: string, defaultValue = false): boolean { | |
const value = process.env[`REACT_APP_${varName}`]; | |
if (value) { | |
return value === "on" || value === "true"; | |
} | |
return defaultValue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment