0
0
NestJSframework~10 mins

Configuration namespaces in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the ConfigModule with a namespace.

NestJS
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule.forRoot({
    load: [() => ({ api: { url: 'http://localhost' } })],
    [1]: 'api',
  })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
Anamespace
BenvFilePath
CisGlobal
Dcache
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'envFilePath' instead of 'namespace'.
Confusing 'isGlobal' with namespace.
Trying to use 'cache' for namespacing.
2fill in blank
medium

Complete the code to access a namespaced config value using ConfigService.

NestJS
constructor(private configService: ConfigService) {}

getApiUrl() {
  return this.configService.get<string>([1]);
}
Drag options to blanks, or click blank then click option'
A'url'
B'api.url'
C'apiUrl'
D'config.api.url'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'url' without the namespace prefix.
Using 'apiUrl' which is not the correct key.
Adding extra prefixes like 'config.api.url'.
3fill in blank
hard

Fix the error in the code to properly inject a namespaced ConfigService.

NestJS
import { ConfigService } from '@nestjs/config';

@Injectable()
export class ApiService {
  constructor(
    private readonly [1]: ConfigService,
  ) {}
}
Drag options to blanks, or click blank then click option'
Aconfig
BConfigService
CapiConfigService
DconfigService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'ConfigService' as the parameter name.
Using unrelated names like 'apiConfigService' without proper injection.
Using generic names like 'config' that may cause confusion.
4fill in blank
hard

Fill both blanks to define and use a namespaced configuration factory.

NestJS
const apiConfig = () => ({
  url: process.env.[1],
  key: process.env.[2],
});

ConfigModule.forRoot({
  load: [apiConfig],
  [3]: 'api',
});
Drag options to blanks, or click blank then click option'
AAPI_URL
BAPI_KEY
Cnamespace
DisGlobal
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase env variable names.
Using 'isGlobal' instead of 'namespace'.
Mixing up the order of blanks.
5fill in blank
hard

Fill all three blanks to create a typed ConfigService for a namespace.

NestJS
interface ApiConfig {
  url: string;
  key: string;
}

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

  get url(): string {
    return this.configService.get<[1]>(
      [2],
      [3],
    );
  }
}
Drag options to blanks, or click blank then click option'
AApiConfig
B'api.url'
C''
D'api.key'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api.key' instead of 'api.url' for the URL getter.
Not providing a default value.
Using wrong generic type.