Skip to content

Commit

Permalink
grpc-loader: map method options and add MethodOptions interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hiepthai authored and n0v1 committed Apr 5, 2024
1 parent c8b5e05 commit fc9db76
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions packages/proto-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestType, ResponseType, OutputRequestType=RequestType, OutputResponseType=ResponseType> {
path: string;
requestStream: boolean;
Expand All @@ -126,8 +173,7 @@ export interface MethodDefinition<RequestType, ResponseType, OutputRequestType=R
originalName?: string;
requestType: MessageTypeDefinition;
responseType: MessageTypeDefinition;
options?: { [k: string]: any };
parsedOptions?: { [k: string]: any };
options?: MethodOptions;
}

export interface ServiceDefinition {
Expand Down Expand Up @@ -222,6 +268,12 @@ function createSerializer(cls: Protobuf.Type): Serialize<object> {
};
}

function mapMethodOptions(options: Partial<MethodOptions>[] | undefined): MethodOptions | undefined {
return Array.isArray(options) ?
options.reduce((obj: MethodOptions, item: Partial<MethodOptions>) => ({ ...obj, ...item }), {}) :
undefined;
}

function createMethodDefinition(
method: Protobuf.Method,
serviceName: string,
Expand All @@ -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),
};
}

Expand Down

0 comments on commit fc9db76

Please sign in to comment.