Skip to content

Commit c3a4926

Browse files
committed
proto-loader: generator: add specific service definition interfaces
1 parent 48afaf1 commit c3a4926

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

packages/proto-loader/bin/proto-loader-gen-types.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
126126
} else if (dependency instanceof Protobuf.Enum) {
127127
importedTypes = `${typeInterfaceName}`;
128128
} else if (dependency instanceof Protobuf.Service) {
129-
importedTypes = `${typeInterfaceName}Client`;
129+
importedTypes = `${typeInterfaceName}Client, ${typeInterfaceName}Definition`;
130130
} else {
131131
throw new Error('Invalid object passed to getImportLine');
132132
}
@@ -136,7 +136,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
136136
} else if (dependency instanceof Protobuf.Enum) {
137137
importedTypes = `${dependency.name} as ${typeInterfaceName}`;
138138
} else if (dependency instanceof Protobuf.Service) {
139-
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client`;
139+
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client, ${dependency.name}Definition as ${typeInterfaceName}Definition`;
140140
} else {
141141
throw new Error('Invalid object passed to getImportLine');
142142
}
@@ -540,11 +540,25 @@ function generateServiceHandlerInterface(formatter: TextFormatter, serviceType:
540540
formatter.writeLine('}');
541541
}
542542

543+
function generateServiceDefinitionInterface(formatter: TextFormatter, serviceType: Protobuf.Service) {
544+
formatter.writeLine(`export interface ${serviceType.name}Definition {`);
545+
formatter.indent();
546+
for (const methodName of Object.keys(serviceType.methods).sort()) {
547+
const method = serviceType.methods[methodName];
548+
const requestType = getTypeInterfaceName(method.resolvedRequestType!);
549+
const responseType = getTypeInterfaceName(method.resolvedResponseType!);
550+
formatter.writeLine(`${methodName}: MethodDefinition<${requestType}, ${responseType}, ${requestType}__Output, ${responseType}__Output>`);
551+
}
552+
formatter.unindent();
553+
formatter.writeLine('}')
554+
}
555+
543556
function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protobuf.Service, options: GeneratorOptions) {
544557
formatter.writeLine(`// Original file: ${serviceType.filename}`);
545558
formatter.writeLine('');
546559
const grpcImportPath = options.grpcLib.startsWith('.') ? getPathToRoot(serviceType) + options.grpcLib : options.grpcLib;
547560
formatter.writeLine(`import type * as grpc from '${grpcImportPath}'`);
561+
formatter.writeLine(`import type { MethodDefinition } from '@grpc/proto-loader'`)
548562
const dependencies: Set<Protobuf.Type> = new Set<Protobuf.Type>();
549563
for (const method of serviceType.methodsArray) {
550564
dependencies.add(method.resolvedRequestType!);
@@ -559,6 +573,9 @@ function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protob
559573
formatter.writeLine('');
560574

561575
generateServiceHandlerInterface(formatter, serviceType, options);
576+
formatter.writeLine('');
577+
578+
generateServiceDefinitionInterface(formatter, serviceType);
562579
}
563580

564581
function generateServiceImports(formatter: TextFormatter, namespace: Protobuf.NamespaceBase, options: GeneratorOptions) {
@@ -576,7 +593,8 @@ function generateSingleLoadedDefinitionType(formatter: TextFormatter, nested: Pr
576593
if (options.includeComments) {
577594
formatComment(formatter, nested.comment);
578595
}
579-
formatter.writeLine(`${nested.name}: SubtypeConstructor<typeof grpc.Client, ${getTypeInterfaceName(nested)}Client> & { service: ServiceDefinition }`)
596+
const typeInterfaceName = getTypeInterfaceName(nested);
597+
formatter.writeLine(`${nested.name}: SubtypeConstructor<typeof grpc.Client, ${typeInterfaceName}Client> & { service: ${typeInterfaceName}Definition }`);
580598
} else if (nested instanceof Protobuf.Enum) {
581599
formatter.writeLine(`${nested.name}: EnumTypeDefinition`);
582600
} else if (nested instanceof Protobuf.Type) {

packages/proto-loader/golden-generated/echo.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type * as grpc from '@grpc/grpc-js';
22
import type { ServiceDefinition, EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';
33

4-
import type { OperationsClient as _google_longrunning_OperationsClient } from './google/longrunning/Operations';
5-
import type { EchoClient as _google_showcase_v1beta1_EchoClient } from './google/showcase/v1beta1/Echo';
4+
import type { OperationsClient as _google_longrunning_OperationsClient, OperationsDefinition as _google_longrunning_OperationsDefinition } from './google/longrunning/Operations';
5+
import type { EchoClient as _google_showcase_v1beta1_EchoClient, EchoDefinition as _google_showcase_v1beta1_EchoDefinition } from './google/showcase/v1beta1/Echo';
66

77
type SubtypeConstructor<Constructor extends new (...args: any) => any, Subtype> = {
88
new(...args: ConstructorParameters<Constructor>): Subtype;
@@ -35,7 +35,7 @@ export interface ProtoGrpcType {
3535
* returns long-running operations should implement the `Operations` interface
3636
* so developers can have a consistent client experience.
3737
*/
38-
Operations: SubtypeConstructor<typeof grpc.Client, _google_longrunning_OperationsClient> & { service: ServiceDefinition }
38+
Operations: SubtypeConstructor<typeof grpc.Client, _google_longrunning_OperationsClient> & { service: _google_longrunning_OperationsDefinition }
3939
WaitOperationRequest: MessageTypeDefinition
4040
}
4141
protobuf: {
@@ -78,7 +78,7 @@ export interface ProtoGrpcType {
7878
* paginated calls. Set the 'showcase-trailer' metadata key on any method
7979
* to have the values echoed in the response trailers.
8080
*/
81-
Echo: SubtypeConstructor<typeof grpc.Client, _google_showcase_v1beta1_EchoClient> & { service: ServiceDefinition }
81+
Echo: SubtypeConstructor<typeof grpc.Client, _google_showcase_v1beta1_EchoClient> & { service: _google_showcase_v1beta1_EchoDefinition }
8282
EchoRequest: MessageTypeDefinition
8383
EchoResponse: MessageTypeDefinition
8484
ExpandRequest: MessageTypeDefinition

packages/proto-loader/golden-generated/google/longrunning/Operations.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Original file: deps/googleapis/google/longrunning/operations.proto
22

33
import type * as grpc from '@grpc/grpc-js'
4+
import type { MethodDefinition } from '@grpc/proto-loader'
45
import type { CancelOperationRequest as _google_longrunning_CancelOperationRequest, CancelOperationRequest__Output as _google_longrunning_CancelOperationRequest__Output } from '../../google/longrunning/CancelOperationRequest';
56
import type { DeleteOperationRequest as _google_longrunning_DeleteOperationRequest, DeleteOperationRequest__Output as _google_longrunning_DeleteOperationRequest__Output } from '../../google/longrunning/DeleteOperationRequest';
67
import type { Empty as _google_protobuf_Empty, Empty__Output as _google_protobuf_Empty__Output } from '../../google/protobuf/Empty';
@@ -230,3 +231,11 @@ export interface OperationsHandlers extends grpc.UntypedServiceImplementation {
230231
WaitOperation: grpc.handleUnaryCall<_google_longrunning_WaitOperationRequest__Output, _google_longrunning_Operation>;
231232

232233
}
234+
235+
export interface OperationsDefinition {
236+
CancelOperation: MethodDefinition<_google_longrunning_CancelOperationRequest, _google_protobuf_Empty, _google_longrunning_CancelOperationRequest__Output, _google_protobuf_Empty__Output>
237+
DeleteOperation: MethodDefinition<_google_longrunning_DeleteOperationRequest, _google_protobuf_Empty, _google_longrunning_DeleteOperationRequest__Output, _google_protobuf_Empty__Output>
238+
GetOperation: MethodDefinition<_google_longrunning_GetOperationRequest, _google_longrunning_Operation, _google_longrunning_GetOperationRequest__Output, _google_longrunning_Operation__Output>
239+
ListOperations: MethodDefinition<_google_longrunning_ListOperationsRequest, _google_longrunning_ListOperationsResponse, _google_longrunning_ListOperationsRequest__Output, _google_longrunning_ListOperationsResponse__Output>
240+
WaitOperation: MethodDefinition<_google_longrunning_WaitOperationRequest, _google_longrunning_Operation, _google_longrunning_WaitOperationRequest__Output, _google_longrunning_Operation__Output>
241+
}

packages/proto-loader/golden-generated/google/showcase/v1beta1/Echo.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Original file: deps/gapic-showcase/schema/google/showcase/v1beta1/echo.proto
22

33
import type * as grpc from '@grpc/grpc-js'
4+
import type { MethodDefinition } from '@grpc/proto-loader'
45
import type { BlockRequest as _google_showcase_v1beta1_BlockRequest, BlockRequest__Output as _google_showcase_v1beta1_BlockRequest__Output } from '../../../google/showcase/v1beta1/BlockRequest';
56
import type { BlockResponse as _google_showcase_v1beta1_BlockResponse, BlockResponse__Output as _google_showcase_v1beta1_BlockResponse__Output } from '../../../google/showcase/v1beta1/BlockResponse';
67
import type { EchoRequest as _google_showcase_v1beta1_EchoRequest, EchoRequest__Output as _google_showcase_v1beta1_EchoRequest__Output } from '../../../google/showcase/v1beta1/EchoRequest';
@@ -189,3 +190,13 @@ export interface EchoHandlers extends grpc.UntypedServiceImplementation {
189190
Wait: grpc.handleUnaryCall<_google_showcase_v1beta1_WaitRequest__Output, _google_longrunning_Operation>;
190191

191192
}
193+
194+
export interface EchoDefinition {
195+
Block: MethodDefinition<_google_showcase_v1beta1_BlockRequest, _google_showcase_v1beta1_BlockResponse, _google_showcase_v1beta1_BlockRequest__Output, _google_showcase_v1beta1_BlockResponse__Output>
196+
Chat: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
197+
Collect: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
198+
Echo: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
199+
Expand: MethodDefinition<_google_showcase_v1beta1_ExpandRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_ExpandRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
200+
PagedExpand: MethodDefinition<_google_showcase_v1beta1_PagedExpandRequest, _google_showcase_v1beta1_PagedExpandResponse, _google_showcase_v1beta1_PagedExpandRequest__Output, _google_showcase_v1beta1_PagedExpandResponse__Output>
201+
Wait: MethodDefinition<_google_showcase_v1beta1_WaitRequest, _google_longrunning_Operation, _google_showcase_v1beta1_WaitRequest__Output, _google_longrunning_Operation__Output>
202+
}

packages/proto-loader/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ export interface EnumTypeDefinition extends ProtobufTypeDefinition {
115115
format: 'Protocol Buffer 3 EnumDescriptorProto';
116116
}
117117

118-
export interface MethodDefinition<RequestType, ResponseType> {
118+
export interface MethodDefinition<RequestType, ResponseType, OutputRequestType=RequestType, OutputResponseType=ResponseType> {
119119
path: string;
120120
requestStream: boolean;
121121
responseStream: boolean;
122122
requestSerialize: Serialize<RequestType>;
123123
responseSerialize: Serialize<ResponseType>;
124-
requestDeserialize: Deserialize<RequestType>;
125-
responseDeserialize: Deserialize<ResponseType>;
124+
requestDeserialize: Deserialize<OutputRequestType>;
125+
responseDeserialize: Deserialize<OutputResponseType>;
126126
originalName?: string;
127127
requestType: MessageTypeDefinition;
128128
responseType: MessageTypeDefinition;

0 commit comments

Comments
 (0)