0
0
NestJSframework~10 mins

Why interceptors add cross-cutting logic in NestJS - Test Your Understanding

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

Complete the code to create a basic interceptor class in NestJS.

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

@Injectable()
export class LoggingInterceptor implements [1] {
  intercept(context: any, next: any) {
    console.log('Before...');
    return next.handle();
  }
}
Drag options to blanks, or click blank then click option'
ANestInterceptor
BController
CService
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using Controller or Service instead of NestInterceptor interface.
Forgetting to import the NestInterceptor interface.
2fill in blank
medium

Complete the code to add logic before and after the request handling in an interceptor.

NestJS
intercept(context: any, next: any) {
  console.log('Before...');
  return next.handle().pipe([1](() => console.log('After...')));
}
Drag options to blanks, or click blank then click option'
Afilter
Breduce
Cmap
Dtap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which changes data instead of just running side effects.
Using filter or reduce which are not for side effects.
3fill in blank
hard

Fix the error in the interceptor code by completing the missing import.

NestJS
import { Injectable, NestInterceptor, ExecutionContext, [1] } from '@nestjs/common';
import { Observable } from 'rxjs';
Drag options to blanks, or click blank then click option'
AController
BService
CCallHandler
Dtap
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated classes like Controller or Service.
Forgetting to import CallHandler causing type errors.
4fill in blank
hard

Fill both blanks to create a logging interceptor that measures execution time.

NestJS
intercept(context: ExecutionContext, next: [1]): Observable<any> {
  const now = Date.now();
  return next.handle().pipe([2](() => console.log(`Execution time: ${Date.now() - now}ms`)));
}
Drag options to blanks, or click blank then click option'
ACallHandler
Btap
Cmap
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for next parameter.
Using map instead of tap for logging.
5fill in blank
hard

Fill all three blanks to create a caching interceptor that stores and returns cached responses.

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

@Injectable()
export class CacheInterceptor implements NestInterceptor {
  private cache = new Map();

  intercept(context: ExecutionContext, next: [2]): Observable<any> {
    const key = context.getHandler().name;
    if (this.cache.has(key)) {
      return [3](this.cache.get(key));
    }
    return next.handle().pipe(tap((value) => {
      this.cache.set(key, value);
    }));
  }
Drag options to blanks, or click blank then click option'
ACallHandler
Cof
Dtap
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong types for next parameter.
Returning raw data instead of an observable.