0.1.1 • Published 5 months ago

@russ-b/nestjs-grpc-bridge v0.1.1

Weekly downloads
-
License
-
Repository
github
Last release
5 months ago

You're right to question this. The customClass option isn't part of the standard NestJS API. Let me provide a corrected README with the proper way to register the custom gRPC client:

nestjs-grpc-bridge

A NestJS library for enhanced gRPC communication with automatic response transformation.

Features

  • Automatic transformation of gRPC responses
  • Support for Google Protocol Buffers Struct to JSON conversion
  • Custom field transformers
  • Type-safe gRPC client proxies
  • Proto definition introspection

Installation

npm install nestjs-grpc-bridge

Usage

Example Setup

import { Module } from '@nestjs/common';
import { ClientsModule } from '@nestjs/microservices';
import { BaseClientGrpcProxy } from 'nestjs-grpc-bridge';

@Module({
    providers: [
        {
            inject: [ConfigService],
            provide: 'NAME_OF_YOUR_SERVICE',
            useFactory(configService: ConfigService) {
                const packageName = 'YOUR_PACKAGE_NAME';
                const protoDir = resolve(__dirname, 'PATH', 'TO', 'YOUR', 'PROTO', 'DIR');
                const protoPath = ProtoPathHelper.readProtoDir(protoDir, packageName);

                return new BaseClientGrpcProxy({
                    url: configService.getOrThrow<string>('VAULT_URL'),
                    package: packageName,
                    protoPath,
                    loader: {
                        longs: Number,
                        objects: true,
                        arrays: true,
                        includeDirs: [protoDir],
                    },
                });
            },
        },
    ],
})
export class AppModule {}

Using the Service

import {Injectable} from '@nestjs/common';
import {Client, ClientGrpc} from '@nestjs/microservices';
import {Observable} from 'rxjs';

interface GrpcService {
    getData(request: any): Observable<any>;
}

@Injectable()
export class AppService {
    private readonly myGrpcServiceClient: MyGrpcServiceClient;

    constructor(
        @Inject('NAME_OF_YOUR_SERVICE') private readonly client: ClientGrpc,
    ) {
        this.myGrpcServiceClient =
            this.client.getService<MyGrpcServiceClient>('MyGrpcService');
    }

    get myGrpcService(): MyGrpcServiceClient {
        return this.myGrpcServiceClient;
    }
    
    async loadData() {
        const { data } = await firstValueFrom(
            this.myGrpcService.findAll({ env: 'DEV' }),
        );
        
        return data;
    }
}

Converts Google Protocol Buffers Struct objects to plain JSON objects.

What server sends

{
  id: 999999,
  key: 'rabbitmq',
  value: {
    fields: {
      port: {
        numberValue: 5672,
      },
      vhost: {
        stringValue: '/',
      }
    }
  }
}

What you get in response

{
  id: 999999,
  key: 'rabbitmq',
  value: {
    port: 5672,
    vhost:'/',
  }
}

The library also supports nested structs.

License

MIT

0.1.1

5 months ago

0.1.0

5 months ago

0.0.13

5 months ago

0.0.12

7 months ago

0.0.11

7 months ago

0.0.10

7 months ago

0.0.9

7 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago