UNPKG

8.05 kBMarkdownView Raw
1[![Master Build](https://travis-ci.org/improbable-eng/ts-protoc-gen.svg?branch=master)](https://travis-ci.org/improbable-eng/ts-protoc-gen)
2[![NPM](https://img.shields.io/npm/v/ts-protoc-gen.svg)](https://www.npmjs.com/package/ts-protoc-gen)
3[![NPM](https://img.shields.io/npm/dm/ts-protoc-gen.svg)](https://www.npmjs.com/package/ts-protoc-gen)
4[![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
5
6# ts-protoc-gen
7
8> Protoc Plugin for generating TypeScript Declarations
9
10This repository contains a [protoc](https://github.com/google/protobuf) plugin that generates TypeScript declarations
11(`.d.ts` files) that match the JavaScript output of `protoc --js_out=import_style=commonjs,binary`. This plugin can
12also output service definitions as both `.js` and `.d.ts` files in the structure required by [grpc-web](https://github.com/improbable-eng/grpc-web), and as `.d.ts` files in the structure required by [grpc-node](https://github.com/grpc/grpc-node).
13
14This plugin is tested and written using TypeScript 2.7.
15
16## Installation
17
18### npm
19
20As a prerequisite, download or install `protoc` (the protocol buffer compiler) for your platform from the [github releases page](https://github.com/google/protobuf/releases) or via a package manager (ie: [brew](http://brewformulas.org/Protobuf), [apt](https://www.ubuntuupdates.org/pm/protobuf-compiler)).
21
22For the latest stable version of the ts-protoc-gen plugin:
23
24```bash
25npm install ts-protoc-gen
26```
27
28For our latest build straight from master:
29
30```bash
31npm install ts-protoc-gen@next
32```
33
34### bazel
35
36The bazel rules have been moved to a separate project [here](https://github.com/Dig-Doug/rules_typescript_proto).
37There is a
38[migration guide](https://github.com/Dig-Doug/rules_typescript_proto/blob/master/docs/migrating_from_ts_protoc_gen.md)
39for existing users.
40
41## Contributing
42
43Contributions are welcome! Please refer to [CONTRIBUTING.md](https://github.com/improbable-eng/ts-protoc-gen/blob/master/CONTRIBUTING.md) for more information.
44
45## Usage
46
47As mentioned above, this plugin for `protoc` serves two purposes:
48
491. Generating TypeScript Definitions for CommonJS modules generated by protoc
502. Generating gRPC service clients for use with [grpc-web](https://github.com/improbable-eng/grpc-web) or [grpc-node](https://github.com/grpc/grpc-node).
51
52### Generating TypeScript Definitions for CommonJS modules generated by protoc
53
54By default, protoc will generate ES5 code when the `--js_out` flag is used (see [javascript compiler documentation](https://github.com/google/protobuf/tree/master/js)). You have the choice of two module syntaxes, [CommonJS](https://nodejs.org/docs/latest-v8.x/api/modules.html) or [closure](https://developers.google.com/closure/library/docs/tutorial). This plugin (`ts-protoc-gen`) can be used to generate Typescript definition files (`.d.ts`) to provide type hints for CommonJS modules only.
55
56To 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.
57
58```bash
59# Path to this plugin
60PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
61
62# Directory to write generated code to (.js and .d.ts files)
63OUT_DIR="./generated"
64
65protoc \
66 --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
67 --js_out="import_style=commonjs,binary:${OUT_DIR}" \
68 --ts_out="${OUT_DIR}" \
69 users.proto base.proto
70```
71
72In 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:
73
74```js
75import { MyMessage } from "../generated/users_pb";
76
77const msg = new MyMessage();
78msg.setName("John Doe");
79```
80
81### Generating gRPC Service Stubs for use with grpc-web
82
83[gRPC](https://grpc.io/) is a framework that enables client and server applications to communicate transparently, and makes it easier to build connected systems.
84
85[grpc-web](https://github.com/improbable-eng/grpc-web) is a comparability layer on both the server and client-side which allows gRPC to function natively in modern web-browsers.
86
87To 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:
88
89```
90# Path to this plugin, Note this must be an abolsute path on Windows (see #15)
91PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
92
93# Directory to write generated code to (.js and .d.ts files)
94OUT_DIR="./generated"
95
96protoc \
97 --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
98 --js_out="import_style=commonjs,binary:${OUT_DIR}" \
99 --ts_out="service=grpc-web:${OUT_DIR}" \
100 users.proto base.proto
101```
102
103The `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.
104
105**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](https://webpack.js.org/).
106**Note** Both `js` and `d.ts` service files will be generated regardless of whether there are service definitions in the proto files.
107
108```js
109import {
110 UserServiceClient,
111 GetUserRequest
112} from "../generated/users_pb_service";
113
114const client = new UserServiceClient("https://my.grpc/server");
115const req = new GetUserRequest();
116req.setUsername("johndoe");
117client.getUser(req, (err, user) => {
118 /* ... */
119});
120```
121
122### Generating gRPC Service Stubs for use with grpc-node
123
124This plugin can generate `.d.ts` files for gRPC service definitions as required by [grpc-node](https://github.com/grpc/grpc-node).
125
126To 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:
127
128```
129# Path to this plugin, Note this must be an abolsute path on Windows (see #15)
130PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
131
132# Path to the grpc_node_plugin
133PROTOC_GEN_GRPC_PATH="./node_modules/.bin/grpc_tools_node_protoc_plugin"
134
135# Directory to write generated code to (.js and .d.ts files)
136OUT_DIR="./generated"
137
138protoc \
139 --plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
140 --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} \
141 --js_out="import_style=commonjs,binary:${OUT_DIR}" \
142 --ts_out="service=grpc-node:${OUT_DIR}" \
143 --grpc_out="${OUT_DIR}" \
144 users.proto base.proto
145```
146
147The `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.
148
149**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.
150
151#### Using `@grpc/grpc-js` instead of `grpc`
152
153Add a mode parameter to generate files that import `@grpc/grpc-js` instead of `grpc`, for example:
154
155```
156--ts_out="service=grpc-node,mode=grpc-js:${OUT_DIR}"
157```
158
159You'll also need to specify the `grpc_js` option within the `--grpc_out` flag, for example:
160
161```
162--grpc_out="grpc_js:${OUT_DIR}"
163```
164
165If you're consuming the server interface types you'll need to use version `@grpc/grpc-js@1.2.0` or higher.
166
167## Examples
168
169- [Example output](https://github.com/improbable-eng/ts-protoc-gen/tree/master/examples) -- Code generated by `ts-protoc-gen`.
170- [Packaging the output as a node module](https://github.com/improbable-eng/ts-protoc-gen/issues/173) -- Example of how to use the generated code as a dependendecy of another module.
171
172## Gotchas
173
174By 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:
175
176```proto
177message Example {
178 uint64 bigInt = 1 [jstype = JS_STRING];
179}
180```