Skip to content

xiaozhongliu/grpc-client-egg

Repository files navigation

grpc-client-egg

npm version

An egg grpc client written in Typescript.

How to use

1 install

npm i -S grpc-client-egg

2 include

import { EggPlugin } from 'egg'

const plugin: EggPlugin = {

    routerPlus: {
        enable: true,
        package: 'egg-router-plus',
    },
    grpcClient: {
        enable: true,
        package: 'grpc-client-egg',
    },
}
export default plugin

3 config

import { EggAppConfig, PowerPartial } from 'egg'

export default () => {
    const config: PowerPartial<EggAppConfig> = {

        grpcClient: {
            clients: [
                {
                    name: 'main',
                    protoPath: 'app/proto/main',
                    host: '0.0.0.0',
                    port: 50051,
                },
            ],
        },
    }
    return config
}

4 invoke promisified grpc service methods

import { Service } from 'egg'

export default class Greeter extends Service {

    readonly service: GreeterService = this.app.grpcClient.main.greeter.Greeter

    public async sayHello(name: string) {
        return this.service.sayHello({ name })
    }

    public async sayGoodbye(name: string) {
        return this.service.sayGoodbye({ name })
    }
}

Default config from the plugin

    loaderOption: {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true,
    },

    clients: [
        {
            name: 'main',
            protoPath: 'app/proto/main',
            host: '0.0.0.0',
            port: 50051,
        },
    ],

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published