GithubHelp home page GithubHelp logo

seanpfeifer / twirp-gen Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 2.0 35 KB

Protobuf client code generators for Twirp services (https://github.com/twitchtv/twirp)

License: MIT License

JavaScript 2.28% Go 10.66% C# 77.57% TypeScript 9.49%
twirp rpc protobuf javascript csharp code-generation

twirp-gen's Introduction

twirp-gen

Simple Protobuf code generators specifically for Twirp services.

This exists because other code generators either required external dependencies or were too complex for my needs.

Installing

# For TypeScript generation
go install github.com/seanpfeifer/twirp-gen/cmd/protoc-gen-twirpts@latest

# For C# generation
go install github.com/seanpfeifer/twirp-gen/cmd/protoc-gen-twirpcs@latest

# For JavaScript generation
go install github.com/seanpfeifer/twirp-gen/cmd/protoc-gen-twirpjs@latest

Ensure your ~/go/bin (Linux) or %USERPROFILE%/go/bin (Windows) are on your PATH.

Usage

# Typical Twirp usage to generate methods from the "examples" dir into "./examples_gen/generated.js"
# For TypeScript
protoc -I ./examples --twirpts_out=./examples_gen/ ./examples/*.proto
# For JavaScript
protoc -I ./examples --twirpjs_out=./examples_gen/ ./examples/*.proto

# If you use twirp.WithServerPathPrefix(), eg `twirp.WithServerPathPrefix("/rpc")`, you can specify the
# prefix with the "pathPrefix" flag
protoc -I ./examples --twirpjs_out=pathPrefix=/rpc:./examples_gen/ ./examples/*.proto

# C# is the same as above two examples, but uses `--twirpcs_out`
protoc -I ./examples --twirpcs_out=./examples_gen/ ./examples/*.proto
protoc -I ./examples --twirpcs_out=pathPrefix=/rpc:./examples_gen/ ./examples/*.proto

# For TypeScript, you can also add `protoCase=true` to generate field names in the same case as the proto file
protoc -I ./examples --twirpts_out=pathPrefix=/rpc,protoCase=true:./examples_gen/ ./examples/*.proto

Generator Options

Option Language Description
pathPrefix TypeScript, JavaScript, C# The path prefix to use for the Twirp service. This is useful if you use twirp.WithServerPathPrefix().
protoCase TypeScript If true, field names will be generated in the same case as the proto file. If false, field names will be generated in camelCase. This is useful if you DO NOT use twirp.WithServerJSONCamelCaseNames().

TypeScript

This is an extremely simple generator for TypeScript, which communicates with JSON only. Assumptions:

  • You are OK with the fetch() browser API
    • That is, you don't need to worry about older browsers that may not support it
  • Uses camelCase fields by default (ie, you have the option twirp.WithServerJSONCamelCaseNames(true) set)
    • If you don't want fields changed to camelCase, use protoCase=true in your twirpts_out options, eg if you want to use the same case as the proto file and to also generate with the path prefix /rpc:
      • --twirpts_out=pathPrefix=/rpc,protoCase=true:./

C#

This is an extremely simple generator for C#. This client makes requests using binary serialized Protobuf.

On error, the generated functions will throw a GeneratedAPI.Exception.

JavaScript

Another extremely simple generator, but for JavaScript, which communicates with JSON only. It makes a few assumptions to keep things simple:

  • Your Twirp server has the option twirp.WithServerJSONCamelCaseNames(true) set
    • This client uses proto3 JSON serialization instead of the snake-case default in Twirp
  • You are OK with the fetch() browser API
    • That is, you don't need to worry about older browsers that may not support it

Unity Example

Note that this example does not deal with errors. It is just a simple example of how to call the generated functions.

using System.Net.Http;
using UnityEngine;
using Google.Protobuf; // For ByteString in this example

public class APICaller : MonoBehaviour {
  public const string SERVER_ADDRESS = "http://localhost:8080/";

  static readonly HttpClient client = new HttpClient();
  static APICaller() {
    client.BaseAddress = new System.Uri(SERVER_ADDRESS);
    client.DefaultRequestHeaders.Accept.Add(GeneratedAPI.CONTENT_TYPE_PROTOBUF);
  }

  // Start is called before the first frame update
  async void Start() {
    var checkoutResult = await GeneratedAPI.CreateCheckoutSession(client, new Shop.CheckoutRequest { ItemId = "item_cool_stuff" });
    Debug.Log(checkoutResult);

    var logoutResult = await GeneratedAPI.Logout(client, new Account.LogoutRequest { AccountId = 42, Token = ByteString.CopyFrom(new byte[]{1,2,3,4,5}) });
    Debug.Log(logoutResult);
  }
}

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.