@sorodriguez/config-client v1.0.0
Config Client
A lightweight client for loading configuration from a Config Server. This library is built with TypeScript and provides seamless integration with NestJS applications, but can also be used with plain Node.js and Express applications.
Features
- Load configuration from a Config Server
- Support for multiple repositories, applications, and profiles
- Basic authentication support
- Automatic environment variable population
- Global module for NestJS applications
Installation
npm install config-client
Built With
- TypeScript
- NestJS integration
- Axios for HTTP requests
Usage
With NestJS
Import the ConfigClientModule
into your application's root module and use the forRoot
method to configure it:
import { Module } from '@nestjs/common';
import { ConfigClientModule } from 'config-client';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
ConfigClientModule.forRoot('https://your-config-server-url/config', [
{
repo: 'your-config-repo',
application: 'your-application-name',
profile: 'dev',
auth: {
username: 'username',
password: 'password'
}
}
]),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
You can then inject the configuration values using the @Inject
decorator:
import { Injectable, Inject } from '@nestjs/common';
@Injectable()
export class AppService {
constructor(@Inject('CONFIG_VALUES') private readonly config: Record<string, any>) {}
getConfig(key: string): any {
return this.config[key];
}
}
With TypeScript (non-NestJS)
You can use the module with plain TypeScript as well:
import { ConfigClientModule } from 'config-client';
async function loadConfig() {
const configModule = ConfigClientModule.forRoot('https://your-config-server-url/config', [
{
repo: 'your-config-repo',
application: 'your-application-name',
profile: 'dev',
auth: {
username: 'username',
password: 'password'
}
}
]);
// Manually invoke the factory function
const configProvider = configModule.providers.find(p => p.provide === 'CONFIG_VALUES');
await configProvider.useFactory();
// Now your configuration is loaded in process.env
console.log(`Database URL: ${process.env.DATABASE_URL}`);
}
loadConfig().catch(console.error);
With JavaScript (Node.js/Express)
const { ConfigClientModule } = require('config-client');
const express = require('express');
const app = express();
async function bootstrap() {
try {
// Configure the config client
const configModule = ConfigClientModule.forRoot('https://your-config-server-url/config', [
{
repo: 'your-config-repo',
application: 'your-application-name',
profile: 'dev',
auth: {
username: 'username',
password: 'password'
}
}
]);
// Manually invoke the factory function
const configProvider = configModule.providers.find(p => p.provide === 'CONFIG_VALUES');
await configProvider.useFactory();
// Start your Express app
app.get('/', (req, res) => {
res.send('Configuration loaded successfully!');
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
} catch (error) {
console.error('Failed to load configuration:', error);
process.exit(1);
}
}
bootstrap();
Multiple Connections
You can connect to multiple repositories, applications, or profiles by providing an array of configuration options:
ConfigClientModule.forRoot('https://your-config-server-url/config', [
// First configuration
{
repo: 'main-config-repo',
application: 'api-service',
profile: 'dev',
auth: {
username: 'username1',
password: 'password1'
}
},
// Second configuration
{
repo: 'shared-config-repo',
application: 'common',
profile: 'prod',
auth: {
username: 'username2',
password: 'password2'
}
}
])
This will load configuration from both sources and merge them into a single configuration object. If there are duplicate keys, the first one encountered will be used for environment variables.
Connecting to Different Config Servers
If you need to connect to multiple config servers, you can create separate instances of the ConfigClientModule:
import { Module } from '@nestjs/common';
import { ConfigClientModule } from 'config-client';
@Module({
imports: [
// Primary config server
ConfigClientModule.forRoot('https://primary-config-server/config', [
{
repo: 'primary-repo',
application: 'your-app',
profile: 'dev'
}
]),
// Secondary config server
ConfigClientModule.forRoot('https://primary-config-server/config', [
{
repo: 'primary-repo',
application: 'your-app',
profile: 'prod'
}
]),
],
})
export class AppModule {}
API Reference
ConfigClientModule
forRoot(url: string, options: ConfigClientOptions[]): DynamicModule
Creates and configures the ConfigClientModule.
url
: The URL of the Config Serveroptions
: An array of configuration options
ConfigClientOptions
repo
: The name of the repositoryapplication
: The name of the applicationprofile
: The profile to use (e.g., 'dev', 'prod')auth
: (Optional) Authentication credentialsusername
: Username for basic authenticationpassword
: Password for basic authentication
License
ISC
3 months ago