-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unscoped variables support #24
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable max-classes-per-file */ | ||
import { Module } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { ConfigModule, UnscopedConfig, Env } from '../src'; | ||
import { String } from '../src/types'; | ||
|
||
describe('UnscopedConfig', () => { | ||
@UnscopedConfig() | ||
class TestUnscopedConfig { | ||
@Env('SOME_UNSCOPED_VARIABLE') | ||
@String() | ||
stringVar: string; | ||
} | ||
|
||
@Module({ | ||
imports: [ConfigModule.forRoot({ configs: [TestUnscopedConfig] })], | ||
}) | ||
class AppModule {} | ||
|
||
beforeAll(() => { | ||
Object.assign(process.env, { | ||
SOME_UNSCOPED_VARIABLE: 'stringVarOfSomeVariableOutOfModuleScope', | ||
}); | ||
}); | ||
|
||
it('should successfully map envs out of module scope', async () => { | ||
const app = await NestFactory.createApplicationContext(AppModule, { | ||
bufferLogs: true, | ||
}); | ||
|
||
const testConfig = app.get<TestUnscopedConfig>(TestUnscopedConfig); | ||
|
||
expect(testConfig).toMatchObject({ | ||
stringVar: 'stringVarOfSomeVariableOutOfModuleScope', | ||
}); | ||
|
||
await app.close(); | ||
}); | ||
}); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { ClassType, ConfigStorage } from './types'; | ||
import { ENV_CONFIG_NAME_SYMBOL } from './symbols'; | ||
import { ENV_CONFIG_NAME_SYMBOL, UNSCOPED_CONFIG_SYMBOL } from './symbols'; | ||
import { plainToClass } from '../transformer'; | ||
|
||
export class ConfigFactory { | ||
public createConfig( | ||
configStorage: ConfigStorage, | ||
ConfigClass: ClassType, | ||
): typeof ConfigClass.prototype { | ||
if (ConfigClass[UNSCOPED_CONFIG_SYMBOL]) { | ||
return plainToClass(ConfigClass, configStorage[UNSCOPED_CONFIG_SYMBOL], { | ||
excludeExtraneousValues: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there will be different behaviour with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, but without excludeExtraneousValues all envs will be automatically exposed to class instance which is more terrible than the inconsistent behavior. Moreover, I do not understand why this option is not enabled in the I think we should describe P.S. But in general, you gave me a strange idea how to reduce workaroundness of this solution, I'll try to implement it. |
||
}); | ||
} | ||
|
||
let name = ConfigClass[ENV_CONFIG_NAME_SYMBOL]; | ||
if (!name) { | ||
// TODO: warning | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const ENV_CONFIG_NAME_SYMBOL = Symbol.for('ENV_CONFIG_NAME'); | ||
export const UNSCOPED_CONFIG_SYMBOL = Symbol.for('UNSCOPED_ENV_CONFIG'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Или "^9.1.4" ?