Giter Club home page Giter Club logo

application.env's Introduction

gitpod ready-to-code npm

application.env

Small utility to load environment configurations that works for both, node and the browser, including a react context helper for react apps.

Usage

In most cases, all you need to do is invoke load function:

import { load } from 'application.env';

load().then((env) => console.log('Env ready', env));

The above will load application.env file and append it to global object (proccess.env for Node and window.env for Browser).

For Node you also get sync option:

import { loadSync } from 'application.env/node';

const env = loadSync();
console.log(env === proccess.env); // true

All load variants takes an optional options argument. Here's how its fields look with their default values:

function load(
  options: LoadOptions = {
    path: 'application.env', // Path to a file
    failSilently: false, // Returns/appends an empty object on error instead of throwing it.
    validator: ObjectValidator<Env> // Environment validator (read bellow)
  }): Promise<ApplicationEnv.Env> {}

Validation

All load functions also provide a way to validate loaded env before appending it to the global objects via the second validator optional argument. It's basically an object describing the shape of your env object with exposed properties to handle missing values.

interface Env {
  propOne: string;
  nonStringProperty: number;
  optionalProperty: string;
  criticalProperty: string;
}

const validator: ObjectValidator<Env> = {
  propOne: {
    errorMessage: "No default provided, throwing an error",
  },
  nonStringProperty: {
    default: 420,
    converter: (value) => Number(value)
  },
  optionalProperty: {
    default: "DEFAULT VALUE",
  },
  criticalProperty: {
    critical: true,
    errorMessage: "The application cannot function without this property. Terminating...",
  },
};

load({validator})

TypeScript

The module comes with it's own type declarations on both, public API and global scope extensions. It's also possible to declare your own named properties on loaded Env object via TS augmentation on ApplicationEnv namespace:

declare global {
  namespace ApplicationEnv {
    interface Env {
      MY_PROP_ONE?: string;
      ANOTHER_REQUIRED_PROP: string;
      ANOTHER_OPTIONAL_PROP: string;
      NODE_ENV?: string;
    }
  }
}

React

This module exposes a React context provider and hook to consume the context in a child component

import ApplicationEnvProvider, { useApplicationEnv } from 'application.env/react';

const App: FC = (): JSX.Element => {
  return (
    <ApplicationEnvProvider path="/application.env">
      <Child>
    </ApplicationEnvProvider>
  );
};

const Child: FC = () : JSX.Element => {
  const envVars = useApplicationEnv();

  return (<h1>{envVars.MY_PROP_ONE}</h1>)
}

export default App;

Behind the scenes

  • Node: the file is loaded from your file system.
  • Browser: the file is fetched from the same server that's serving HTML & JS files.
  • React: the file is fetched from the same server that's serving HTML & JS files.

application.env's People

Contributors

mpetuska avatar renovate[bot] avatar

Watchers

 avatar  avatar

Forkers

ed-sparkes

application.env's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency typescript to v4.9.5
  • Update dependency eslint-config-prettier to v9
  • Update dependency eslint-plugin-prettier to v5
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
.gitpod.Dockerfile
github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • github/codeql-action v2
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
npm
.yarn/sdks/eslint/package.json
.yarn/sdks/prettier/package.json
.yarn/sdks/typescript/package.json
package.json
  • @tsconfig/recommended ^1.0.1
  • @types/eslint ^8.0.0
  • @types/eslint-plugin-prettier ^3
  • @types/node ^18.0.0
  • @types/prettier ^2
  • @types/react ^18.0.0
  • @typescript-eslint/eslint-plugin ^5.0.0
  • @typescript-eslint/parser ^5.0.0
  • eslint ^8.0.0
  • eslint-config-google ^0.14.0
  • eslint-config-prettier ^8.0.0
  • eslint-plugin-prettier ^4.0.0
  • husky ^8.0.0
  • prettier ^2.2.1
  • react ^16 || ^17 || ^18.0.0
  • typescript ^4.1.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.