Coder Social home page Coder Social logo

sp-conf-ts's Introduction

SP Conf

SimPle CONFig TypeScript

A small component for reading config. It will log all the variables your are reading so you can see what values your app it using, obfuscating passwords, It will also check if any variables are missing and allow your app to respond.

Gettings Started

$ yarn add sp-conf-ts

How to use

Check out example.ts that shows an example of using it.

import {
  readString, 
  readNumber, 
  readBoolean, 
  missingEnvVars, 
  readPassword, 
  readUrl
} from 'sp-conf-ts'

const regexForIpV4Address = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/

const myconfig = {
  port: readNumber('PORT', {defaultValue: 8080}),
  user: readString(['CURRENT_USER', 'DEFAULT_USER']),
  serviceUrl: readUrl('SERVICE_URL'),
  database: {
    host: readString('DB_HOST_IP', {validator: regexForIpV4Address}),
    port: readNumber('DB_PORT'),
    username: readString('DB_USERNAME'),
    password: readPassword('DB_PASSWORD'),
    keepConnectionOpen: readBoolean('KEEP_CONNECTION_OPEN')
  }
}

if (missingEnvVars) {
  console.error('Some required env vars were missing. Terminating')
  process.exit(1)
}

export default myconfig

Options common to all methods

  • defaultValue - The value to use if the specified value is not available

  • validator - A regular expressing to specify the format of the input value

  • log - A function that logs about reading env var. Defaults to logging to stdout. E.g. To log messages with a prefix:

log: (msg: string) => console.log('message:', msg)
  • error - A function that reports errors while reading env var. Defaults to logging to stderr. E.g. To log errors with a prefix:
error: (err: string) => console.error('error:', err)
  • source - The object to read env vars from. Defaults to process.env

Methods available

  • readString - Read a string not applying any special rules

  • readNumber - Read a number and complain if its not a number

  • readPassword - Read a string but will obfuscate when logging the value out

  • readUrl - Read a URL and will obfuscate the password if the URL contains one.

  • readBool - Read a boolean and complain if it's not valid. Expected characters are:

    • truthy values - "true", "t", "on", "1"
    • falsy falues - "false", "f", "off", "0" It is not case sensitive so, for example, both "True" and "TRUE" work just fine

    To use your own value for true/false supply the trueValue and/or falseValue. E.g. To log errors with a prefix:

    keepConnectionOpen: readBoolean('LOAD_STUFF', {trueValue: 'yes', falseValue: 'no' })

    If you supply isSetIsTrue insted, then the result will be true if the envvar exists (even empty) and false if not.

  • readCertificate - Read a certificate (a string start is bookmarked by -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- by default) Set options beginCertificate and endCertificate to use different headder and footer.

sp-conf-ts's People

Contributors

thecodeite avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.