1.0.0 • Published 3 months ago

@moveaxlab/grpc-gen v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

gRPC gen

NPM License NPM Version

This package contains some opinionated utility functions to generate TypeScript types from protobuf files, to interact with gRPC services and protobuf in general.

The goal of this package is to avoid the code bloat caused by generating JS code: we prefer to rely on packages like @grpc/proto-loader to generate code at runtime from raw .proto files. For this reason we want to generate only TypeScript types, and let other packages generate the code needed to parse protobuf messages.

Installation

yarn add --dev @moveaxlab/grpc-gen

Usage

This tool comes with opinionated defaults on how protobuf files must be structured and how code will be generated.

The expected directory structure is the following:

protobuf/
  common/
    file1.proto
    file2.proto
  service1/
    file1.proto
    file2.proto
  service2/
    file1.proto
    file2.proto

To generate code:

yarn grpc-gen -i <protobuf directory> -o <output directory> [services ...]

Omitting the services positional argument will generate code for all services contained in the protobuf directory option.

The output directory will contain an index file with types for all packages, and a Parser.ts file that allows you to manually decode and encode protobuf messages in a type safe way.

To use the parser:

import { Parser } from "./__generated__/Parser";

// obtain a parser for a generated package
const packageParser = Parser.forPackage("mypackage");

// use the parser to encode and decode protobuf data
const encodedBuffer = packageParser.encode("myType", {
  /* data */
});
const decodedValue = packageParser.decode("myType", encodedBuffer);

The parser relies on @grpc/proto-loader to load protobuf definitions at runtime, and must be configured before usage to know where protobuf models are:

import { join, dirname } from "path";
import { setModelsFolder, addIncludeDir } from "./__generated__/Parser";

setModelsFolder(
  process.env.NODE_ENV === "production" ? "/app/protobuf" : "../../protobuf",
);

addIncludeDIr(
  process.env.NODE_ENV == "production"
    ? "/app/node_modules/grpc-tools/bin"
    : join(dirname(require.resolve("grpc-tools")), "bin"),
);

Generated types are compatible with @moveaxlab/nestjs-grpc-client.