zeidoo / protoc-gen-ts

Generate Typescript files from Proto files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Protoc Gen Typescript

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.

Example

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 @@"

Key Differences

This protoc plugin does generate;

  • Fields as getter setters.
  • Enums as enums.
  • Messages within a namespace if the proto has a package directive.

Usage

Without Bazel

npm install -g protoc-gen-ts

protoc -I=sourcedir --ts_out=dist myproto.proto

With Bazel

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

Roadmap

  • Support for repeated non-integer fields
  • Generate 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.

Ideas

  • Support map<TYPE, TYPE> types as ES Map.
  • Make services strongly typed.
  • Support for Promise in rpcs.

Alternatives

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

About

Generate Typescript files from Proto files.


Languages

Language:JavaScript 81.8%Language:Starlark 10.7%Language:TypeScript 7.5%