1.2.0-dev.1 • Published 9 months ago

@nersent/grpc-nest v1.2.0-dev.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

☁️ grpc-nest

Type-safe thin wraper around grpc-js for Nest.js with support for promises.

Note: This library is in an early development stage. Expect breaking changes.

Installation

yarn add @nersent/grpc-nest

Example

Quick Start

1. Compile proto

yarn grpc-nest ./example/*.proto ./example/generated

2.

index.ts

import { createGrpcTransport } from '@nersent/grpc-nest'; 

const app = await NestFactory.create(AppModule);

app.connectMicroservice<MicroserviceOptions>(
  createGrpcTransport({ address: "0.0.0.0:4269" }, app),
);

await app.startAllMicroservices();

app_module.ts

import { GrpcModule } from '@nersent/grpc-nest';

@Module({
  imports: [GrpcModule],
  controllers: [ApiController]
})
export class AppModule {}

api_controller.ts

import {
  IExampleApiService,
  ExampleApiService,
} from "./generated/example_grpc_pb";
import {
  ExampleApiStatusRequest,
  ExampleApiStatusResponse,
} from "./generated/example_pb";

@GrpcController(ExampleApiService)
export class ApiController implements IGrpcController<IExampleApiService> {
  public async status(
    req: ExampleApiStatusRequest,
  ): Promise<ExampleApiStatusResponse> {
    const name = req.getName();
    const res = new ExampleApiStatusResponse();
    res.setMessage(name);
    return res;
  }
}

Made by Nersent