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

@devanshdeveloper/default-config

v1.0.0

Published

A lib for managing custom configs

Downloads

42

Readme

DefaultConfig

DefaultConfig is a TypeScript class designed to manage configuration settings dynamically. It supports deep merging of configurations, allows dynamic access to nested properties, and validates configurations against a schema using Zod.

Features

  • Dynamic Configuration: Get and set nested configuration values using dot notation.
  • Deep Merging: Merge user configurations with default configurations recursively.
  • Validation: Validate configurations using a Zod schema to ensure they meet defined types and constraints.
  • Type Safety: Leverage TypeScript for type safety in your configuration management.

Installation

To install the package, run:

npm install default-config zod

Usage

Importing the DefaultConfig

import DefaultConfig from "default-";
import { z } from "zod";

Define Your Configuration Schema

Use Zod to create a schema that your configuration must adhere to.

const configSchema = z.object({
  api: z.object({
    url: z.string().url(),
    timeout: z.number().min(1000),
    headers: z
      .object({
        authorization: z.string().optional(),
      })
      .optional(),
  }),
  features: z.object({
    enableLogging: z.boolean(),
    flags: z
      .object({
        featureX: z.boolean().optional(),
        featureY: z.boolean(),
      })
      .optional(),
  }),
});

Create Default and Initial Configurations

Define your default and initial configurations.

const defaultConfig = {
  api: {
    url: "https://example.com",
    timeout: 5000,
  },
  features: {
    enableLogging: false,
    flags: {
      featureY: true,
    },
  },
};

const initialConfig = {
  api: {
    headers: {
      authorization: "Bearer token",
    },
  },
};

Instantiate the DefaultConfig

Create an instance of the DefaultConfig using the schema, default config, and initial config.

const defaultConfig = new DefaultConfig(
  configSchema,
  initialConfig,
  defaultConfig
);

Accessing Configuration Values

You can access configuration values dynamically using dot notation.

console.log(defaultConfig.get("api.url")); // 'https://example.com'
console.log(defaultConfig.get("api.headers.authorization")); // 'Bearer token'
console.log(defaultConfig.get("features.flags.featureY")); // true

Modifying Configuration Values

You can update configuration values dynamically, and the new values will be validated.

defaultConfig.set("api.timeout", 6000); // Update timeout
defaultConfig.set("features.flags.featureX", true); // Enable featureX

// This will throw an error if the config value is invalid
try {
  defaultConfig.set("api.url", "invalid-url"); // Invalid URL
} catch (e) {
  console.error(e.message); // Config validation failed: Invalid input
}

Retrieve the Entire Configuration

You can get the entire merged and validated configuration.

console.log(DefaultConfig.getAll());

API

DefaultConfig

Constructor

constructor(schema: ZodSchema<T>, initialConfig: Partial<T>, defaultConfig: Partial<T>)
  • schema: A Zod schema that defines the structure and validation rules for the configuration.
  • initialConfig: An optional object with initial configuration values.
  • defaultConfig: An optional object with default configuration values.

Methods

  • get(key: string): any: Retrieve a configuration value by its dot-notated key.
  • set(key: string, value: any): void: Set a configuration value dynamically using a dot-notated key.
  • getAll(): T: Retrieve the entire current configuration.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or create an issue.

License

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