ts-protoc-gen
Protoc Plugin for TypeScript Declarations and Service Definitions
Last updated 19 days ago by jonnyreeves .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ts-protoc-gen 
SYNC missed versions from official npm registry.

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, and as .d.ts files in the structure required by grpc-node.

This plugin is tested and written using TypeScript 2.7.

Installation

npm

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:

npm install ts-protoc-gen

For our latest build straight from master:

npm install ts-protoc-gen@next

bazel

The bazel rules have been moved to a separate project here. There is a migration guide for existing users.

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 clients for use with grpc-web or grpc-node.

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.

# Path to this plugin
PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR="./generated"

protoc \
    --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:

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

const msg = new MyMessage();
msg.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=grpc-web param to the --ts_out flag, eg:

# Path to this plugin, Note this must be an abolsute path on Windows (see #15)
PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR="./generated"

protoc \
    --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
    --js_out="import_style=commonjs,binary:${OUT_DIR}" \
    --ts_out="service=grpc-web:${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.

Note Note that these modules require a CommonJS environment. If you intend to consume these stubs in a browser environment you will need to use a module bundler such as webpack. Note Both js and d.ts service files will be generated regardless of whether there are service definitions in the proto files.

import {
  UserServiceClient,
  GetUserRequest
} from "../generated/users_pb_service";

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

Generating gRPC Service Stubs for use with grpc-node

This plugin can generate .d.ts files for gRPC service definitions as required by grpc-node.

To generate these declaration files from your protobuf files you must configure ts-protoc-gen to emit service definitions by passing the service=grpc-node param to the --ts_out flag, eg:

# Path to this plugin, Note this must be an abolsute path on Windows (see #15)
PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"

# Path to the grpc_node_plugin
PROTOC_GEN_GRPC_PATH="./node_modules/.bin/grpc_tools_node_protoc_plugin"

# Directory to write generated code to (.js and .d.ts files)
OUT_DIR="./generated"

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

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

Note This plugin does not generate the _grpc_pb.js files itself; those are generated by the protoc-gen-grpc plugin. This plugin only generates the _grpc_pb.d.ts files.

Using @grpc/grpc-js instead of grpc

Add a mode parameter to generate files that import @grpc/grpc-js instead of grpc, for example:

--ts_out="service=grpc-node,mode=grpc-js:${OUT_DIR}"

You'll also need to specify the grpc_js option within the --grpc_out flag, for example:

--grpc_out="grpc_js:${OUT_DIR}"

Examples

Gotchas

By default the google-protobuf library will use the JavaScript number type to store 64bit float and integer values; this can lead to overflow problems as you exceed JavaScript's Number.MAX_VALUE. To work around this, you should consider using the jstype annotation on any 64bit fields, ie:

message Example {
  uint64 bigInt = 1 [jstype = JS_STRING];
}

Current Tags

  • 0.13.0                                ...           latest (19 days ago)
  • 0.12.1-pre.6383416                                ...           next (19 days ago)

