Git Product home page Git Product logo

envious's Introduction

Envious

Environment variable parsing using Dotenv & Typebox

Install

npm i @pitininja/envious

Usage

import { Type } from '@sinclair/typebox';
import { envious } from '@pitininja/envious';

export const schema = Type.Object({
    STRING_VAR: Type.String(),
    NUMBER_VAR: Type.Integer(),
    BOOLEAN_VAR_WITH_DEFAULT: Type.Boolean({ default: false }),
    OPTIONAL_VAR: Type.Optional(Type.String())
});

/**
Parse your environment variables.
If the environment variables don't match the schema, an error will be thrown.
*/
const env = envious(schema);

envious's People

Contributors

pitininja avatar

Stargazers

Elliot Nash avatar Mateo avatar Daniel Bodnar avatar Daniele avatar Christian Erhardt avatar  avatar Ludvík Prokopec avatar

Watchers

 avatar

envious's Issues

Implement proper parsing of input object with more typebox features

Hi,

I stumbled accross your library because we want to properly parse our environment and are already using TypeScritpt. During testing i recognized, that it is currently not possible to set default values for environment variables even if Typebox supports setting of default values.

I stumbled accross this post in the Typebox repo where they implement a proper parsing function. I tested it in my code and it was working as expected. When setting a default value and calling the parse function, the optional properties with a default value are set to their respective default value.

A small example here:

import {Static, TSchema, Type} from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';

const Parse = <T extends TSchema>(T: T, value: unknown) => 
  Value.Decode(T,
    Value.Clean(T,
      Value.Default(T,
        Value.Convert(T, value),
      )
    )
  )

const ConfigSchema = Type.Object({
  FOO: Type.Optional(
    Type.String({
      default: 'mydefaultfooval',
    })
  ),
});
type ConfigSchema = Static<typeof Schema>;

env = Parse(ConfigSchema , process.env);

then env would be type of ConfigSchema and the result would be:

{
  FOO: 'mydefaultfooval',
}

Maybe this is something you want to implement?

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.