Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aapzu/tsdotenv

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aapzu/tsdotenv

Creates and parses a typed dotenv config from a given schema

  • 1.5.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
14
decreased by-26.32%
Maintainers
1
Weekly downloads
 
Created
Source

tsdotenv

Tsdotenv is a package which generates a validated and strongly typed config object from .env file (or process.env variables).

Travis (.com) GitHub

Installation

# using npm
npm install @aapzu/tsdotenv

# using yarn
yarn add @aapzu/tsdotenv

Usage

Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:

DB_HOST=localhost
DB_PORT=5432
DEBUG=true

Create a config file as a single place of truth for the environment variables

import { parse } from '@aapzu/tsdotenv'

const SCHEMA = {
  DB_HOST: String,
  DB_PORT: { type: Number, default: 3000 },
  DEBUG: { type: Boolean, optional: true },
  CUSTOM_ENV: { type: ['test', 'prod'] },
  // enums and optionals only get typed properly if the
  // schema is a readonly object
} as const

const config = parse(SCHEMA, {
  path: 'path_to_dotenv_file',
})

/*
  typeof config = {
    DB_HOST: string,
    DB_PORT: number,
    DEBUG: boolean | undefined
    CUSTOM_ENV: 'test' | 'prod'
  }
 */
export default config

Use that config file in other files

import config from '../path/to/config'

console.log(DB_HOST, typeof DB_HOST)
// localhost string

console.log(DB_PORT, typeof DB_PORT)
// 5432 number

console.log(DEBUG, typeof DEBUG)
// true boolean

Schema

Schema is the heart of the library. The parsing and validation of the values is done according to the schema. Possible schema value types are:

namesyntax
stringString
numberNumber
booleanBoolean
enum['value1', 'value2']
string arrayArray(String)
number arrayArray(Number)
boolean arrayArray(Boolean)

A schema item has type and possibly a default value. If the item has optional: true without a default value, it's possible that the value ends up being undefined.

An example schema is as follows:

const schema = {
  BOOLEAN_ARRAY: Array(Boolean),
  BOOLEAN: {
    type: Boolean,
    optional: true,
  },
  ENUM: {
    type: ['foo', 'bar'],
    default: 'foo',
  },
  NUMBER_ARRAY: Array(Number),
  NUMBER: {
    type: Number,
    default: 42,
  },
  STRING: String,
  STRING_ARRAY: Array(String),
}

Options

path

Default: path.resolve(process.cwd(), '.env')

You may specify a custom path if your file containing environment variables is located elsewhere.

parse(schema, { path: '/custom/path/to/.env' })
camelCaseKeys

Default: false

Maps the keys of the config object into camelCase. Example:

import { parse } from '@aapzu/tsdotenv'

const config = parse({
  DB_HOST: String,
  DB_PORT: { type: Number, default: 3000 },
}, {
  path: 'path_to_dotenv_file',
  camelCaseKeys: true
})

/*
  typeof config = {
    dbHost: string,
    dbPort: number,
  }
 */
export default config
encoding

Default: utf8

You may specify the encoding of your file containing environment variables.

parse(schema, { encoding: 'latin1' })
debug

Default: false

You may turn on logging to help debug why certain keys or values are not being set as you expect.

parse(schema, { debug: process.env.DEBUG })

Keywords

FAQs

Package last updated on 19 May 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc