1
+ import { TypedJSON } from 'typedjson' ;
1
2
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" ;
6
3
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' ;
11
8
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 {
13
21
private httpClient : AxiosInstance ;
14
22
private endpoint : string ;
15
23
private customHeaders : Record < string , string > ;
@@ -26,22 +34,25 @@ export class HttpHandler implements IHandler{
26
34
27
35
/** @throws {HttpError, Error } */
28
36
async processCall ( params : RpcRequest ) : Promise < RpcResponse > {
37
+ const serializer = new TypedJSON ( RpcRequest ) ;
29
38
let body : string ;
30
39
31
40
try {
32
- body = JSON . stringify ( params ) ;
41
+ body = serializer . stringify ( params ) ;
33
42
} catch ( err ) {
34
- throw new Error ( `${ ErrParamsJsonStringifyHandler . message } , details: ${ err . message } ` ) ;
43
+ throw new Error (
44
+ `${ ErrParamsJsonStringifyHandler . message } , details: ${ err . message } `
45
+ ) ;
35
46
}
36
47
37
48
const config : AxiosRequestConfig = {
38
49
method : 'POST' ,
39
50
url : this . endpoint ,
40
51
headers : {
41
52
'Content-Type' : 'application/json' ,
42
- ...this . customHeaders ,
53
+ ...this . customHeaders
43
54
} ,
44
- data : body ,
55
+ data : body
45
56
} ;
46
57
47
58
try {
@@ -54,17 +65,26 @@ export class HttpHandler implements IHandler{
54
65
try {
55
66
return response . data ;
56
67
} catch ( err ) {
57
- throw new Error ( `${ ErrRpcResponseUnmarshal . message } , details: ${ err . message } ` ) ;
68
+ throw new Error (
69
+ `${ ErrRpcResponseUnmarshal . message } , details: ${ err . message } `
70
+ ) ;
58
71
}
59
72
} catch ( err ) {
60
73
if ( axios . isAxiosError ( err ) ) {
61
74
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
+ ) ;
63
79
} else {
64
- throw new Error ( `${ ErrProcessHttpRequest . message } , details: ${ err . message } ` ) ;
80
+ throw new Error (
81
+ `${ ErrProcessHttpRequest . message } , details: ${ err . message } `
82
+ ) ;
65
83
}
66
84
} else {
67
- throw new Error ( `${ ErrReadHttpResponseBody . message } , details: ${ err . message } ` ) ;
85
+ throw new Error (
86
+ `${ ErrReadHttpResponseBody . message } , details: ${ err . message } `
87
+ ) ;
68
88
}
69
89
}
70
90
}
0 commit comments