1.0.16 • Published 1 year ago

@hmviet/webrpc-ts v1.0.16

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Install package

npm i @hmviet/webrpc-ts

A. Docs

grpcClient.unary

unary(
  url: string,
  data: RpcMetaData,
  options?: UnaryOptions
): Promise<RpcResponse>;

grpcClient.clientStream

clientStream(url: string, options?: StreamOptions): Stream;

grpcClient.serverStream

serverStream(
    url: string,
    data: RpcMetaData,
    options?: StreamOptions
  ): Stream;

grpcClient.stream

stream(url: string, options?: StreamOptions): Stream;

B. Sample code

Init Client

const clientOptions: ClientRpcOptions = {
  host: "https://localhost.com",
  // custom websocket end point.
  streamEndPoint: "/examples.time.Time/ws",
};
const grpcClient = new Client(clientOptions);

1. Fetch Unary

//options
const options: UnaryOptions = {
  // false: fetch via GET
  // true : fetch via POST
  query: false,
  // custom headers
  headers: {
    // your headers
  },
};
// req
const data: RpcMetaData = newData();
// make request
const res = await grpcClient.unary("/examples.time.Time/Unary", data, options);

2. Client Stream

const options: StreamOptions = {
  // custom headers
  headers: {
    // your headers
  },
};
const url = "/examples.time.Time/ClientStream";
const stream = grpcClient.clientStream(url, options);
// send
const data: RpcMetaData = newData();
// do send message
tream.send(data);
const data2: RpcMetaData = newData();
stream.send(data2);
// close send
stream.closeSend();
// wait response
const res = await stream.receive();

3. Server Stream

const url = "/examples.time.Time/ServerStream";
const data: RpcMetaData = newData();
const options: StreamOptions = {
  // custom headers
  headers: {
    // your headers
  },
};
const stream = grpcClient.serverStream(url, data, options);
// wait receive data
const res = await stream.receive();

4. Bidi Stream

const options: StreamOptions = {
  headers: {},
  onResponse: (message: RpcMessage) => {
    // streaming response
    console.log("onResponse", message);
  },
};
const url = "/examples.time.Time/Stream";
const stream = grpcClient.stream(url, options);
// send
const data: RpcMetaData = newData();
// do send message
tream.send(data);
const data2: RpcMetaData = newData();
stream.send(data2);
// close send
stream.closeSend();

Screenshot 2022-12-29 at 07 57 51

1.0.16

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago