Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (protoc-gen-nestjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
protoc-gen-nestjs
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.