ConfigService
is a class that manages the application configuration. It allows you to initialize the configuration, set its values, and access individual configuration parameters.
To create an instance of ConfigService
, you can pass a default configuration object. Then, using the init
method, you can initialize the configuration.
type InitialConfig = {
apiUrl: string;
}
const configService = new ConfigService<InitialConfig>();
configService.init({ apiUrl: 'https://new-api.example.com' });
-
defaultConfig
(optional): a configuration object that will be set as the default when creating an instance.
Initializes the configuration with a new object.
-
config
: the configuration object.
Returns the current configuration object. If the configuration has not been initialized, it throws an error.
Example:
const currentConfig = configService.config;
Returns the value of a configuration parameter by its key. If the configuration has not been initialized, it throws an error.
-
key
: the key whose value needs to be retrieved from the configuration.
Example:
const apiUrl = configService.get('apiUrl');
- If you try to access the configuration before it is initialized, an error will be thrown:
ConfigService is not initialized
.