neezer / node-cfg Goto Github PK
View Code? Open in Web Editor NEWcfg is a library to manage loading configuration into your Node JS application.
cfg is a library to manage loading configuration into your Node JS application.
Also relocate tests closer to implementation
If none provided, default to using
if (errors !== null) {
errors.forEach(error => {
process.stderr.write(`config error: ${error}\n`);
process.exit(1);
}
}
Try with localhost:2435
instead of http://localhost:2435
... might not fail when it should, or at least default to HTTP.
Currently readme is wrong:
- const config = cfg<MyConfig>({ check: assert });
+ const [errors, config] = cfg<MyConfig>({ check: assert });
config.aValue === "whatever";
Currently you have to pass in the type you want to cast configuration as into cfg
. I think TS is powerful enough to do this for you. Need to learn a bit more about advanced types first...
That would change invocation to
import { cfg } from "@neezer/cfg";
// handled internally
- interface MyConfig {
- aValue: string;
- }
- // use a typeguard to definitely type your config
- const guard = (value: any): value is MyConfig => {
- return !!value && "aValue" in value;
- };
- // create an assertion function using the typeguard
- const assert = (v: any) => {
- if (guard(v)) {
- return v;
- } else {
- throw new Error("not a valid configuration!");
- }
- };
// given `process.env.A_VALUE === 'whatever'`
- const config = cfg<MyConfig>({ check: assert });
+ const config = cfg();
config.aValue === "whatever";
Should support configuration by flags.
Currently silenced, possibly expose via log level env?
Sometimes we can't use a connection string as-is because it might contain secrets that are hard to manage in a secure way in operations. Thus, we might have all the parts of a URL spread across multiple environment variables, like so:
Single | Multiple |
---|---|
DB_URL | DB_NAME |
DB_HOST | |
DB_PORT | |
DB_USERNAME | |
DB_PASSWORD | |
DB_OPTIONS | |
DB_PROTOCOL |
In this case, cfg
should be smart enough to "bundle up" the various parts and still expose the final value as a URL.
Perhaps the configuration for such a value might look like this?
{
"url": {
"desc": "a connection string in parts",
"env": "DB_URL",
"format": "url",
"stitchFromParts": {
"port": "DB_PORT",
"host": "DB_HOST",
"username": "DB_USERNAME",
"password": "DB_PASSWORD",
"protocol": "DB_PROTOCOL",
"query": "DB_OPTIONS"
}
}
}
Presently, undefined is falsey, and defaults to "false" value. Instead, throw an error if a bool env is not specified.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.