Complete the code to import the ConfigModule with a namespace.
import { ConfigModule } from '@nestjs/config'; @Module({ imports: [ConfigModule.forRoot({ load: [() => ({ api: { url: 'http://localhost' } })], [1]: 'api', })], }) export class AppModule {}
The namespace option scopes the configuration under a specific key.
Complete the code to access a namespaced config value using ConfigService.
constructor(private configService: ConfigService) {}
getApiUrl() {
return this.configService.get<string>([1]);
}When using namespaces, access values with dot notation like 'api.url'.
Fix the error in the code to properly inject a namespaced ConfigService.
import { ConfigService } from '@nestjs/config'; @Injectable() export class ApiService { constructor( private readonly [1]: ConfigService, ) {} }
The injected parameter name should be configService to match the type and NestJS conventions.
Fill both blanks to define and use a namespaced configuration factory.
const apiConfig = () => ({
url: process.env.[1],
key: process.env.[2],
});
ConfigModule.forRoot({
load: [apiConfig],
[3]: 'api',
});The environment variables are API_URL and API_KEY. The config module uses namespace to scope the config.
Fill all three blanks to create a typed ConfigService for a namespace.
interface ApiConfig {
url: string;
key: string;
}
@Injectable()
export class ApiConfigService {
constructor(private configService: ConfigService) {}
get url(): string {
return this.configService.get<[1]>(
[2],
[3],
);
}
}The generic type is ApiConfig. To get the URL, use 'api.url' as the key and provide an empty string as default.