From fc9db761a9b4ab0e2774a827d8b2da6efa88c0df Mon Sep 17 00:00:00 2001 From: Hiep Thai Date: Wed, 21 Sep 2022 10:45:42 +0700 Subject: [PATCH] grpc-loader: map method options and add MethodOptions interface --- packages/proto-loader/src/index.ts | 59 ++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/packages/proto-loader/src/index.ts b/packages/proto-loader/src/index.ts index 05ec158c..c1c78d28 100644 --- a/packages/proto-loader/src/index.ts +++ b/packages/proto-loader/src/index.ts @@ -115,6 +115,53 @@ export interface EnumTypeDefinition extends ProtobufTypeDefinition { format: 'Protocol Buffer 3 EnumDescriptorProto'; } +enum IdempotencyLevel { + IDEMPOTENCY_UNKNOWN = 0, + NO_SIDE_EFFECTS = 1, + IDEMPOTENT = 2 +} + +interface NamePart { + namePart: string; + isExtension: boolean; +} + +interface UninterpretedOption { + name?: (NamePart[]|null); + identifierValue?: (string|null); + positiveIntValue?: (number|Long|string|null); + negativeIntValue?: (number|Long|string|null); + doubleValue?: (number|null); + stringValue?: (Uint8Array|string|null); + aggregateValue?: (string|null); +} + +interface CustomHttpPattern { + kind?: (string|null); + path?: (string|null); +} + +interface HttpRule { + selector?: (string|null); + get?: (string|null); + put?: (string|null); + post?: (string|null); + delete?: (string|null); + patch?: (string|null); + custom?: (CustomHttpPattern|null); + body?: (string|null); + responseBody?: (string|null); + additionalBindings?: (HttpRule[]|null); +} + +interface MethodOptions { + deprecated?: (boolean|null); + idempotency_level?: (IdempotencyLevel|keyof typeof IdempotencyLevel|null); + uninterpreted_option?: (UninterpretedOption[]|null); + "(google.api.http)"?: (HttpRule|null); + "(google.api.method_signature)"?: (string[]|null); +} + export interface MethodDefinition { path: string; requestStream: boolean; @@ -126,8 +173,7 @@ export interface MethodDefinition { }; } +function mapMethodOptions(options: Partial[] | undefined): MethodOptions | undefined { + return Array.isArray(options) ? + options.reduce((obj: MethodOptions, item: Partial) => ({ ...obj, ...item }), {}) : + undefined; +} + function createMethodDefinition( method: Protobuf.Method, serviceName: string, @@ -244,8 +296,7 @@ function createMethodDefinition( originalName: camelCase(method.name), requestType: createMessageDefinition(requestType, fileDescriptors), responseType: createMessageDefinition(responseType, fileDescriptors), - options: method.options, - parsedOptions: method.parsedOptions, + options: mapMethodOptions(method.parsedOptions), }; }