improbable-eng/ts-protoc-gen

Name: ts-protoc-gen

Owner: Improbable Engineering

Description: Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.

Created: 2017-03-13 11:18:31.0

Updated: 2018-05-24 07:06:48.0

Pushed: 2018-05-24 12:08:34.0

Homepage:

Size: 260

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Master Build NPM NPM Apache 2.0 License

ts-protoc-gen

Protoc Plugin for generating TypeScript Declarations

This repository contains a protoc plugin that generates TypeScript declarations (.d.ts files) that match the JavaScript output of protoc --js_out=import_style=commonjs,binary. This plugin can also output service definitions as both .js and .d.ts files in the structure required by grpc-web.

This plugin is tested and written using TypeScript 2.7.

Installation

As a prerequisite, download or install protoc (the protocol buffer compiler) for your platform from the github releases page or via a package manager (ie: brew, apt).

For the latest stable version of the ts-protoc-gen plugin:

install ts-protoc-gen

For our latest build straight from master:

install ts-protoc-gen@next
Contributing

Contributions are welcome! Please refer to CONTRIBUTING.md for more information.

Usage

As mentioned above, this plugin for protoc serves two purposes:

  1. Generating TypeScript Definitions for CommonJS modules generated by protoc
  2. Generating gRPC Service Stubs for use with grpc-web.
Generating TypeScript Definitions for CommonJS modules generated by protoc

By default, protoc will generate ES5 code when the --js_out flag is used (see javascript compiler documentation). You have the choice of two module syntaxes, CommonJS or closure. This plugin (ts-protoc-gen) can be used to generate Typescript definition files (.d.ts) to provide type hints for CommonJS modules only.

To generate TypeScript definitions you must first configure protoc to use this plugin and then specify where you want the TypeScript definitions to be written to using the --ts_out flag.

th to this plugin
OC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

rectory to write generated code to (.js and .d.ts files) 
DIR="./generated"

oc \
--plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
--js_out="import_style=commonjs,binary:${OUT_DIR}" \
--ts_out="${OUT_DIR}" \
users.proto base.proto

In the above example, the generated folder will contain both .js and .d.ts files which you can reference in your TypeScript project to get full type completion and make use of ES6-style import statements, eg:

rt { MyMessage } from "../generated/users_pb";

t msg = new MyMessage();
setName("John Doe");
Generating gRPC Service Stubs for use with grpc-web

gRPC is a framework that enables client and server applications to communicate transparently, and makes it easier to build connected systems.

grpc-web is a comparability layer on both the server and client-side which allows gRPC to function natively in modern web-browsers.

To generate client-side service stubs from your protobuf files you must configure ts-protoc-gen to emit service definitions by passing the service=true param to the --ts_out flag, eg:

th to this plugin
OC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

rectory to write generated code to (.js and .d.ts files) 
DIR="./generated"

oc \
--plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
--js_out="import_style=commonjs,binary:${OUT_DIR}" \
--ts_out="service=true:${OUT_DIR}" \
users.proto base.proto

The generated folder will now contain both pb_service.js and pb_service.d.ts files which you can reference in your TypeScript project to make RPCs.

rt { UserServiceClient, GetUserRequest } from "../generated/users_pb_service"

t client = new UserServiceClient("https://my.grpc/server");
t req = new GetUserRequest();
setUsername("johndoe");
nt.getUser(req, (err, user) => { /* ... */ });
Examples

For a sample of the generated protos and service definitions, see examples


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.