GithubHelp home page GithubHelp logo

Comments (6)

bojand avatar bojand commented on May 20, 2024

A proposal:

From an api perspective... we could keep the present api for easy of use, simplicity, and compatibility. In addition we could introduce a new "lower-level" Request and Response API, something similar to:

Fluent Request API:

const caller = require('grpc-caller')
const PROTO_PATH = path.resolve(__dirname, './protos/helloworld.proto')
const client = caller('0.0.0.0:50051', PROTO_PATH, 'Greeter')

const req = new client.Request('sayHello', { name: 'Bob'}) // the method we wish to invoke with the params
  .setOptions({}) // any normal "call" options
  .setMetadata({'request-id': '1234'}) // request call metadata
  .responseMetadata(true) // we care about getting the response metadata, if false we won't wait for and catch the 'metadata' event
  .responseStatus(true) // we care about getting the response status, if false we won't wait for and catch the 'status' event

const res = await req.exec() // promise based, optionally take callback for callback-based API

console.log(res.response) 
// the response payload / "body"
// { message: 'Hello Bob!' }

console.log(res.metadata) 
// the response "header" metadata converted into a map for us
// { foo: 'bar' }

console.log(res.status) 
/*
the response status / trailer metadata: 
{ code: 0,
  details: 'OK',
  metadata: { biz: 'baz' } }
*/

console.log(res.call) // just the call instance itself

We could wrap the above in an simpler functional API:

const res = await client.request('sayHello', { name: 'Bob'}) 
  .setOptions({})
  .setMetadata({'request-id': '1234'})
  .responseMetadata(true)
  .responseStatus(true)
  .exec()

or callback based:

client.request('sayHello', { name: 'Bob'}) 
  .setOptions({})
  .setMetadata({'request-id': '1234'})
  .responseMetadata(true)
  .responseStatus(true)
  .exec((err, res) => {
    console.log(res.response)
})

Underneath the present "simple" API would be implemented using the more verbose API.

from grpc-caller.

yoitsro avatar yoitsro commented on May 20, 2024

Hey @bojand. First, thanks for all your gRPC work with Node. I appreciate it massively!

Second, how's progress coming along with this? I've seen you've got your req_res branch in progress and it's looking good so far!

I need access to the response metadata, hence why I'm here :)

from grpc-caller.

bojand avatar bojand commented on May 20, 2024

Hello, Yea I think I got it mostly working but wasn't really happy with the resulting API or something for some reason and I think I kind of abandoned it... Don't remember now heh. I'll have to return to this soon and review it with a clearer mind now and see what's up.

from grpc-caller.

bojand avatar bojand commented on May 20, 2024

@yoitsro I believe #11 adds the functionality. Please provide feedback on the new API. Note that now you have to explicitly add grpc dependency in your package.json. Thanks!

from grpc-caller.

yoitsro avatar yoitsro commented on May 20, 2024

@bojand Thank you so much for this! I've not had chance to check it out yet, but will do in a few days time. And yeah, it makes sense having grpc required separately. It's similar to what I do with https://github.com/yoitsro/joigoose and mongoose :)

from grpc-caller.

bojand avatar bojand commented on May 20, 2024

#11 is merged and published as 0.5.0.

from grpc-caller.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.