@coderspirit/safe-env
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@coderspirit/safe-env

NPM version TypeScript License npm downloads Known Vulnerabilities Security Score

Small library to load strongly typed values from environment variables

Install instructions

Node

# With PNPM
pnpm add @coderspirit/safe-env

# With NPM
npm install @coderspirit/safe-env

# Or with Yarn:
yarn add @coderspirit/safe-env

Example

import { getSafeEnv } from '@coderspirit/safe-env'

// It validates the specified constraints at construction time
const safeEnv = getSafeEnv(process.env, {
  host: { type: 'string', default: 'localhost' },
  port: { type: 'uint16', default: 4321 },

  githubToken: { type: 'string', optional: true },
  secretToken: { type: 'string' },
})

// It leverages the powerful TypeScript's type system to tell you at
// compile time if you made a mistake.
const host = safeEnv.get('hostt') // Type Error

const host = safeEnv.get('host') // All good

// The return type tells you not only that you have a number, but also
// that it is an integer and positive.
const port = safeEnv.get('port')

// It will return undefined if the variable does not exist
const githubToken = safeEnv.get('githubToken')

// It fill fail if the variable does not exist
const secretToken = safeEnv.get('secretToken')

Package Sidebar

Install

npm i @coderspirit/safe-env

Weekly Downloads

236

Version

1.1.0

License

MIT

Unpacked Size

31.8 kB

Total Files

8

Last publish

Collaborators

  • castarco