-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add before and after request methods to base service (#961)
* testing with simple.ts * what we want it to look like * working * passes tests * docs: document new feature * correctly call using req/res instead of metadata * fix typing
- Loading branch information
1 parent
b4933e7
commit 19ba6a5
Showing
13 changed files
with
828 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
integration/before-after-request/before-after-request-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { FooServiceClientImpl, FooServiceCreateRequest, FooServiceCreateResponse } from "./simple"; | ||
import { MessageType } from "./typeRegistry"; | ||
|
||
interface Rpc { | ||
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>; | ||
beforeRequest?<T extends { [k in keyof T]: unknown }>(request: T): void; | ||
afterResponse?<T extends { [k in keyof T]: unknown }>(response: T): 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(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).toHaveBeenCalledWith(req); | ||
}); | ||
|
||
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).toHaveBeenCalledWith(exampleData); | ||
}); | ||
|
||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
outputBeforeRequest=true,outputAfterResponse=true |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
Oops, something went wrong.