Coder Social home page Coder Social logo

juntaki / protoc-gen-twirp_typescript Goto Github PK

View Code? Open in Web Editor NEW

This project forked from larrymyers/protoc-gen-twirp_typescript

0.0 2.0 0.0 63 KB

Protobuf Plugin for Generating a Twirp Typescript Client

License: MIT License

Makefile 4.14% Go 81.76% TypeScript 14.10%

protoc-gen-twirp_typescript's Introduction

Twirp Typescript Plugin

A protoc plugin for generating a twirp client suitable for browser and node.js projects.

There are two options when generating the client code:

  1. Generate code directly into an existing typescript project with a compilation and build process already in place.
  2. Generate a standalone npm package that can be published with the compiled javascript and typings.

Setup

The protobuf v3 compiler is required. You can get the latest precompiled binary for your system here:

https://github.com/google/protobuf/releases

Twirp Go Server (optional)

While not required for generating the client code, it is required to run the server component of the example.

go get github.com/twitchtv/twirp/protoc-gen-twirp
go get -u github.com/golang/protobuf/protoc-gen-go

Dependencies

Both a Promise and fetch implementation must be provided.

Promise should be a polyfill, while the fetch implementation is directly provided to services as a constructor argument.

Providing the fetch function directly to the service is intentional, since it allows for custom fetch implementations that will automatically handle concerns such as authentication and logging.

IMPORTANT: For browser environments use the following pattern to prevent an error like Failed to execute 'fetch' on 'Window': Illegal invocation.

const haberdasher = new Haberdasher('http://localhost:8080', window.fetch.bind(window));

Suggested polyfills for cross browser and node.js support:

Usage

go get -u go.larrymyers.com/protoc-gen-twirp_typescript
protoc --twirp_typescript_out=./example/ts_client ./example/service.proto

All generated files will be placed relative to the specified output directory for the plugin.
This is different behavior than the twirp Go plugin, which places the files relative to the input proto files.

This decision is intentional, since only client code is generated, and the destination is likely somewhere different than the server code.

Using the Twirp hashberdasher proto:

import 'isomorphic-fetch';
import {Haberdasher} from 'service';

const haberdasher = new Haberdasher('http://localhost:8080', fetch);

haberdasher.makeHat({inches: 10})
    .then((hat) => {
        console.log(hat);
    })
    .catch((err) => {
        console.error(err);
    });

Parameters

The plugin parameters should be added in the same manner as other protoc plugins. Key/value pairs separated by a single equal sign, and multiple parameters comma separated.

package_name

If you'd like to publish the generated code as an npm package then use this parameter to set the name in the package.json file. An index module will be created that will expose all exported interfaces and classes.

protoc --twirp_typescript_out=package_name=haberdasher:./example/ts_client ./example/service.proto

Using the Example

Run the server:

make run
go run cmd/haberdasher/main.go

In a new terminal run the client:

cd example/ts_client
npm install && npm run prepare
node main.js

protoc-gen-twirp_typescript's People

Contributors

larrymyers avatar jonathaningram avatar

Watchers

James Cloos avatar Jumpei Takiyasu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.