0.2.0 • Published 5 months ago

protoc-gen-nestjs v0.2.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
5 months ago

protoc-gen-nestjs

npm license

The code generator for Protocol Buffers for NestJS, based on @bufbuild/protoc-gen-es.

Highly inspired by ts-proto and generates roughly the same code as it.

Features

  • Generates interfaces of controllers from gRPC services.
  • Generates decorators that unifies @GrpcMethod and @GrpcStreamMethod.

Installation

@bufbuild/protoc-gen-es and @bufbuild/protobuf is required. See protoc-gen-es's doc for detail.

npm install --save-dev @bufbuild/protoc-gen-es protoc-gen-nestjs
npm install @bufbuild/protobuf

Usage

The demo implementation is available here.

Generating codes

Add a new configuration file buf.gen.yaml:

# buf.gen.yaml defines a local generation template.
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
  # This will invoke protoc-gen-es and write output to src/gen
  - name: es
    out: src/gen
    opt: target=ts
  # This will invoke protoc-gen-nestjs and write output to src/gen
  - name: nestjs
    out: src/gen
    opt: target=ts

Add the following script to your package.json:

{
  "name": "your-package",
  "version": "1.0.0",
  "scripts": {
    "generate": "buf generate"
  }
  // ...
}

To generate code for all protobuf files within your project, simply run:

npm run generate

Implementation

For example, the following definition:

service ElizaService {
  rpc Say(SayRequest) returns (SayResponse) {}
}

message SayRequest {
  string sentence = 1;
}
message SayResponse {
  string sentence = 1;
}

ElizaServiceController interface and ElizaServiceMethods decorator will be generated.

You can implement the controller like this:

@Controller()
@ElizaServiceMethods()
export class ElizaController implements ElizaServiceController {
  async say(request: SayRequest): Promise<SayResponse> {
    return new SayResponse({
      sentence: request.sentence,
    });
  }
}

Streaming is also supported. See NestJS's doc for detail.