A JavaScript client library for interacting with the Configleam service, providing an easy-to-use interface for fetching configuration data.
Note: If you're looking for the main Configleam project, which provides the backend service, it is located here.
You can install the Configleam JavaScript client via npm:
npm install configleam-js-client
import { Configleam, ConfigleamOptions } from 'configleam-js-client';
// Create a Configleam instance
const options: ConfigleamOptions = {
addrs: 'https://example.com/configleam',
key: "<access-key>"
};
const configleam = new Configleam(options);
// Read configuration
const params = {
env: 'production',
groups: ['api-one'],
globals: ['database', 'cache'],
};
const config = await configleam.readConfig(params);
const defaultPort = 8081;
const port = config.get("api-one.port", defaultPort)
Creates a new instance of the Configleam client with the specified options.
options
: A configuration object for initializing the Configleam client.addrs
: The base URL of the Configleam service API.key
: The access-key to access Configleam service API.
Fetches configuration data from the Configleam service endpoint.
params
(ReadConfigParam
): An object containing parameters for fetching configuration data.env
(string
): The environment for which configuration data is requested.groups
(Array<string>|string
) (optional): An array of groups to filter the configuration data.globals
(Array<string>|string
) (optional): An array of global keys to include in the configuration data.
Promise<ConfigleamConfig>
: A promise that resolves to an instance ofConfigleamConfig
.
const params = {
env: 'production',
groups: ['api-one', 'api-two'],
globals: ['global-1', 'global-2']
};
try {
const config = await configleam.readConfig(params);
} catch (err) {
// handle error
}
The ConfigleamConfig
class encapsulates configuration data fetched from the Configleam service, providing an intuitive interface for accessing, monitoring, and interacting with this data. Instances of ConfigleamConfig
are returned by the readConfig
method of the Configleam
client, equipped with methods to retrieve specific configuration properties, subscribe to their updates, handle errors, and unsubscribe from updates.
Subscribes to updates for a specific configuration property. When the property's value changes, the specified callback function is invoked with the new value. An optional error callback can be provided to handle any errors that occur during subscription or update fetching.
-
key
(string
): The key representing the configuration property to subscribe to updates for. This should be a unique identifier within your configuration data structure. -
callback
(UpdateCallback
): A function that is called when the subscribed configuration property is updated. The function receives the new property value as its parameter. -
errback
(UpdateErrback
): An optional function that is called if an error occurs during the subscription process or while fetching updates. This function receives an error object as its parameter.
ConfigleamConfig
: Resolves to theConfigleamConfig
instance, allowing for chaining further operations or subscriptions.
import { Configleam } from 'configleam-js-client';
// Assuming `configleam` is an already initialized instance of Configleam and config is already read
// Define the callback for configuration updates
function onDatabasePasswordUpdated(newPassword) {
console.log('New database password is:', newPassword);
}
// Define the error callback
function onDatabasePasswordError(error) {
console.error('Failed to subscribe to configuration updates:', error);
}
// Subscribe to updates for the 'database' configuration
config.onUpdate('api.database.password', onDatabasePasswordUpdated, onDatabasePasswordError);
Sets a global error callback function to handle any errors that occur during the process of fetching configuration updates. This method provides a way to globally handle errors instead of specifying error callbacks for each subscription.
errback
(UpdateErrback
): A function that is called when an error occurs during configuration updates. The function receives an error object as its parameter. This error object typically contains details about the error that occurred, allowing for appropriate error handling or logging.
ConfigleamConfig
: Resolves to theConfigleamConfig
instance, allowing for chaining further operations or subscriptions.
// Assuming `configleam` is an already initialized instance of Configleam and config is already read
// Define the global error handler
function globalUpdateErrorHandler(error) {
console.error('A configuration update error occurred:', error);
}
// Set the global error handler
config.onError(globalUpdateErrorHandler);
// Now, any errors that occur during configuration updates will be handled by the globalUpdateErrorHandler function
In scenarios where the connection to the server is lost, the errback
function provided to onError
will be called with an error object detailing the connectivity issue. The library implements a resilient connection strategy, attempting to reconnect every 1 second. This reconnection attempt continues indefinitely, or until the program is terminated, ensuring that the application remains up-to-date with the latest configuration data as soon as connectivity is restored.
Terminates all active subscriptions for configuration property updates previously established by the onUpdate
method. This method is essential for resource management and ensures that your application does not continue to receive updates after they are no longer needed, such as when an application component is unmounted or when shutting down an application gracefully.
// Assuming `configleam` is an already initialized instance of Configleam and config is already read
// Subscribe to configuration updates
const onUpdateCallback = (newValue) => {
console.log('Configuration updated:', newValue);
};
config.onUpdate('someConfigKey', onUpdateCallback);
// Later in your application, when updates are no longer needed
config.stop();
// After calling stop(), the onUpdateCallback will no longer be called for any configuration updates.
Contributions are welcome! Please see the Contribution Guidelines for more information.
Please report any issues or feature requests on the Issue Tracker.
This project is licensed under the MIT License.
LinkedIn [Mykhaylo Gusak] - mykhaylogusak@hotmail.com
Project Link: https://github.com/raw-leak/configleam-js-client