Skip to content

Commit

Permalink
Fixed RPC serialization, updated names for RPC response / request, fi…
Browse files Browse the repository at this point in the history
…xed parsers for Tranform class
  • Loading branch information
alexmyshchyshyn committed Nov 15, 2024
1 parent f395932 commit a3fe0a1
Show file tree
Hide file tree
Showing 31 changed files with 1,411 additions and 919 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 37 additions & 17 deletions src/rpc/http_handler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { TypedJSON } from 'typedjson';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import { HttpError } from "./error";
import { RpcRequest } from "./request";
import { RpcResponse } from "./response";
import { IHandler } from "./client";

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

export class HttpHandler implements IHandler{
export const ErrParamsJsonStringifyHandler = new Error(
"failed to stringify json rpc request's params"
);
export const ErrProcessHttpRequest = new Error('failed to send http request');
export const ErrReadHttpResponseBody = new Error(
'failed to read http response body'
);
export const ErrRpcResponseUnmarshal = new Error(
'failed to unmarshal rpc response'
);

export class HttpHandler implements IHandler {
private httpClient: AxiosInstance;
private endpoint: string;
private customHeaders: Record<string, string>;
Expand All @@ -26,22 +34,25 @@ export class HttpHandler implements IHandler{

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

try {
body = JSON.stringify(params);
body = serializer.stringify(params);
} catch (err) {
throw new Error(`${ErrParamsJsonStringifyHandler.message}, details: ${err.message}`);
throw new Error(
`${ErrParamsJsonStringifyHandler.message}, details: ${err.message}`
);
}

const config: AxiosRequestConfig = {
method: 'POST',
url: this.endpoint,
headers: {
'Content-Type': 'application/json',
...this.customHeaders,
...this.customHeaders
},
data: body,
data: body
};

try {
Expand All @@ -54,17 +65,26 @@ export class HttpHandler implements IHandler{
try {
return response.data;
} catch (err) {
throw new Error(`${ErrRpcResponseUnmarshal.message}, details: ${err.message}`);
throw new Error(
`${ErrRpcResponseUnmarshal.message}, details: ${err.message}`
);
}
} catch (err) {
if (axios.isAxiosError(err)) {
if (err.response) {
throw new HttpError(err.response.status, new Error(err.response.statusText));
throw new HttpError(
err.response.status,
new Error(err.response.statusText)
);
} else {
throw new Error(`${ErrProcessHttpRequest.message}, details: ${err.message}`);
throw new Error(
`${ErrProcessHttpRequest.message}, details: ${err.message}`
);
}
} else {
throw new Error(`${ErrReadHttpResponseBody.message}, details: ${err.message}`);
throw new Error(
`${ErrReadHttpResponseBody.message}, details: ${err.message}`
);
}
}
}
Expand Down
37 changes: 16 additions & 21 deletions src/rpc/id_value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { jsonMember, jsonObject } from 'typedjson';

@jsonObject
export class IDValue {
private intValue: number | null = null;
private strValue: string | null = null;
private isIntValue: boolean;
@jsonMember({ constructor: Number, isRequired: false, preserveNull: true })
intValue: number | null = null;

@jsonMember({ constructor: String, isRequired: false, preserveNull: true })
strValue: string | null = null;

@jsonMember({ constructor: Boolean })
isIntValue: boolean;

constructor(value: string | number) {
if (typeof value === 'number') {
Expand Down Expand Up @@ -41,25 +49,12 @@ export class IDValue {
return this.toString();
}

static fromJSON(data: string): IDValue {
try {
const parsedInt = JSON.parse(data) as number;
if (typeof parsedInt === 'number') {
return IDValue.fromInt(parsedInt);
}
} catch (e) {
// Not a number, continue to try parsing as a string
static fromJSON(data: string | number): IDValue {
if (typeof data === 'number') {
return IDValue.fromInt(data);
} else if (typeof data === 'string') {
return IDValue.fromString(data);
}

try {
const parsedStr = JSON.parse(data) as string;
if (typeof parsedStr === 'string') {
return IDValue.fromString(parsedStr);
}
} catch (e) {
throw new Error('IDValue should be an int or string');
}

throw new Error('IDValue should be an int or string');
}
}
Loading

0 comments on commit a3fe0a1

Please sign in to comment.