-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrpc.js
48 lines (43 loc) · 954 Bytes
/
rpc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import axios from 'axios';
class Rpc {
constructor() {
this._defaults = {
jsonrpc: '2.0',
method: 'call',
};
this.instanceAxios = axios.create();
this.instanceAxios.interceptors.response.use((response) => {
if (Object.prototype.hasOwnProperty.call(response.data, 'error')) {
return Promise.reject(response.data.error);
}
return response;
}, err => Promise.reject(err));
}
/**
* Create axios request
*
* @param {String} url
* @param {Object} params
*
* @returns {Promise}
*/
__jsonrpc(url, params) {
return this.instanceAxios.post(url, {
...this._defaults,
id: Math.floor(Math.random() * 1000 * 1000 * 1000),
params,
});
}
/**
* Create axios request
*
* @param {String} url
* @param {Object} params
*
* @returns {Promise}
*/
rpc(url, params) {
return this.__jsonrpc(url, params);
}
}
export default Rpc;