Skip to content

Commit

Permalink
Add @grpc/grpc-js Server interface (#247)
Browse files Browse the repository at this point in the history
* Generate Server interface

* Update examples

* Use service method descriptor types to server interface definitions

This approach provides between type compatibility between grpc and grpc-js packages

* Install @grpc/grpc-js as devDependency

* Add note about grpc-js version compatibility

* Remove grpc-js dev dependency
  • Loading branch information
badsyntax committed Dec 3, 2020
1 parent d58dfe2 commit 7b8d3ca
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -162,6 +162,8 @@ You'll also need to specify the `grpc_js` option within the `--grpc_out` flag, f
--grpc_out="grpc_js:${OUT_DIR}"
```

If you're consuming the server interface types you'll need to use version `@grpc/grpc-js@1.2.0` or higher.

## Examples

- [Example output](https://github.com/improbable-eng/ts-protoc-gen/tree/master/examples) -- Code generated by `ts-protoc-gen`.
Expand Down
Expand Up @@ -18,6 +18,14 @@ interface ISimpleServiceService extends grpc.ServiceDefinition<grpc.UntypedServi

export const SimpleServiceService: ISimpleServiceService;

export interface ISimpleServiceServer extends grpc.UntypedServiceImplementation {
doUnary: grpc.handleUnaryCall<proto_examplecom_simple_service_pb.UnaryRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doServerStream: grpc.handleServerStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doClientStream: grpc.handleClientStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, google_protobuf_empty_pb.Empty>;
doBidiStream: grpc.handleBidiStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
delete: grpc.handleUnaryCall<proto_examplecom_simple_service_pb.UnaryRequest, proto_examplecom_simple_service_pb.UnaryResponse>;
}

export class SimpleServiceClient extends grpc.Client {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
doUnary(argument: proto_examplecom_simple_service_pb.UnaryRequest, callback: grpc.requestCallback<proto_othercom_external_child_message_pb.ExternalChildMessage>): grpc.ClientUnaryCall;
Expand Down
5 changes: 5 additions & 0 deletions examples/generated-grpc-js-node/proto/orphan_grpc_pb.d.ts
Expand Up @@ -13,6 +13,11 @@ interface IOrphanServiceService extends grpc.ServiceDefinition<grpc.UntypedServi

export const OrphanServiceService: IOrphanServiceService;

export interface IOrphanServiceServer extends grpc.UntypedServiceImplementation {
doUnary: grpc.handleUnaryCall<proto_orphan_pb.OrphanUnaryRequest, proto_orphan_pb.OrphanMessage>;
doStream: grpc.handleServerStreamingCall<proto_orphan_pb.OrphanStreamRequest, proto_orphan_pb.OrphanMessage>;
}

export class OrphanServiceClient extends grpc.Client {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
doUnary(argument: proto_orphan_pb.OrphanUnaryRequest, callback: grpc.requestCallback<proto_orphan_pb.OrphanMessage>): grpc.ClientUnaryCall;
Expand Down
Expand Up @@ -18,6 +18,14 @@ interface ISimpleServiceService extends grpc.ServiceDefinition<grpc.UntypedServi

export const SimpleServiceService: ISimpleServiceService;

export interface ISimpleServiceServer extends grpc.UntypedServiceImplementation {
doUnary: grpc.handleUnaryCall<proto_examplecom_simple_service_pb.UnaryRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doServerStream: grpc.handleServerStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
doClientStream: grpc.handleClientStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, google_protobuf_empty_pb.Empty>;
doBidiStream: grpc.handleBidiStreamingCall<proto_examplecom_simple_service_pb.StreamRequest, proto_othercom_external_child_message_pb.ExternalChildMessage>;
delete: grpc.handleUnaryCall<proto_examplecom_simple_service_pb.UnaryRequest, proto_examplecom_simple_service_pb.UnaryResponse>;
}

export class SimpleServiceClient extends grpc.Client {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
doUnary(argument: proto_examplecom_simple_service_pb.UnaryRequest, callback: grpc.requestCallback<proto_othercom_external_child_message_pb.ExternalChildMessage>): grpc.ClientUnaryCall;
Expand Down
5 changes: 5 additions & 0 deletions examples/generated-grpc-node/proto/orphan_grpc_pb.d.ts
Expand Up @@ -13,6 +13,11 @@ interface IOrphanServiceService extends grpc.ServiceDefinition<grpc.UntypedServi

export const OrphanServiceService: IOrphanServiceService;

export interface IOrphanServiceServer extends grpc.UntypedServiceImplementation {
doUnary: grpc.handleUnaryCall<proto_orphan_pb.OrphanUnaryRequest, proto_orphan_pb.OrphanMessage>;
doStream: grpc.handleServerStreamingCall<proto_orphan_pb.OrphanStreamRequest, proto_orphan_pb.OrphanMessage>;
}

export class OrphanServiceClient extends grpc.Client {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
doUnary(argument: proto_orphan_pb.OrphanUnaryRequest, callback: grpc.requestCallback<proto_orphan_pb.OrphanMessage>): grpc.ClientUnaryCall;
Expand Down
20 changes: 20 additions & 0 deletions src/service/grpcnode.ts
Expand Up @@ -43,6 +43,8 @@ function generateTypeScriptDefinition(fileDescriptor: FileDescriptorProto, expor
printer.printEmptyLn();
printService(printer, service);
printer.printEmptyLn();
printServer(printer, service);
printer.printEmptyLn();
printClient(printer, service);
});

Expand All @@ -62,6 +64,24 @@ function printService(printer: Printer, service: RPCDescriptor) {
printer.printLn(`export const ${serviceName}: I${serviceName};`);
}

function printServer(printer: Printer, service: RPCDescriptor) {
const serverName = `${service.name}Server`;
printer.printLn(`export interface I${serverName} extends grpc.UntypedServiceImplementation {`);
service.methods
.forEach(method => {
if (!method.requestStream && !method.responseStream) {
printer.printIndentedLn(`${method.nameAsCamelCase}: grpc.handleUnaryCall<${method.requestType}, ${method.responseType}>;`);
} else if (!method.requestStream) {
printer.printIndentedLn(`${method.nameAsCamelCase}: grpc.handleServerStreamingCall<${method.requestType}, ${method.responseType}>;`);
} else if (!method.responseStream) {
printer.printIndentedLn(`${method.nameAsCamelCase}: grpc.handleClientStreamingCall<${method.requestType}, ${method.responseType}>;`);
} else {
printer.printIndentedLn(`${method.nameAsCamelCase}: grpc.handleBidiStreamingCall<${method.requestType}, ${method.responseType}>;`);
}
});
printer.printLn("}");
}

function printClient(printer: Printer, service: RPCDescriptor) {
printer.printLn(`export class ${service.name}Client extends grpc.Client {`);
printer.printIndentedLn("constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);");
Expand Down

0 comments on commit 7b8d3ca

Please sign in to comment.