npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@beecode/msh-env

v1.1.0

Published

[![Build Status](https://beecode.semaphoreci.com/badges/msh-env/branches/main.svg?style=shields)](https://beecode.semaphoreci.com/projects/msh-env) [![codecov](https://codecov.io/gh/beecode-rs/msh-env/branch/main/graph/badge.svg?token=fHc0YaxEiB)](https:/

Downloads

36

Readme

Build Status codecov GitHub license
NPM

msh-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-env

Usage

import { MshNodeEnv } from '@beecode/msh-env'
import { cacheUtil } from '@beecode/msh-util/dist/lib/cache-util'

const env = MshNodeEnv()

export const config = cacheUtil.singleton(() => 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,
}))

Diagram

vision-diagram

MshNodeEnv options

| Name | Default | Description | | ---------------------------------------- | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | locationStrategy[] | [ new SimpleEnvLookup() ] | [Optional] Define how are we getting env values. Available: EnvironmentLocation, DockerSecretsLocation | | namingStrategy[] | [ new SimpleName() ] | [Optional] Define how are we checking for env names. Available: SimpleName, PrefixName, SuffixName |

Location Strategy

Location strategy is used to define the way we are getting the variables. We can combine multiple location strategies. The env is going to look through all chosen locations in the order that they are defined, and it will stop as soon as the env value is found.

EnvironmentLocation

We are simple checking the process.env for the env name.

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

Usage:

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

const env = MshNodeEnv()

DockerSecretsLocation

We are looking in docker swarm secrets.

Usage:

import { MshNodeEnv } from '@beecode/msh-env'
import { DockerSecretsLocation } from '@beecode/msh-env/location/docker-secrets-location'

const env = MshNodeEnv({ locationStrategy: [new location.DockerSecretsLocation()] })

CliArgsMinimistLocation

We can parse predefined command line arguments and use them as configuration. Also we can override environment environment variables

Usage:
env is going to try and find value in first location strategy, in our example CliArgsMinimistLocation, ad if we find DB_NAME we are going to use it instead from EnvironmentLocation

import { MshNodeEnv } from '@beecode/msh-env'
import { CliArgsMinimistLocation } from '@beecode/msh-env/location/cli-args-minimist-location'
import { EnvironmentLocation } from '@beecode/msh-env/location/environment-location'
import { Options } from 'minimist-options'

const options: Options = { DB_NAME: { alias: ['d', 'db-name', 'dbName'], type: 'string' } }
    const env = MshNodeEnv({
      locationStrategies: [new CliArgsMinimistLocation({ options, args: args.slice(2) }), new EnvironmentLocation()],
    })
    const config = Object.freeze({
      dbName: env('DB_NAME').string.required,
      dbPassword: env('DB_PASS').string.required,
    })

Naming Strategy

We are using naming strategy to give us flexibility to introduce isolated env values.
Idea: If we are using docker-compose and have one .env file with the database credentials that multiple containers.
Something like:

#.env
DB_USER=user
DB_PASS=pass
DB_NAME=db_name

If we wanted to change the database name for just one container, we can use PrefixName strategy and create name isolation, and we can set name of the container as prefix.

import { MshNodeEnv } from '@beecode/msh-env'
import { PrefixName } from '@beecode/msh-env/naming/prefix-name'

const env = MshNodeEnv({ namingStrategy: [new PrefixName('SOME_APP_')] })

Then we can add another env value prefixed with that container name.

#.env
DB_USER=user
DB_PASS=pass
DB_NAME=db_name
SOME_APP_DB_NAME=db_different_name

SimpleName

Simple name strategy is just a placeholder, the default strategy. It is not doing anything. :)

PrefixName

Prefix strategy is adding prefix to the existing name. There are two arguments available (prefix, joinChar).

Usage:

import { MshNodeEnv } from '@beecode/msh-env'
import { PrefixName } from '@beecode/msh-env/naming/prefix-name'

const env = MshNodeEnv({ namingStrategy: [new PrefixName('FOO_'), new PrefixName('BAR_')] })
const test = env('TEST').string.required // env look up in this order 1) BAR_FOO_TEST, 2) FOO_TEST, 3) TEST

SuffixName

Suffix strategy is adding suffix to the existing name. there are two arguments available (suffix, joinChar).

Usage:

import { MshNodeEnv } from '@beecode/msh-env'
import { SuffixName } from '@beecode/msh-env/naming/suffix-name'

const env = MshNodeEnv({ namingStrategy: [new SuffixName('_FOO'), new SuffixName('_BAR')] })
const test = env('TEST').string.required // env look up in this order 1) TEST_FOO_BAR, 2) TEST_FOO, 3) TEST

Logger Strategy

Define how and if we are logging. We are using @beecode/msh-log For more details read the msh-log readme

Usage:

import { MshNodeEnv } from '@beecode/msh-env'
import { LogLevelType } from '@beecode/msh-logger'
import { ConsoleLogger } from '@beecode/msh-logger/console-logger'

NodeEnvLogger(new ConsoleLogger(LogLevel.DEBUG))

const env = MshNodeEnv()