feat(learning): 完成NestJS核心概念和自定义提供者的学习
- 引入并配置了Fastify替代Express作为HTTP服务器,提高应用性能。 - 实现了PostController及其CRUD操作,包括请求方法和参数处理。 - 创建并使用CreatePostDto和UpdatePostDto进行请求数据验证。 - 完成了多种自定义提供者的实现和应用,如值提供者、类提供者和工厂提供者。 - 添加了一些全局类型定义,如RecordAny和BaseType,用于增强代码的可读性和健壮性。 - 通过ConfigService添加和管理新的环境配置项。
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { DynamicModule, Global, Module } from '@nestjs/common';
|
||||
|
||||
import { ConfigService } from './services/config.service';
|
||||
|
||||
@Global()
|
||||
@Module({})
|
||||
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],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { get } from 'lodash';
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService {
|
||||
protected config: RecordAny = {};
|
||||
|
||||
constructor(data: RecordAny) {
|
||||
this.config = data;
|
||||
}
|
||||
|
||||
get<T>(key: string, defaultValue?: T): T | undefined {
|
||||
return get(this.config, key, defaultValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user