Complete the code to set the provider scope to default (singleton).
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: [1] }) export class MyService {}
The default provider scope in NestJS is Scope.DEFAULT, which means the provider is a singleton.
Complete the code to set the provider scope to request-scoped.
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: [1] }) export class RequestService {}
Request scope means a new instance is created for each incoming request.
Fix the error in the provider scope declaration to make it transient.
import { Injectable, Scope } from '@nestjs/common'; @Injectable({ scope: [1] }) export class TransientService {}
Transient scope means a new instance is created every time the provider is injected.
Fill both blanks to create a request-scoped provider and inject it into a controller.
import { Injectable, Scope, Controller, Get } from '@nestjs/common'; @Injectable({ scope: [1] }) export class UserService { getUser() { return 'User Data'; } } @Controller('users') export class UserController { constructor(private readonly userService: [2]) {} @Get() getUser() { return this.userService.getUser(); } }
The provider is set to request scope with Scope.REQUEST. The controller injects the UserService to use its methods.
Fill all three blanks to create a transient provider, inject it, and call its method in a controller.
import { Injectable, Scope, Controller, Get } from '@nestjs/common'; @Injectable({ scope: [1] }) export class LoggerService { log(message: string) { return `Log: ${message}`; } } @Controller('logs') export class LogController { constructor(private readonly logger: [2]) {} @Get() getLog() { return this.logger.[3]('Hello'); } }
The provider is transient scoped with Scope.TRANSIENT. The controller injects LoggerService and calls its log method.