# grpc-helper

> gRPC helper, an improved gRPC client for grpc-node

Latest version **0.5.1** (published 2019-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install grpc-helper
pnpm add grpc-helper
yarn add grpc-helper
bun add grpc-helper
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2019-11-18 |
| First published | 2018-08-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 8 |
| Unpacked size | 78.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | Xu Zhipei |
| Maintainers | xizhibei |
| Keywords | grpc, helper, promise, load-balancer, typescript |

## Links

- npm: https://www.npmjs.com/package/grpc-helper
- Repository: https://github.com/xizhibei/grpc-helper
- Homepage: https://github.com/xizhibei/grpc-helper#readme
- Issues: https://github.com/xizhibei/grpc-helper/issues
- npm.io page: https://npm.io/package/grpc-helper

## Dependencies (8)

- [grpc](https://npm.io/package/grpc.md) ^1.14.2
- [debug](https://npm.io/package/debug.md) ^3.1.0
- [brakes](https://npm.io/package/brakes.md) ^2.6.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.0
- [async-retry](https://npm.io/package/async-retry.md) ^1.2.3
- [prom-client](https://npm.io/package/prom-client.md) ^10.2.3
- [@grpc/proto-loader](https://npm.io/package/@grpc/proto-loader.md) ^0.3.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.5.1 (latest) — 2019-11-18
- 0.5.0 — 2019-05-28
- 0.4.1 — 2018-12-18
- 0.4.0 — 2018-09-13
- 0.3.0 — 2018-09-11
- 0.2.3 — 2018-09-08
- 0.2.2 — 2018-09-06
- 0.2.0 — 2018-09-06
- 0.1.0 — 2018-09-05
- 0.0.0 — 2018-08-26

## README

# gRPC helper

gRPC helper is an improved gRPC client with lots of helpful features.

[![Build Status](https://travis-ci.org/xizhibei/grpc-helper.svg?branch=master&style=flat)](https://travis-ci.org/xizhibei/grpc-helper)
[![Coverage Status](https://coveralls.io/repos/github/xizhibei/grpc-helper/badge.svg?branch=master)](https://coveralls.io/github/xizhibei/grpc-helper?branch=master)
[![npm version](https://badge.fury.io/js/grpc-helper.svg?style=flat)](http://badge.fury.io/js/grpc-helper)
[![Dependency Status](https://img.shields.io/david/xizhibei/grpc-helper.svg?style=flat)](https://david-dm.org/xizhibei/grpc-helper)
[![npm](https://img.shields.io/npm/l/grpc-helper.svg)](https://github.com/xizhibei/grpc-helper/blob/master/LICENSE)

### Getting Started

### Installing

```bash
npm i grpc-helper --save
```

or

```bash
yarn add grpc-helper
```

### Features

- Promised unary & client stream call
- Client Load balance
- Service health checking
- Service discovery (static, dns srv)
- Circuit breaker based on [Brakes](https://github.com/awolden/brakes)
- Retry based on [async-retry](https://github.com/zeit/async-retry)
- Metrics for [prometheus](https://prometheus.io/) based on [prom-client](https://github.com/siimon/prom-client)
- Highly custom [options](src/common.ts)

### Usage

#### DNS Service discovery
```ts
const helper = new GRPCHelper({
  packageName: 'helloworld',
  serviceName: 'Greeter',
  protoPath: path.resolve(__dirname, './fixtures/hello.proto'),
  // intervalMs will determine how frequent the resolver lookup the records
  sdUri: 'dns://_grpc._tcp.greeter?intervalMs=5000',
});

await helper.waitForReady();

const res = await helper.SayHello({
  name: 'foo',
});
```

#### Static Service discovery
```ts
const helper = new GRPCHelper({
  packageName: 'helloworld',
  serviceName: 'Greeter',
  protoPath: path.resolve(__dirname, './fixtures/hello.proto'),
  sdUri: 'static://localhost:50051,localhost:50052,localhost:50053',
});

await helper.waitForReady();

const res = await helper.SayHello({
  name: 'foo',
});
```

#### Resolve with full response
```ts
const helper = new GRPCHelper({
  packageName: 'helloworld',
  serviceName: 'Greeter',
  protoPath: path.resolve(__dirname, './fixtures/hello.proto'),
  sdUri: 'static://localhost:50051',
  resolveFullResponse: true,
});

await helper.waitForReady();

const { message, peer, status, metadata } = await helper.SayHello({
  name: 'foo',
});
```


#### Client stream call
```ts
const stream = new stream.PassThrough({ objectMode: true });

const promise = helper.SayMultiHello(stream);

stream.write({
  name: 'foo1',
});

stream.write({
  name: 'foo2',
});

stream.write({
  name: 'foo3',
});

stream.end();

const result = await promise; // { message: 'hello foo1,foo2,foo3' }
```

#### Retry
```ts
const helper = new GRPCHelper({
  packageName: 'helloworld',
  serviceName: 'Greeter',
  protoPath: path.resolve(__dirname, './fixtures/hello.proto'),
  sdUri: 'static://localhost:50051',
  retryOpts: {
    enable: true,
    retries: 5,
    bailError(err, attempt) {
      // Just for example !!! It will not retry when code is 2
      return err.code === 2;
    },
  },
});

await helper.waitForReady();

await helper.SayHello({
  name: 'foo',
});
```


#### More

Please take a look at the [test](test/) folder for more examples.

### TODO

- [x] Better api
- [x] Doc
- [x] Test code
- [x] Retry on lb level when error
- [ ] Auto load proto when only one service available
- [ ] Consul/etcd/zk service discovery


### License
This project is licensed under the MIT License - see the LICENSE file for details

---
_Source: https://npm.io/package/grpc-helper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
