Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yukukotani/protoc-gen-nestjs
The code generator for Protocol Buffers for NestJS, based on protobuf-es
https://github.com/yukukotani/protoc-gen-nestjs
nestjs protobuf
Last synced: 3 months ago
JSON representation
The code generator for Protocol Buffers for NestJS, based on protobuf-es
- Host: GitHub
- URL: https://github.com/yukukotani/protoc-gen-nestjs
- Owner: yukukotani
- License: apache-2.0
- Created: 2022-11-02T08:49:16.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T16:10:40.000Z (3 months ago)
- Last Synced: 2024-10-24T17:29:17.179Z (3 months ago)
- Topics: nestjs, protobuf
- Language: TypeScript
- Homepage:
- Size: 95.7 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protoc-gen-nestjs
[![npm](https://img.shields.io/npm/v/protoc-gen-nestjs)](https://www.npmjs.com/package/protoc-gen-nestjs)
[![license](https://img.shields.io/npm/l/protoc-gen-nestjs)](https://github.com/yukukotani/protoc-gen-nestjs/blob/main/LICENSE)The code generator for Protocol Buffers for [NestJS](https://docs.nestjs.com/microservices/grpc), based on [@bufbuild/protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es).
Highly inspired by [ts-proto](https://github.com/stephenh/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](https://github.com/bufbuild/protobuf-es/tree/main/packages/protoc-gen-es#installation) for detail.
```
npm install --save-dev @bufbuild/protoc-gen-es protoc-gen-nestjs
npm install @bufbuild/protobuf
```## Usage
The demo implementation is available [here](/demo).
### Generating codes
Add a new configuration file `buf.gen.yaml`:
```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`:
```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:
```proto
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:
```ts
@Controller()
@ElizaServiceMethods()
export class ElizaController implements ElizaServiceController {
async say(request: SayRequest): Promise {
return new SayResponse({
sentence: request.sentence,
});
}
}
```Streaming is also supported. See [NestJS's doc](https://docs.nestjs.com/microservices/grpc) for detail.