@hmviet/webrpc-ts
v1.0.16
Published
```sh npm i @hmviet/webrpc-ts ```
Downloads
2
Readme
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();