@nestjs/config

  • Version 12.0.1
  • Published
  • 50.4 kB
  • 4 dependencies
  • MIT license

Install

npm i @nestjs/config
yarn add @nestjs/config
pnpm add @nestjs/config

Overview

Nest - modern, fast, powerful node.js web framework (@config)

Index

Functions

function getConfigToken

getConfigToken: (token: string | symbol) => string;

function getDefaultParser

getDefaultParser: () => Parser;

    function registerAs

    registerAs: <
    TConfig extends ConfigObject,
    TFactory extends ConfigFactory<ConfigObject> = ConfigFactory<TConfig>
    >(
    token: string | symbol,
    configFactory: TFactory
    ) => TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>>;
    • Registers the configuration object behind a specified token.

    Classes

    class ConditionalModule

    class ConditionalModule {}

    method registerWhen

    static registerWhen: (
    module: ModuleMetadata,
    condition: string | ((env: NodeJS.ProcessEnv) => boolean),
    options?: { timeout?: number; debug?: boolean }
    ) => Promise<Required<Pick<DynamicModule, 'exports' | 'imports' | 'module'>>>;

    class ConfigModule

    class ConfigModule {}

    property envVariablesLoaded

    static readonly envVariablesLoaded: Promise<void>;
    • This promise resolves when parser completes loading environment variables. When "ignoreEnvFile" is set to true, then it will resolve immediately after the "ConfigModule#forRoot" method is called.

    method forFeature

    static forFeature: (config: ConfigFactory) => DynamicModule;
    • Registers configuration object (partial registration).

      Parameter config

    method forRoot

    static forRoot: (options?: ConfigModuleOptions) => Promise<DynamicModule>;
    • Loads environment variables based on the "ignoreEnvFile" flag and "envFilePath" value. Additionally, registers custom configurations globally.

      Parameter options

    class ConfigService

    class ConfigService<
    K = Record<string | symbol, unknown>,
    WasValidated extends boolean = false
    > {}

    constructor

    constructor(internalConfig?: Record<string, any>);

      property changes$

      readonly changes$: any;
      • Returns a stream of configuration changes. Each event contains the attribute path, the old value and the new value.

      method get

      get: {
      <T = any>(propertyPath: KeyOf<K>): ValidatedResult<WasValidated, T>;
      <
      T = K,
      P extends Path<T> = any,
      R extends PathValue<T, P> = PathValue<T, P>
      >(
      propertyPath: P,
      options: ConfigGetOptions
      ): ValidatedResult<WasValidated, R>;
      <T = any>(propertyPath: KeyOf<K>, defaultValue: NoInferType<T>): T;
      <
      T = K,
      P extends Path<T> = any,
      R extends PathValue<T, P> = PathValue<T, P>
      >(
      propertyPath: P,
      defaultValue: NoInferType<R>,
      options: ConfigGetOptions
      ): Exclude<R, undefined>;
      };
      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host").

        Parameter propertyPath

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host").

        Parameter propertyPath

        Parameter options

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). It returns a default value if the key does not exist.

        Parameter propertyPath

        Parameter defaultValue

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). It returns a default value if the key does not exist.

        Parameter propertyPath

        Parameter defaultValue

        Parameter options

      method getOrThrow

      getOrThrow: {
      <T = any>(propertyPath: KeyOf<K>): Exclude<T, undefined>;
      <
      T = K,
      P extends Path<T> = any,
      R extends PathValue<T, P> = PathValue<T, P>
      >(
      propertyPath: P,
      options: ConfigGetOptions
      ): Exclude<R, undefined>;
      <T = any>(propertyPath: KeyOf<K>, defaultValue: NoInferType<T>): Exclude<
      T,
      undefined
      >;
      <
      T = K,
      P extends Path<T> = any,
      R extends PathValue<T, P> = PathValue<T, P>
      >(
      propertyPath: P,
      defaultValue: NoInferType<R>,
      options: ConfigGetOptions
      ): Exclude<R, undefined>;
      };
      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host").

        Parameter propertyPath

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host").

        Parameter propertyPath

        Parameter options

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). It returns a default value if the key does not exist. If the default value is undefined an exception will be thrown.

        Parameter propertyPath

        Parameter defaultValue

      • Get a configuration value (either custom configuration or process environment variable) based on property path (you can use dot notation to traverse nested object, e.g. "database.host"). It returns a default value if the key does not exist. If the default value is undefined an exception will be thrown.

        Parameter propertyPath

        Parameter defaultValue

        Parameter options

      method set

      set: <T = any>(propertyPath: KeyOf<K>, value: T) => void;
      • Sets a configuration value based on property path.

        Parameter propertyPath

        Parameter value

      method setEnvFilePaths

      setEnvFilePaths: (paths: string[]) => void;
      • Sets env file paths from config.module.ts to parse.

        Parameter paths

      method setParser

      setParser: (parser: Parser) => void;
      • Sets parser from config.module.ts.

        Parameter parser

      Interfaces

      interface ConfigChangeEvent

      interface ConfigChangeEvent<OldValue = any, NewValue = any> {}
      • Represents a change in the configuration object. Dispatched when one updates the configuration object through the ConfigService#set method.

      property newValue

      newValue: NewValue;

        property oldValue

        oldValue: OldValue;

          property path

          path: string;

            interface ConfigFactoryKeyHost

            interface ConfigFactoryKeyHost<T = unknown> {}

            property KEY

            KEY: string | symbol;

              method asProvider

              asProvider: () => {
              imports: [ReturnType<typeof ConfigModule.forFeature>];
              useFactory: (config: T) => T;
              inject: [string | symbol];
              };

                interface ConfigGetOptions

                interface ConfigGetOptions {}

                property infer

                infer: true;
                • If present, "get" method will try to automatically infer a type of property based on the type argument specified at the "ConfigService" class-level (example: ConfigService).

                interface ConfigModuleOptions

                interface ConfigModuleOptions<
                ValidationOptions extends StandardSchemaV1.Options = StandardSchemaV1.Options
                > {}

                property cache

                cache?: boolean;
                • If "true", values from the process.env object will be cached in the memory. This improves the overall application performance. See: https://github.com/nodejs/node/issues/3104

                property envFilePath

                envFilePath?: string | string[];
                • Path to the environment file(s) to be loaded.

                property expandVariables

                expandVariables?: boolean | DotenvExpandOptions;
                • A boolean value indicating the use of expanded variables, or object containing options to pass to dotenv-expand. If .env contains expanded variables, they'll only be parsed if this property is set to true.

                property ignoreEnvFile

                ignoreEnvFile?: boolean;
                • If "true", environment files (.env) will be ignored.

                property ignoreEnvVars

                ignoreEnvVars?: boolean;
                • If "true", predefined environment variables will not be validated.

                  Deprecated

                  Use validatePredefined instead.

                property isGlobal

                isGlobal?: boolean;
                • If "true", registers ConfigModule as a global module. See: https://docs.nestjs.com/modules#global-modules

                property load

                load?: Array<ConfigFactory | Promise<ConfigFactory>>;
                • Array of custom configuration files to be loaded. See: https://docs.nestjs.com/techniques/configuration

                property override

                override?: boolean;
                • If "true", existing environment variables will be overridden by values from the .env file. false

                property parser

                parser?: Parser;
                • A function used to parse a buffer into a configuration object.

                property skipProcessEnv

                skipProcessEnv?: boolean;
                • If "true", process environment variables (process.env) will be ignored and not picked up by the ConfigService#get method. false

                property validate

                validate?: (config: Record<string, any>) => Record<string, any>;
                • Custom function to validate environment variables. It takes an object containing environment variables as input and outputs validated environment variables. If exception is thrown in the function it would prevent the application from bootstrapping. Also, environment variables can be edited through this function, changes will be reflected in the process.env object.

                property validatePredefined

                validatePredefined?: boolean;
                • If "true", predefined environment variables will be validated. Predefined environment variables are process variables (process.env variables) that were set before the module was imported. For example, if you start your application with PORT=3000 node main.js, then PORT is a predefined environment variable. Variables that were loaded by the ConfigModule from the .env file are not considered predefined. true

                property validationOptions

                validationOptions?: ValidationOptions;
                • Options to pass to the Standard Schema validation.

                property validationSchema

                validationSchema?: StandardSchemaV1;
                • Environment variables validation schema (Standard Schema, e.g. Zod, Arktype).

                  See Also

                  • https://standardschema.dev/

                Type Aliases

                type ConfigFactory

                type ConfigFactory<T extends ConfigObject = ConfigObject> =
                () => ConfigFactoryReturnValue<T>;

                  type ConfigObject

                  type ConfigObject = Record<string, any>;

                  type ConfigType

                  type ConfigType<T extends (...args: any) => any> = T extends (
                  ...args: any
                  ) => infer ReturnVal
                  ? ReturnVal extends Promise<infer AsyncReturnVal>
                  ? AsyncReturnVal
                  : ReturnVal
                  : any;

                  type NoInferType

                  type NoInferType<T> = [T][T extends any ? 0 : never];

                    type Parser

                    type Parser = (buffer: Buffer) => Record<string, any>;

                      type Path

                      type Path<T> = keyof T extends string
                      ? PathImpl2<T> extends infer P
                      ? P extends string | keyof T
                      ? P
                      : keyof T
                      : keyof T
                      : never;

                        type PathImpl

                        type PathImpl<T, Key extends keyof T> = Key extends string
                        ? IsAny<T[Key]> extends true
                        ? never
                        : T[Key] extends Record<string, any>
                        ?
                        | `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> &
                        string}`
                        | `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
                        : never
                        : never;

                          type PathImpl2

                          type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;

                            type PathValue

                            type PathValue<T, P extends Path<T>> = P extends `${infer Key}.${infer Rest}`
                            ? Key extends keyof T
                            ? Rest extends Path<T[Key]>
                            ? PathValue<T[Key], Rest>
                            : never
                            : never
                            : P extends keyof T
                            ? T[P]
                            : never;

                              Package Files (15)

                              Dependencies (4)

                              Dev Dependencies (22)

                              Peer Dependencies (2)

                              Badge

                              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@nestjs/config.

                              • Markdown
                                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@nestjs/config)
                              • HTML
                                <a href="https://www.jsdocs.io/package/@nestjs/config"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>