villadora / express-http-proxy

Proxy middleware for express/connect
Other
1.22k stars 236 forks source link

Major usability issue without `decorateRequest` #507

Closed sscarduzio closed 1 year ago

sscarduzio commented 1 year ago

My task requires to mutate both request.path and request.body to a value that is function of the original request path.

In the past, there was a single callback decorateRequest to modify everything at once, now we have two (proxyReqOptDecorator and proxyReqBodyDecorator).

This makes everything extremely more complex because:

Removing decorateRequest has a big impact on usability. What was the strong argument to remove it? I could not find any from the PR comments.

Please advise what's the best course of action?

monkpow commented 1 year ago

@sscarduzio Thanks for your interest in this library! I understand how frustrating it can be when libraries change their interfaces.

In general, each of the workflow steps is designed to limit the surface area of change to the expected properties for this operation. Separately, the userReq is passed (but unmodifiable) to each step in the workflow, so the question of how to compute the body from the original url, for instance, sounds like the library might already provide the information you need.

The final request about how to park the body value in a temporary variable I would refer to your own code to solve this problem ( e.g. create a small dictionary for state, or a function that computes state from the current requisition object) based on the information I have on hand, but feel free to share more details if you think the library should accommodate for this.

     // somewhere else in your file
     const computePageFromOriginalUrl = (userReq) {
         return howeverYouComputeThePageFromTheOriginalUrl(userReq)
     }

      app.use(proxy('localhost:12345', {
        proxyReqBodyDecorator: function(bodyContent, userReq) {
             bodyContent += howeverYouComputeThePageFromTheOriginalUrl(userReq)
             return bodyContent;
        }
      }));