Library for loading and checking the presence of environment variables.
If the env variable must be there, but the developer forgot to define it in the .env file, the library will throw an error
You'll need to install the package from npm npm i react-env-load
.
import reactEnvLoad from 'react-env-load';
type IConfig = {
REACT_APP_TEST_STRING: string;
REACT_APP_TEST_NUMBER: number;
REACT_APP_TEST_BOOLEAN: boolean;
REACT_APP_TEST_OPTIONAL: string;
REACT_APP_TEST_EMAIL: string;
REACT_APP_TEST_ARRAY: string[];
};
const config = reactEnvLoad<IConfig>([
'REACT_APP_TEST_STRING',
{ name: 'REACT_APP_TEST_NUMBER', type: 'number' },
{ name: 'REACT_APP_TEST_BOOLEAN', type: 'boolean' },
{ name: 'REACT_APP_TEST_OPTIONAL', optional: true, default: 'default value' },
{
name: 'REACT_APP_TEST_EMAIL',
validate: (value: string | undefined) => {
if (!value?.endsWith('@icloud.com')) {
return { error: 'invalid email' };
}
return { value };
},
},
{
name: 'REACT_APP_TEST_ARRAY',
validate: (value: string | undefined) => {
return { value: value?.split(',') || [] };
},
},
]);
const App = () => {
return <div>{JSON.stringify(config)}</div>;
};
- Maksim Schiriy @maksim-schiriy