1.2.2 • Published 9 months ago

@pandelis/protoc-gen-nestjs v1.2.2

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 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.

Knwon limitations

  • Currently, only Promises so no streaming via Observable is supported.

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,
    });
  }
}
1.2.2

9 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.1

9 months ago

1.1.0

10 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago