Config
This package provides a solution for handling the configuration for NodeJs applications. The package supports both working as a standalone solution and online using config-server.
The configs are defined in the schemas package. It is used for validation, defining environment variable override, and for generating typescript types.
Installation
In order to use the package you need to install both the package itself, and the schemas package that handles all the config schemas.
npm install @map-colonies/schemas @map-colonies/config
API documentation
Check the autogenerated documentation here.
Usage
import { config } from '@map-colonies/config';
import { commonBoilerplateV4 } from '@map-colonies/schemas';
const configInstance = await config({
configName: 'boiler-config',
configServerUrl: 'http://localhost:8080',
schema: commonBoilerplateV4,
version: 'latest',
offlineMode: false
});
const port = configInstance.get('server.port');
API Documentation
This section describes the API provided by the package for interacting with the configuration.
ConfigInstance<T>
The ConfigInstance interface represents the your way to interact with the configuration. It provides methods to retrieve configuration values and parts.
T is the typescript type associated with the chosen schema. it can be imported from the @map-colonies/schemas package.
Methods
get<TPath extends string>(path: TPath): _.GetFieldType<T, TPath>
- Description: Retrieves the value at the specified path from the configuration object. Note that the type of returned object is based on the path in the schema.
- Parameters:
path(TPath): The path to the desired value.
- Returns: The value at the specified path.
getAll(): T
- Description: Retrieves the entire configuration object.
- Returns: The entire configuration object.
getConfigParts(): { localConfig: object; config: object; envConfig: object }
- Description: Retrieves different parts of the configuration object before being merged and validated. Useful for debugging.
- Returns: An object containing the
localConfig,config, andenvConfigparts of the configuration.localConfig: The local configuration object.config: The remote configuration object.envConfig: The environment configuration object.
getResolvedOptions(): BaseOptions
- Description: Retrieves the resolved options from the configuration object. Useful for debugging.
- Returns: The resolved options, which are an instance of
BaseOptions.
initializeMetrics(registry: promClient.Registry): void
- Description: Initializes the metrics for the configuration.
- Parameters:
registry(promClient.Registry): The prometheus registry to use for the metrics.
Configuration Options
This package allows you to configure various options for loading and managing configurations. Below are the available options and their descriptions.
Options
schema
- Type:
T extends SchemaWithType - Optional:
false - Description: The schema of the configuration object.
configName
- Type:
string - Optional:
true - Description: The name of the remote configuration.
- Environment Variable:
CONFIG_NAME
version
- Type:
'latest' | number - Optional:
true - Default:
latest - Description: The version of the remote configuration. It can be either
'latest'or a number. - Environment Variable:
CONFIG_VERSION
configServerUrl
- Type:
string - Optional:
true - Default:
http://localhost:8080 - Description: The URL of the configuration server.
- Environment Variable:
CONFIG_SERVER_URL
offlineMode
- Type:
boolean - Optional:
true - Default:
false - Description: Indicates whether the configuration should be loaded in offline mode.
- Environment Variable:
CONFIG_OFFLINE_MODE
ignoreServerIsOlderVersionError
- Type:
boolean - Optional:
true - Description: Indicates whether to ignore the error when the server version is older than the requested version.
- Environment Variable:
CONFIG_IGNORE_SERVER_IS_OLDER_VERSION_ERROR
localConfigPath
- Type:
string - Optional:
true - Default:
./config - Description: The path to the local configuration folder.
Environment Variable Configuration
The following environment variables can be used to configure the options:
CONFIG_NAME: Sets theconfigNameoption.CONFIG_VERSION: Sets theversionoption.CONFIG_SERVER_URL: Sets theconfigServerUrloption.CONFIG_OFFLINE_MODE: Sets theofflineModeoption.CONFIG_IGNORE_SERVER_IS_OLDER_VERSION_ERROR: Sets theignoreServerIsOlderVersionErroroption.
Configuration Merging and Validation
The package supports merging configurations from multiple sources (local, remote, and environment variables) and then validates the merged configuration against the schema.
Local Configuration
- The local configuration is loaded from the path specified by the
localConfigPathoption. The default path is./config.
Remote Configuration
- The remote configuration is fetched from the server specified by the
configServerUrloption. - If the
versionis set to'latest', the latest version of the configuration is fetched. Otherwise, the specified version is fetched.
Environment Variables
Configuration options can be overridden by setting the corresponding environment variables as described in schema using the x-env-value key.
If an environment variable is set, it takes precedence over the value from the remote or local configuration.,
If the value of the x-env-format key is json, the environment variable value is parsed as JSON.
Merging Configurations
-
The configurations are merged in the following order of precedence:
- Environment variables
- Remote configuration
- Local configuration
-
If a configuration option is specified in multiple sources, the value from the source with higher precedence (as listed above) is used.
Validation
- After merging, the final configuration is validated against the defined schema using ajv.
- The validation ensures that all required properties are present, and the types and values of properties conform to the schema.
- Any default value according to the schema is added to the final object.
- If the validation fails, an error is thrown, indicating the invalid properties and their issues.
Error handling
This section describes the possible errors that can occur when using the package, along with their codes and payload structures.