A simple typescript client for Twirp protobuf services based on Axios. Designed for use with the service wrappers generated by ts-proto.
Install using npm:
npm install --save twirp-rpc-client
Generate your service code using the protobuf compiler and ts-proto.
For example: protoc --plugin=node_modules/ts-proto/protoc-gen-ts_proto example/service.proto -I./example --ts_proto_out=./example
Then, import the ClientImpl
generated by ts-proto and configure the twirpProtobufClient
with the base url of your twirp service.
import {HaberdasherClientImpl, Hat} from './generated/service'
import twirpProtobufClient from "../src";
const haberdasherClient = new HaberdasherClientImpl(twirpProtobufClient({
url: "https://localhost:3000/twirp"
}))
haberdasherClient.MakeHat({inches: 12})
.then((hat:Hat) => console.log(hat))
.catch(error => console.log(error))
For more details, see the docs regarding ts-proto service generation.
TwirpClientProps {
url: string; // base url of the twirp service
headers?: object; // additional headers to add to the request e.g. { "x-custom-header": "header-value" }
timeout?: number; // timeout in milliseconds
auth?: { // basic auth helper
username: string;
password: string;
};
}