Skip to content

Commit

Permalink
fix: grpc context typings (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Aug 20, 2022
1 parent 28b7e40 commit 9449097
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
32 changes: 29 additions & 3 deletions packages/grpc/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@ import {
IMidwayContext,
NextFunction as BaseNextFunction
} from '@midwayjs/core';
import { Server, ServerCredentials, Metadata, ServerDuplexStream, ClientWritableStream, ClientDuplexStream, ClientReadableStream, ClientUnaryCall, ChannelOptions, ClientOptions } from '@grpc/grpc-js';
import {
Server,
ServerCredentials,
Metadata,
ClientWritableStream,
ClientDuplexStream,
ClientReadableStream,
ClientUnaryCall,
ChannelOptions,
ClientOptions,
ServerUnaryCall,
ServerReadableStream,
ServerWritableStream,
ServerDuplexStream,
} from '@grpc/grpc-js';

type GrpcHandleCall<RequestType, ResponseType> =
Partial<ServerUnaryCall<RequestType, ResponseType> &
ServerReadableStream<RequestType, ResponseType> &
ServerWritableStream<RequestType, ResponseType> &
ServerDuplexStream<RequestType, ResponseType>>;

export interface Context extends IMidwayContext<ServerDuplexStream<any, any>> {
metadata: Metadata;
export interface Context<RequestType = unknown, ResponseType = unknown> extends IMidwayContext<GrpcHandleCall<RequestType, ResponseType>> {
method: string;
}

export type IMidwayGRPCApplication = IMidwayApplication<Context, Server>;

export type Application = IMidwayGRPCApplication;
Expand Down Expand Up @@ -73,23 +93,29 @@ export interface DefaultConfig extends IConfigurationOptions {

export interface IClientUnaryService<reqType, resType> {
sendMessage(reqData: reqType, handler?: (call: ClientUnaryCall) => void): Promise<resType>;

sendMessageWithCallback(content: reqType, callback): ClientUnaryCall;
}

export interface IClientReadableStreamService<reqType, resType> {
sendMessage(reqData: reqType): Promise<resType[]>;

getCall(): ClientReadableStream<resType>;
}

export interface IClientWritableStreamService<reqType, resType> {
sendMessage(reqData: reqType): IClientWritableStreamService<reqType, resType>;

end(): Promise<resType>;

getCall(): ClientWritableStream<reqType>;
}

export interface IClientDuplexStreamService<reqType, resType> {
sendMessage(reqData: reqType): Promise<resType>;

getCall(): ClientDuplexStream<reqType, resType>;

end(): void;
}

Expand Down
12 changes: 7 additions & 5 deletions packages/grpc/src/provider/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
sendUnaryData,
Server,
ServerCredentials,
ServerUnaryCall,
setLogger,
UntypedServiceImplementation,
UntypedHandleCall,
ServerUnaryCall,
} from '@grpc/grpc-js';
import {
BaseFramework,
Expand Down Expand Up @@ -113,7 +115,7 @@ export class MidwayGRPCFramework extends BaseFramework<
classMetadata.serviceName || Utils.pascalCase(providerName);

if (serviceClassDefinition.has(classMetadata?.package)) {
const serviceInstance = {};
const serviceInstance = {} as UntypedServiceImplementation;
const serviceDefinition: any = serviceClassDefinition.get(
classMetadata.package
)[`${classMetadata?.package}.${serviceName}`];
Expand All @@ -126,11 +128,11 @@ export class MidwayGRPCFramework extends BaseFramework<

for (const method in serviceDefinition) {
serviceInstance[method] = async (
call: ServerUnaryCall<any, any>,
call: Parameters<UntypedHandleCall>[0],
callback?: sendUnaryData<any>
) => {
// merge ctx and call
const ctx = call as any;
const ctx = call as Context;
ctx.method = method;
this.app.createAnonymousContext(ctx);

Expand Down Expand Up @@ -178,7 +180,7 @@ export class MidwayGRPCFramework extends BaseFramework<
service,
ctx,
callback,
data: call.request,
data: (call as ServerUnaryCall<any, any>).request,
grpcMethodData,
});
}
Expand Down

0 comments on commit 9449097

Please sign in to comment.