-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support using config in decorators with selectConfig
- Loading branch information
Showing
13 changed files
with
209 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './typed-config.module'; | ||
export * from './interfaces'; | ||
export * from './loader'; | ||
export * from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './select-config.util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DynamicModule, ValueProvider } from '@nestjs/common'; | ||
import { ClassConstructor } from 'class-transformer'; | ||
|
||
export const selectConfig = <T>( | ||
module: DynamicModule, | ||
Config: ClassConstructor<T>, | ||
): T => { | ||
const providers = module.providers as ValueProvider<T>[]; | ||
const selectedConfig = (providers || []).filter( | ||
({ provide }) => provide === Config, | ||
)[0]; | ||
if (!selectedConfig) { | ||
throw new Error(`You can only select config which exists in providers`); | ||
} | ||
return selectedConfig.useValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { selectConfig } from '../../lib'; | ||
import { AppModule } from '../src/app.module'; | ||
import { Config, DatabaseConfig, TableConfig } from '../src/config.model'; | ||
|
||
describe('Local toml', () => { | ||
it(`should be able to select config`, async () => { | ||
const module = AppModule.withRawModule(); | ||
|
||
const config = selectConfig(module, Config); | ||
expect(config.isAuthEnabled).toBe(true); | ||
|
||
const databaseConfig = selectConfig(module, DatabaseConfig); | ||
expect(databaseConfig.port).toBe(3000); | ||
|
||
const tableConfig = selectConfig(module, TableConfig); | ||
expect(tableConfig.name).toBe('test'); | ||
}); | ||
|
||
it(`can only select existing config`, async () => { | ||
const module = AppModule.withRawModule(); | ||
|
||
try { | ||
selectConfig(module, class {}); | ||
} catch (err) { | ||
expect(err.message).toMatch( | ||
/You can only select config which exists in providers/, | ||
); | ||
} | ||
|
||
try { | ||
selectConfig({ module: class {} }, class {}); | ||
} catch (err) { | ||
expect(err.message).toMatch( | ||
/You can only select config which exists in providers/, | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.