0
0
NestJSframework~20 mins

Interceptor interface in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this NestJS interceptor do to the response?
Consider this NestJS interceptor code. What will be the output behavior when applied to a controller method that returns a string?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, map } from 'rxjs';

@Injectable()
export class AppendInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map(data => data + '!!!')
    );
  }
}
AThe interceptor blocks the response and returns an empty string.
BThe interceptor appends three exclamation marks to the original string response.
CThe interceptor converts the response string to uppercase.
DThe interceptor throws an error and stops the response.
Attempts:
2 left
💡 Hint
Look at the map operator inside the pipe and what it does to the data.
lifecycle
intermediate
1:30remaining
When is the intercept() method called in a NestJS interceptor?
In the lifecycle of a NestJS request, at what point does the intercept() method of an interceptor run?
AOnly during application startup.
BOnly after the route handler finishes and sends the response.
CBefore the route handler is executed and after the response is sent.
DBefore the route handler executes and can modify the request or response stream.
Attempts:
2 left
💡 Hint
Interceptors wrap around the route handler execution.
📝 Syntax
advanced
2:30remaining
Which option correctly implements a NestJS interceptor that logs the time taken by a request?
Choose the correct interceptor code that logs the time taken to process a request.
A
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, tap } from 'rxjs';

@Injectable()
export class TimingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const start = Date.now();
    return next.handle().pipe(
      tap(() =&gt; console.log(`Request took ${Date.now() - start}ms`))
    );
  }
}
B
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, delay, tap } from 'rxjs';

@Injectable()
export class TimingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const start = Date.now();
    return next.handle().pipe(
      delay(1000),
      tap(() =&gt; console.log(`Request took ${Date.now() - start}ms`))
    );
  }
}
C
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, map } from 'rxjs';

@Injectable()
export class TimingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const start = Date.now();
    return next.handle().pipe(
      map(() =&gt; {
        console.log(`Request took ${Date.now() - start}ms`);
        return;
      })
    );
  }
}
D
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class TimingInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const start = Date.now();
    next.handle().subscribe(() =&gt; console.log(`Request took ${Date.now() - start}ms`));
    return next.handle();
  }
}
Attempts:
2 left
💡 Hint
Use tap to perform side effects without changing the data stream.
🔧 Debug
advanced
2:00remaining
Why does this interceptor cause a runtime error?
Identify the cause of the runtime error in this interceptor code.
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, map } from 'rxjs';

@Injectable()
export class ErrorInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map(data => data.toUpperCase())
    );
  }
}
AThe data might not be a string, so calling toUpperCase() causes a runtime error.
BThe map operator is not imported from rxjs, causing a ReferenceError.
CThe interceptor is missing the @Injectable decorator.
DThe intercept method must return a Promise, not an Observable.
Attempts:
2 left
💡 Hint
Check the type of data before calling string methods.
🧠 Conceptual
expert
1:30remaining
What is the main purpose of the NestJS Interceptor interface?
Choose the best description of what the Interceptor interface allows you to do in NestJS.
ATo define custom decorators for controller methods.
BTo directly access and modify the database before any controller logic runs.
CTo modify incoming requests and outgoing responses by wrapping route handler execution with additional logic.
DTo replace the entire routing system with custom logic for handling HTTP requests.
Attempts:
2 left
💡 Hint
Think about how interceptors wrap around the handler execution.