0.2.1 β€’ Published 1 year ago

@helpful-toolkit/config v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Config Helper For Node.js (env manager)

πŸš€ A flexible and powerful configuration management tool for Node.js and NestJS applications.
version license npm GitHub issues

Русская вСрсия / Russian Version πŸ‡·πŸ‡Ί

>> Version for Nest.js here <<

Overview

ConfigService is a lightweight, flexible tool for managing environment variables in Node.js and NestJS applications. It provides a simple yet powerful API for accessing values from .env files, with full support for validation, type safety, and extensibility.

Table of Contents

Features ✨

  • Dynamic configuration: Load and manage environment variables with ease.
  • Type-safe getters: Retrieve values with proper type validation (string, number, boolean, etc.).
  • Customizable: Easily extendable with new configuration methods.
  • Error handling: Provides clear errors when variables are missing or have incorrect types.

Installation πŸ“₯

You can install @helpful-toolkit/config using your preferred package manager.

Using npm

npm install @helpful-toolkit/config    

Using yarn

yarn add @helpful-toolkit/config    

Using pnpm

pnpm add @helpful-toolkit/config    

Usage πŸ› 

  1. Initialization

Before using ConfigService, you need to initialize it by loading the environment variables.

import { ConfigService } from '@helpful-toolkit/config';    
    
ConfigService.initialize();    
  1. Getting Configuration Values

Use the various getters to retrieve values from your .env or environment:
| Getter | Description | Description |
|--|--|--|
| getString(key) | Returns a string value. | const dbHost = ConfigService.getString('DB_HOST'); |
| getNumber(key) | Returns a numeric value. | const dbPort = ConfigService.getNumber('DB_PORT'); |
| getBoolean(key) | Returns a boolean value. | const isEnabled = ConfigService.getBoolean('FEATURE_ENABLED'); |
| getArray(key) | Returns an array of values. | const allowedDomains = ConfigService.getArray('ALLOWED_DOMAINS'); |
| getDate(key) | Returns a Date object. | const expiration = ConfigService.getDate('EXPIRATION_DATE'); |
| getJSON(key) | Returns a JSON object. | const jsonConfig = ConfigService.getJSON('JSON_CONFIG'); |

  1. Error Handling

If a required environment variable is not found, or has an invalid type, the ConfigService will throw an error:

try {  
  const dbHost = ConfigService.getString('DB_HOST');  
} catch (error) {  
  console.error('Config Error:', error.message);  
}  

API πŸ“š

  • getString(key: string, defaultValue?: string): string: Returns the value of the variable as a string.
  • getNumber(key: string, defaultValue?: number): number: Returns the value of the variable as a number.
  • getBoolean(key: string, defaultValue?: boolean): boolean: Returns the value of the variable as a boolean.
  • getArray(key: string, separator: string, defaultValue?: string[]): string[]: Returns the value of the variable as an array.
  • getDate(key: string, defaultValue?: Date): Date: Returns the value of the variable as a Date object.
  • getJSON<T>(key: string, defaultValue?: T): T: Returns the value of the variable as a JSON object.
  • has(key: string): boolean: Checks whether the variable exists in the environment.

License

This project is licensed under the Apache 2.0 License.

Related Projects

  • NestJS - A framework for building efficient, reliable, and scalable server-side applications.