91 Versions

  • 0.13.0                                ...           19 days ago
  • 0.12.1-pre.6383416                                ...           19 days ago
  • 0.12.1-pre.3266132                                ...           4 months ago
  • 0.12.1-pre.abb949e                                ...           4 months ago
  • 0.12.1-pre.a78a914                                ...           a year ago
  • 0.12.1-pre.46e6630                                ...           a year ago
  • 0.12.0                                ...           a year ago
  • 0.11.1-pre.dc70c35                                ...           a year ago
  • 0.11.0                                ...           a year ago
  • 0.10.1-pre.49c229b                                ...           a year ago
  • 0.10.1-pre.252fec7                                ...           a year ago
  • 0.10.1-pre.90f1ac5                                ...           a year ago
  • 0.10.1-pre.7a3d3c8                                ...           a year ago
  • 0.10.1-pre.005b479                                ...           a year ago
  • 0.10.1-pre.7a65c0d                                ...           a year ago
  • 0.10.1-pre.c511ff3                                ...           a year ago
  • 0.10.1-pre.e3d1c32                                ...           a year ago
  • 0.10.1-pre.2f98b5c                                ...           a year ago
  • 0.10.1-pre.cbcd353                                ...           a year ago
  • 0.10.1-pre.7b144ed                                ...           a year ago
  • 0.10.1-pre.cf38460                                ...           a year ago
  • 0.10.1-pre.987f176                                ...           a year ago
  • 0.10.1-pre.78cf7ae                                ...           a year ago
  • 0.10.1-pre.6d22676                                ...           a year ago
  • 0.10.1-pre.e8c15bc                                ...           a year ago
  • 0.10.0                                ...           a year ago
  • 0.9.1-pre.ad36364                                ...           a year ago
  • 0.9.1-pre.cbe7398                                ...           a year ago
  • 0.9.1-pre.cdb79ec                                ...           a year ago
  • 0.9.1-pre.63a2845                                ...           a year ago
  • 0.9.1-pre.aa468e9                                ...           a year ago
  • 0.9.1-pre.901b08e                                ...           a year ago
  • 0.9.1-pre.c8a573d                                ...           a year ago
  • 0.9.1-pre.7f532e5                                ...           2 years ago
  • 0.9.1-pre.50abb56                                ...           2 years ago
  • 0.9.0                                ...           2 years ago
  • 0.8.1-pre.9c8f487                                ...           2 years ago
  • 0.8.1-pre.dd6273b                                ...           2 years ago
  • 0.8.1-pre.f127046                                ...           2 years ago
  • 0.8.1-pre.14d69f6                                ...           2 years ago
  • 0.8.0                                ...           2 years ago
  • 0.8.1-pre.a7b6422                                ...           2 years ago
  • 0.7.8-pre.b4e921a                                ...           2 years ago
  • 0.7.8-pre.217169b                                ...           2 years ago
  • 0.7.8-pre.51f57ea                                ...           2 years ago
  • 0.7.8-pre.7846e2f                                ...           2 years ago
  • 0.7.8-pre.b7267ea                                ...           2 years ago
  • 0.7.8-pre                                ...           2 years ago
  • 0.7.7                                ...           2 years ago
  • 0.7.7-pre.d9ec484                                ...           2 years ago
  • 0.7.7-pre                                ...           2 years ago
  • 0.7.7-pre.34d3d68                                ...           2 years ago
  • 0.7.7-pre.8ca48fe                                ...           2 years ago
  • 0.7.7-pre.67e0c93                                ...           2 years ago
  • 0.7.7-pre.687bcc7                                ...           2 years ago
  • 0.7.7-pre.5b06c73                                ...           2 years ago
  • 0.7.7-pre.1d7e310                                ...           2 years ago
  • 0.7.7-pre.5959599                                ...           2 years ago
  • 0.7.7-pre.24fbad6                                ...           2 years ago
  • 0.7.7-pre.50bb30c                                ...           2 years ago
  • 0.7.6                                ...           2 years ago
  • 0.7.5-pre.64914b2                                ...           2 years ago
  • 0.7.4-pre.53aa49c                                ...           2 years ago
  • 0.7.4-pre.05c52b8                                ...           2 years ago
  • 0.7.4-pre.54d261f                                ...           2 years ago
  • 0.7.4-pre                                ...           2 years ago
  • 0.7.4-pre.c79cabb                                ...           2 years ago
  • 0.7.4-pre.7247cc3                                ...           2 years ago
  • 0.7.4-pre.4911ace                                ...           2 years ago
  • 0.7.4-pre.f79924d                                ...           2 years ago
  • 0.7.0                                ...           2 years ago
  • 0.7.1-pre.44987dc                                ...           2 years ago
  • 0.7.1-pre                                ...           2 years ago
  • 0.7.4-pre.eabf953                                ...           2 years ago
  • 0.7.3                                ...           2 years ago
  • 0.7.2                                ...           2 years ago
  • 0.7.1                                ...           2 years ago
  • 0.7.0-pre                                ...           2 years ago
  • 0.6.0                                ...           2 years ago
  • 0.5.2                                ...           3 years ago
  • 0.5.1                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Downloads
Today 0
This Week 26
This Month 492
Last Day 13
Last Week 220
Last Month 326
Dependencies (1)
Dev Dependencies (24)

Copyright 2014 - 2016 © taobao.org |