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

Add Server interface #247

Merged
merged 6 commits into from Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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