grpc-caller
Advanced tools
Comparing version 0.1.0 to 0.1.1
21
index.js
@@ -11,5 +11,6 @@ const grpc = require('grpc') | ||
* Create client isntance. | ||
* @param {String} host - The host to connect to | ||
* @param {String|Object} proto Path to the protocol buffer definition file or | ||
* the static client constructor object itself | ||
* @param {Object} name - In case of proto path the name of the service as defined in the proto definition. | ||
* @param {String} name - In case of proto path the name of the service as defined in the proto definition. | ||
* @param {Object} options - Options to be passed to the gRPC client constructor | ||
@@ -86,4 +87,18 @@ * @returns {Object} | ||
} | ||
const args = _.compact([metadata, options, fn]) | ||
return v.call(this, ...args) | ||
if (fn) { // normal call | ||
const args = _.compact([metadata, options, fn]) | ||
return v.call(this, ...args) | ||
} else { // dual return promsified call with return { call, res } | ||
const r = {} | ||
const p = new Promise((resolve, reject) => { | ||
const args = _.compact([metadata, options, fn]) | ||
args.push((err, result) => { | ||
if (err) reject(err) | ||
else resolve(result) | ||
}) | ||
r.call = v.call(this, ...args) | ||
}) | ||
r.res = p | ||
return r | ||
} | ||
} | ||
@@ -90,0 +105,0 @@ } else if (v.responseStream && v.requestStream) { |
{ | ||
"name": "grpc-caller", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "An improved Node.js gRPC client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,5 +5,9 @@ # grpc-caller | ||
[![npm version](https://img.shields.io/npm/v/grpc-caller.svg?style=flat-square)](https://www.npmjs.com/package/grpc-caller) | ||
[![build status](https://img.shields.io/travis/bojand/grpc-caller/master.svg?style=flat-square)](https://travis-ci.org/bojand/grpc-caller) | ||
#### Features | ||
* Promisifies request / response calls if no callback is supplied | ||
* Promisifies request stream / response calls if no callback is supplied | ||
* Automatically converts plain javascript object to metadata in calls. | ||
@@ -54,2 +58,55 @@ | ||
#### Improved request stream / response calls | ||
Lets say we have a remote call `writeStuff` that accepts a stream of messages | ||
and returns some result based on processing of the stream input. | ||
Works as standard gRPC client: | ||
```js | ||
const call = client.writeStuff((err, res) => { | ||
if (err) console.error(err) | ||
console.log(res) | ||
}) | ||
// ... write stuff to call | ||
``` | ||
If no callback is provided we promisify the call such that it returns an **object | ||
with two properties** `call` and `res` such that: | ||
* `call` - the standard stream to write to as returned normally by grpc | ||
* `res` - a promise that's resolved / rejected when the call is finished, in place of the callback. | ||
Using destructuring we can do something like: | ||
```js | ||
const { call, res } = client.writeStuff() | ||
res | ||
.then(res => console.log(res)) | ||
.catch(err => console.error(err)) | ||
// ... write stuff to call | ||
``` | ||
This means we can abstract the whole operation into a nicer promise returning | ||
async function to use with `async / await` | ||
```js | ||
async function writeStuff() { | ||
const { call, res } = client.writeStuff() | ||
// ... write stuff to call | ||
return res | ||
} | ||
const res = await writeStuff() | ||
console.log(res) | ||
``` | ||
#### Automatic `Metadata` creation | ||
@@ -87,3 +144,3 @@ | ||
### caller(proto, name, options) ⇒ <code>Object</code> | ||
### caller(host, proto, name, options) ⇒ <code>Object</code> | ||
Create client isntance. | ||
@@ -95,4 +152,5 @@ | ||
| --- | --- | --- | | ||
| host | <code>String</code> | The host to connect to | | ||
| proto | <code>String</code> | <code>Object</code> | Path to the protocol buffer definition file or the static client constructor object itself | | ||
| name | <code>Object</code> | In case of proto path the name of the service as defined in the proto definition. | | ||
| name | <code>String</code> | In case of proto path the name of the service as defined in the proto definition. | | ||
| options | <code>Object</code> | Options to be passed to the gRPC client constructor | | ||
@@ -99,0 +157,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25287
116
178