New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

nestjs-config-module

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-config-module

Configuration module for Nest framework (node.js)

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
4
33.33%
Maintainers
1
Weekly downloads
 
Created
Source

Nest.js Config Module

Configuration module for Nest framework (node.js)

NPM Version Package License NPM Downloads CI

Installation

npm install --save nestjs-config-module

Usage

You have to import the Module ConfigModule and initialize it with the forRoot function, this fetches a Javascript object with the loader function, then maps and validates that to the schema

This Module is not used as injection dependency directly, but rather its schema class and subclasses.

import { Module } from '@nestjs/common';
import { ConfigModule, envJsonLoaderFactory } from 'nestjs-config-module';
import { IsOptional, ValidateNested, IsArray, ArrayUnique, IsString, IsIn } from 'class-validator';
import { Type } from 'class-transformer';
import { LogLevel } from "@nestjs/common";

export class LogConfiguration {
    @IsArray()
    @ArrayUnique()
    @IsString({
        each: true
    })
    @IsIn(['log', 'error', 'warn', 'debug', 'verbose'], {
        each: true
    })
    levels: LogLevel[] = ['log', 'error', 'warn'];
}

export class Configuration {
    @IsOptional()
    host?: string;
    @ValidateNested() @Type(() => LogConfiguration)
    log = new LogConfiguration();
}

@Module({
  imports: [ConfigModule.forRoot({
    isGlobal: true,
    loader: envJsonLoaderFactory('DEMO_'),
    schema: Configuration,
  })],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { Configuration } from 'app.module';

@Injectable()
export class FooService {
    constructor(
        private configuration: Configuration,
    ) {}
}
import { Injectable } from '@nestjs/common';
import { LogConfiguration } from 'app.module';

@Injectable()
export class BarService {
    constructor(
        private logConfiguration: LogConfiguration,
    ) {}
}

Loaders

this module comes with 2 builtin loaders:

  • envJsonLoaderFactory
  • jsonFileLoaderFactory

For the documentation on how to use the envJsonLoader see: env-json-parser

It's also possible to implement your own loader, it just needs to be a function that returns a Promise which resolves an object

ConfigModule.forRoot({
  isGlobal: true,
  loader: (): Promise<object> => {
    return Promise.resolve({
      host: 'localhost'
   })
  },
  schema: Configuration,
})

Keywords

nest

FAQs

Package last updated on 23 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts