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

@cristobalgvera/nestjs-environment

v0.2.3

Published

Environment module configuration library that allows you to easily setup and validate your environment with minimal configuration.

Downloads

6

Readme

NestJS Environment

Environment module configuration library that allows you to easily setup and validate your environment with minimal configuration.

  • Type safe environment usage.
  • Complex environments variables (arrays and objects) validation.

Installation

npm install @cristobalgvera/nestjs-environment

By the moment, the unique accepted source of validation is Joi (there are plans to expand to any source of validation like Zod), so, you have to install it too.

npm install joi

Usage

The most basic usage possible is the following:

  1. Create the Environment class. It needs to implements BaseEnvironment.

    // environment/environment.model.ts
    
    import { BaseEnvironment } from '@cristobalgvera/nestjs-environment';
    
    export class Environment implements BaseEnvironment {
      NODE_ENV: string; // or 'development' | 'test' | 'production'
    }
  2. Create a validation schema for that Environment using Joi. Remember to create the test file too.

    // environment/environment.schema.ts
    
    import * as Joi from 'joi';
    import { Environment } from './environment.model';
    
    export const environmentSchema = Joi.object<Environment, true>({
      NODE_ENV: Joi.string().default('development'),
    });
  3. Import the EnvironmentModule using the forRoot static method and provide the Environment class and the validation schema.

    // app.module.ts
    
    import { EnvironmentModule } from '@cristobalgvera/nestjs-environment';
    import { Module } from '@nestjs/common';
    import { Environment, environmentSchema } from './environment';
    
    @Module({
      imports: [
        EnvironmentModule.forRoot({
          environmentClass: Environment,
          validationSchema: environmentSchema,
        }),
      ],
    })
    export class AppModule {}
  4. Use the EnvironmentService in any place you want through DI. You need to provide as generic parameter the Environment class previously created and it will be type-safe.

    // test.service.ts
    
    import { EnvironmentService } from '@cristobalgvera/nestjs-environment';
    import { Injectable } from '@nestjs/common';
    import { Environment } from './environment';
    
    @Injectable()
    export class TestService {
      constructor(
        private readonly environmentService: EnvironmentService<Environment>,
      ) {}
    
      getNodeEnvironment(): string {
        const NODE_ENV = this.environmentService.get('NODE_ENV');
        //        ^? const NODE_ENV: string
        return NODE_ENV;
      }
    }
  5. Finally, to test it, simply mock it.

    // test.service.spec.ts
    
    import { TestBed } from '@automock/jest';
    import { EnvironmentService } from '@cristobalgvera/nestjs-environment';
    import { Environment } from './environment';
    import { TestService } from './test.service';
    
    describe('TestService', () => {
      let underTest: TestService;
      let environmentService: EnvironmentService<Environment>;
    
      beforeEach(() => {
        const { unit, unitRef } = TestBed.create(TestService).compile();
    
        underTest = unit;
        environmentService = unitRef.get(EnvironmentService);
      });
    
      describe('getNodeEnvironment', () => {
        const environment = {
          NODE_ENV: 'development',
        } as Readonly<Environment>;
    
        beforeEach(() => {
          jest
            .spyOn(environmentService, 'get')
            .mockImplementation((key) => environment[key]);
        });
    
        it('should return the node environment', () => {
          const expected = environment.NODE_ENV;
    
          const actual = underTest.getNodeEnvironment();
    
          expect(actual).toEqual(expected);
        });
      });
    });

Advance usage

Following the same structure described above, you can create complex environment variables that can be validated. This complex types can be arrays or custom objects that has an inner validation too.

The unique change you have to do it the following:

  1. Add the ParseEnvironment decorator to the complex types.

    // environment/environment.model.ts
    
    import {
      BaseEnvironment,
      ParseEnvironment,
    } from '@cristobalgvera/nestjs-environment';
    import { User } from './user.model';
    
    export class Environment implements BaseEnvironment {
      NODE_ENV: 'development' | 'test' | 'production';
      PORT: number;
      IS_SWAGGER_ENABLED: boolean;
    
      @ParseEnvironment() // <-- Use this to parse arrays of primitive values
      ALLOWED_IPS: string[];
    
      @ParseEnvironment({ toClass: true }) // <-- Use this to parse classes
      INTERNAL_USER: User;
    
      @ParseEnvironment({ toClass: true }) // <-- Use this to parse arrays of classes
      EXTERNAL_USERS: User[];
    }
  2. Create the validation schema. Note the usage of a userSchema to validate the users. This way you can easily test each schema separately.

    // environment/environment.schema.ts
    
    import * as Joi from 'joi';
    import { Environment } from './environment.model';
    import { userSchema } from './user.schema'; // Users can have their own schema
    
    export const environmentSchema = Joi.object<Environment, true>({
      NODE_ENV: Joi.string()
        .valid('development', 'test', 'production')
        .default('development'),
      PORT: Joi.number().port().default(8080),
      IS_SWAGGER_ENABLED: Joi.boolean().default(true),
      ALLOWED_IPS: Joi.array().items(Joi.string().ip()).required(),
      INTERNAL_USER: userSchema.required(),
      EXTERNAL_USERS: Joi.array().items(userSchema).required(),
    });