Skip to content

Commit 83c280f

Browse files
committed
- response packing by JSON Rpc specification
- fixed unit tests
1 parent da70cdf commit 83c280f

File tree

4 files changed

+27
-43
lines changed

4 files changed

+27
-43
lines changed

src/client-proxy.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export class JSONRPCClient extends ClientProxy {
66
constructor(private readonly url: string) {
77
super();
88
}
9+
private counter: number = 0;
10+
private jsonrpc: string = "2.0";
11+
912
connect(): Promise<any> {
1013
throw new Error('The "connect()" method is not supported in JSONRPC mode.');
1114
}
@@ -26,19 +29,26 @@ export class JSONRPCClient extends ClientProxy {
2629
}
2730
getService<SvcInterface>(namespace: string): ServiceClient<SvcInterface> {
2831
let url = this.url;
32+
let id = this.counter;
33+
let jsonrpc = this.jsonrpc;
2934
return new Proxy(
3035
{},
3136
{
3237
get(obj, prop) {
3338
return function(params: any) {
3439
return axios
35-
.post(url, { method: namespace + "." + prop.toString(), params, jsonrpc: "2.0" })
36-
.then(res => Promise.resolve(res))
40+
.post(url, {
41+
method: namespace + "." + prop.toString(),
42+
params,
43+
jsonrpc: "2.0",
44+
id: ++id
45+
})
46+
.then(res => Promise.resolve({ jsonrpc, result: res, id }))
3747
.catch(err => {
3848
const { code, message, data } = err.response.data;
3949
let resp = { code, message, data };
4050

41-
return Promise.resolve(resp);
51+
return Promise.resolve({ jsonrpc, error: resp, id });
4252
});
4353
};
4454
}

src/index.spec.ts

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { JSONRPCClient } from "./client-proxy";
1010
describe("json-rpc-e2e", () => {
1111
let app: INestMicroservice;
1212
let server: JSONRPCServer;
13+
let client: JSONRPCClient;
14+
let service: TestService;
1315

1416
beforeAll(async () => {
1517
let moduleRef = await Test.createTestingModule({
@@ -21,61 +23,32 @@ describe("json-rpc-e2e", () => {
2123
port: 8080
2224
});
2325

24-
app = moduleRef.createNestMicroservice({ strategy: server });
25-
await new Promise(resolve => app.listen(resolve));
26-
});
26+
client = new JSONRPCClient("http://localhost:8080/rpc/v1");
2727

28-
it(`/rpc/v1/ test.invoke (POST)`, () => {
29-
return request(server.server)
30-
.post("/rpc/v1")
31-
.send({ method: "test.invoke", params: { data: "hi" } })
32-
.expect(200)
33-
.expect({
34-
data: "hi"
35-
});
36-
});
28+
service = client.getService<TestService>("test");
3729

38-
it(`should throw an error on /rpc/v1/ test.testError (POST)`, () => {
39-
const errorObj = {
40-
message: "RPC EXCEPTION",
41-
code: 403,
42-
data: {
43-
fromService: "Test Service",
44-
params: { data: "hi" }
45-
}
46-
};
47-
return request(server.server)
48-
.post("/rpc/v1")
49-
.send({ method: "test.testError", params: { data: "hi" } })
50-
.expect(403)
51-
.expect(errorObj);
30+
app = moduleRef.createNestMicroservice({ strategy: server });
31+
await new Promise(resolve => app.listen(resolve));
5232
});
5333

5434
it(`should make and RPC call with the JSONRPCClient`, () => {
55-
console.log("Testing RPC CLient");
56-
const client = new JSONRPCClient("http://localhost:8080/rpc/v1");
57-
const service = client.getService<TestService>("test");
58-
5935
return service
6036
.invokeClientService({ data: "hi" })
61-
.then(res => expect(res.data).toStrictEqual({ data: "hi" }));
37+
.then(res => expect(res.result.data).toStrictEqual({ data: "hi" }));
6238
});
6339

6440
it(`should return an error from JSONRPCClient call`, () => {
65-
console.log("Testing RPC CLient");
66-
const client = new JSONRPCClient("http://localhost:8080/rpc/v1");
67-
const service = client.getService<TestService>("test");
68-
6941
const errorObj = {
7042
message: "RPC EXCEPTION",
7143
code: 403,
7244
data: {
7345
fromService: "Test Service",
7446
params: { data: "hi" }
75-
}
47+
},
48+
id: 2
7649
};
7750

78-
return service.testError({ data: "hi" }).then(res => expect(res).toStrictEqual(errorObj));
51+
return service.testError({ data: "hi" }).then(res => expect(res.error).toStrictEqual(errorObj));
7952
});
8053

8154
afterAll(async () => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
6262
// let handlers = this.getHandlers();
6363

6464
let handler = this.getHandlerByPattern(req.body.method);
65-
console.log(handler);
65+
6666
if (handler == null) {
6767
return res.status(404).json({ error: "Not Found" });
6868
}

src/test-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class TestService implements ITestClientService {
8181
@UsePipes(TestPipe)
8282
@UseInterceptors(TestInterceptor)
8383
@UseGuards(TestGuard)
84-
public async testError(params: any) {
84+
public async testError(params: any): Promise<any> {
8585
// construct the error object with some data inside
8686
throw new CodedRpcException("RPC EXCEPTION", 403, { fromService: "Test Service", params });
8787
}
@@ -96,6 +96,7 @@ export class TestService implements ITestClientService {
9696
}
9797

9898
export interface ITestClientService {
99+
invoke(params: any): any;
99100
invokeClientService(params: any): any;
100-
testError(params: any): any;
101+
testError(params: any): Promise<any>;
101102
}

0 commit comments

Comments
 (0)