twirp-rpc-client

Typescript twirp client built for use with ts-proto

Usage no npm install needed!

<script type="module">
  import twirpRpcClient from 'https://cdn.skypack.dev/twirp-rpc-client';
</script>

README

Twirp Rpc Client

Build Coverage Status npm

A simple typescript client for Twirp protobuf services based on Axios. Designed for use with the service wrappers generated by ts-proto.

Usage

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.

Configuration Options

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;
  };
}

Current Limitations

  • Only supports protocol buffer serialization from twirp services. This is because ts-proto currently only generates protobuf service client implementations.