npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@logunify/protoc-gen-ts

v0.4.3

Published

Compile protocol buffers descriptors to Typescript.

Downloads

9

Readme

Protoc Gen Typescript

test npm npm npm

Compile .proto files to plain TypeScript. Supports gRPC Node and gRPC Web.

Key Differences

  • No d.ts files. Just plain typescript sources with actual code.
  • Fields as getter setters.
  • Enums as enums.
  • Messages within a namespace if the proto has a package directive. (can be controlled via --ts_opt=no_namespace)
  • fromObject and toObject methods to work with json data.
  • Support for gRPC Node and gRPC Web.
  • You get what you define in proto files. No such prefixes as "getField" or "getFieldList".
  • Generates bindings with either as-is names (message.field_name) or JSON-compatible names (message.fieldName).

Example

syntax = "proto3";

message Author {
    string name = 1;
    string role = 2;
}

message Change {
    Kind kind = 1;
    string patch = 2;
    repeated string tags = 3; 
    oneof name_or_id {
        string name = 4;
        string id = 5;
    }
    Author author = 6;
}

enum Kind {
    UPDATED = 0;
    DELETED = 1;
}
// Constructed message
const change = new Change({
    kind: Kind.UPDATED,
    patch: "@@ -7,11 +7,15 @@",
    tags: ["no prefix", "as is"],
    name: "patch for typescript 4.5",
    author: new Author({
        name: "mary poppins",
        role: "maintainer"
    })
});

// Sent over the wire
const bytes: Uint8Array = change.serialize();

const receivedChange: Change = Change.deserialize(bytes);

console.log(receivedChange.kind == Kind.UPDATED) // true
console.log(receivedChange.patch) // "@@ -7,11 +7,15 @@"
console.log(receivedChange.tags) // ["no prefix", "as is"]
console.log(receivedChange.name) // "patch for typescript 4.5"
// see which one of the fields were filled
console.log(receivedChange.name_or_id) // "name"
console.log(receivedChange.author.name) // "mary poppins"

Support for Message.fromObject and Message.toObject

When mapping raw json data to message classes, dealing with nested structures can be rather annoying. To overcome this problem, every generated message class has a static method called fromObject and toObject which can handle the mapping bidirectionally for you, even with the deeply structured messages. since it is aware of the field graph, it does not rely on any runtime type information thus we get the chance to keep it fast.

One can write code as;

const change = Change.fromObject({
    kind: Kind.UPDATED,
    patch: "@@ -7,11 +7,15 @@",
    tags: ["no prefix", "as is"],
    name: "patch for typescript 4.5",
    author: {
        name: "mary poppins",
        role: "maintainer"
    }
});

console.log(change.author instanceof Author) // true

Usage with @grpc/grpc-js or grpc

There is a seperate documentation for the usage of protoc-gen-ts along with either @grpc/grpc-js or grpc. By default this generated gRPC interfaces will use @grpc/grpc-js.

Checkout rpcs.

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.

load("@npm//protoc-gen-ts:index.bzl", "ts_proto_library")

ts_proto_library(
    name = "protos",
    deps = [
        ":some_proto_library_target"
    ]
)

# Checkout the examples/bazel directory for an example.

Supported Options

  • With --ts_opt=unary_rpc_promise=true, the service definition will contain a promise based rpc with a calling pattern of const result = await client.METHOD(message). Note: all of the metadata and options parameters are still available to you.

  • With --ts_opt=grpc_package=xxxx, you can specify a different package to import rather than @grpc/grpc-js.

  • With --ts_opt=no_namespace, you can control whether you get nested messages inside namespaces or prefixed with their parent message or directive.

  • With --ts_opt=json_names, fields will be converted to lowerCamelCase, for compatibility with the JSON mapping done by the first-party protobuf libraries.

  • With --ts_opt=explicit_override, inherited methods are generated with override modifier, this fixes transpilation error when noImplicitOverride is enabled.

  • With --ts_opt=target=node, the generated client class will be compatible with gRPC Node @grpc/grpc-js or grpc.

  • With --ts_opt=target=web, the generated client class will be compatible with gRPC Web via grpc-web.

  • With --ts_opt=no_grpc, grpc service code won't be generated.

Support

We need your constant support to keep protoc-gen-ts well maintained and add new features.

If your corporate has a OSS funding scheme, please consider supporting us monthly through open collective.

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 for import directive.
  • Support for Promise in rpcs.
  • Make services strongly typed.
  • Support oneof fields
  • Support map<TYPE, TYPE> types as ES Map.
  • Support for @deprecated annotations via deprecated option.
  • Support for grpc-web without any manual intervention.
  • Interopability with well knowns.

Alternatives

| Plugin | google-protobuf | Typescript | Declarations | gRPC Node | gRPC Web | ES6 Support | Notes | |------------------------------|-----------------|:----------:|:------------:|:---------:|:--------:|:-----------:|:-----------------------------------------------------------------------------------------------------------------------------------:| | thesayyn/protoc-gen-ts | Yes | Yes | Yes | Yes | Yes | Yes | | | improbable-eng/ts-protoc-gen | Yes | No | Yes | No | Yes | Partial | Drawback: You can't bundle generated files with rollup sincethey are not >= ES6 compatible. | | stephenh/ts-proto | No | Yes | Yes | No | No | Yes | There is no support for rpcs.See: https://github.com/stephenh/ts-proto/issues/2 |

Development

Generates appropriate Protocol Buffer sources from Proto files directly through TypeScript Compiler API.

# when you make changes to the plugin, you will have to run the command below
yarn update_checked_in_test
# this command will run the plugin with your changes and update generated test source accordingly.

# then invoke the tests
yarn test
# additionally if you want to see error details
yarn test --test_output=errors

Contributors

GitHub Contributors Image