Skip to content

Commit 99d8bca

Browse files
committed
feat: allow to use symbol as a token
1 parent 1694ea6 commit 99d8bca

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

lib/config.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type KeyOf<T> = keyof T extends never ? string : keyof T;
4343
*/
4444
@Injectable()
4545
export class ConfigService<
46-
K = Record<string, unknown>,
46+
K = Record<string | symbol, unknown>,
4747
WasValidated extends boolean = false,
4848
> {
4949
private set isCacheEnabled(value: boolean) {
@@ -72,7 +72,7 @@ export class ConfigService<
7272
@Optional()
7373
@Inject(CONFIGURATION_TOKEN)
7474
private readonly internalConfig: Record<string, any> = {},
75-
) {}
75+
) { }
7676

7777
/**
7878
* Returns a stream of configuration changes.
@@ -142,7 +142,7 @@ export class ConfigService<
142142
}
143143
const defaultValue =
144144
this.isGetOptionsObject(defaultValueOrOptions as Record<string, any>) &&
145-
!options
145+
!options
146146
? undefined
147147
: defaultValueOrOptions;
148148

lib/utils/get-config-token.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @publicApi
33
*/
4-
export function getConfigToken(token: string): string {
5-
return `CONFIGURATION(${token})`;
4+
export function getConfigToken(token: string | symbol): string {
5+
return `CONFIGURATION(${token.toString()})`;
66
}

lib/utils/register-as.util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { getConfigToken } from './get-config-token.util';
1212
* @publicApi
1313
*/
1414
export interface ConfigFactoryKeyHost<T = unknown> {
15-
KEY: string;
15+
KEY: string | symbol;
1616
asProvider(): {
1717
imports: [ReturnType<typeof ConfigModule.forFeature>];
1818
useFactory: (config: T) => T;
19-
inject: [string];
19+
inject: [string | symbol];
2020
};
2121
}
2222

@@ -29,7 +29,7 @@ export function registerAs<
2929
TConfig extends ConfigObject,
3030
TFactory extends ConfigFactory = ConfigFactory<TConfig>,
3131
>(
32-
token: string,
32+
token: string | symbol,
3333
configFactory: TFactory,
3434
): TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>> {
3535
const defineProperty = (key: string, value: unknown) => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { Test } from '@nestjs/testing';
3+
import { AppModule } from '../src/app.module';
4+
5+
describe('Symbol Files', () => {
6+
let app: INestApplication;
7+
8+
beforeEach(async () => {
9+
const module = await Test.createTestingModule({
10+
imports: [AppModule.withSymbolLoadedConfigurations()],
11+
}).compile();
12+
13+
app = module.createNestApplication();
14+
await app.init();
15+
});
16+
17+
it(`should return symbol loaded configuration`, () => {
18+
const config = app.get(AppModule).getSymbolDatabaseConfig();
19+
expect(config.host).toEqual('host');
20+
expect(config.port).toEqual(4000);
21+
});
22+
23+
afterEach(async () => {
24+
await app.close();
25+
});
26+
});

tests/src/app.module.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ConfigModule } from '../../lib/config.module';
66
import { ConfigService } from '../../lib/config.service';
77
import databaseConfig from './database.config';
88
import nestedDatabaseConfig from './nested-database.config';
9+
import symbolDatabaseConfig, { DATABASE_SYMBOL_TOKEN } from './symbol-database.config';
910

1011
type Config = {
1112
database: ConfigType<typeof databaseConfig> & {
@@ -31,7 +32,7 @@ export class AppModule {
3132
@Optional()
3233
@Inject(databaseConfig.KEY)
3334
private readonly dbConfig: ConfigType<typeof databaseConfig>,
34-
) {}
35+
) { }
3536

3637
/**
3738
* This method is not meant to be used anywhere! It just here for testing
@@ -175,6 +176,17 @@ export class AppModule {
175176
};
176177
}
177178

179+
static withSymbolLoadedConfigurations(): DynamicModule {
180+
return {
181+
module: AppModule,
182+
imports: [
183+
ConfigModule.forRoot({
184+
load: [symbolDatabaseConfig],
185+
}),
186+
],
187+
};
188+
}
189+
178190
static withDynamicLoadedConfigurations(
179191
configFactory: ConfigFactory[],
180192
): DynamicModule {
@@ -249,4 +261,8 @@ export class AppModule {
249261
getNestedDatabaseHost() {
250262
return this.configService.get('database.driver.host');
251263
}
264+
265+
getSymbolDatabaseConfig() {
266+
return this.configService.get(DATABASE_SYMBOL_TOKEN)
267+
}
252268
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { registerAs } from '../../lib/utils';
2+
3+
export const DATABASE_SYMBOL_TOKEN = Symbol('database');
4+
5+
export default registerAs(DATABASE_SYMBOL_TOKEN, () => ({
6+
host: 'host',
7+
port: 4000,
8+
}));

0 commit comments

Comments
 (0)