0
0
NestJSframework~10 mins

Interceptor interface 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 Interceptor interface from NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AModule
BController
CNestInterceptor
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Controller instead of NestInterceptor
Using Injectable instead of NestInterceptor
Forgetting to import NestInterceptor
2fill in blank
medium

Complete the code to implement the Interceptor interface in a class.

NestJS
export class LoggingInterceptor implements [1] { }
Drag options to blanks, or click blank then click option'
APipeTransform
BNestInterceptor
CExceptionFilter
DGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Implementing PipeTransform instead of NestInterceptor
Implementing Guard or ExceptionFilter by mistake
3fill in blank
hard

Fix the error in the intercept method signature by completing the parameter type.

NestJS
intercept(context: [1], next: CallHandler): Observable<any> { return next.handle(); }
Drag options to blanks, or click blank then click option'
AResponse
BHttpContext
CRequest
DExecutionContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of ExecutionContext
Using a non-existent HttpContext type
4fill in blank
hard

Fill both blanks to correctly import and use CallHandler in the interceptor.

NestJS
import { [1], CallHandler } from '@nestjs/common';

intercept(context: ExecutionContext, next: [2]): Observable<any> {
  return next.handle();
}
Drag options to blanks, or click blank then click option'
AExecutionContext
BCallHandler
CObservable
DNestInterceptor
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong types or missing CallHandler
Using wrong parameter types in intercept method
5fill in blank
hard

Fill all three blanks to create a basic interceptor that logs before and after the request.

NestJS
import { Injectable, [1], ExecutionContext, CallHandler } from '@nestjs/common';
import { [2] } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements [3] {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    console.log('Before...');
    const now = Date.now();
    return next.handle().pipe(
      tap(() => console.log(`After... ${Date.now() - now}ms`))
    );
  }
}
Drag options to blanks, or click blank then click option'
AInjectable
Btap
CNestInterceptor
DObservable
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing tap operator with Observable type
Forgetting to add @Injectable decorator
Not implementing NestInterceptor interface