Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/knpwrs/envariant
https://github.com/knpwrs/envariant
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/knpwrs/envariant
- Owner: knpwrs
- License: unlicense
- Created: 2022-08-09T15:39:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-19T21:06:56.000Z (about 2 years ago)
- Last Synced: 2024-12-13T12:40:05.967Z (about 1 month ago)
- Language: TypeScript
- Size: 536 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `envariant`
This is a small snippet that I found myself writing for multiple projects in
the form of an npm module. `envariant` is essentially a way to guarantee that
an environment variable contains a truthy string at runtime. If the environment
variable is not defined, `envariant` will throw. This is useful for [type
narrowing][tn] your environment variables that you expect to be defined.## Usage
```ts
import env from '@knpwrs/envariant';const DB_URI = env('DB_URI');
// DB_URI is guaranteed to be defined at this point, as long as there is a `DB_URI` environment variable defined.
// If there isn't a `DB_URI` variable defined then `env` will throw and this script will crash.// There is an optional second parameter that lets you supply an object to read variables from; for
// instance, the `env` parameter in Cloudflare workers:
const ENDPOINT = env('ENDPOINT', env);
```## Installation
```sh
npm install @knpwrs/envariant
```## License
[Unlicensed](https://unlicense.org/)
[tn]: https://github.com/alexreardon/tiny-invariant/tree/31cf8fb78a6fd76d4e50217267de8bca6b43f674#type-narrowing