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

@nodeflip/nest-env-config

v1.0.3

Published

An environment configuration module for NestJS that allows for easy configuration using decorators.

Downloads

25

Readme

example workflow npm version downloads

🚀 Nest Environment Configuration Module

Overview

The @nodeflip/nest-env-config module provides a convenient way to manage environment configuration in your NestJS applications. By using decorators, you can define configuration properties that are automatically populated from environment variables with optional default values. This module ensures that your configuration is strongly typed and easily accessible throughout your application.

Installation

To install the module, run the following command:

npm install @nodeflip/nest-env-config

How to use it ?

Setting Up the Module

First, import the EnvironmentConfigModule in your application's module and call the forRoot method with your configuration class.

import { Module } from '@nestjs/common';
import { EnvironmentConfigModule } from '@nodeflip/nest-env-config';
import { AppConfig } from './app-config';
import { DBConfig } from './db-config';

@Module({
  imports: [
    EnvironmentConfigModule.forRoot({configClass: AppConfig}),
    EnvironmentConfigModule.forRoot({configClass: DBConfig, serviceName: 'DB_CONFIG_SERVICE'})
  ],
})
export class AppModule {}

Creating a Configuration Class

Define a configuration class and use the @Prop decorator to specify the environment variables and their default values.

import { Prop } from '@nodeflip/nest-env-config';

export class AppConfig {
  @Prop('APP_PORT', 3000)
  public port: number;

  @Prop('APP_ENV', 'development')
  public env: string;

  @Prop('DATABASE_URL')
  public databaseUrl: string;
}

Injecting the Configuration Service

Inject the EnvironmentConfigService into your services to access the configuration properties.

import { Injectable } from '@nestjs/common';
import { EnvironmentConfigService } from '@nodeflip/nest-env-config';
import { AppConfig } from './app-config';
import { DBConfig } from './db-config';

@Injectable()
export class AppService {
  constructor(
    private readonly appConfigService: EnvironmentConfigService<AppConfig>,
    @Inject('DB_CONFIG_SERVICE') private readonly dbConfigService: EnvironmentConfigService<DBConfig>
  ) {
    console.log(this.appConfigService.config);
    console.log(this.dbConfigService.config);
  }

  getPort(): number {
    return this.dbConfigService.config.port;
  }
}

Testing

The EnvironmentConfigModule can be easily tested using the NestJS testing utilities. Below is an example of how to set up and test the configuration service.

Example Test Configuration Class
import { Prop } from '@nodeflip/nest-env-config';

export class DBConfig {
  @Prop('DB_NAME', 'test_db')
  name: string;

  @Prop('DB_USERNAME', 'test_user')
  username: string;

  @Prop('DB_PASSWORD', 'test_password')
  password: string;

  @Prop('DB_HOST', 'test_host')
  host: string;

  @Prop('DB_PORT', 5432)
  port: number;

  @Prop('ENABLE_LOGGING', false)
  logging: boolean;
}
Example Test Suite
import { Test, TestingModule } from '@nestjs/testing';
import { EnvironmentConfigModule } from '@nodeflip/nest-env-config';
import { EnvironmentConfigService } from '@nodeflip/nest-env-config';
import { DBConfig } from './db-config';

describe('EnvironmentConfigService', () => {
  let service: EnvironmentConfigService<DBConfig>;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [EnvironmentConfigModule.forRoot({configClass: DBConfig})],
    }).compile();

    service = module.get<EnvironmentConfigService<DBConfig>>(EnvironmentConfigService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it('should retrieve configuration properties', () => {
    const config = service.config;
    expect(config).toBeDefined();
    expect(config.name).toBe('test_db');
    expect(config.username).toBe('test_user');
    expect(config.password).toBe('test_password');
    expect(config.host).toBe('test_host');
    expect(config.port).toBe(5432);
    expect(config.logging).toBe(false);
  });
});

Package Contents

The module includes the following main components:

  • EnvironmentConfigModule: The module to be imported into your application module.
  • EnvironmentConfigService: A service that provides access to the configuration properties.
  • Prop Decorator: A decorator for defining configuration properties.

EnvironmentConfigModule

The EnvironmentConfigModule is a dynamic module that registers the EnvironmentConfigService with the specified configuration class.

EnvironmentConfigService

The EnvironmentConfigService retrieves the configuration values from the environment variables and maps them to the properties defined in the configuration class.

Prop Decorator

The Prop decorator is used to define class properties that will be populated with values from the environment variables.

Contributing

Contributions are welcome! Please follow the standard GitHub workflow to contribute to this project.

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. Open a pull request.

License

This project is licensed under the MIT License.

Dependencies

  • @nestjs/common
  • @nestjs/config
  • @nestjs/core
  • reflect-metadata

Enjoy using @nodeflip/nest-env-config!