0.1.1 • Published 8 months ago

react-env-load v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Introduction

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

Navigation

Setup

You'll need to install the package from npm npm i react-env-load.

Example

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>;
};

Authors

0.1.1

8 months ago

0.1.0

8 months ago

0.1.0-canary.1

8 months ago

0.1.0-canary.0

8 months ago