Skip to content

Commit

Permalink
fix: ensure default service streaming methods compile when middleware…
Browse files Browse the repository at this point in the history
… methods are enabled (#996)

* mostly done, need to write tests

* run bin2ts

* tests passing

* improvements
  • Loading branch information
lukealvoeiro committed Feb 3, 2024
1 parent 8dc2b5e commit a9e975b
Show file tree
Hide file tree
Showing 8 changed files with 1,884 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Observable, map, of } from "rxjs";
import { DashStateClientImpl, DashUserSettingsState, DashStateServiceName } from "./example";

interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
clientStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Promise<Uint8Array>;
serverStreamingRequest(service: string, method: string, data: Uint8Array): Observable<Uint8Array>;
bidirectionalStreamingRequest(service: string, method: string, data: Observable<Uint8Array>): Observable<Uint8Array>;
beforeRequest?<T extends { [k in keyof T]: unknown }>(service: string, method: string, request: T): void;
afterResponse?<T extends { [k in keyof T]: unknown }>(service: string, method: string, response: T): void;
handleError?(service: string, method: string, error: Error): Error;
}

describe("before-after-request-streaming", () => {
const reqData = {
email: "john.cena@gmail.com",
urls: undefined,
flashes: [{ msg: "dun dun dun dun", type: 0 }],
};
const resData = {
email: "cena.john@gmail.com",
urls: undefined,
flashes: [{ msg: "dun dun dun dun", type: 0 }],
};
let rpc: Rpc = {
request: jest.fn(() => Promise.resolve(new Uint8Array([21]))),
clientStreamingRequest: jest.fn(() => Promise.resolve(new Uint8Array([21]))),
serverStreamingRequest: jest.fn(() => of(new Uint8Array([21]))),
bidirectionalStreamingRequest: (service: string, method: string, data: Observable<Uint8Array>) => {
return data.pipe(map((data) => new Uint8Array([21])));
},
};
let client = new DashStateClientImpl(rpc);
const beforeRequest = jest.fn();
const afterResponse = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(DashUserSettingsState, "decode").mockReturnValue(resData);
});

it("compiles", () => {
const service = new DashStateClientImpl(rpc);
expect(service).not.toBeUndefined();
});

it("performs function before request if specified", () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc, beforeRequest: beforeRequest });
const reqObservable = of(req);
client.ChangeUserSettingsStream(reqObservable).subscribe();
expect(beforeRequest).toHaveBeenCalledWith(
DashStateServiceName,
"ChangeUserSettingsStream",
DashUserSettingsState.encode(req).finish(),
);
});

it("performs function after request if specified", async () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc, afterResponse: afterResponse });
const reqObservable = of(req);
client.ChangeUserSettingsStream(reqObservable).subscribe();
expect(afterResponse).toHaveBeenCalledWith(DashStateServiceName, "ChangeUserSettingsStream", resData);
});

it("doesn't perform function before or after request if they are not specified", async () => {
const req = DashUserSettingsState.create(reqData);
client = new DashStateClientImpl({ ...rpc });
await client.ChangeUserSettingsStream(of(req));
expect(beforeRequest).not.toHaveBeenCalled();
expect(afterResponse).not.toHaveBeenCalled();
});
});
Binary file not shown.
70 changes: 70 additions & 0 deletions integration/before-after-request-streaming/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
syntax = "proto3";
import "google/protobuf/wrappers.proto";
package rpx;

service DashState {
rpc UserSettings(Empty) returns (DashUserSettingsState);
rpc ActiveUserSettingsStream(Empty) returns (stream DashUserSettingsState);
// not supported in grpc-web, but should still compile
rpc ChangeUserSettingsStream (stream DashUserSettingsState) returns (stream DashUserSettingsState) {}
}

message DashFlash {
string msg = 1;
Type type = 2;

enum Type {
Undefined = 0;
Success = 1;
Warn = 2;
Error = 3;
}
}

message DashUserSettingsState {
string email = 1;
URLs urls = 6;
repeated DashFlash flashes = 7;

message URLs {
string connect_google = 1;
string connect_github = 2;
}
}


//----------------------
// API Creds
//----------------------
service DashAPICreds {
rpc Create(DashAPICredsCreateReq) returns (DashCred);
rpc Update(DashAPICredsUpdateReq) returns (DashCred);
rpc Delete(DashAPICredsDeleteReq) returns (DashCred);
rpc Uppercase(google.protobuf.StringValue) returns (google.protobuf.StringValue);
}

message DashCred {
string description = 2;
string metadata = 3;
string token = 4;
string id = 7;
}

message DashAPICredsCreateReq {
string description = 1;
string metadata = 2;
}

message DashAPICredsUpdateReq {
string cred_sid = 1;
string description = 2;
string metadata = 3;
string id = 5;
}

message DashAPICredsDeleteReq {
string cred_sid = 1;
string id = 3;
}

message Empty {}
Loading

0 comments on commit a9e975b

Please sign in to comment.