Bird
0
0

You want to create an interceptor that adds a timestamp to every response object. Which code snippet correctly implements this?

hard📝 component behavior Q8 of 15
NestJS - Interceptors
You want to create an interceptor that adds a timestamp to every response object. Which code snippet correctly implements this?
Aintercept(context, next) { return next.handle().pipe(map(data => ({ ...data, timestamp: Date.now() }))); }
Bintercept(context, next) { return next.handle().pipe(filter(data => data.timestamp = Date.now())); }
Cintercept(context, next) { return next.handle().pipe(map(data => data.timestamp = Date.now())); }
Dintercept(context, next) { return next.handle().pipe(reduce((acc, data) => ({ ...acc, timestamp: Date.now() }))); }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct RxJS operator for transformation

    map is used to transform each emitted value by adding timestamp.
  2. Step 2: Verify correct object spreading and timestamp addition

    intercept(context, next) { return next.handle().pipe(map(data => ({ ...data, timestamp: Date.now() }))); } spreads original data and adds timestamp property correctly.
  3. Final Answer:

    Use map with object spread and timestamp property -> Option A
  4. Quick Check:

    map + spread + timestamp = intercept(context, next) { return next.handle().pipe(map(data => ({ ...data, timestamp: Date.now() }))); } [OK]
Quick Trick: Use map and spread operator to add properties safely [OK]
Common Mistakes:
  • Using filter which filters instead of transforms
  • Assigning timestamp inside map without returning object
  • Using reduce incorrectly for this purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes