0.0.53 • Published 1 year ago

@kth/default-envs v0.0.53

Weekly downloads
88
License
MIT
Repository
github
Last release
1 year ago

Default-envs Continous Integration

@kth/default-envs

Usage

Basic behaviour

Process env:s that are not configured on start up, but accessed via process.env.ENV_NAME in the application are added with there default values, as specified as a key-value object.

const defaultEnvs = require("@kth/default-envs");

const DEFAULTS = {
  LOG_LEVEL: "info",
  PORT: 80,
  APPINSIGHTS_INSTRUMENTATIONKEY: "",
};

defaultEnvs.set(DEFAULTS);

console.log(process.env.PORT); // 80

Override default values

If an env is set on startup it will be used, not the default.

APPINSIGHTS_INSTRUMENTATIONKEY="abc-123" node app.js
const defaultEnvs = require("@kth/default-envs");

const DEFAULTS = {
  LOG_LEVEL: "info",
  PORT: 80,
  APPINSIGHTS_INSTRUMENTATIONKEY: "",
};

defaultEnvs.set(DEFAULTS);

console.log(process.env.APPINSIGHTS_INSTRUMENTATIONKEY); // abc-123

Required values without any defaults

Some envs do not have defaults and must exist before starting your service. defaultEnvs.required([], console) will print information about if a logger is passed, and if the env is missing when invoking required throw an error Required env 'PASSWORD' does not exist. .

node app.js
const defaultEnvs = require("@kth/default-envs");

const DEFAULTS = {
  USER: "admin",
  URI: 'example.com'
};

defaultEnvs.set(DEFAULTS); 
defaultEnvs.required(['PASSWORD']); // Exception:Required env 'PASSWORD' does not exist.
PASSWORD='s3cret' node app.js
const defaultEnvs = require("@kth/default-envs");

const DEFAULTS = {
  USER: "admin",
  URI: 'example.com'
};

defaultEnvs.set(DEFAULTS); 
defaultEnvs.required(['PASSWORD']);
console.log(process.env.PASSWORD); // s3cret

Log the defaults used

If you pass a logger like console or any other that implements logger functions debug, info or warn you will get information about what defaults are used when invoking defaultEnvs.set({}, logger);

TOKEN="xxxx-yyyy" PORT=3000 node app.js
const defaultEnvs = require("@kth/default-envs");

const DEFAULTS = {
  LOG_LEVEL: "info",
  PORT: 80,
  APPINSIGHTS_INSTRUMENTATIONKEY: "",
};

defaultEnvs.set(DEFAULTS, console);
defaultEnvs.required(['TOKEN', 'PASSWORD']); 
08:57:00.808Z  INFO my-app:  - Env 'LOG_LEVEL' is not set, defaulting to 'info'.
08:57:00.811Z  INFO my-app:  - Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
08:57:00.811Z  INFO my-app:  - ✅ Found required env 'TOKEN'
08:57:00.811Z  INFO my-app:  - 🚨 Missing required env 'PASSWORD'
08:57:00.811Z  WARN my-app:  Required env 'PASSWORD' does not exist.

  Exception: Required env 'PASSWORD' does not exist.

Tests

npm install npm test

  Default Envs 

    ✔ Throw an error if a required env is missing.
    ✔ Do not throw an error if a required env exists.
    ✔ If a default value is set you can access it wia process.env.
    ✔ If a env is already set prior to running set(defaults), process.env will return it.
    ✔ After runnign unset() all defaults values are removed from process.env array.
    ✔ After runnign unset() all emvs set on startup are still availible.
    ✔ If a logger is passed to the set({}, logger), use it to log.

  7 passing (10ms)
  

Demo

  1. Go to the directory https://github.com/KTH/default-envs/tree/master/demo
  2. npm install
  3. npm run ok
> default-envs-demo@0.0.1 ok
> PASSWORD='s3cret' TOKEN='xxxx-1111' APPLICATION_NAME='Super default-envs-demo 🚀' node demo.js

 - Env 'LOG_LEVEL' is not set, defaulting to 'info'.
 - Env 'PORT' is not set, defaulting to '3000'.
 - Env 'API_HOST' is not set, defaulting to 'https://api.kth.se'.
 - Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
 - ✅ Found required env 'PASSWORD'
 - ✅ Found required env 'TOKEN'

Application name: Super default-envs-demo 🚀
  1. npm run fail
> default-envs-demo@0.0.1 fail
> TOKEN='xxxx-1111' node demo.js

 - Env 'APPLICATION_NAME' is not set, defaulting to 'Demo-app'.
 - Env 'LOG_LEVEL' is not set, defaulting to 'info'.
 - Env 'PORT' is not set, defaulting to '3000'.
 - Env 'API_HOST' is not set, defaulting to 'https://api.kth.se'.
 - Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
 - 🚨 Missing required env 'PASSWORD'
 - ✅ Found required env 'TOKEN'
Required env 'PASSWORD' does not exist.

/Users/patricjansson/dev/kth/gita.sys.kth.se/default-envs/demo/node_modules/@kth/default-envs/index.js:69
      throw message;
      ^
Required env 'PASSWORD' does not exist.
(Use `node --trace-uncaught ...` to show where the exception was thrown)
0.0.53

1 year ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.40

2 years ago

0.0.41

2 years ago

0.0.42

2 years ago

0.0.43

2 years ago

0.0.44

2 years ago

0.0.45

2 years ago

0.0.46

2 years ago

0.0.47

2 years ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.39

2 years ago

0.0.33

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.36

2 years ago

0.0.48

2 years ago

0.0.32

2 years ago

0.0.31

3 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.28

3 years ago

0.0.27

3 years ago

0.0.25

3 years ago

0.0.26

3 years ago

0.0.24

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago