Skip to content

Commit

Permalink
chore: update config attributes to more self descriptive names
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 17, 2025
1 parent ce18cb8 commit c2eaf04
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class ConfigModule {
);

/**
* Loads process environment variables depending on the "ignoreEnvFile" flag and "envFilePath" value.
* Also, registers custom configurations globally.
* Loads environment variables based on the "ignoreEnvFile" flag and "envFilePath" value.
* Additionally, registers custom configurations globally.
* @param options
*/
static async forRoot<ValidationOptions extends Record<string, any>>(
Expand Down Expand Up @@ -106,8 +106,8 @@ export class ConfigModule {
if (options.cache) {
untypedConfigService.isCacheEnabled = true;
}
if (options.skipPredefined) {
untypedConfigService.skipPredefined = true;
if (options.skipProcessEnv) {
untypedConfigService.skipProcessEnv = true;
}

configService.setEnvFilePaths(envFilePaths);
Expand Down
12 changes: 6 additions & 6 deletions lib/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ export class ConfigService<
return this._isCacheEnabled;
}

private set skipPredefined(value: boolean) {
this._skipPredefined = value;
private set skipProcessEnv(value: boolean) {
this._skipProcessEnv = value;
}

private get skipPredefined(): boolean {
return this._skipPredefined;
private get skipProcessEnv(): boolean {
return this._skipProcessEnv;
}

private readonly cache: Partial<K> = {} as any;
private readonly _changes$ = new Subject<ConfigChangeEvent>();
private _skipPredefined = false;
private _skipProcessEnv = false;
private _isCacheEnabled = false;
private envFilePaths: string[] = [];

Expand Down Expand Up @@ -146,7 +146,7 @@ export class ConfigService<
? undefined
: defaultValueOrOptions;

if (!this.skipPredefined) {
if (!this._skipProcessEnv) {
const processEnvValue = this.getFromProcessEnv(
propertyPath,
defaultValue,
Expand Down
8 changes: 5 additions & 3 deletions lib/interfaces/config-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ export interface ConfigModuleOptions<

/**
* If "true", predefined environment variables will be validated.
* Predefined environment variables are process variables that were set before the module was imported.
* 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.
* @default true
*/
validatePredefined?: boolean;

/**
* If "true", predefined environment variables will be ignored and not picked up by the `ConfigService#get` method.
* If "true", process environment variables (process.env) will be ignored and not picked up by the `ConfigService#get` method.
* @default false
*/
skipPredefined?: boolean;
skipProcessEnv?: boolean;

/**
* Environment variables validation schema (Joi).
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/skip-predefined.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { Test } from '@nestjs/testing';
import { ConfigModule, ConfigService } from '../../lib';
import { AppModule } from '../src/app.module';

describe('Environment variables (skip predefined)', () => {
describe('Environment variables (skip process env)', () => {
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withSkipPredefined()],
imports: [AppModule.withSkipProcessEnv()],
}).compile();

app = module.createNestApplication();
await app.init();
});

it(`should ignore predefined environment variables`, async () => {
it(`should ignore predefined environment variables (process.env)`, async () => {
process.env.RANDOM_PREDEFINED = 'test';
await ConfigModule.envVariablesLoaded;

Expand Down
4 changes: 2 additions & 2 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ export class AppModule {
};
}

static withSkipPredefined(): DynamicModule {
static withSkipProcessEnv(): DynamicModule {
return {
module: AppModule,
imports: [
ConfigModule.forRoot({
envFilePath: join(__dirname, '.env'),
load: [() => ({ obj: { test: 'true', test2: undefined } })],
skipPredefined: true,
skipProcessEnv: true,
}),
],
};
Expand Down

0 comments on commit c2eaf04

Please sign in to comment.