npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ruvipers-config-manager

v0.2.3

Published

RCM (RuVIPeR's Config Manager) is a TypeScript-based configuration manager that enables schema-based configuration handling, automatic loading, validation, and file watching for real-time config updates.

Downloads

422

Readme

Overview

ruvipers-config-manager is a TypeScript-based configuration manager that helps manage application configuration files. It allows you to define a configuration schema with default values, automatically load and validate config files, and watch for changes in the configuration file, updating the in-memory configuration as needed.

This module supports types such as boolean, number, string, array, and object. The configuration can be validated against a schema, and if no valid config is found, default values are used.

Features

  • Schema-based configuration: Define a config schema with default values and types.
  • Auto-loading and validation: Automatically loads the config from a JSON file and validates it against the schema.
  • File watching: Watches the configuration file for changes and reloads it automatically (with debounce).
  • Custom error handling: Option to throw errors when invalid or missing config values are found.
  • Proxy access: Supports accessing and updating config values via a proxy object.

Installation

npm install

Usage

1. Basic Setup

import RCM from "./RCM";

const configManager = new RCM(
  {
    host: { type: String, default: "localhost" },
    port: { type: Number, default: 0 },
    isEnabled: { type: Boolean, default: false },
    "payment.currencyCodes": { type: Array, default: ["eur", "usd"] }, // You can use dot notation for nested objects
  },
  {
    configPath: "./config.json", // Path to config file
    useDefinition: true, // Use the schema definition
    throwOnError: false, // Do not throw errors if config is invalid. If disabled, RCM try to use default values
    watchConfigFile: true, // Enable file watching
  }
);

2. Accessing Configuration Values

You can access configuration values with the get method:

const host = configManager.get("host");
console.log(host); // Outputs the value of 'host' from the config

Or via proxy object:

const configProxy = configManager.getProxy();
console.log(configProxy.host); // Outputs the same value

3. Setting Configuration Values

You can set values using the set method:

configManager.set("host", "127.0.0.1");

or via the proxy:

configProxy.host = "value";
configProxy.save(); // if you want save to filesystem

4. Saving Configuration

Changes to the configuration can be saved to the file using the save() method:

configManager.save();

The proxy object also supports the save method:

configProxy.save();

5. Watching Configuration File

When the watchConfigFile option is enabled, the configuration manager automatically reloads the config file if it detects changes:

const configManager = new RCM(..., {
  watchConfigFile: true // Enable watching
});

If the config file changes, the initializeConfig() method is called, and the new configuration is loaded.

Configuration Options

  • configPath: Path to the JSON config file (default: './config.json').
  • useDefinition: Whether to use the provided schema to validate the config file. (default: true)
  • throwOnError: If true, throws an error if any value in the config file doesn't match the schema. (default: false)
  • watchConfigFile: If true, watches the config file for changes and reloads the config automatically. (default: true)

Type Schema Definition

The schema defines the expected structure of the config file. Each key in the schema corresponds to a configuration property, with a defined type and an optional default value.

Supported types:

  • BooleanConstructor
  • NumberConstructor
  • StringConstructor
  • ArrayConstructor
  • ObjectConstructor

Example schema definition:

const configSchema: ConfigDefinitionSchema = {
  isEnabled: { type: Boolean, default: true },
  maxConnections: { type: Number, default: 10 },
  databaseUrl: { type: String, default: "localhost" },
  allowedIPs: { type: Array, default: ["127.0.0.1"] },
  settings: { type: Object, default: { someParameters: "someValue" } },
};

Example

Here’s how you can initialize the RCM and use it in a project:

const configManager = new RCM(
  {
    "express.host": { type: String, default: "localhost" },
    "express.port": { type: Number, default: 3001 },
    "express.useHttps": { type: Boolean, default: false },
    "auth.login": { type: String, default: "root" },
    "auth.password": { type: String, default: "changeme" },
    "auth.parameters.defaultPermissions": {
      type: Array,
      default: ["read", "write", "create"],
    },
  },
  {
    throwOnError: false,
    watchConfigFile: true,
  }
);

TODOs

  • Regex value verifier
  • watchConfigFile event on parameter change

License

This project is licensed under the MIT License.