Skip to content

Commit

Permalink
Add missing types definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
stanley-cheung committed Jul 8, 2020
1 parent 664fd9c commit d6d2b6e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
6 changes: 3 additions & 3 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,10 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
"client_: grpcWeb.AbstractClientBase;\n"
"hostname_: string;\n"
"credentials_: null | { [index: string]: string; };\n"
"options_: null | { [index: string]: string; };\n\n"
"options_: null | { [index: string]: any; };\n\n"
"constructor (hostname: string,\n"
" credentials?: null | { [index: string]: string; },\n"
" options?: null | { [index: string]: string; }) {\n");
" options?: null | { [index: string]: any; }) {\n");
printer->Indent();
printer->Print("if (!options) options = {};\n");
printer->Print("if (!credentials) credentials = {};\n");
Expand Down Expand Up @@ -765,7 +765,7 @@ void PrintGrpcWebDtsClientClass(Printer* printer, const FileDescriptor* file,
printer->Print(
"constructor (hostname: string,\n"
" credentials?: null | { [index: string]: string; },\n"
" options?: null | { [index: string]: string; });\n\n");
" options?: null | { [index: string]: any; });\n\n");
for (int method_index = 0; method_index < service->method_count();
++method_index) {
const MethodDescriptor* method = service->method(method_index);
Expand Down
36 changes: 35 additions & 1 deletion net/grpc/gateway/examples/echo/ts-example/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,41 @@ class EchoApp {
}
}

const echoService = new EchoServiceClient('http://localhost:8080', null, null);
class StreamResponseInterceptor implements grpcWeb.StreamInterceptor<any, any> {
intercept(
request: grpcWeb.Request<any, any>,
invoker: (request: grpcWeb.Request<any, any>) =>
grpcWeb.ClientReadableStream<any>) {
class InterceptedStream implements grpcWeb.ClientReadableStream<any> {
stream: grpcWeb.ClientReadableStream<any>;
constructor(stream: grpcWeb.ClientReadableStream<any>) {
this.stream = stream;
};
on(eventType: string, callback: any) {
if (eventType == 'data') {
const newCallback = (response: any) => {
response.setMessage('[Intcpt Resp1]'+response.getMessage());
callback(response);
};
this.stream.on(eventType, newCallback);
} else {
this.stream.on(eventType, callback);
}
return this;
};
removeListener(eventType: string, callback: any) {
}
cancel() {}
}
var reqMsg = request.getRequestMessage();
reqMsg.setMessage('[Intcpt Req1]'+reqMsg.getMessage());
return new InterceptedStream(invoker(request));
};
}

var opts = {'streamInterceptors' : [new StreamResponseInterceptor()]};

const echoService = new EchoServiceClient('http://localhost:8080', null, opts);

const echoApp = new EchoApp(echoService);
echoApp.load();
60 changes: 50 additions & 10 deletions packages/grpc-web/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,71 @@ declare module "grpc-web" {
}

export class ClientReadableStream<Response> {
on (type: "error",
on (eventType: "error",
callback: (err: Error) => void): ClientReadableStream<Response>;
on (type: "status",
on (eventType: "status",
callback: (status: Status) => void): ClientReadableStream<Response>;
on (type: "metadata",
on (eventType: "metadata",
callback: (status: Metadata) => void): ClientReadableStream<Response>;
on (type: "data",
on (eventType: "data",
callback: (response: Response) => void): ClientReadableStream<Response>;
on (type: "end",
on (eventType: "end",
callback: () => void): ClientReadableStream<Response>;
on (eventType: string,
callback: any): ClientReadableStream<Response>;

removeListener (type: "error",
removeListener (eventType: "error",
callback: (err: Error) => void): void;
removeListener (type: "status",
removeListener (eventType: "status",
callback: (status: Status) => void): void;
removeListener (type: "metadata",
removeListener (eventType: "metadata",
callback: (status: Metadata) => void): void;
removeListener (type: "data",
removeListener (eventType: "data",
callback: (response: Response) => void): void;
removeListener (type: "end",
removeListener (eventType: "end",
callback: () => void): void;
removeListener (eventType: string,
callback: any): void;

cancel (): void;
}

export interface StreamInterceptor<Req, Resp> {
intercept(request: Request<Req, Resp>,
invoker: (request: Request<Req, Resp>) =>
ClientReadableStream<Resp>): ClientReadableStream<Resp>;
}

export class CallOptions {
constructor(options: { [index: string]: any; });
}

export class MethodDescriptor<Req, Resp> {
constructor(name: string,
methodType: any,
requestType: any,
responseType: any,
requestSerializeFn: any,
responseDeserializeFn: any);
createRequest(requestMessage: Req,
metadata: Metadata,
callOptions: CallOptions): UnaryResponse<Req, Resp>;
}

export class Request<Req, Resp> {
getRequestMessage(): Req;
getMethodDescriptor(): MethodDescriptor<Req, Resp>;
getMetadata(): Metadata;
getCallOptions(): CallOptions;
}

export class UnaryResponse<Req, Resp> {
getResponseMessage(): Resp;
getMetadata(): Metadata;
getMethodDescriptor(): MethodDescriptor<Req, Resp>;
getStatus(): Status;
}

export interface GrpcWebClientBaseOptions {
format?: string;
suppressCorsPreflight?: boolean;
Expand Down

0 comments on commit d6d2b6e

Please sign in to comment.