0.0.4-alpha.1 • Published 5 years ago

@beecode/msh-node-env v0.0.4-alpha.1

Weekly downloads
108
License
mit
Repository
github
Last release
5 years ago

Build Status codecov GitHub license
NPM

msh-node-env

Micro-service helper: node environment

This project is intended to be used in typescript project to validate and add types to the project configuration

Install

npm i @beecode/msh-node-env

Usage

import MshNodeEnv from '@beecode/msh-node-env'
const env = MshNodeEnv()

export const config = Object.freeze({
  someRequiredString: env('SOME_REQUIRED_STRING').string.required,
  strWithDefaultValue: env('STR_WITH_DEFAULT_VALUE').string.default('default-value').required,
  optionalString: env('OPTIONAL_STRING').string.optional,
  defKeyName: env('ANY_KEY_NAME').string.required,
  someNumberValue: env('SOME_NUMBER_VALUE').number.required,
  someBooleanValue: env('SOME_BOOLEAN_VALUE').boolean.required,
  someJsonValue: env('SOME_JSON_VALUE').json().required,
})

MshNodeEnv options

NameDefaultDescription
locationStrategy?new SimpleEnvLookup()Optional Chose the strategy how the values are going to be loaded. Available: SimpleEnvLookup, ProjectNameIsolation, VersionProjectNameIsolation
loggerStrategy?new NoLogger()Optional Chose the strategy how the logging is provided. Available: NoLogger, ConsoleLogger

Location Strategy

Location strategy is used to define the way we are getting the variables.

SimpleEnvLookup

As the name implies this strategy is simply using the env name passed to env().

env('SOME_ENV_KEY') => process.env.SOME_ENV_KEY

ProjectNameIsolation

Usage:

import MshNodeEnv, { location } from '@beecode/msh-node-env'

const projectName = 'SomeProject'
const env = MshNodeEnv({ locationStrategy: new location.ProjectNameIsolation(projectName) })

For this strategy we need to provide the project name. It tries to get the env with the project name (snake upper case) prefixed (project specific), and if it does not find it falls back to the simple env lookup strategy.

env('SOME_ENV_KEY') => process.env.SOME_PROJECT_SOME_ENV_KEY || process.env.SOME_ENV_KEY

VersionProjectNameIsolation

Usage:

import MshNodeEnv, { location } from '@beecode/msh-node-env'

const projectName = 'SomeProject'
const version = '1.0.1'
const env = MshNodeEnv({ locationStrategy: new location.VersionProjectNameIsolation(projectName, version) })

For this strategy we need to provide the project name and version. It tries to get the env with the project name+version (snake upper case) prefixed (project specific), and if it does not find it falls back to the project name isolation strategy.

env('SOME_ENV_KEY') => process.env.SOME_PROJECT_1_0_1_SOME_ENV_KEY || process.env.SOME_PROJECT_SOME_ENV_KEY || process.env.SOME_ENV_KEY

Logger Strategy

Define how and if we are logging

NoLogger

This is the default logging strategy, meaning the logging is ignored

ConsoleLogger

This is a simple logging strategy, it outputs all logs to console with a prefix of the log level (ERROR:, WARN:, INFO:, DEBUG:)

import MshNodeEnv, { logger } from '@beecode/msh-node-env'

const env = MshNodeEnv({ loggerStrategy: new logger.ConsoleLogger(logger.LogLevel.INFO) })
1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1-alpha

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.7-alpha

5 years ago

0.0.7

5 years ago

0.0.7-alpha.0

5 years ago

0.0.6

5 years ago

0.0.6-alpha.1

5 years ago

0.0.5-alpha.1

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.4-alpha.1

5 years ago

0.0.4-dev.1

5 years ago

0.0.4-dev.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.2-dev.0

5 years ago

0.0.1-dev.1

5 years ago

0.0.1-dev.0

5 years ago

0.0.1

5 years ago

0.0.1-dev

5 years ago