1.3.0 • Published 5 days ago

@coderspirit/safe-env v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 days ago

@coderspirit/safe-env

NPM version TypeScript License npm downloads Known Vulnerabilities Security Score

Small library to load strongly typed values from environment variables and enforcing constraints on them.

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')

Supported types

  • boolean
  • string and string[]
  • int8, int16, int32, int54
  • int8[], int16[], int32[], int54[]
  • uint8, uint16, uint32
  • uint8[], uint16[], uint32[]

Supported Constraints

For numbers

  • min
  • max

For strings

  • minLength
  • maxLength
  • pattern: regular expression object.

For arrays

  • minLength
  • maxLength
  • valueConstraints: number or string constraints.
1.2.0

5 days ago

1.3.0

5 days ago

1.1.1

9 days ago

1.1.0

15 days ago

1.0.2

17 days ago

1.0.4

17 days ago

1.0.3

17 days ago

1.0.1

24 days ago

1.0.0

24 days ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago