Provide app configuration as key-value pairs from multiple providers. Inspired by ASP.net Core
npm install i-do-configprocess.env.ENV_VAR_NAME) and the .env file. When using the .env file you'll most likely use the dotenv package.
typescript
const providers = [
new EnvVarConfigurationProvider();
new JsonConfigurationProvider();
];
const config = new Configuration(providers);
const value = config.getValue("ENV_VAR"); // Returns a single value, eg. "my-value"
const section = config.getSection("foo"); // Returns an object, eg. { "key-a": "value-a", "key-b": "value-b", ... }
`
Note: In your application you'll probably setup the providers via dependency injection.
Dependency injection
In your inversify.config.ts file do this:
`typescript
import Container from "inversify";
import { Configuration, ExampleConfigProvider, IConfiguration, IConfigurationValueProvider } from "i-do-config";
const di = new Container({ defaultScope: "Singleton" });
decorate(injectable(), Configuration);
decorate(injectable(), ExampleConfigProvider);
di.bind("ConfigProvider").to(ExampleConfigProvider);
di.bind("Configuration").to(Configuration);
`
> Please note: ExampleConfigProvider does exist in this repository. Nor elsewhere.
Tests
Written in TypeScript, done with Mocha & Chai. Here's a working VS Code launch configuration:
`json
{
"type": "node",
"request": "launch",
"name": "Tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"cwd": "${workspaceRoot}",
"args": [
"-u", "tdd",
"--no-timeouts",
"-r", "ts-node/register",
"--colors",
"${workspaceRoot}/test/*/test.ts"
],
"protocol": "inspector",
"sourceMaps": true,
"internalConsoleOptions": "openOnSessionStart"
}
`
Provider Options
You can pass provider-specific options to provider class instances. The only (optional) default member is the name property. You may implement other behaviour as you see fit (e.g. key/value formatters).
`typescript
interface IConfigurationValueProviderOptions {
name?: string;
}
``