Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mahabubx7/tenv
This package is a type-safe parser for .env files. It is built for node.js based typescript applications.
https://github.com/mahabubx7/tenv
dotenv env environment-variables tenv tsenv type-safe-env type-safe-environment-variables typedenv
Last synced: about 1 month ago
JSON representation
This package is a type-safe parser for .env files. It is built for node.js based typescript applications.
- Host: GitHub
- URL: https://github.com/mahabubx7/tenv
- Owner: mahabubx7
- License: mit
- Created: 2024-01-29T21:56:50.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-03-04T16:24:18.000Z (10 months ago)
- Last Synced: 2024-08-10T10:08:42.354Z (4 months ago)
- Topics: dotenv, env, environment-variables, tenv, tsenv, type-safe-env, type-safe-environment-variables, typedenv
- Language: HTML
- Homepage: https://www.npmjs.com/package/@mx7/tenv
- Size: 431 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Type-safe .env variable parser
This package is a type-safe parser for .env files. It is built for node.js based typescript applications.
### Installation
**Recommending NodeJS v18.x (LTS) or later**
> This package cannot load the environment variables itself. You need to use something like `dotenv` or latest NodeJs built-in env-file loader
```bash
$ npm i @mx7/tenv
# or
$ yarn add @mx7/tenv
# or
$ pnpm add @mx7/tenv
```### Sample Usages
```typescript
import 'dotenv/config'; // peer dependency (if you are using dotenv)
import Env from '@mx7/tenv';const parsed = process.env as Record;
const env = new Env(parsed); // instance of Env classconst port = env.key('PORT', true).integer().unsigned().get();
// or, set a default value instead of make this required
const port = env.key('PORT', 3000).integer().unsigned().get();console.log(port); // i.e. 3000 as number
```### More Information
| Method | Description | Action |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `get()` | Get the value of the environment variable. Should be called at last as chained | value with
expected type |
| `key()` | `key(key: string, factor?: boolean \| T)`
Define the variable name as `key` & follow next:
- set `factor`: `true` if it is required!
- set `factor`: `false` or `undefined` (no need to pass) for accepting undefined value if not exists!
- set `factor`: `T` (generic) as your default value | |
| `email()` | It will validate the passed string as email | |
| `url()` | It will validate the passed string as url.
if it requires IPv6 address, do it as `url({ ipv6: true })` | |
| `integer()` | It will validate the passed value as integer | |
| `float()` | It will validate the passed value as float | |
| `signed()` | It will validate the passed value as signed number | |
| `unsigned()` | It will validate the passed value as unsigned number | |
| `boolean()` | It will validate the passed value as boolean | |### Author
Made with ❤️ by [@mahabubx7](https://github.com/mahabubx7)
### Changelogs
| Version | Released At | Description |
| --------------- | ----------- | ---------------------------------------------------------------------------------------- |
| v1.1.0 `minor` | 2024-01-31 | Minor update release v1.1 |
| v1.0.0 `stable` | 2024-01-31 | Major stable release v1.x |
| v0.7.7 `stable` | 2024-01-31 | First stable release with new feature => default value can attached with `key()` method |
| v0.7.2 `beta` | 2024-01-31 | Added more supports and fixes small & minor issues with changes |
| v0.7.0 `beta` | 2024-01-30 | `@mx7/tenv` with nodejs v18.x LTS or later compatible for type-safe .env variable parser |