Search code examples
typescriptprotocol-buffersgrpcgrpc-node

How to generate the Typescript files from proto files?


I'm attempting to generate some Typescript files for a React project that would utilize gRPC through proto files.

I ran a script:

#!/bin/bash

IN_DIR="./protos"
OUT_DIR="./dist"

# Generate the types
yarn proto-loader-gen-types \
    --grpcLib=@grpc/grpc-js \
    --outDir=${OUT_DIR}/types/ \
    ${IN_DIR}/*.proto

This somehow only generates the types and interfaces without the actual services. Apologizes if I am using the wrong terms, I am new to doing gRPCs.

Is there another script that I need to run to generate the meat of the files that I need to start working with gRPC?


Solution

  • If any input file has service definitions, the output will have the corresponding service interface definitions. No additional options are needed to make that happen. Keep in mind that the output directory structure reflects the packages declared in the .proto files, so this proto file:

    syntax = "proto3";
    
    import "google/protobuf/empty.proto";
    
    package foo.bar;
    
    service TestService {
      rpc TestMethod (google.protobuf.Empty) returns (google.protobuf.Empty);
    }
    

    will result in a file containing the service interfaces in ${outDir}/foo/bar/TestService.ts.