react-env-load
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

react-env-load

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

Package Sidebar

Install

npm i react-env-load

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

10.7 kB

Total Files

11

Last publish

Collaborators

  • muzikanto