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

@class-config/core

v0.6.0

Published

The core of class config

Downloads

2

Readme

@class-config/core

Node.js CI codecov

The config package to define and read the configuration

Prepare

This package just support Typescript.

You should install reflect-metadata package. And set follow code on tsconfig.json.

// tsconfig.json
{
  "compilerOptions": {
    ...
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    ...
  }
}

Usage

Define a config class

First, You can use @Config define your configuration class. Next, use @ConfigField defined a configuration field.

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField } from '@class-config/core';

@Config()
class Configuration extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  public port!: number;
}

Config Source

The 'config source' specifies where to load the config data from. Below are some config sources:

You can use @From load config field from a config source.

For example:

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField, From } from '@class-config/core';
import { Env } from '@class-config/source-env';

process.env.SERVER_HOST = 'localhost';
process.env.SERVER_PORT = '8080';

@Config()
class Configuration extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  @From(new Env('SERVER_HOST'))
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  @From(new Env('SERVER_PORT'))
  public port!: number;
}

// configuration = { "host": "localhost", "port": 8080 }
const configuration = await Configuration.init<Configuration>();

Default value

You can use @DefaultValue load config field by default value.

For example:

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField, DefaultValue } from '@class-config/core';

@Config()
class Configuration extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  @DefaultValue('localhost')
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  @DefaultValue(8080)
  public port!: number;
}

// configuration = { "host": "localhost", "port": 8080 }
const configuration = await Configuration.init<Configuration>();

Customize parser

You can customize a field's parser by @ClassField

@Config()
class DatabaseConfig extends BaseConfig {
  /**
   * Database hosts
   */
  @ConfigField({
    parser: (value) => value.split(','),
  })
  @From(new Env('HOSTS'))
  public hosts!: string[];
}

// configuration = { "hosts": ["127.0.0.1", "127.0.0.2"] }
const configuration = await DatabaseConfig.init<DatabaseConfig>();

Validator

The validator will check if your config file is valid. Below is some validator.

You can't set validator by init.

For example:

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField, DefaultValue } from '@class-config/core';
import { ClassValidator } from '@class-config/validator-class';
import { IsString, IsNumber } from 'class-validator';

@Config()
class Database extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  @DefaultValue('localhost')
  @IsString()
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  @DefaultValue('8080')
  @IsNumber()
  public port!: number;
}

const config = await Database.init<Database>({
  validator: new ClassValidator(),
});

Example

Basic

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField, DefaultValue, From } from '@class-config/core';
import { Env } from '@class-config/source-env';

@Config()
class Configuration extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  @From(new Env('SERVER_HOST'))
  @DefaultValue('localhost')
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  @From(new Env('SERVER_PORT'))
  @DefaultValue('8080')
  public port!: number;
}

// configuration = { "host": "localhost", "port": 8080 }
const configuration = await Configuration.init<Configuration>();

Nested

import 'reflect-metadata';
import { BaseConfig, Config, ConfigField, DefaultValue, From } from '@class-config/core';
import { Env } from '@class-config/source-env';

@Config()
class Database extends BaseConfig {
  /**
   * The server host
   */
  @ConfigField()
  @From(new Env('SERVER_HOST'))
  @DefaultValue('localhost')
  public host!: string;

  /**
   * The server port
   */
  @ConfigField()
  @From(new Env('SERVER_PORT'))
  @DefaultValue('8080')
  public port!: number;
}

@Config()
class Configuration {
  /**
   * Database config
   */
  @ConfigField()
  public database!: Database;
}

// configuration = { "database: { "host": "localhost", "port": 8080 } }
const configuration = await Configuration.init<Configuration>();