Skip to content

🐛 Bug Report: **IdempotencyInterceptor emits a Promise instead of the response due to async callback in RxJS map** #12117

Description

@ANSHSINGH050404

📜 Description

IdempotencyInterceptor uses an async callback inside the RxJS map operator in handleNewRequest().

return next.handle().pipe(
  map(async (response) => {
    await this.setCache(...);

    return response;
  }),
);

Since async functions always return a Promise, the map operator emits Promise<Response> instead of Response. RxJS map does not await Promises.

As a result, downstream interceptors (such as ResponseInterceptor) receive a Promise rather than the resolved response and may wrap or serialize it incorrectly (e.g. { data: Promise }).

The callback performs asynchronous work (await this.setCache(...)), so a flattening operator such as mergeMap should be used instead of map.


👟 Reproduction steps

  1. Start the API with idempotency enabled.

  2. Send a POST or PATCH request containing a valid Idempotency-Key header.

  3. Ensure the request reaches IdempotencyInterceptor.handleNewRequest().

  4. Observe that the interceptor executes:

    map(async (response) => ...)
  5. Inspect the value emitted by the observable or the response received by downstream interceptors.

👍 Expected behavior

The interceptor should emit the resolved response object after caching it.

The observable should emit:

Observable<Response>

Asynchronous cache writes should be flattened using mergeMap, concatMap, or another appropriate RxJS flattening operator.


👎 Actual Behavior with Screenshots

The interceptor emits:

Observable<Promise<Response>>

instead of:

Observable<Response>

This can cause downstream interceptors to receive a Promise instead of the actual response, resulting in incorrect response wrapping or serialization.


Novu version

Novu SaaS

npm version

NA

node version

NA

📃 Provide any additional context for the Bug.

NA

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to submit PR?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions