0.15.0 • Published 3 years ago

ts-protoc-gen v0.15.0

Weekly downloads
72,496
License
Apache-2.0
Repository
github
Last release
3 years ago

ts-protoc-gen

Protoc Plugin for generating TypeScript Declarations

Master Build NPM Apache 2.0 License quality: alpha

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

  • Install the standard C++ implementation of protocol buffers from developers.google.com/protocol-buffers
  • Install this package using npm, eg: npm install ts-protoc-gen or clone, install and build this repository.

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.

# 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=true param to the --ts_out flag, eg:

# 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="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.

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) => { /* ... */ });

Examples

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

Contributing

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

zoobc-sdk-jsuntaek-chat-worker-nodetrcomplete-protobuftickle-protobufgcore-base@cory-miller/contacts-apiblockjoy-grpcblockjoy-mock-grpc@infinitebrahmanuniverse/nolb-ts-pcogment@everything-registry/sub-chunk-2978mavsdk-protoself-sdk@diginet/dfm2jsoncshrdna-platform-hookscacaheute-objectsceler-web-sdkceler-light-clientclient_alice@catalyst-net-js/truffle-provider@cogment/cogment-js-sdk@c4dt/libcrowdnotifier@cognite/seismic-sdk-js@dolittle/fundamentals.services.grpc@aitmed/protorepo@aitmed/protorepo-testtest-delete-me-1streem-sdk-websvc-apiseventsocketclientjsevergreen-shrubaitmed-protorepo-test@uptown-runners/common@tcn/tcnapi-grpc-web@ultipa-graph/ultipa-node-sdkflow-bridgetemporamolestiaegas-price-protobuf@futureverse/sylo-notifications-sdkwundergraph-protobufkilowattgcp-rdmgrpc-instrumentumgrpc-demogrpc-form-tsgrpc-proto-js-generatorgrpcdemo-nodejs-apigrpc-web-ts-compilerjsmarketstoreiroha-helpersiroha-helpers-eugeneiroha-helpers-eugene0iroha-helpers-new-grpciroha-helpers-ts@hyperledgendary/fabric-ledger-protos@lily-technologies/lnrpcnars-clientmassa-proto-cli@jamaljsr/lnrpc@lewnelson/grpc-ts@huddly/camera-switch-proto@helium/onboarding@joemphilips/walletts@lavanet/lava-sdk@mthang1801/domain-driven-design@mthang1801/library@permify/permify-nodenonsimiliquenode_protoc2phrasa-controlpbrpcpigweedjs@radar/lnrpc@radartech/lnrpc@dylanratcliffe/sdpproto-ws-shared-structsprotocpilerprotoc-gen-csharpprotoc-gen-dclprotoc-gen-dclunity@overmindtech/sdp@oraichain/oraidex-universal-swap@olympus-protocol/ogen-protojs@openmined/psi.jsnpm-proto-demo@sendit-th/4pl-resource-planner-grpc-node
0.15.0

3 years ago

0.14.0

3 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.8-pre

6 years ago

0.7.7

6 years ago

0.7.7-pre

6 years ago

0.7.6

6 years ago

0.7.4-pre

6 years ago

0.7.3

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.7.1-pre

6 years ago

0.7.0-pre

6 years ago

0.6.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago