Skip to content

Commit fc9db76

Browse files
hiepthain0v1
authored andcommitted
grpc-loader: map method options and add MethodOptions interface
1 parent c8b5e05 commit fc9db76

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

packages/proto-loader/src/index.ts

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,53 @@ export interface EnumTypeDefinition extends ProtobufTypeDefinition {
115115
format: 'Protocol Buffer 3 EnumDescriptorProto';
116116
}
117117

118+
enum IdempotencyLevel {
119+
IDEMPOTENCY_UNKNOWN = 0,
120+
NO_SIDE_EFFECTS = 1,
121+
IDEMPOTENT = 2
122+
}
123+
124+
interface NamePart {
125+
namePart: string;
126+
isExtension: boolean;
127+
}
128+
129+
interface UninterpretedOption {
130+
name?: (NamePart[]|null);
131+
identifierValue?: (string|null);
132+
positiveIntValue?: (number|Long|string|null);
133+
negativeIntValue?: (number|Long|string|null);
134+
doubleValue?: (number|null);
135+
stringValue?: (Uint8Array|string|null);
136+
aggregateValue?: (string|null);
137+
}
138+
139+
interface CustomHttpPattern {
140+
kind?: (string|null);
141+
path?: (string|null);
142+
}
143+
144+
interface HttpRule {
145+
selector?: (string|null);
146+
get?: (string|null);
147+
put?: (string|null);
148+
post?: (string|null);
149+
delete?: (string|null);
150+
patch?: (string|null);
151+
custom?: (CustomHttpPattern|null);
152+
body?: (string|null);
153+
responseBody?: (string|null);
154+
additionalBindings?: (HttpRule[]|null);
155+
}
156+
157+
interface MethodOptions {
158+
deprecated?: (boolean|null);
159+
idempotency_level?: (IdempotencyLevel|keyof typeof IdempotencyLevel|null);
160+
uninterpreted_option?: (UninterpretedOption[]|null);
161+
"(google.api.http)"?: (HttpRule|null);
162+
"(google.api.method_signature)"?: (string[]|null);
163+
}
164+
118165
export interface MethodDefinition<RequestType, ResponseType, OutputRequestType=RequestType, OutputResponseType=ResponseType> {
119166
path: string;
120167
requestStream: boolean;
@@ -126,8 +173,7 @@ export interface MethodDefinition<RequestType, ResponseType, OutputRequestType=R
126173
originalName?: string;
127174
requestType: MessageTypeDefinition;
128175
responseType: MessageTypeDefinition;
129-
options?: { [k: string]: any };
130-
parsedOptions?: { [k: string]: any };
176+
options?: MethodOptions;
131177
}
132178

133179
export interface ServiceDefinition {
@@ -222,6 +268,12 @@ function createSerializer(cls: Protobuf.Type): Serialize<object> {
222268
};
223269
}
224270

271+
function mapMethodOptions(options: Partial<MethodOptions>[] | undefined): MethodOptions | undefined {
272+
return Array.isArray(options) ?
273+
options.reduce((obj: MethodOptions, item: Partial<MethodOptions>) => ({ ...obj, ...item }), {}) :
274+
undefined;
275+
}
276+
225277
function createMethodDefinition(
226278
method: Protobuf.Method,
227279
serviceName: string,
@@ -244,8 +296,7 @@ function createMethodDefinition(
244296
originalName: camelCase(method.name),
245297
requestType: createMessageDefinition(requestType, fileDescriptors),
246298
responseType: createMessageDefinition(responseType, fileDescriptors),
247-
options: method.options,
248-
parsedOptions: method.parsedOptions,
299+
options: mapMethodOptions(method.parsedOptions),
249300
};
250301
}
251302

0 commit comments

Comments
 (0)