A custom provider in NestJS is a way to define how a dependency is created or supplied, allowing you to control the instance or value injected into your classes.
Click to reveal answer
beginner
How do you define a custom provider using the 'useValue' syntax?
You define a custom provider by specifying an object with a 'provide' key for the token and a 'useValue' key for the fixed value to inject, like { provide: 'TOKEN', useValue: someValue }.
Click to reveal answer
intermediate
Explain the difference between 'useClass' and 'useFactory' in custom providers.
'useClass' tells NestJS to instantiate a specific class when injecting, while 'useFactory' lets you provide a function that returns the value or instance, allowing dynamic or async creation.
Click to reveal answer
intermediate
What is the purpose of the 'inject' array in a custom provider using 'useFactory'?
The 'inject' array lists the dependencies that NestJS should pass as arguments to the factory function, enabling you to use other providers inside your factory.
Click to reveal answer
intermediate
Why would you use a custom provider instead of a regular service class in NestJS?
Custom providers let you control exactly how and what is injected, useful for injecting constants, third-party libraries, or creating dynamic or mocked instances for testing.
Click to reveal answer
Which property do you use to provide a fixed value in a custom provider?
AuseFactory
BuseClass
CuseValue
DuseExisting
✗ Incorrect
The 'useValue' property is used to provide a fixed value directly as the provider.
What does the 'useClass' property do in a custom provider?
ASpecifies a class to instantiate
BReuses an existing provider
CDefines a factory function
DReturns a fixed value
✗ Incorrect
'useClass' tells NestJS to instantiate the given class when injecting.
How do you inject dependencies into a factory function in a custom provider?
AUsing 'useClass'
BUsing the 'inject' array
CUsing 'useValue'
DUsing 'provide'
✗ Incorrect
The 'inject' array lists dependencies to pass as arguments to the factory function.
Which custom provider type allows asynchronous creation of the injected value?
AuseFactory
BuseClass
CuseValue
DuseExisting
✗ Incorrect
'useFactory' can return a Promise, enabling async creation.
What is a common use case for custom providers in NestJS?
ATo create UI components
BTo style HTML elements
CTo write SQL queries
DTo control dependency injection behavior
✗ Incorrect
Custom providers let you control how dependencies are created and injected.
Describe how to create a custom provider in NestJS using 'useFactory' and explain when you might use it.
Think about when you need to create a value dynamically or asynchronously.
You got /4 concepts.
Explain the differences between 'useValue', 'useClass', and 'useFactory' in NestJS custom providers.
Consider how each method controls what gets injected.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a custom provider in NestJS?
easy
A. To manage database connections automatically
B. To define routes for HTTP requests
C. To control how NestJS creates or finds a service or value
D. To handle user authentication and authorization
Solution
Step 1: Understand the role of providers in NestJS
Providers are responsible for creating and managing services or values that can be injected into other parts of the app.
Step 2: Identify what custom providers do
Custom providers let you control exactly how NestJS creates or finds these services or values, making your app more flexible.
Final Answer:
To control how NestJS creates or finds a service or value -> Option C
Quick Check:
Custom provider purpose = control creation/finding [OK]
Hint: Custom providers control service creation or retrieval [OK]
Common Mistakes:
Confusing providers with routing or middleware
Thinking providers manage database or auth directly
Assuming providers only register classes, not values or factories
2. Which of the following is the correct syntax to provide a custom value provider in a NestJS module?
easy
A. { provide: 'API_URL', useValue: 'https://api.example.com' }
B. { provide: 'API_URL', useClass: 'https://api.example.com' }
C. { provide: 'API_URL', useFactory: 'https://api.example.com' }
D. { provide: 'API_URL', useExisting: 'https://api.example.com' }
Solution
Step 1: Recall the syntax for value providers
Value providers use the useValue key to assign a fixed value to a token.
Step 2: Check each option's correctness
{ provide: 'API_URL', useValue: 'https://api.example.com' } correctly uses useValue with a string value. Other options misuse useClass, useFactory, or useExisting which expect different types.
Final Answer:
{ provide: 'API_URL', useValue: 'https://api.example.com' } -> Option A
Hint: Always register providers in module's providers array [OK]
Common Mistakes:
Assuming useFactory must be async
Thinking tokens must be classes only
Believing useFactory can't return objects
5. You want to create a custom provider that returns different database connection strings based on environment variables. Which approach correctly implements this using a factory provider in NestJS?
To choose a value based on environment variables at runtime, a factory provider is ideal because it runs code to decide the value.
Step 2: Evaluate each option
{ provide: 'DB_CONNECTION', useFactory: () => process.env.NODE_ENV === 'prod' ? 'prod-db' : 'dev-db' } uses useFactory with a function that returns the correct string based on process.env.NODE_ENV. { provide: 'DB_CONNECTION', useValue: process.env.NODE_ENV === 'prod' ? 'prod-db' : 'dev-db' } uses useValue which is static at module load time. Options C and D misuse useClass and useExisting with strings or conditions incorrectly.