Skip to content

Commit

Permalink
Correct generated client, bidi stream method stubs
Browse files Browse the repository at this point in the history
Fixes #132, #133
  • Loading branch information
johanbrandhorst committed Oct 20, 2018
1 parent 51f57ea commit 93c4444
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions examples/generated/proto/orphan_pb_service.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ function generateTypescriptDefinition(fileDescriptor: FileDescriptorProto, expor
printer.printIndentedLn(`on(type: 'end', handler: () => void): RequestStream<T>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): RequestStream<T>;`);
printer.printLn(`}`);
printer.printLn(`interface BidirectionalStream<T> {`);
printer.printIndentedLn(`write(message: T): BidirectionalStream<T>;`);
printer.printLn(`interface BidirectionalStream<ReqT, ResT> {`);
printer.printIndentedLn(`write(message: ReqT): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`end(): void;`);
printer.printIndentedLn(`cancel(): void;`);
printer.printIndentedLn(`on(type: 'data', handler: (message: T) => void): BidirectionalStream<T>;`);
printer.printIndentedLn(`on(type: 'end', handler: () => void): BidirectionalStream<T>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): BidirectionalStream<T>;`);
printer.printIndentedLn(`on(type: 'data', handler: (message: ResT) => void): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`on(type: 'end', handler: () => void): BidirectionalStream<ReqT, ResT>;`);
printer.printIndentedLn(`on(type: 'status', handler: (status: Status) => void): BidirectionalStream<ReqT, ResT>;`);
printer.printLn(`}`);
printer.printEmptyLn();

Expand Down Expand Up @@ -531,9 +531,9 @@ function printServerStreamStubMethodTypes(printer: CodePrinter, method: RPCMetho
}

function printClientStreamStubMethodTypes(printer: CodePrinter, method: RPCMethodDescriptor) {
printer.printLn(`${method.nameAsCamelCase}(metadata?: grpc.Metadata): RequestStream<${method.responseType}>;`);
printer.printLn(`${method.nameAsCamelCase}(metadata?: grpc.Metadata): RequestStream<${method.requestType}>;`);
}

function printBidirectionalStubMethodTypes(printer: CodePrinter, method: RPCMethodDescriptor) {
printer.printLn(`${method.nameAsCamelCase}(metadata?: grpc.Metadata): BidirectionalStream<${method.responseType}>;`);
printer.printLn(`${method.nameAsCamelCase}(metadata?: grpc.Metadata): BidirectionalStream<${method.requestType}, ${method.responseType}>;`);
}
18 changes: 13 additions & 5 deletions test/integration/service/grpcweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ describe("service/grpc-web", () => {
});

describe("client streaming", () => {
const [ payload ] = makePayloads("some value");
const payload = new StreamRequest();
payload.setSomeString("some value");

it("should route the request to the expected endpoint", (done) => {
let targetUrl = "";
Expand Down Expand Up @@ -389,7 +390,10 @@ describe("service/grpc-web", () => {
});

it("should allow the caller to supply multiple messages", (done) => {
const [ reqMsgOne, reqMsgTwo ] = makePayloads("one", "two");
const reqMsgOne = new StreamRequest();
reqMsgOne.setSomeString("one");
const reqMsgTwo = new StreamRequest();
reqMsgTwo.setSomeString("two");
const sentMessageBytes: ArrayBufferView[] = [];

makeClient(new StubTransportBuilder().withMessageListener(v => { sentMessageBytes.push(v); }))
Expand Down Expand Up @@ -449,7 +453,8 @@ describe("service/grpc-web", () => {
});

describe("bidirectional streaming", () => {
const [ payload ] = makePayloads("some value");
const payload = new StreamRequest();
payload.setSomeString("some value");

it("should route the request to the expected endpoint", (done) => {
let targetUrl = "";
Expand Down Expand Up @@ -503,7 +508,7 @@ describe("service/grpc-web", () => {

it("should handle an error returned ahead of any data by the server", (done) => {
makeClient(new StubTransportBuilder().withPreMessagesError(grpc.Code.Internal, "some error"))
.doClientStream()
.doBidiStream()
.on("status", (status) => {
assert.equal(status.code, grpc.Code.Internal, "expected grpc status code returned");
assert.equal(status.details, "some error", "expected grpc error details returned");
Expand Down Expand Up @@ -531,7 +536,10 @@ describe("service/grpc-web", () => {
});

it("should allow the caller to supply multiple messages", (done) => {
const [ reqMsgOne, reqMsgTwo ] = makePayloads("one", "two");
const reqMsgOne = new StreamRequest();
reqMsgOne.setSomeString("one");
const reqMsgTwo = new StreamRequest();
reqMsgTwo.setSomeString("two");
const sentMessageBytes: ArrayBufferView[] = [];

makeClient(new StubTransportBuilder().withMessageListener(v => { sentMessageBytes.push(v); }))
Expand Down

0 comments on commit 93c4444

Please sign in to comment.