Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/refzlund/define-env
https://github.com/refzlund/define-env
Last synced: 20 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/refzlund/define-env
- Owner: Refzlund
- Created: 2023-02-20T16:19:02.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-07-06T08:30:00.000Z (over 1 year ago)
- Last Synced: 2024-11-09T15:49:10.035Z (3 months ago)
- Language: TypeScript
- Size: 13.8 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
define-env
This package allows you to define environmental variables alongside their default development values.
If the values are missing it will throw an exception. This ensures production code always contains the expected environmental variables.
## Quick Start
npm i -D define-env
/pnpm add -D define-env
```typescript
const envVar = defineEnv(e => ({
secret: e.VITE_SECRET,
mongo: {
uri: e.VITE_MONGOURI,
database: e.VITE_MONGODATABASE
},
isNumber: e.VITE_NUMBER(parseFloat)
}),
/** Development default values */
{
secret: 'dev',
mongo: {
uri: 'mongodb://0.0.0.0:27017',
database: 'test-db',
},
isNumber: '25'
})
```### Example exception
```ts
These environment variables are missing:
mongo:
uri: VITE_MONGOURI
```## In-depth
Create your env variable```typescript
import defineEnv from 'define-env'const envVar = defineEnv(e => ({...}))
export default envVar
```Use `e` to access ENV variables
```typescript
const envVar = defineEnv(e => ({
secret: e.VITE_SECRET,
mongo: {
uri: e.VITE_MONGOURI,
database: e.VITE_MONGODATABASE
},
}))
```Format a value if it exists, to ex. a number
```typescript
const envVar = defineEnv(e => ({
number: e.VITE_NUMBER(parseFloat),
upper: e.VITE_SOMESTR(v => v.toUpperCase())
}))
```Set default values for development.
```typescript
const envVar = defineEnv(e => ({
secret: e.VITE_SECRET,
mongo: {
uri: e.VITE_MONGOURI,
database: e.VITE_MONGODATABASE
},
isNumber: e.VITE_NUMBER(parseFloat)
}),
/** Development default values */
{
secret: 'dev',
mongo: {
uri: 'mongodb://0.0.0.0:27017',
database: 'test-db',
},
isNumber: '25'
})
```Example exception:
```bash
These environment variables are missing:
mongo:
uri: VITE_MONGOURI
```