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

nestjs-safe-config

v0.1.1

Published

A helper for creating NestJS config module with validation and type-safe property access.

Downloads

2

Readme

NestJS Safe Config

A helper for creating NestJS config module with validation and type-safe property access.

Overview

A config class is required. With the config class, the plain config value (typically loaded from file) is transformed by class-transformer and validated by class-validator. The config class instance is provided by using its class as the injection token.

Getting Started

This package is designed to be used with NestJS. Other than NestJS dependencies, class-transformer and class-validator are also peer dependencies.

Npm

npm install --save nestjs-safe-config class-transformer class-validator

Yarn

yarn add nestjs-safe-config class-transformer class-validator

Config Module

This package provides a ConfigModule class to create dynamic module to serve the config.

class ConfigModule {
	static register(options?: ConfigModuleOptions): DynamicModule;
}

interface ConfigModuleOptions {
	imports?: ModuleMetadata['imports'];
	providers?: ModuleMetadata['providers'];
	global?: boolean;
	configOptions?: CreateConfigProviderOptions<Record<string, any>>[];
}

type CreateConfigProviderOptions<T> =
	| CreateConfigValueProviderOptions<T>
	| CreateConfigFactoryProviderOptions<T>;

interface CreateConfigValueProviderOptions<T>
	extends TransformAndValidateConfigOptions {
	/**
	 * Config class.
	 */
	cls: Type<T>;
	/**
	 * Plain config object.
	 */
	value: Record<string, unknown>;
}

interface CreateConfigFactoryProviderOptions<T>
	extends TransformAndValidateConfigOptions {
	/**
	 * Config class.
	 */
	cls: Type<T>;
	/**
	 * Factory to return plain config object. Accept promise.
	 */
	factory: (
		...args: any[]
	) => Record<string, unknown> | Promise<Record<string, unknown>>;
	/**
	 */
	inject?: (Type<any> | string | symbol | Abstract<any> | Function)[];
	/**
	 * Optional enum defining lifetime of the provider that is returned by the Factory function.
	 */
	scope?: Scope;
}

interface TransformAndValidateConfigOptions {
	/**
	 * Options passed to plainToClass of class-transformer.
	 */
	classTransformerOptions?: ClassTransformOptions;
	/**
	 * Options passed to validate of class-validator.
	 * Provided options will be merged with default options.
	 * Default: { whitelist: true }
	 */
	classValidatorOptions?: ValidatorOptions;
}

Load Utility

This package also provides load utilities for loading different types of config file. Note that the loaded config value is plain object and not validated. Also, other peer dependencies may be required by the load utilities.

JSON

/**
 * Load json config file with JSON.parse.
 * @param options - Options for loadJsonConfig.
 * @returns Plain config object.
 */
function loadJsonConfig(
	options: LoadJsonConfigOptions
): Record<string, unknown>;

interface LoadJsonConfigOptions {
	/**
	 * Path to json config file.
	 */
	path: string;
}

Yaml

/**
 * Load dotenv config file with "js-yaml" package.
 * @remark
 * Please make sure "js-yaml" package is installed.
 * @param options - Options for loadYamlConfig.
 * @returns Plain config object.
 */
function loadYamlConfig(
	options: LoadYamlConfigOptions
): Record<string, unknown>;

interface LoadYamlConfigOptions {
	/**
	 * Path to yaml config file.
	 */
	path: string;
}

Dotenv

/**
 * Load dotenv config file with "dotenv" package.
 * @remark
 * Please make sure "dotenv" package is installed.
 * If you want to use the expandDotEnv option, please also make sure "dotenv-expand" package is installed.
 * @param options - Options for loadDotenvConfig.
 * @return Plain config object.
 */
function loadDotenvConfig(
	options: LoadDotenvConfigOptions
): Record<string, unknown>;

interface LoadDotenvConfigOptions {
	/**
	 * Path to dotenv config file.
	 */
	path: string;
	/**
	 * If true, expand variables in dotenv config with "dotenv-expand" package. Default: false.
	 */
	expandDotenv?: boolean;
	/**
	 * If true, merge dotenv config with process.env. process.env overrides dotenv config. Default: false.
	 */
	mergeProcessEnv?: boolean;
	/**
	 * If true, assign dotenv config to process.env. Default: false.
	 */
	assignProcessEnv?: boolean;
	/**
	 * If provided, it will be used to transform key of dotEnv config.
	 */
	transformEnvKey?: (key: string) => string;
}

Example

Examples can be found in here.