Skip to content

teikun-86/js-config

Repository files navigation

@teikun-86/config-js

A flexible configuration loader for Node.js projects, supporting both JavaScript and TypeScript configuration files. This package allows you to easily manage configuration settings in your application with a simple API.

Table of Contents

Installation

To install the package, use npm:

npm install config-js

Or using yarn:

yarn add config-js

Usage

Initial Setup

First, you need to initialize the configuration loader with the directory where your configuration files are located. You can do this using the initializeConfigLoader method.

import { config } from '@teikun-86/config-js';
import * as path from 'path';

// Initialize the configuration loader with the directory path
config.initializeConfigLoader(path.resolve(__dirname, 'config'));

Loading Configuration Values

Once the loader is initialized, you can use the config function to retrieve configuration values. The config function takes a key (in dot notation) and an optional default value.

const appName = config<string>('app.name');
const appEnv = config<string>('app.env');
const dbHost = config<string>('database.host');
const dbPort = config<number>('database.port');

console.log(`App Name: ${appName}`);
console.log(`App Environment: ${appEnv}`);
console.log(`Database Host: ${dbHost}`);
console.log(`Database Port: ${dbPort}`);

Changing Configuration Directory

If you need to change the configuration directory at runtime, you can use the setConfigDir method provided by the loader.

const loader = config.getConfigLoader();
loader.setConfigDir(path.resolve(__dirname, 'new-config'));

// Now, config values will be loaded from the new directory
const newAppName = config<string>('app.name');
console.log(`New App Name: ${newAppName}`);

Configuration File Formats

Your configuration files can be in either JavaScript or TypeScript or JSON format. The loader will handle both formats seamlessly.

JavaScript Configuration File (config/app.js):

module.exports = {
  name: 'MyApp',
  env: 'development',
};

TypeScript Configuration File (config/database.ts):

export default {
  host: 'localhost',
  port: 3306,
};

JSON Configuration File (config/app.json):

{
  "name": "MyApp",
  "env": "development"
}

API

config(key: string, defaultValue?: any): any

Retrieve the configuration value for the specified key. If the key does not exist, the optional defaultValue will be returned.

config.initializeConfigLoader(directory: string): void

Initialize the configuration loader with the directory where your configuration files are located.

config.getConfigLoader(): ConfigLoader

Retrieve the singleton instance of the configuration loader. The ConfigLoader interface provides methods to get configuration values and set the configuration directory.

ConfigLoader Interface

setConfigDir(directory: string): void

Set the configuration directory. This will clear any cached configuration values.

get<T>(key: string, defaultValue?: T): T

Retrieve the configuration value for the specified key. If the key does not exist, the optional defaultValue will be returned.

Example

Here is a complete example to illustrate how to use the config-js package:

import { config } from '@teikun-86/config-js';
import * as path from 'path';

// Initialize the configuration loader
config.initializeConfigLoader(path.resolve(__dirname, 'config'));

// Retrieve configuration values
const appName = config<string>('app.name');
const appEnv = config<string>('app.env');
const dbHost = config<string>('database.host');
const dbPort = config<number>('database.port');

console.log(`App Name: ${appName}`);
console.log(`App Environment: ${appEnv}`);
console.log(`Database Host: ${dbHost}`);
console.log(`Database Port: ${dbPort}`);

// Change the configuration directory
const loader = config.getConfigLoader();
loader.setConfigDir(path.resolve(__dirname, 'new-config'));

// Retrieve configuration values from the new directory
const newAppName = config<string>('app.name');
console.log(`New App Name: ${newAppName}`);

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or suggestions.

Acknowledgements

This package was inspired by the need for a simple and flexible configuration loader for Node.js applications, with support for both JavaScript and TypeScript configuration files.