thiagobustamante / typescript-rest

This is a lightweight annotation-based expressjs extension for typescript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trigger @PostProcessor only if request succeded ?

d-theo opened this issue · comments

Hello,
I have a big API that has some side effects (which are tranparent for the client).
For example: "if the client creates a new item: it updates the database and then sends a notification to X service if everything went well".
These cases are more and more frequent and I would like to group these transverse cases into decorators.

Is it possible to do something like this ?

@Path("/")
  @POST
  @PostProcessor(sendNotification) // only if CreateItem went well
  async createItem(item: Item): Promise<Item> {
        return await CreateItem(item);
  }

I would prefer not to use express middleware as it would split the logic of my API in different places.

thanks for your help :-)

In my test, PostProcess is triggered ONLY when the request is succeeded. I use ProstProcessor as HTTP logging. When an exception is raised, the express middleware deals with it/

I found the problem. When the request handler method returns a promise, the post processor is being called before the promise completes. It was fixed in release 3.0.2