Following the NestJS documentation, I created a config.ts
file using js-yaml
:
import { readFileSync } from "fs"
import * as yaml from "js-yaml"
import { join } from "path"
const YAML_CONFIG_FILENAME = "config.yaml"
export type Config = {
app: {
name: string
env: string
}
backend: {
host: string
token: string
}
}
export default () => {
return yaml.load(
readFileSync(join(__dirname, YAML_CONFIG_FILENAME), "utf8"),
) as Config
};
In my module that uses HttpModule, I inject ConfigService as shown below:
import { Module } from "@nestjs/common"
import { ConfigService } from "@nestjs/config"
import { HttpModule } from "@nestjs/axios"
import { Config } from "@/config/config"
@Module({
imports: [
HttpModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const app = configService.get("app")
const backend = configService.get("backend")
return {
baseURL: backend.host,
headers: {
"Content-Type": "application/vnd.api+json",
"Authorization": backend.token,
}
}
},
inject: [ConfigService],
}),
],
controllers: [
NetworkController,
],
providers: [
NetworkService,
],
exports: [],
})
export class NetworkModule {
}
What I would like to do is get the entire configuration object instead of fetching each property one by one:
const config = configService.getAll()
const app = config.app
const backend = config.backend
I checked the ConfigService
types and noticed that it has an internalConfig
field, but it’s set to private.
Is it possible to retrieve all configuration properties at once?