@therms/rpc-client
A Remote Procedure Call framework for Javascript environments (Node.js & Browser).
npm i @therms/rpc-client
Client
Basic Usage
import { RPCClient } from '@therms/rpc-client'
const rpc = new RPCClient({
hosts: {
http: 'http://localhost:3995/rpc',
websocket: 'ws://localhost:3996',
},
});
const {
code,
data,
message,
success,
} = await rpc.call({
args: {
email: 'me@gmail.com',
password: '1234567',
},
procedure: 'login',
scope: 'auth',
version: '1',
});
Advanced Usage
Create a client instance w/ cache and call deadline/timeout
const rpc = new RPCClient({
cacheMaxAgeMs: 5 * 60 * 1000,
deadlineMs: 5000,
hosts: {
http: 'localhost:3995/rpc',
},
});
Create request interceptor
const rpc = new RPCClient({
hosts: {
http: 'localhost:3995/rpc',
},
requestInterceptor: (request) => {
request.args.count = request.args.count + 1;
return request;
},
});
const { data } = await rpc.call({
args: { count: 1 },
procedure: 'intercept-request',
});
console.log(data.count);
Create response interceptor
const rpc = new RPCClient({
hosts: {
http: 'localhost:3995/rpc',
},
responseInterceptor: (response) => {
response.data.count = 100;
return response;
},
});
const { data } = await rpc.call({
args: { count: 1 },
procedure: 'intercept-request',
});
console.log(data.count);
Create a client instance w/ custom transports
First, setup a custom Transport
:
see the required interface in src/client/Transports/Transport.ts
class CustomHTTPTransport {
isConnected() {
return true;
}
name: 'CustomHTTPTransport';
async sendRequest(call) {
const response = await fetch('localhost:3901/rpc', {
data: call,
}).then((res) => res.json());
return response;
}
setIdentity(identity) {
this.identity = identity;
}
}
Then, use the CustomHTTPTransport
when you create a client instance:
const rpc = new RPCClient({
cacheMaxAgeMs: 5 * 60 * 1000,
deadlineMs: 5000,
transports: {
http: new CustomHTTPTransport(),
},
});