# @samokat/abstract-env

> This in companion of [parcel-plugin-real-env](https://github.com/samokat-oss/parcel-plugin-real-env). It just wraps global *env* variable for safty using configuration.

Latest version **1.1.1** (published 2020-01-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @samokat/abstract-env
pnpm add @samokat/abstract-env
yarn add @samokat/abstract-env
bun add @samokat/abstract-env
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2020-01-14 |
| First published | 2019-12-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Igor Kamyshev |
| Maintainers | igor.kamyshev, vlaislav.melnikov.98 |

## Links

- npm: https://www.npmjs.com/package/@samokat/abstract-env
- Repository: https://github.com/samokat-oss/abstract-env
- Homepage: https://github.com/samokat-oss/abstract-env#readme
- Issues: https://github.com/samokat-oss/abstract-env/issues
- npm.io page: https://npm.io/package/@samokat/abstract-env

## Dependencies (1)

- [nanoption](https://npm.io/package/nanoption.md) ^1.0.1

## Recent versions

- 1.1.1 (latest) — 2020-01-14
- 1.1.0 — 2020-01-14
- 1.0.1 — 2019-12-05
- 1.0.0 — 2019-12-05
- 0.0.1 — 2019-12-04

## README

# abstract-env

This in companion of [parcel-plugin-real-env](https://github.com/samokat-oss/parcel-plugin-real-env). It just wraps global *env* variable for safty using configuration.

## TL;DR

```
yarn add @samokat/abstarct-env
```

```js
// config.js

import { createConfig } from '@samokat/abstarct-env'

const config = createConfig({
  API_URL: 'fallback-for-empty-var',
})

const apiUrl = config('API_URL')
```

## Details

Library provides two way for creating configuration: `createConfig` (throws Error for empty value) and `createOptionalConfig` (returns [Option](https://github.com/igorkamyshev/nanoption/) for every value).

Both creators accept fallback values as first agrument and optional name of global config store (default is `_env_`) as second.

Example with simple creator:

```js
// config.js

import { createConfig } from '@samokat/abstarct-env'

const config = createConfig({
  API_URL: 'fallback-for-empty-var',
}, '_custom_variable_in_global_scope_')

const apiUrl = config('API_URL') // 'fallback-for-empty-var'
```

Example with optional creator:

```js
// config.js

import { createOptionalConfig } from '@samokat/abstarct-env'

const config = createOptionalConfig({}, '_custom_variable_in_global_scope_')

const apiUrl = config('API_URL') // Option<string>
apiUrl.isEmpty() // true
```

## Common usage

In common project we use [Parcel](http://parceljs.org/) as bundler, and it can pass env variables to application in dev mode.

```js
// config.js

import { createConfig } from '@samokat/abstarct-env'

const config = createConfig({
  // In dev mode we accept current env variable
  // In prod mode it will be empty
  // and we accept value from global store, injected by external forces
  API_URL: process.env.API_URL,
})

const apiUrl = config('API_URL')
```

---
_Source: https://npm.io/package/@samokat/abstract-env · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
