@ueno/config
Advanced tools
Comparing version 0.0.2 to 0.0.3
'use strict'; | ||
const config = {}; | ||
const browser = {}; | ||
const defaults = {}; | ||
function Config() { | ||
const proto = Object.getPrototypeOf(this); | ||
const browser = {}; | ||
const defaults = {}; | ||
const variableName = '__uenoConfig'; | ||
// Only server side | ||
if (typeof window === 'undefined') { | ||
// Setup dotenv | ||
require('dotenv').config(); | ||
// Assign env to config | ||
Object.assign(config, process.env); | ||
// TODO: Warn when stuff is accessed without any default declerations. | ||
} | ||
// Only server side | ||
if (typeof window === 'undefined') { | ||
// Setup dotenv | ||
require('dotenv').config(); | ||
} else if (typeof window[variableName] === 'object') { | ||
// Update real config object | ||
Object.assign(this, window[variableName]); | ||
} | ||
// Set defaults | ||
config.setDefaults = function setDefaults(defaultConfig) { | ||
// Update default config | ||
Object.assign(defaults, defaultConfig); | ||
proto.setDefaults = function setDefaults(defaultConfig) { | ||
// Update default config | ||
Object.assign(defaults, defaultConfig); | ||
// Update the real config object | ||
Object.entries(defaultConfig) | ||
.forEach(([key, value]) => { | ||
if (typeof config[key] === 'undefined') { | ||
// Update the real config object | ||
Object.entries(defaultConfig).forEach(([key, value]) => { | ||
if (typeof this[key] !== 'undefined') { | ||
return; | ||
} | ||
if (typeof process.env[key] !== 'undefined') { | ||
this[key] = process.env[key]; | ||
} else { | ||
// Every key is treated as string to maintain persistency | ||
// between POSIX and JS environment. | ||
config[key] = String(value); | ||
this[key] = String(value); | ||
} | ||
}); | ||
} | ||
}; | ||
// get browser config | ||
config.getBrowser = function getBrowser() { | ||
const res = {}; | ||
proto.setBrowser = function setBrowser(browserConfig) { | ||
Object.assign(browser, browserConfig); | ||
}; | ||
Object.entries(browser) | ||
.forEach(([key, value]) => { | ||
// get browser config | ||
proto.getBrowser = function getBrowser() { | ||
const res = {}; | ||
Object.entries(browser).forEach(([key, value]) => { | ||
// Make sure the environment already exists | ||
if (typeof config[key] === 'undefined') { | ||
if (typeof this[key] === 'undefined') { | ||
return; | ||
@@ -44,3 +53,3 @@ } | ||
if (value !== false) { | ||
res[key] = config[key]; | ||
res[key] = String(this[key]); | ||
} | ||
@@ -53,10 +62,6 @@ | ||
return res; | ||
return res; | ||
}; | ||
} | ||
// Rehydration function for the browser | ||
config.rehydrate = function rehydrate(obj) { | ||
Object.assign(config, obj); | ||
} | ||
module.exports = config; | ||
module.exports = new Config(); |
{ | ||
"name": "@ueno/config", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Environment configuration toolkit", | ||
@@ -5,0 +5,0 @@ "repository": "ueno-llc/create-react-app", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
2039
53
3