Skip to content

Commit a3fe0a1

Browse files
Fixed RPC serialization, updated names for RPC response / request, fixed parsers for Tranform class
1 parent f395932 commit a3fe0a1

31 files changed

+1411
-919
lines changed

package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rpc/http_handler.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
import { TypedJSON } from 'typedjson';
12
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
2-
import { HttpError } from "./error";
3-
import { RpcRequest } from "./request";
4-
import { RpcResponse } from "./response";
5-
import { IHandler } from "./client";
63

7-
export const ErrParamsJsonStringifyHandler = new Error("failed to stringify json rpc request's params");
8-
export const ErrProcessHttpRequest = new Error("failed to send http request");
9-
export const ErrReadHttpResponseBody = new Error("failed to read http response body");
10-
export const ErrRpcResponseUnmarshal = new Error("failed to unmarshal rpc response");
4+
import { HttpError } from './error';
5+
import { RpcRequest } from './request';
6+
import { RpcResponse } from './response';
7+
import { IHandler } from './client';
118

12-
export class HttpHandler implements IHandler{
9+
export const ErrParamsJsonStringifyHandler = new Error(
10+
"failed to stringify json rpc request's params"
11+
);
12+
export const ErrProcessHttpRequest = new Error('failed to send http request');
13+
export const ErrReadHttpResponseBody = new Error(
14+
'failed to read http response body'
15+
);
16+
export const ErrRpcResponseUnmarshal = new Error(
17+
'failed to unmarshal rpc response'
18+
);
19+
20+
export class HttpHandler implements IHandler {
1321
private httpClient: AxiosInstance;
1422
private endpoint: string;
1523
private customHeaders: Record<string, string>;
@@ -26,22 +34,25 @@ export class HttpHandler implements IHandler{
2634

2735
/** @throws {HttpError, Error} */
2836
async processCall(params: RpcRequest): Promise<RpcResponse> {
37+
const serializer = new TypedJSON(RpcRequest);
2938
let body: string;
3039

3140
try {
32-
body = JSON.stringify(params);
41+
body = serializer.stringify(params);
3342
} catch (err) {
34-
throw new Error(`${ErrParamsJsonStringifyHandler.message}, details: ${err.message}`);
43+
throw new Error(
44+
`${ErrParamsJsonStringifyHandler.message}, details: ${err.message}`
45+
);
3546
}
3647

3748
const config: AxiosRequestConfig = {
3849
method: 'POST',
3950
url: this.endpoint,
4051
headers: {
4152
'Content-Type': 'application/json',
42-
...this.customHeaders,
53+
...this.customHeaders
4354
},
44-
data: body,
55+
data: body
4556
};
4657

4758
try {
@@ -54,17 +65,26 @@ export class HttpHandler implements IHandler{
5465
try {
5566
return response.data;
5667
} catch (err) {
57-
throw new Error(`${ErrRpcResponseUnmarshal.message}, details: ${err.message}`);
68+
throw new Error(
69+
`${ErrRpcResponseUnmarshal.message}, details: ${err.message}`
70+
);
5871
}
5972
} catch (err) {
6073
if (axios.isAxiosError(err)) {
6174
if (err.response) {
62-
throw new HttpError(err.response.status, new Error(err.response.statusText));
75+
throw new HttpError(
76+
err.response.status,
77+
new Error(err.response.statusText)
78+
);
6379
} else {
64-
throw new Error(`${ErrProcessHttpRequest.message}, details: ${err.message}`);
80+
throw new Error(
81+
`${ErrProcessHttpRequest.message}, details: ${err.message}`
82+
);
6583
}
6684
} else {
67-
throw new Error(`${ErrReadHttpResponseBody.message}, details: ${err.message}`);
85+
throw new Error(
86+
`${ErrReadHttpResponseBody.message}, details: ${err.message}`
87+
);
6888
}
6989
}
7090
}

src/rpc/id_value.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import { jsonMember, jsonObject } from 'typedjson';
2+
3+
@jsonObject
14
export class IDValue {
2-
private intValue: number | null = null;
3-
private strValue: string | null = null;
4-
private isIntValue: boolean;
5+
@jsonMember({ constructor: Number, isRequired: false, preserveNull: true })
6+
intValue: number | null = null;
7+
8+
@jsonMember({ constructor: String, isRequired: false, preserveNull: true })
9+
strValue: string | null = null;
10+
11+
@jsonMember({ constructor: Boolean })
12+
isIntValue: boolean;
513

614
constructor(value: string | number) {
715
if (typeof value === 'number') {
@@ -41,25 +49,12 @@ export class IDValue {
4149
return this.toString();
4250
}
4351

44-
static fromJSON(data: string): IDValue {
45-
try {
46-
const parsedInt = JSON.parse(data) as number;
47-
if (typeof parsedInt === 'number') {
48-
return IDValue.fromInt(parsedInt);
49-
}
50-
} catch (e) {
51-
// Not a number, continue to try parsing as a string
52+
static fromJSON(data: string | number): IDValue {
53+
if (typeof data === 'number') {
54+
return IDValue.fromInt(data);
55+
} else if (typeof data === 'string') {
56+
return IDValue.fromString(data);
5257
}
53-
54-
try {
55-
const parsedStr = JSON.parse(data) as string;
56-
if (typeof parsedStr === 'string') {
57-
return IDValue.fromString(parsedStr);
58-
}
59-
} catch (e) {
60-
throw new Error('IDValue should be an int or string');
61-
}
62-
6358
throw new Error('IDValue should be an int or string');
6459
}
6560
}

0 commit comments

Comments
 (0)