Generates appropriate Protocol Buffer sources from Proto files directly through TypeScript Compiler API.
This plugin generates plain Typescript files that can be used AMD, UMD, CommonJS module systems.
Aim of this protoc plugin is to make usage of protocol buffers easy in Javascript/Typescript by taking modern approaches.
syntax = "proto3";
message Change {
Kind kind = 1;
string patch = 2;
}
enum Kind {
UPDATED = 0;
DELETED = 1;
}
// Constructed message
const change = new Change({
kind: Kind.UPDATED,
patch: "@@ -7,11 +7,15 @@"
});
// Sent over the wire
const bytes: Uint8Array = change.serialize();
const receivedChange: Change = Change.deserialize(bytes);
console.log(receivedChange.kind == Kind.UPDATE) // true
console.log(receivedChange.patch) // "@@ -7,11 +7,15 @@"
This protoc plugin does generate;
- Fields as getter setters.
- Enums as enums.
- Messages within a namespace if the proto has a package directive.
npm install -g protoc-gen-ts
protoc -I=sourcedir --ts_out=dist myproto.proto
# Add protoc-gen-ts to dependencies section of your package.json file.
# Then use it like you would use the other bazel compatible npm packages.
load("@npm_protoc_gen_ts//:index.bzl", "ts_proto_library")
ts_proto_library(
name = "protos",
srcs = [
":some_proto_library_target"
]
)
# Checkout the examples/bazel directory for an example.
Support for repeated non-integer fieldsGenerate appropriate service code that is usable with node grpc package.Support for creating protocol buffer messages directly from their constructors with an object.- Support
map<TYPE, TYPE>
fields. - Support for
import
directive. - Interopability with well knowns.
- Support
map<TYPE, TYPE>
types as ESMap
. - Make services strongly typed.
- Support for
Promise
in rpcs.
Plugin | google-protobuf | Typescript | Declarations | gRPC Node | gRPC Web | ES6 Support | Notes |
---|---|---|---|---|---|---|---|
thesayyn/protoc-gen-ts | Yes | Yes | Yes | Yes | Partial | Yes | The generated messages are compatible with ever-green browsers. However, you might need to use third-party packages to use rpcs. |
improbable-eng/ts-protoc-gen | Yes | No | Yes | No | Yes | Partial | Drawback: You can't bundle generated files with rollup since they are not >= ES6 compatible. |
stephenh/ts-proto | No | Yes | Yes | No | No | Yes | There is no support for rpcs. See: stephenh/ts-proto#2 |