edit core config

This commit is contained in:
liuyi 2025-05-12 15:32:07 +08:00
parent 57c72e010b
commit c196b00708

View File

@ -1,4 +1,4 @@
import { Global, Injectable, Module } from '@nestjs/common'; import { DynamicModule, Global, Injectable, Module } from '@nestjs/common';
import { get } from 'lodash'; import { get } from 'lodash';
import { AppController } from '@/app.controller'; import { AppController } from '@/app.controller';
@ -11,6 +11,12 @@ const config: Record<string, any> = {
@Injectable() @Injectable()
export class ConfigService { export class ConfigService {
protected config: RecordAny = {};
constructor(data: RecordAny) {
this.config = data;
}
get<T>(key: string, defaultValue?: T): T | undefined { get<T>(key: string, defaultValue?: T): T | undefined {
return get(config, key, defaultValue); return get(config, key, defaultValue);
} }
@ -21,10 +27,26 @@ export class ConfigService {
providers: [ConfigService], providers: [ConfigService],
exports: [ConfigService], exports: [ConfigService],
}) })
export class CoreModule {} export class CoreModule {
static forRoot(options: { config: RecordAny }): DynamicModule {
return {
module: CoreModule,
global: true,
providers: [
{
provide: ConfigService,
useFactory() {
return new ConfigService(options.config);
},
},
],
exports: [ConfigService],
};
}
}
@Module({ @Module({
imports: [ContentModule, CoreModule], imports: [ContentModule, CoreModule.forRoot({ config: { name: 'ray' } })],
providers: [AppService], providers: [AppService],
controllers: [AppController], controllers: [AppController],
}) })