npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-grpc-client

v0.1.8

Published

grpc client module for nestjsß

Downloads

122

Readme

Description

NestJs 的 grpc 客户端模块。nestjs自带的@Client() 需要传入rpc服务的地址,不能通过配置去注入这个地址。所以有了这个项目,可以注入远程服务的地址来生成 grpc 客户端

Basic useage

hello.proto


syntax = "proto3";

package hello;

service OrderService {
    rpc say (string) returns (string) {}
}

connections

  • GrpcClientModule.register(options: GrpcOptions):DynamicModule
    初始化grpc客户端。返回客户端提供者
  • GrpcClientModule.registerAsync(options: GrpcOptions, injectOption: GrpcClientModuleAsyncOptions) DynamicModule
    异步依赖初始化grpc客户端的连接。injectOption 为依赖的提供者
import {Module} from '@nestjs/common';
import {GrpcClientModule} from "nestjs-grpc-client";
import {Transport} from "@nestjs/common/enums/transport.enum";
import {join} from "path";

@Module({
  imports: [
    GrpcClientModule.register([
      {
        transport: Transport.GRPC,
          options: {
            url: '0.0.0.0:8888',
            package: 'hello',
            protoPath: join(__dirname, 'hello.proto'),
            loader: {
              arrays: true
            }
          }
      }
    ]),
    TestModule
  ],
})
export class AppModule {
}

import {Module} from '@nestjs/common';
import {GrpcClientModule} from "nestjs-grpc-client";
import {Transport} from "@nestjs/common/enums/transport.enum";
import {join} from "path";
import {ConfigModule, ConfigService} from 'nestjs-configure';

@Module({
  imports: [
    ConfigModule.load(),
    GrpcClientModule.registerAsync([
      {
        transport: Transport.GRPC,
          options: {
            package: 'hello',
            protoPath: join(__dirname, 'hello.proto'),
            loader: {
              arrays: true
            }
          }
      }
    ], {
      useFactory: (configService) => configService.get('grpcOptions'),
      inject: [ConfigService],
    }),
    TestModule
  ],
})
export class AppModule {
}

// bootstrap.yml

grpcOptions:
  -
    package: hello
    url: 127.0.0.1:8888

decorators

  • InjectGrpcClient(packageName: strint)
    return ClientGrpcProxy。注入 grpc 客户端连接。全局,可以在任何地方使用,接受proto文件中定义的 package
  • InjectGrpcClientService(packageName: string, serviceName: string)
    返回proto中定一个的service。注入 grpc 中的service

import {Injectable} from "@nestjs/common";
import {InjectGrpcClient} from "nestjs-grpc-client";
import {ClientGrpcProxy} from "@nestjs/microservices";

@Injectable()
export class TestService {

  constructor(
    @InjectGrpcClient('hello') private grpcClient: ClientGrpcProxy,
  ) {
  }

  async hello() {
    const helloService: any = this.grpcClient.getService('HelloService');
    return await helloService.say('hello');
  }
}


import {Module} from "@nestjs/common";
import {GrpcClientModule} from "nestjs-grpc-client";

@Module({
  imports: [GrpcClientModule.forClientServices([
    {package: 'hello', services: ['HelloService']}
  ])],
})
export class TestModule {
}


import {Injectable} from "@nestjs/common";
import {InjectGrpcClientService} from "nestjs-grpc-client";
import {ClientGrpcProxy} from "@nestjs/microservices";

@Injectable()
export class TestService {

  constructor(
    @InjectGrpcClientService('hello', 'HelloService') private helloService,
  ) {
  }

  async hello() {
    return await this.helloService.say('hello');
  }
}