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-env-config

v0.4.2

Published

```console npm install nestjs-env-config --save ```

Downloads

11

Readme

NestJS-Env-Config

Getting Started

Installation

npm install nestjs-env-config --save

API

It's easy to use NestJSConfig APIs to parse, validate and load environment config in NestJS.

The config is divided 3 sections.

  • Config: The config object that you will find in your NestJS API.
  • DTO: A class validator DTO to validate config.
  • Default: Defines all default values here.

config.ts

export interface APIConfig extends BaseConfig {
    database: DatabaseConfig;
    ssl: SSLConfig;
    jwt: JWTConfig;
}

export interface JWTConfig {
    secret: string;
    expiresIn: string;
}

export interface SSLConfig {
    enabled: boolean;
    keyPath: string;
    certPath: string;
}

export interface DatabaseConfig {
    host: string;
    port: number | null;
    username: string | null;
    password: string | null;
    name: string | null;
}

export const getConfig = (env: Record<string, unknown>): APIConfig => {
    const config = validateConfig(ConfigEnvironmentDto, defaultConfig, env);

    return {
        port: config.AP_APP_PORT,
        currentEnv: config.AP_APP_ENV,
        verbose: config.AP_APP_VERBOSE,
        whitelistedOrigins: config.AP_APP_URLS_WHITELIST,
        frontendUrl: config.AP_APP_FRONTEND_URL,
        database: {
            host: config.AP_DB_HOST,
            port: config.AP_DB_PORT,
            name: config.AP_DB_NAME,
            username: config.AP_DB_USER,
            password: config.AP_DB_PASSWORD,
        },
        ssl: {
            enabled: config.AP_SSL_ENABLED,
            keyPath: config.AP_SSL_KEY_PATH,
            certPath: config.AP_SSL_CERT_PATH,
        },
        jwt: {
            secret: config.AP_JWT_SECRET,
            expiresIn: config.AP_JWT_EXPIRE_IN,
        },
    };
};

config.environment.dto.ts

const UseDefault = () => BaseUseDefault(defaultConfig);

export class ConfigEnvironmentDto {
  //Base config
  @ConfigValidators(isPortNumber)
  @Expose()
  @UseDefault()
  @UseEnvPort()
  @TransformNumber()
  @Desc('Port to listen on')
  AP_APP_PORT: number;

  @ConfigValidator(isEnum, Environment)
  @Expose()
  @UseDefault()
  @Desc('Environment to run in')
  AP_APP_ENV: Environment;

  @ConfigValidators(isBoolean)
  @Expose()
  @UseDefault()
  @TransformBoolean()
  @Desc('Enable debug mode')
  AP_APP_VERBOSE: boolean;

  @ConfigValidators(isUrlArray)
  @Expose()
  @TransformArray()
  @UseDefault()
  @Desc('List of urls to proxy')
  AP_APP_URLS_WHITELIST: string[];

  @ConfigValidators(isString)
  @Expose()
  @UseDefault()
  @Desc('Path of the frontend')
  AP_APP_FRONTEND_URL: string;

  // Database
  @ConfigValidators(isString)
  @Expose()
  @UseDefault()
  @Desc('Database host')
  @Secret()
  AP_DB_HOST: string;

  @Expose()
  @UseDefault()
  @Desc('Database name')
  AP_DB_NAME: string | null;

  @Expose()
  @UseDefault()
  @Desc('Database user')
  @Secret()
  AP_DB_USER: string | null;

  @Expose()
  @UseDefault()
  @Desc('Database password')
  @Secret()
  AP_DB_PASSWORD: string | null;

  @Expose()
  @UseDefault()
  @TransformNumber()
  @Desc('Database port')
  AP_DB_PORT: number | null;

  // SSL
  @ConfigValidators(isBoolean)
  @Expose()
  @TransformBoolean()
  @UseDefault()
  @Desc('Is database ssl enabled')
  AP_SSL_ENABLED: boolean;

  @ConfigValidators(isString)
  @Expose()
  @UseDefault()
  @Desc('Database ssl key path')
  AP_SSL_KEY_PATH: string;

  @ConfigValidators(isString)
  @Expose()
  @UseDefault()
  @Desc('Database ssl cert path')
  AP_SSL_CERT_PATH: string;

  // JWT
  @ConfigValidators(isString)
  @Expose()
  @UseDefault()
  @Desc('JWT secret')
  @Secret()
  AP_JWT_SECRET: string;

  @ConfigValidators(isValidPeriod, isString)
  @Expose()
  @UseDefault()
  @Desc('JWT expiration time')
  AP_JWT_EXPIRE_IN: string;
}

config.default

export enum Environment {
  DEV = 'dev',
  STAGING = 'staging',
  PROD = 'prod',
  TEST = 'test',
}

export const defaultConfig: ConfigEnvironmentDto = {
  AP_APP_PORT: 3000,
  AP_APP_ENV: Environment.DEV,
  AP_APP_VERBOSE: false,
  AP_APP_URLS_WHITELIST: ['http://localhost:4200'],
  AP_APP_FRONTEND_URL: 'http://localhost:4200',

  // Database
  AP_DB_HOST: 'localhost',
  AP_DB_NAME: null,
  AP_DB_USER: null,
  AP_DB_PASSWORD: null,
  AP_DB_PORT: null,

  // SSL
  AP_SSL_ENABLED: false,
  AP_SSL_KEY_PATH: '/ssl/cert.key',
  AP_SSL_CERT_PATH: '/ssl/cert.crt',

  // JWT
  AP_JWT_SECRET: 'myverysecretkey',
  AP_JWT_EXPIRE_IN: '1-day',
};

Use it in your application getConfig()
In my case I used it in a custom function, but you can use it directly in the config service loader.

const loadAPIConfig = async (): Promise<{
  config: APIConfig;
  sslOptions: NestApplicationOptions;
}> => {
  const config = getConfig(process.env);
  return { config, sslOptions: await getSSLOptions(config.ssl) };
};

License

NestJSConfig is licensed under a MIT License.