Skip to content

stefanoruth/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript Env Parser

Build & Tests

A typescript env parser, that validates server configuration and works well integration with libraries such as dotenv

Installation

yarn add @stefanoruth/env

Example

import { envParser } from '@stefanoruth/env'

const config = envParser({
    port: { type: 'number', defaultValue: 3000, env: 'PORT' },
    domain: { type: 'string', env: 'DOMAIN' },
    secret: { type: 'string', env: 'JWT_SECRET', optional: true },
    mode: { type: 'const', options: ['development', 'production'] as const, env: 'NODE_ENV' },
})

config.port // number
config.domain // string
config.secret // string | undefined
config.mode // 'development' | 'production'

Documentation

Supported types

Type Notes
number
string
boolean
const Const takes in an array of allowed values, the system with throw errors if the value from runtime or default value has not been set, (Note the return will be strict if you cast the array as const else, the array will be treated as an array of strings)

Each type has a the options:

Option Description
env Determines which key should be used when extracting the value from process.env
optional If a value is not marked as optional the system will automaticly throw an error when being called
defaultValue Each type has a default value that relates to the chosen type

About

Parsing environment variables typescript

Resources

Stars

Watchers

Forks

Releases

No releases published