@vegajs/config
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta.31 • Public • Published

@vegajs/config

Description

ConfigService is a class that manages the application configuration. It allows you to initialize the configuration, set its values, and access individual configuration parameters.

General Usage

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' });

Constructor

constructor(defaultConfig?: Config)

  • defaultConfig (optional): a configuration object that will be set as the default when creating an instance.

Methods

init(config: Config): void

Initializes the configuration with a new object.

  • config: the configuration object.

get config(): Config

Returns the current configuration object. If the configuration has not been initialized, it throws an error.

Example:

const currentConfig = configService.config;

get<K extends keyof Config>(key: K): Config[K]

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');

Errors

  • If you try to access the configuration before it is initialized, an error will be thrown: ConfigService is not initialized.

Package Sidebar

Install

npm i @vegajs/config

Weekly Downloads

15

Version

1.0.0-beta.31

License

MIT

Unpacked Size

5.29 kB

Total Files

8

Last publish

Collaborators

  • zlobin_andy