nice-grpc-client-middleware-deadline
npm i nice-grpc-client-middleware-deadline

nice-grpc-client-middleware-deadline

A TypeScript gRPC library that is nice to you

by deeplay-io

1.1.0 (see all)License:MITTypeScript:Built-In
npm i nice-grpc-client-middleware-deadline
Readme

nice-grpc-client-middleware-deadline npm version

Client middleware for nice-grpc that adds support for setting deadline for a call, after which the call will get cancelled, and a ClientError with status code DEADLINE_EXCEEDED will be thrown.

Installation

npm install nice-grpc-client-middleware-deadline

Usage

import {
  createClientFactory,
  createChannel,
  ClientError,
  Status,
} from 'nice-grpc';
import {deadlineMiddleware} from 'nice-grpc-client-middleware-deadline';
import {addSeconds} from 'date-fns';

const clientFactory = createClientFactory().use(deadlineMiddleware);

const channel = createChannel(address);
const client = clientFactory.create(ExampleService, channel);

try {
  const response = await client.exampleMethod(request, {
    deadline: addSeconds(new Date(), 15),
  });
} catch (error: unknown) {
  if (error instanceof ClientError && error.code === Status.DEADLINE_EXCEEDED) {
    // timed out
  } else {
    throw error;
  }
}

Alternatively, you can specify deadline as a relative offset in milliseconds:

import ms from 'ms';

const response = await client.exampleMethod(request, {
  deadline: ms('15s'),
});

Downloads/wk

584

GitHub Stars

106

LAST COMMIT

4mos ago

MAINTAINERS

1

CONTRIBUTORS

4

OPEN ISSUES

4

OPEN PRs

0
VersionTagPublished
1.1.0
latest
2mos ago
No alternatives found
No tutorials found
Add a tutorial