New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@solid-soda/config

Package Overview
Dependencies
Maintainers
3
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@solid-soda/config

Provides several classes to help you find, load, combine, autofill and validate configuration values of any kind.

npmnpm
Version
2.0.0
Version published
Weekly downloads
36
-16.28%
Maintainers
3
Weekly downloads
 
Created
Source

@solid-soda/config

Provides several classes to help you find, load, combine, autofill and validate configuration values of any kind.

Why this library:

  • simple way to configure application
  • don't read or rewrite global object (like process.env) in an app
  • different configs for different environments
  • friendly for DI containers

Installation

yarn add @solid-soda/config

TL;DR

In example app we want to use DotEnvConfiguration in dev environment and EnvConfiguraton in production. Just create a simple factory function:

import { DotEnvConfiguration, EnvConfiguraton } from '@solid-soda/config'

function isDev() {
  return process.env.NODE_ENV === 'development'
}

export const getConfig = () => {
  if (isDev()) {
    return new DotEnvConfiguration('../.env')
  }

  return new EnvConfiguraton()
}

That is all. We can use getConfig in any place of our application, or pass the result to DI container, etc.

import { getConfig } from './config'

const secret = getConfig()
  .get('APP_SECRET', 'DefaultSecret')

Basics

Every configuration implements Configuration interface.

@solid-soda/config uses nanoption for nullable values

import { Option } from 'nanoption'

interface Configuration {
  get(key: string): Option<string>
  getString(key: string): Option<string>
  getNumber(key: string): Option<number>
  getBoolean(key: string): Option<boolean>
  getDate(key: string): Option<Date>

  getOrElse(key: string, or: string): string
  getStringOrElse(key: string, or: string): string
  getNumberOrElse(key: string, or: number): number
  getBooleanOrElse(key: string, or: boolean): boolean
  getDateOrElse(key: string, or: Date): Date

  getOrThrow(key: string): string
  getStringOrThrow(key: string): string
  getNumberOrThrow(key: string): number
  getBooleanOrThrow(key: string): boolean
  getDateOrThrow(key: string): Date

  isDev(): boolean
  isProd(): boolean
}

Method *OrThrow throws ParameterNotFound exception, if a value for the provided key is empty.

Load configs

Library provides classes for comfortable loading of configs from different sources.

DotEnvConfiguration

uses .env file to load configuration. Built over great dotenv lib.

Example:

import { DotEnvConfiguration } from '@solid-soda/config'

const config = new DotEnvConfiguration('./configs/.env')

EnvConfiguraton

uses process.env to load configuration.

Example:

import { EnvConfiguration } from '@solid-soda/config'

const config = new EnvConfiguration()

ExternalConfiguration

uses plain object as configuration source.

import { ExternalConfiguration } from '@solid-soda/config'

const config = new ExternalConfiguration({
  apiToken: 'jkfdshfk323.fjkhdksf.aodsa34',
  applySecurity: true,
})

FileConfiguration

can accept any file as configuration. You must pass fileParse as second argument to parse file.

import { FileConfiguration, jsonParse } from '@solid-soda/config'

const config = new FileConfiguration('./configs/params.json', jsonParse)
Available parsers
  • jsonParse

Also you can create the custom parser. It must be a function (file: stirng) => ConfigDict, where ConfigDict is object with string keys and string or undefined values.

Custom configuration

Of course, you can create the custom Configuration. Just implement Configuration interface and use it.

Also, you can extend helper class AbstractConfiguration and implement only get and isDev methods.

The following configuration has no values and always returns empty Option.


import { AbstractConfiguration } from '@solid-soda/config'

class NeverConfiguration extends AbstractConfiguration {
  public get = (key: string) => Option.of(null)

  public isDev = () => true
}

Combine configs

Work in progress

Validate configs

Work in progress

Autofill configs

Work in progress

FAQs

Package last updated on 27 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts