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

classy-config

v1.0.1

Published

Class based configuration management

Downloads

3

Readme

classy-config

codecov

Better configuration with power of classes and decorators

Installation

  1. Install classy-config

    pnpm add classy-config

  2. Install reflect-metadata

    pnpm add reflect-metadata

  3. Install class-transformer and class-validator (optional)

    pnpm add class-transformer

    pnpm add class-validator

Loading configuration

ClassyConfig.load

This loads configuration from class, glob path, webpack context

import { ClassyConfig } from 'classy-config'

// Using class
const loadingWithClass = ClassyConfig.load(MainConfig)

// Using glob
const loadingWithGlob = ClassyConfig.load('./config/*.config{.ts,.js}')

// Using webpack context
const loadingWithContext = ClassyConfig.load(require.context('.', true, /\.config\.ts$/))

class type and glob string can be passed with an array

You can pass env to override picking up process.env.NODE_ENV

import { ClassyConfig } from 'classy-config'

ClassyConfig.load(MainConfig, { env: 'new-env' })

Creating configuration

If only one class without name picked up, it will create a class instance

import { ClassyConfig } from 'classy-config'

@Config()
export class MainConfig {
  @Default(process.env.SECRET_KEY, { envs: ['prod', 'dev'] })
  @Default('this-is-going-to-be-secret')
  secretKey: string
}

const loadedConfig = ClassyConfig.load(MainConfig);
console.log(loadedConfig)
/*
MainConfig {
  secretKey: 'this-is-going-to-be-secret'
}
*/

If multiple classes is passed it will create an object

import { ClassyConfig } from 'classy-config'

@Config()
export class ConfigOne {
  @Default(process.env.SECRET_KEY, { envs: ['prod', 'dev'] })
  @Default('this-is-going-to-be-secret')
  secretKey: string
}

@Config()
export class ConfigTwo {
  @Default(process.env.NAME, { envs: ['prod', 'dev'] })
  @Default('foo')
  name: string
}

const loadedConfig = ClassyConfig.load([ConfigOne, ConfigTwo]);
console.log(loadedConfig)
/*
{
  secretKey: 'this-is-going-to-be-secret',
  name: 'foo'
}
*/

If multiple classes is passed with names it will create an object with key as name and value as class instance

import { ClassyConfig } from 'classy-config'

@Config('one')
export class ConfigOne {
  @Default(process.env.SECRET_KEY, { envs: ['prod', 'dev'] })
  @Default('this-is-going-to-be-secret')
  secretKey: string
}

@Config('two')
export class ConfigTwo {
  @Default(process.env.NAME, { envs: ['prod', 'dev'] })
  @Default('foo')
  name: string
}

const loadedConfig = ClassyConfig.load([ConfigOne, ConfigTwo]);
console.log(loadedConfig)
/*
{
  one: [class ConfigOne],
  two: [class ConfigTwo],
}
*/

Transforming

After default values are initiated, transform will occur.

Enable transform with transform: true while loading configuration. If options are needed then pass transformOptions with transform: true

import { ClassyConfig } from 'classy-config'
import { Type } from 'class-transformer'

@Config()
export class MainConfig {
  @Default('1')
  @Type(() => Number)
  num: number
}

const loadedConfig = ClassyConfig.load(MainConfig, { transform: true });
console.log(loadedConfig)
/*
MainConfig {
  num: 1
}
*/

Validating

After transformations are done, it will validate configuration files.

Enable validation with validate: true while loading configuration. If options are needed then pass validationOptions with validate: true

Currently, validation does not (yet) run in parallel.

import { ClassyConfig } from 'classy-config'
import { Type } from 'class-transformer'

@Config()
export class MainConfig {
  @Default('1')
  @IsNumber()
  @Type(() => String)
  num: number
}

const loadedConfig = ClassyConfig.load(MainConfig, { validate: true });
// Throws ValidationErrors