@zcodeapp/configuration
v0.1.9
Published
Configuration for load system variables
Downloads
4
Readme
Configuration
Overview
The Configuration
library is a robust and flexible solution for configuration management in TypeScript applications. It allows for the integration of multiple configuration strategies, facilitating adaptability across different environments and scenarios. With support for asynchronous loading and customizable strategies, this library is ideal for projects requiring an extensible and reliable configuration system.
Features
Customizable Configuration Strategies
Integrate different configuration sources using customizable strategies.
Asynchronous Loading
Supports asynchronous loading of configuration data.
Extensive Logging
Comes with built-in logging capabilities for monitoring the configuration loading and retrieval process.
Scalable and Extendable
Easily scalable for complex applications, allowing the addition of new configuration strategies as needed.
Error Handling
Robust error handling during the configuration loading process.
Installation
Include the Configuration
class in your TypeScript project:
npm install @zcodeapp/configuration @zcodeapp/logger
Usage
To use the Configuration
class in your TypeScript project:
import { Di } from "@zcodeapp/di";
import { ExampleStrategy } from "exampleStrategy";
import { Configuration } from "@zcodeapp/configuration";
const di = Di.getInstance();
const config = di.get(Configuration);
const node_env = config.get("NODE_ENV");
API Reference
addStrategy(strategy: IConfigurationStrategy): void
Add a new configuration strategy.
load(): Promise<void>
Asynchronously load configuration data using the added strategies.
get(key?: string): string
Retrieve a specific configuration value by key.
getData(): IConfigurationData[]
Get all loaded configuration data.
Example
Using without dependency injection
exampleStrategy.ts
export class ExampleStrategy implements IConfigurationStrategy {
public async load(): Promise<IConfigurationData[]> {
// load github, azure, gcp ...
}
}
main.ts
import { Configuration, EnvironmentStrategy } from "@zcodeapp/configuration";
import { Logger } from "@zcodeapp/logger";
// Get env values
const config = new Configuration(Logger.getInstance(), new EnvironmentStrategy());
const node_env = config.get("NODE_ENV");
Using with dependency injection @zcodeapp/di
exampleStrategy.ts
import { Injectable } from "@zcodeapp/di";
@Injectable()
export class ExampleStrategy implements IConfigurationStrategy {
public async load(): Promise<IConfigurationData[]> {
// load github, azure, gcp ...
// return IConfigurationData[]
}
}
main.ts
import { Di } from "@zcodeapp/di";
import { ExampleStrategy } from "exampleStrategy";
import { Configuration } from "@zcodeapp/configuration";
const di = Di.getInstance();
const config = di.get(Configuration);
config.addStrategy(di.get(ExampleStrategy));
const custom_value = config.get("CUSTOM_VALUE");
License
This project is licensed under the MIT License - see the LICENSE file for details.