1.1.3 • Published 4 years ago

ts-twirp v1.1.3

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
4 years ago

ts-twirp

Build Status

ts-twirp is a TypeScript code generator for Twirp servers and clients.

The client portion of this library is nascent.

Make a Twirp Server

Install ts-twirp.

npm install --save-dev ts-twirp

ts-twirp takes an opinionated approach to file organization. Each protobuf file lives in its own directory and ts-twirp generates sibling files that you can .gitignore.

Place your server's protobuf file in its own directory.

/src
  /twirp
    service.proto

Run ts-twirp on service.proto.

npx ts-twirp src/twirp/service.proto

This generates sibling files in the same directory.

/src
  /twirp
    index.ts # Exports the code you'll use to implement your server
    service.pb.d.ts # Protobuf types generated from your service.proto file
    service.pb.ts # Runtime protobuf serialization/deserialization code
    service.proto # The service protobuf definition
    server.ts # Runtime TypeScript server code

Use src/twirp/index.ts as the entry point to the service types and runtime code.

// ./src/server.ts
import http from 'http';
import { createHaberdasherHandler } from './twirp';

const handler = createHaberdasherHandler({
  async makeHat(size) {
    return ({
      color: 'red',
      name: 'fancy hat',
      size: size.inches
    });
  }
});

http.createServer(handler).listen(8000);

Make a Twirp Client

Running the generator will create both a protobuf and a JSON client.

Using the protobuf client:

import { createHaberdasherProtobufClient } from './twirp';

async function run() {
  const haberdasher = createHaberdasherProtobufClient({
    host: 'localhost',
    port: 8000,
  });

  const hat = await haberdasher.makeHat({
    inches: 42
  });

  console.log(hat);
}

run();

As you might expect using the JSON client is nearly identical.

import { createHaberdasherJSONClient } from './twirp';

async function run() {
  const haberdasher = createHaberdasherJSONClient({
    host: 'localhost',
    port: 8000,
  });

  const hat = await haberdasher.makeHat({
    inches: 42
  });

  console.log(hat);
}

run();
1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.1

5 years ago