Javascript NestJS和typescript配置强类型?

Javascript NestJS和typescript配置强类型?,javascript,typescript,nestjs,Javascript,Typescript,Nestjs,我的应用程序中有一个主配置,它通过环境变量(process.env)表示。如何将它与下一个JS作为一个对象公开?在下面的示例中,我可以通过键获取值。但是我传递了一个字符串,这里没有输入脚本 从'@nestjs/common'导入{Module}; 从“./app.controller”导入{AppController}; 从“/app.service”导入{AppService}; 从'@nestjs/config'导入{ConfigModule}; 从“./interfaces/Config”

我的应用程序中有一个主配置,它通过环境变量(process.env)表示。如何将它与下一个JS作为一个对象公开?在下面的示例中,我可以通过键获取值。但是我传递了一个字符串,这里没有输入脚本

从'@nestjs/common'导入{Module};
从“./app.controller”导入{AppController};
从“/app.service”导入{AppService};
从'@nestjs/config'导入{ConfigModule};
从“./interfaces/Config”导入{envVarsValidator};
@模块({
进口:[
ConfigModule.forRoot({
isGlobal:没错,
验证方案:envVarsValidator,
})
],
控制器:[AppController],
提供者:[AppService],
})
导出类AppModule{}
从'@nestjs/common'导入{Injectable};
从“@nestjs/config”导入{ConfigService};
@可注射()
导出类应用程序服务{
构造函数(私有configService:configService){}
getHello():字符串{
返回这个.configService.get('hello');//不是我需要的;
}
}
我需要的伪代码:


导出类服务{
构造函数(私有configService:configService){}
someLogic():任何{
const port=this.configService.config.port;
//我需要的是一个主配置对象,通过typescript在该对象上提供突出显示属性
}
}
请参见

e、 g

config/database.config.ts JS

export default registerAs('database', () => ({
  host: process.env.DATABASE_HOST,
  port: process.env.DATABASE_PORT || 5432
}));
并注入类型化对象

constructor(
  @Inject(databaseConfig.KEY)
  private dbConfig: ConfigType<typeof databaseConfig>,
) {}
构造函数(
@注入(databaseConfig.KEY)
私有dbConfig:ConfigType,
) {}

我采用了一种略为简单的方法,用我自己的ConfigService包装nest的ConfigService,并将其作为模块的一部分导出(以及我在应用程序中经常使用的其他常见服务),如下所示:

import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'

@Injectable()
export class ConfigurationService {
  constructor(private configService: ConfigService) {}

  get MY_CONFIG_STRING() {
    // this reads the value from your .env
    return this.configService.get<string>('MY_CONFIG_STRING')!
  }

  get MY_CONFIG_Number() {
    // this reads the value from your .env
    return this.configService.get<number>('MY_CONFIG_NUMBER')!
  }
}
只需确保在ConfigurationService模块中引导Nest的配置服务,如下所示:

import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { ConfigurationService } from './configuration.service'

@Module({
  imports: [ConfigModule.forRoot()],
  providers: [
    ConfigService,
    ConfigurationService,
  ],
  exports: [ConfigurationService],
})
export class CommonModule {}
我们开发了这个模块来解决这个问题

首先,您需要创建配置模型:

// config.ts
export class Config {
    @IsString()
    public readonly host!: string;

    @IsNumber()
    public readonly port!: number;
}
AppModule
中注册
TypedConfigModule

// app.module.ts
import { Module } from '@nestjs/common';
import { TypedConfigModule, fileLoader } from 'nest-typed-config';
import { AppService } from './app.service';
import { Config } from './config';

@Module({
    imports: [
        TypedConfigModule.forRoot({
            schema: Config,
            load: fileLoader(),
            isGlobal: true,
        }),
    ],
    providers: [AppService],
})
export class AppModule {}
就这样!您现在可以在任何服务中插入
Config

// app.service.ts
import { Config } from './config';

@Injectable()
export class AppService {
    constructor(private readonly config: Config) {}

    show() {
        // Full TypeScript support
        console.log(`http://${this.config.host}:${this.config.port}`)
    }
}
// app.service.ts
import { Config } from './config';

@Injectable()
export class AppService {
    constructor(private readonly config: Config) {}

    show() {
        // Full TypeScript support
        console.log(`http://${this.config.host}:${this.config.port}`)
    }
}