Giter Site home page Giter Site logo

zeddy-config's Introduction

Zeddy Config

Zeddy config is a simple configuration library inspired by convict built in TypeScript. It closely follows suggestions written in my upcoming book 'Pragmatic Node.js development: Primer in NestJS'. It also follows the suggestions from The Twelve-Factor App for configuring variables via environment variables.

Usage

In config.ts write your schema. Full interface of options are given in Interfaces section.

import { configz, dotenvize } from "zeddy-config";

dotenvize();

export const config = configz({
  env: {
    description: "Running environment",
    envVar: "NODE_ENV",
    type: "string"
  },
  server: {
    description: "Server info",
    type: "object",
    properties: {
      port: {
        description: "Server port",
        envVar: "SERVER_PORT",
        type: "int",
        validator: (port) => {
          return port === 3000;
        }
      },
      host: {
        description: "Server host",
        envVar: "SERVER_HOST",
        type: "string"
      },
    }
  }
});

// resulting config interface is {env: string; server: {port: number; host: string}}

If you want to use .env files with overrides per NODE_ENV environment variable you can call dotenvize() before configz() to load all necessary environment variables from .env file and .env.${process.env.NODE_ENV}files.

Example project

Example project can be found on https://github.com/zveljkovic/zeddy-config-example.

Interfaces

String value interface expects environment variable in any format.

type ZeddyConfigSchemaStringElementInterface = {
  type: "string";
  envVar: string;
  required?: boolean; // default true
  description?: string;
  validator?: ZeddyConfigSchemaValidator;
}

Integer value interface expects environment variable as whole value.

type ZeddyConfigSchemaIntElementInterface = {
  type: "int";
  envVar: string;
  required?: boolean;
  description?: string;
  validator?: ZeddyConfigSchemaValidator;
}

Float value interface expects environment variable as whole or decimal value.

type ZeddyConfigSchemaFloatElementInterface = {
  type: "float";
  envVar: string;
  required?: boolean;
  description?: string;
  validator?: ZeddyConfigSchemaValidator;
}

Boolean value interface expects environment variable as true/false.

type ZeddyConfigSchemaBooleanElementInterface = {
  type: "boolean";
  envVar: string;
  required?: boolean;
  description?: string;
  validator?: ZeddyConfigSchemaValidator;
}

Object value interface serves as a grouping of environment variables.

type ZeddyConfigSchemaObjectElementInterface = {
  type: "object";
  properties: Record<string, ZeddyConfigSchemaElement>;
  description?: string;
 }

zeddy-config's People

Contributors

zveljkovic avatar

Stargazers

Marko Stevanovic avatar

Watchers

James Cloos avatar Marko Stevanovic avatar  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.