Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add before and after request methods to base service #961

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=outputServices=false`, or `=none`, ts-proto will output NO service definitions.

- With `--ts_proto_opt=outputBeforeRequest=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `beforeRequest(request: <RequestType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `beforeRequest` before performing it's request.

- With `--ts_proto_opt=outputAfterResponse=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `afterResponse(response: <ResponseType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `afterResponse` before returning the response.

- With `--ts_proto_opt=useAbortSignal=true`, the generated services will accept an `AbortSignal` to cancel RPC calls.

- With `--ts_proto_opt=useAsyncIterable=true`, the generated services will use `AsyncIterable` instead of `Observable`.
Expand Down
49 changes: 49 additions & 0 deletions integration/before-after-request/before-after-request-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { FooServiceClientImpl, FooServiceCreateRequest, FooServiceCreateResponse } from "./simple";
import { MessageType } from "./typeRegistry";

interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
afterResponse?(response: MessageType): void;
beforeRequest?(request: MessageType): void;
}

describe("before-after-request", () => {
const exampleData = {
kind: 1,
};
let rpc = {
request: jest.fn(() => Promise.resolve(new Uint8Array())),
};
let client = new FooServiceClientImpl(rpc);
const beforeRequest = jest.fn();
const afterResponse = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
jest
.spyOn(FooServiceCreateResponse, "decode")
.mockReturnValue({ $type: "simple.FooServiceCreateResponse", ...exampleData });
});

it("performs function before request if specified", async () => {
const req = FooServiceCreateRequest.create(exampleData);
client = new FooServiceClientImpl({ ...rpc, beforeRequest: beforeRequest });
await client.Create(req);
expect(beforeRequest).toHaveBeenCalled();
lukealvoeiro marked this conversation as resolved.
Show resolved Hide resolved
});

it("performs function after request if specified", async () => {
const req = FooServiceCreateRequest.create(exampleData);
client = new FooServiceClientImpl({ ...rpc, afterResponse: afterResponse });
await client.Create(req);
expect(afterResponse).toHaveBeenCalled();
lukealvoeiro marked this conversation as resolved.
Show resolved Hide resolved
});

it("doesn't perform function before or after request if they are not specified", async () => {
const req = FooServiceCreateRequest.create(exampleData);
client = new FooServiceClientImpl({ ...rpc });
await client.Create(req);
expect(beforeRequest).not.toHaveBeenCalled();
expect(afterResponse).not.toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions integration/before-after-request/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputBeforeRequest=true,outputAfterResponse=true,outputTypeRegistry=true
Binary file added integration/before-after-request/simple.bin
Binary file not shown.
37 changes: 37 additions & 0 deletions integration/before-after-request/simple.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
syntax = "proto3";
package simple;
import "simple2.proto";

enum SimpleEnum {
LOCAL_DEFAULT = 0;
LOCAL_FOO = 1;
LOCAL_BAR = 2;
}

message Simple {
string name = 1;
simple2.Simple otherSimple = 2;
}

message DifferentSimple {
string name = 1;
optional simple2.Simple otherOptionalSimple2 = 2;
}

message SimpleEnums {
SimpleEnum local_enum = 1;
simple2.SimpleEnum import_enum = 2;
}

message FooServiceCreateRequest {
simple2.FooService kind = 1;
}

message FooServiceCreateResponse {
simple2.FooService kind = 1;
}

service FooService {
rpc Create (FooServiceCreateRequest) returns (FooServiceCreateResponse);
}

Loading
Loading