From 1e52c243a95ffc51f032d458cfc33720c7c367b0 Mon Sep 17 00:00:00 2001 From: Andres Correa Casablanca Date: Fri, 16 Apr 2021 08:08:11 +0200 Subject: [PATCH] feat: extract final client interface --- internal/generator/runer_generate_service.go | 58 +++++++++++++++++++- tests/__tests__/v2/generated/Test.ts | 27 ++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/internal/generator/runer_generate_service.go b/internal/generator/runer_generate_service.go index 016a483..7bfa6db 100644 --- a/internal/generator/runer_generate_service.go +++ b/internal/generator/runer_generate_service.go @@ -12,6 +12,7 @@ func (r *Runner) generateTypescriptServiceDefinitions(generatedFileStream *proto for _, serviceSpec := range protoFile.Proto.GetService() { r.generateTypescriptServiceImplementationInterface(generatedFileStream, serviceSpec) r.generateTypescriptServiceDefinition(generatedFileStream, serviceSpec) + r.generateTypescriptClientInterface(generatedFileStream, serviceSpec) r.generateTypescriptClient(generatedFileStream, serviceSpec) } } @@ -87,12 +88,67 @@ func (r *Runner) generateTypescriptServiceDefinitionMethod(generatedFileStream * r.P(generatedFileStream, "},") } +func (r *Runner) generateTypescriptClientInterface(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto) { + r.P( + generatedFileStream, + "export interface I"+strcase.ToCamel(serviceSpec.GetName())+"Client", + "extends joinGRPC.IClient> ", + ", joinGRPC.IExtendedClient {", + ) + r.indentLevel += 2 + + for _, methodSpec := range serviceSpec.GetMethod() { + r.generateTypescriptClientInterfaceMethod(generatedFileStream, serviceSpec, methodSpec) + } + + r.indentLevel -= 2 + r.P(generatedFileStream, "}\n") +} + +func (r *Runner) generateTypescriptClientInterfaceMethod(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto, methodSpec *descriptorpb.MethodDescriptorProto) { + // Function's Signature + methodName := strcase.ToCamel(methodSpec.GetName()) + r.P(generatedFileStream, methodName+"(") + r.indentLevel += 2 + + inputTypeName := methodSpec.GetInputType() + inputInterface := r.getEnumOrMessageTypeName(inputTypeName, true) + outputTypeName := methodSpec.GetOutputType() + outputInterface := r.getEnumOrMessageTypeName(outputTypeName, true) + + clientStream := methodSpec.GetClientStreaming() + serverStrean := methodSpec.GetServerStreaming() + + if !clientStream { + r.P(generatedFileStream, "request: "+inputInterface+",") + } + + r.P(generatedFileStream, + "metadata?: Record,", + "options?: grpc.CallOptions,", + ) + + var returnType string + if clientStream && serverStrean { + returnType = "grpc.ClientDuplexStream<" + inputInterface + ", " + outputInterface + ">" + } else if !clientStream && !serverStrean { + returnType = "joinGRPC.IUnaryRequest<" + outputInterface + ">" + } else if clientStream { + returnType = "joinGRPC.IClientStreamRequest<" + inputInterface + ", " + outputInterface + ">" + } else { // if serverStream + returnType = "grpc.ClientReadableStream<" + outputInterface + ">" + } + + r.indentLevel -= 2 + r.P(generatedFileStream, "): "+returnType) +} + func (r *Runner) generateTypescriptClient(generatedFileStream *protogen.GeneratedFile, serviceSpec *descriptorpb.ServiceDescriptorProto) { r.P( generatedFileStream, "export class "+strcase.ToCamel(serviceSpec.GetName())+"Client", "extends joinGRPC.Client> ", - "implements joinGRPC.IExtendedClient {", + "implements I"+strcase.ToCamel(serviceSpec.GetName())+"Client {", ) r.indentLevel += 2 diff --git a/tests/__tests__/v2/generated/Test.ts b/tests/__tests__/v2/generated/Test.ts index 64994ad..57e52f3 100644 --- a/tests/__tests__/v2/generated/Test.ts +++ b/tests/__tests__/v2/generated/Test.ts @@ -485,9 +485,34 @@ export namespace Foo { }, } + export interface IUsersClient + extends joinGRPC.IClient< + grpc.ServiceDefinition + >, + joinGRPC.IExtendedClient { + Find( + request: IRequest, + metadata?: Record, + options?: grpc.CallOptions + ): joinGRPC.IUnaryRequest + FindClientStream( + metadata?: Record, + options?: grpc.CallOptions + ): joinGRPC.IClientStreamRequest + FindServerStream( + request: IRequest, + metadata?: Record, + options?: grpc.CallOptions + ): grpc.ClientReadableStream + FindBidiStream( + metadata?: Record, + options?: grpc.CallOptions + ): grpc.ClientDuplexStream + } + export class UsersClient extends joinGRPC.Client> - implements joinGRPC.IExtendedClient { + implements IUsersClient { public Find( request: IRequest, metadata?: Record,