Generates ts
files from proto
file definitions.
To install the generator (bear in mind that it could be installed in ${HOME}/.local/bin
too):
curl -Lo protoc-gen-tsx \
https://github.com/join-com/protoc-gen-ts/releases/download/[VERSION]/protoc-gen-tsx.darwin.arm64 \
&& chmod +x protoc-gen-tsx \
&& sudo mv protoc-gen-tsx /usr/local/bin
- If you need a binary for Linux, you can change the word "darwin" in the url by "linux".
- If you need support for the Intel processor, or any other ARM processor, change "arm64" by "amd64".
will generate TS implementations into proto-ts
folder for all your proto files inside proto
:
protoc proto/*.proto -I proto --tsx_out=proto-ts
In order to enable the "required fields" feature and the "type flavors" feature,
copy the following files from this repository into your proto
directory:
google/protobuf/descriptor.proto
options.proto
We can mark all fields as required in a message, with a "message custom option":
message ExampleMessage {
option (join.protobuf.typescript_required_fields) = true;
int32 firstField = 1;
int32 secondField = 2;
string thirdField = 3 [(join.protobuf.typescript_optional) = true]; // We can mark fields as optional
}
We can also mark individual fields as required:
message ExampleMessage {
int32 firstField = 1;
int32 secondField = 2 [(join.protobuf.typescript_required) = true];
string thirdField = 3;
}
We can also generate flavor nominal types for our fields with primitive types:
message UserProfile {
int32 id = 1 [(join.protobuf.typescript_flavor) = "UserId"];
string username = 2;
repeated string emails = 3 [(join.protobuf.typescript_flavor) = "Email"];
}
By default, the generated type for the field mask property will be { paths: string[] }
. To add clarity which fields can be send as a mask use:
message GetRequest {
int32 id = 1;
google.protobuf.FieldMask read_mask = 2 [(join.protobuf.typescript_mask) = "Company"];
}
- Follow instructions to install Go and add /usr/local/go/bin to the PATH environment variable
- Run
./build.sh
to compile the package - Or navigate to
tests
folder and runyarn compile
- Run
yarn proto:build:package(1/2/3)
to generate one of the packages for tests - Run
yarn test
to run tests