Skip to content

Commit

Permalink
Merge pull request #14 from join-com/simplify-client-interface
Browse files Browse the repository at this point in the history
feat: extract final client interface
  • Loading branch information
castarco authored Apr 16, 2021
2 parents 82f4f62 + 1e52c24 commit 6606200
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
58 changes: 57 additions & 1 deletion internal/generator/runer_generate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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<grpc.ServiceDefinition<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation>> ",
", joinGRPC.IExtendedClient<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation> {",
)
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<string, string>,",
"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<grpc.ServiceDefinition<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation>> ",
"implements joinGRPC.IExtendedClient<I"+strcase.ToCamel(serviceSpec.GetName())+"ServiceImplementation> {",
"implements I"+strcase.ToCamel(serviceSpec.GetName())+"Client {",
)
r.indentLevel += 2

Expand Down
27 changes: 26 additions & 1 deletion tests/__tests__/v2/generated/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,34 @@ export namespace Foo {
},
}

export interface IUsersClient
extends joinGRPC.IClient<
grpc.ServiceDefinition<IUsersServiceImplementation>
>,
joinGRPC.IExtendedClient<IUsersServiceImplementation> {
Find(
request: IRequest,
metadata?: Record<string, string>,
options?: grpc.CallOptions
): joinGRPC.IUnaryRequest<Common_Common.IOtherPkgMessage>
FindClientStream(
metadata?: Record<string, string>,
options?: grpc.CallOptions
): joinGRPC.IClientStreamRequest<IRequest, Common_Common.IOtherPkgMessage>
FindServerStream(
request: IRequest,
metadata?: Record<string, string>,
options?: grpc.CallOptions
): grpc.ClientReadableStream<Common_Common.IOtherPkgMessage>
FindBidiStream(
metadata?: Record<string, string>,
options?: grpc.CallOptions
): grpc.ClientDuplexStream<IRequest, Common_Common.IOtherPkgMessage>
}

export class UsersClient
extends joinGRPC.Client<grpc.ServiceDefinition<IUsersServiceImplementation>>
implements joinGRPC.IExtendedClient<IUsersServiceImplementation> {
implements IUsersClient {
public Find(
request: IRequest,
metadata?: Record<string, string>,
Expand Down

0 comments on commit 6606200

Please sign in to comment.