jkyberneees / fastify-gateway

A Node.js API gateway that just works!
https://www.npmjs.com/package/k-fastify-gateway
MIT License
108 stars 14 forks source link

Aggregated request? #22

Closed parthibd closed 5 years ago

parthibd commented 5 years ago

How do I make aggregated requests ?

jkyberneees commented 5 years ago

Hi @parthibd, there are many ways to pre/post process requests using this library. Can you describe with an example you particular use case so I can offer you a proper solution?

Thanks.

parthibd commented 5 years ago

Say I have two endpoints in two different services /custdetails & /orders . I want to call /user/1/details . What I really want is to call there two endpoints ,get the response and merge them . Is this possible ?

jkyberneees commented 5 years ago

It is possible, I will get back to you with an example ASAP.

parthibd commented 5 years ago

Thanks a lot . I am planning to add features like loading config from a file ( most probably json/yml?) , adding a service discovery manager .

parthibd commented 5 years ago

Any updates ? :)

jkyberneees commented 5 years ago

Hi @parthibd, all contributions are welcome here. Of course it would be great to have proper examples and details on your proposals before you actually start working on it.

Thanks for collaborating!

parthibd commented 5 years ago

Say I have a local endpoint like this "/user/:id/details"

Now I want to aggregate I over two service endpoints

{
    "path":"/user/:id/details",
    "aggregate":true,
    "aggregations":[
        {
        "service":"service1",
        "endpoint":"/userdetails/:id"
        },
        {
        "service":"service2",
        "endpoint":"/orders/:id"
        }
    ]
}

Is this possible ?

parthibd commented 5 years ago

How should I go about implementing it ?

jkyberneees commented 5 years ago

I think you missed the target property names on the aggregations endpoints ;)

Such developments can be plug-in into the the proxy hooks:

  hooks: {
      async onRequest (req, reply) {
      //   // we can optionally reply from here if required
      //   reply.send('Hello World!')
      //
      //   return true // truthy value returned will abort the request forwarding
      },
      onResponse (req, reply, res) {  
        // do some post-processing here
        // ...
        // forward response to origin client once finished
        reply.send(res) 
      }

      // other options allowed https://github.com/fastify/fastify-reply-from#replyfromsource-opts
    }

Here I also believe that such extensions should not land into the core, build them as separated modules. Afterward those will be properly referenced in here.

Thanks.

parthibd commented 5 years ago

Sorry , my bad :) . How should I go ahead to implementing it ? Your guidance will be immensely helpful.

jkyberneees commented 5 years ago

Create a new module that is able to transform high level routes configurations like you propose into low level k-fastify-gateway compatibility. I can create an initial implementation here if you like, as soon as I get some free time.

Regards

parthibd commented 5 years ago

I got you . So basically you mean the new module should be a layer above the gateway . I get the idea, but can you just give me some sample so that I will know I am going into the right direction ?

Thanks .