Skip to content

Commit

Permalink
update typescript generation to work in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
henriiik committed Oct 27, 2018
1 parent 3956048 commit ec54a0f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
18 changes: 10 additions & 8 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
printer->Print(
"client_: grpcWeb.AbstractClientBase;\n"
"hostname_: string;\n"
"credentials_: {};\n"
"options_: { [s: string]: {}; };\n\n"
"credentials_: null | {};\n"
"options_: null | { [s: string]: {}; };\n\n"
"constructor (hostname: string,\n"
" credentials: {},\n"
" options: { [s: string]: {}; }) {\n");
" credentials: null | {},\n"
" options: null | { [s: string]: {}; }) {\n");
printer->Indent();
printer->Print("if (!options) options = {};\n");
if (vars["mode"] == GetModeVar(Mode::GRPCWEB)) {
Expand Down Expand Up @@ -466,8 +466,8 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
printer->Indent();
printer->Print(
"constructor (hostname: string,\n"
" credentials: {},\n"
" options: { [s: string]: {}; });\n\n");
" credentials: null | {},\n"
" options: null | { [s: string]: {}; });\n\n");
for (int method_index = 0; method_index < service->method_count();
++method_index) {
const MethodDescriptor* method = service->method(method_index);
Expand All @@ -482,7 +482,8 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata\n");
printer->Outdent();
printer->Print("): grpcWeb.ClientReadableStream;\n\n");
printer->Print(vars,
"): grpcWeb.ClientReadableStream<$output_type$>;\n\n");
} else {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
Expand All @@ -492,7 +493,8 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void\n");
printer->Outdent();
printer->Print("): grpcWeb.ClientReadableStream;\n\n");
printer->Print(vars,
"): grpcWeb.ClientReadableStream<$output_type$>;\n\n");
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions net/grpc/gateway/examples/echo/ts-example/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ class EchoApp {
request.setMessageCount(count);
request.setMessageInterval(EchoApp.INTERVAL);

const stream: grpcWeb.ClientReadableStream =
this.echoService_.serverStreamingEcho(
request, {'custom-header-1': 'value1'});
const stream = this.echoService_.serverStreamingEcho(
request, {'custom-header-1': 'value1'});
const self = this;
stream.on('data', (response: ServerStreamingEchoResponse) => {
EchoApp.addRightMessage(response.getMessage());
Expand Down
2 changes: 1 addition & 1 deletion net/grpc/gateway/examples/echo/ts-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noImplicitAny": true,
"strict": true,
"allowJs": true,
"outDir": "./dist",
"types": ["node"],
Expand Down
36 changes: 21 additions & 15 deletions packages/grpc-web/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@ declare module "grpc-web" {
export interface Metadata { [s: string]: string; }

export namespace AbstractClientBase {
class MethodInfo {
constructor (responseType: {},
requestSerializeFn: (request: {}) => {},
responseDeserializeFn: (bytes: {}) => {});
class MethodInfo<Request, Response> {
constructor (responseType: new () => Response,
requestSerializeFn: (request: Request) => {},
responseDeserializeFn: (bytes: {}) => Response);
}
}

export class AbstractClientBase {
rpcCall (method: string,
request: {},
rpcCall<Request, Response> (method: string,
request: Request,
metadata: Metadata,
methodInfo: AbstractClientBase.MethodInfo,
callback: (err: Error, response: {}) => void
): ClientReadableStream;
methodInfo: AbstractClientBase.MethodInfo<Request, Response>,
callback: (err: Error, response: Response) => void
): ClientReadableStream<Response>;

serverStreaming (method: string,
request: {},
request: Request,
metadata: Metadata,
methodInfo: AbstractClientBase.MethodInfo
): ClientReadableStream;
methodInfo: AbstractClientBase.MethodInfo<Request, Response>
): ClientReadableStream<Response>;
}

export class ClientReadableStream {
on (type: string,
callback: (...args: Array<{}>) => void): ClientReadableStream;
export class ClientReadableStream<Response> {
on (type: "error",
callback: (err: Error) => void): ClientReadableStream<Response>;
on (type: "status",
callback: (status: Status) => void): ClientReadableStream<Response>;
on (type: "data",
callback: (response: Response) => void): ClientReadableStream<Response>;
on (type: "end",
callback: () => void): ClientReadableStream<Response>;
cancel (): void;
}

Expand Down

0 comments on commit ec54a0f

Please sign in to comment.