BackendStack21/fast-gateway

Path variables à la express

Closed this issue · 7 comments

In Express environment is common to express routes like /user/:userId/group where userId is a variable that can be used across middlewares.

I would find awesome an interface like

{
    pathRegex: '',
    prefix: '/users/:userId/group',
    target: `http://targetwebsite.com/group?withuser=${userId}`,
}

Would it be possible?

Hi @enricodeleo, unfortunately this feature is not implemented at the moment.
However, you can make use of the onRequest hook to modify the request URL as desired.

Can you check that it works for you?

Yes I think I'll end up using that hook like I did for response transformations.

Actually, I think the my proposed interface is near to be available, at least the prefix part is already working.

In fact, in this scenario

{
    pathRegex: '/:group/:id/products',
    prefix: '/partners',
    prefixRewrite: '/api/v1.0/partners',
    target: API_HOST,
    hooks: {
      rewriteRequestHeaders(req, headers) {
        req.log.debug(req);
        headers['accept-encoding'] = 'identity';
        return headers;
      },
    },
  },

I can see params: { group: 'whatever', id: 'whatever' }, as expected within req.

The only part missing is a function that would parse target url as well. It would be super helpful when using fast-gateway as a migration tool for legacy apis (you could use it in order to listen to request urls in a more "standard" way and then piece by piece migrate each endpoint to its own microservice).

I understand and I agree with you, let me come up with a flexible approach to update the path or request url without going into the hooks...

Stay tuned

Any chances you had some idea on how to address it?

Hi @enricodeleo, excuse me for the delay on this.

What do you think about this proposal?
#61

no reason to excuse, you already gave us a great tool! so thank you for the time you spent into this new feature. It does look like a great api !

Hi @enricodeleo, the new urlRewrite hook is now available in v2.8.0. Thanks for your input!
Have fun ;)