@samokat/abstract-env
v1.1.1
Published
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.
Downloads
5
Readme
abstract-env
This in companion of parcel-plugin-real-env. It just wraps global env variable for safty using configuration.
TL;DR
yarn add @samokat/abstarct-env
// 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 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:
// 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:
// 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 as bundler, and it can pass env variables to application in dev mode.
// 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')