Skip to content

Latest commit

History

History
109 lines (75 loc) 路 2.63 KB

File metadata and controls

109 lines (75 loc) 路 2.63 KB

Dynamic variables based on environment (or anything else!)
feats' dynamic-variables is a minimalist choice for switching environmental variables in your app.

[ Getting started 馃 | Check it on NPM 馃憣 ]

divider

Setting up

Installing it

You can install it from one of these 3 options:

with NPM

  $ npm install dynamic-variables

with Yarn

  $ yarn add dynamic-variables

manually

you may also install it as a development dependency in a package.json file:

  // package.json
  "dependencies": {
    "dynamic-variables": "latest"
  }

Then install it with either npm install or yarn install

divider

Getting started

Basic usage

import { env } from 'dynamic-variables'

// it will be assigned 'API_SERVER_PUBLIC` in the browser and 'API_SERVER_CONTAINER' in the server
const backendEndpoint = env(process.env.API_SERVER_PUBLIC, process.env.API_SERVER_CONTAINER)

Advanced usage

Multiple named environments + Custom environment detector

import { env, setDetector } from 'dynamic-variables'

setDetector(() => process.browser ? 'BROWSER' : 'SSR')

// it will be assigned 'API_SERVER_PUBLIC` in the browser and 'API_SERVER_CONTAINER' in the server
const backendEndpoint = env({BROWSER: process.env.API_SERVER_PUBLIC, SSR: process.env.API_SERVER_CONTAINER})

Multiple services endpoints

import environment, { env } from 'dynamic-variables'

module.exports = environment({
  redis: env(process.env.REDIS_CONTAINER),
  backend: {
    graphq: env(process.env.GRAPHQL_SERVER_PUBLIC, process.env.GRAPHQL_SERVER_CONTAINER),
    // 馃寛 Note that backend.rest below is a function! 鈽勶笍
    // As such, it can avoid ReferenceErrors in case the variables in it were not defined
    rest: () => env(process.env.REST_SERVER_PUBLIC, process.env.REST_SERVER_CONTAINER),
  }
})

For more examples, please check the tests file.

I hope you enjoy using this tiny lib! 馃帀

divider




This project is sponsored by feats.
Join a community of people helping each other get credit for their roles in projects.
From developing products and apps to architecture and campaigns.