Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): export ServiceClientConstructor type #1951

Merged
merged 1 commit into from Oct 26, 2021

Conversation

howyi
Copy link
Contributor

@howyi howyi commented Oct 24, 2021

export ServiceClientConstructor, ProtobufTypeDefinition .

When writing a server configured with dynamic_codegen in TypeScript, it is necessary to TypeHint the value read from proto, and being able to annotate with this type helps it be a bit more seamless

example dynamic_codegen ts code diff

orig: https://github.com/grpc/grpc/blob/v1.41.0/examples/node/dynamic_codegen/greeter_server.js

var PROTO_PATH = __dirname + '/../../../examples/proto/helloworld.proto';

import * as grpc from '@grpc/grpc-js'
- import {ServiceClientConstructor} from '@grpc/grpc-js/build/src/make-client'
import * as protoLoader from '@grpc/proto-loader'

const packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true
    });
const hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld as grpc.GrpcObject;

/**
 * Implements the SayHello RPC method.
 */
function sayHello(call: any, callback: any) {
  callback(null, {message: 'Hello ' + call.request.name});
}

/**
 * Starts an RPC server that receives requests for the Greeter service at the
 * sample server port
 */
function main() {
  var server = new grpc.Server();
-  server.addService((hello_proto.Greeter as ServiceClientConstructor).service, {sayHello: sayHello});
+  server.addService((hello_proto.Greeter as grpc.ServiceClientConstructor).service, {sayHello: sayHello});
  server.bindAsync('localhost:50051', grpc.ServerCredentials.createInsecure(), () => {
    server.start();
  });
}

main();

@murgatroid99 murgatroid99 merged commit 5a49a93 into grpc:master Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants