@leofcoin/endpoint-clients 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/exports/http.js CHANGED
@@ -1,4 +1,6 @@
1
- class http {
1
+ class Client {
2
+ url;
3
+ networkVersion;
2
4
  get isHttpClient() {
3
5
  return true;
4
6
  }
@@ -6,10 +8,19 @@ class http {
6
8
  this.url = url;
7
9
  this.networkVersion = networkVersion;
8
10
  }
9
- async _fetch(method, params) {
11
+ async _fetch(method, params, type = 'json') {
10
12
  params = new URLSearchParams(params).toString();
11
13
  const response = await fetch(`${this.url}/${method}?${params}`);
12
- return response.json();
14
+ return response[type]();
15
+ }
16
+ async _fetchString(method, params) {
17
+ return String(await this._fetch(method, params, 'text'));
18
+ }
19
+ async _fetchNumber(method, params) {
20
+ return Number(await this._fetchString(method, params));
21
+ }
22
+ async _fetchBoolean(method, params) {
23
+ return Boolean((await this._fetchString(method, params)) === 'true');
13
24
  }
14
25
  balances() {
15
26
  return this._fetch('balances');
@@ -18,7 +29,7 @@ class http {
18
29
  return this._fetch(`balanceOf`, { address, format });
19
30
  }
20
31
  selectedAccount() {
21
- return this._fetch('selectedAccount');
32
+ return this._fetchString('selectedAccount');
22
33
  }
23
34
  selectAccount(address) {
24
35
  return this._fetch('selectAccount', { address });
@@ -27,7 +38,7 @@ class http {
27
38
  return this._fetch('accounts');
28
39
  }
29
40
  hasTransactionToHandle() {
30
- return this._fetch('hasTransactionToHandle');
41
+ return this._fetchBoolean('hasTransactionToHandle');
31
42
  }
32
43
  getBlock(index) {
33
44
  return this._fetch('getBlock', { index });
@@ -39,7 +50,7 @@ class http {
39
50
  return this._fetch('sendTransaction', transaction);
40
51
  }
41
52
  peerId() {
42
- return this._fetch('peerId');
53
+ return this._fetchString('peerId');
43
54
  }
44
55
  peers() {
45
56
  return this._fetch('peers');
@@ -54,34 +65,34 @@ class http {
54
65
  return this._fetch('staticCall', { contract, method, params });
55
66
  }
56
67
  nativeBurns() {
57
- return this._fetch('nativeBurns');
68
+ return this._fetchNumber('nativeBurns');
58
69
  }
59
70
  contracts() {
60
71
  return this._fetch('contracts');
61
72
  }
62
73
  nativeMints() {
63
- return this._fetch('nativeMints');
74
+ return this._fetchNumber('nativeMints');
64
75
  }
65
76
  nativeToken() {
66
- return this._fetch('nativeToken');
77
+ return this._fetchString('nativeToken');
67
78
  }
68
79
  nativeTransfers() {
69
- return this._fetch('nativeTransfers');
80
+ return this._fetchNumber('nativeTransfers');
70
81
  }
71
82
  totalSize() {
72
- return this._fetch('totalSize');
83
+ return this._fetchNumber('totalSize');
73
84
  }
74
85
  totalTransactions() {
75
- return this._fetch('totalTransactions');
86
+ return this._fetchNumber('totalTransactions');
76
87
  }
77
88
  totalBlocks() {
78
- return this._fetch('totalBlocks');
89
+ return this._fetchNumber('totalBlocks');
79
90
  }
80
91
  nativeCalls() {
81
- return this._fetch('nativeCalls');
92
+ return this._fetchNumber('nativeCalls');
82
93
  }
83
94
  participating() {
84
- return this._fetch('participating');
95
+ return this._fetchBoolean('participating');
85
96
  }
86
97
  participate(address) {
87
98
  return this._fetch('participate', { address });
@@ -92,6 +103,12 @@ class http {
92
103
  deployContract(code, params) {
93
104
  return this._fetch('deployContract', { code, params });
94
105
  }
106
+ network() {
107
+ return this._fetch('network');
108
+ }
109
+ networkStats() {
110
+ return this._fetch('networkStats');
111
+ }
95
112
  }
96
113
 
97
- export { http as default };
114
+ export { Client as default };
package/exports/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export { default as ws } from './ws.js';
2
2
  export { default as http } from './http.js';
3
+ import 'socket-request-client';
@@ -1,32 +1,47 @@
1
- export default class {
1
+ type responseType = 'json' | 'text' | 'arrayBuffer' | 'clone' | 'formData' | 'blob';
2
+ type networkVersion = 'peach' | string;
3
+ export default class Client {
4
+ url: URL;
5
+ networkVersion: networkVersion;
2
6
  get isHttpClient(): boolean;
3
7
  constructor(url: any, networkVersion: any);
4
- _fetch(method: any, params: any): Promise<any>;
8
+ _fetch(method: any, params?: any, type?: responseType): Promise<any>;
9
+ _fetchString(method: string, params?: any): Promise<string>;
10
+ _fetchNumber(method: string, params?: any): Promise<number>;
11
+ _fetchBoolean(method: string, params?: any): Promise<boolean>;
5
12
  balances(): Promise<any>;
6
13
  balanceOf(address: any, format: any): Promise<any>;
7
- selectedAccount(): Promise<any>;
14
+ selectedAccount(): Promise<string>;
8
15
  selectAccount(address: any): Promise<any>;
9
16
  accounts(): Promise<any>;
10
- hasTransactionToHandle(): Promise<any>;
17
+ hasTransactionToHandle(): Promise<boolean>;
11
18
  getBlock(index: any): Promise<any>;
12
19
  blocks(amount: any): Promise<any>;
13
20
  sendTransaction(transaction: any): Promise<any>;
14
- peerId(): Promise<any>;
21
+ peerId(): Promise<string>;
15
22
  peers(): Promise<any>;
16
23
  validators(): Promise<any>;
17
24
  lookup(name: any): Promise<any>;
18
25
  staticCall(contract: any, method: any, params: any): Promise<any>;
19
- nativeBurns(): Promise<any>;
26
+ nativeBurns(): Promise<number>;
20
27
  contracts(): Promise<any>;
21
- nativeMints(): Promise<any>;
22
- nativeToken(): Promise<any>;
23
- nativeTransfers(): Promise<any>;
24
- totalSize(): Promise<any>;
25
- totalTransactions(): Promise<any>;
26
- totalBlocks(): Promise<any>;
27
- nativeCalls(): Promise<any>;
28
- participating(): Promise<any>;
28
+ nativeMints(): Promise<number>;
29
+ nativeToken(): Promise<string>;
30
+ nativeTransfers(): Promise<number>;
31
+ totalSize(): Promise<number>;
32
+ totalTransactions(): Promise<number>;
33
+ totalBlocks(): Promise<number>;
34
+ nativeCalls(): Promise<number>;
35
+ participating(): Promise<boolean>;
29
36
  participate(address: any): Promise<any>;
30
37
  createContractAddress(owner: any, code: any, params: any): Promise<any>;
31
38
  deployContract(code: any, params: any): Promise<any>;
39
+ network(): Promise<any>;
40
+ networkStats(): {
41
+ version: string;
42
+ peers: {}[];
43
+ accounts: number;
44
+ accountsHolding: number;
45
+ };
32
46
  }
47
+ export {};
@@ -37,4 +37,11 @@ export default class Client {
37
37
  participate(address: string): {};
38
38
  createContractAddress(owner: string, code: string, params: {}): {};
39
39
  deployContract(code: string, params: {}): {};
40
+ network(): any;
41
+ networkStats(): {
42
+ version: string;
43
+ peers: {}[];
44
+ accounts: number;
45
+ accountsHolding: number;
46
+ };
40
47
  }
package/exports/ws.js CHANGED
@@ -101,6 +101,12 @@ class Client {
101
101
  deployContract(code, params) {
102
102
  return this.request('deployContract', { code, params });
103
103
  }
104
+ network() {
105
+ return this.request('network');
106
+ }
107
+ networkStats() {
108
+ return this.request('networkStats');
109
+ }
104
110
  }
105
111
 
106
112
  export { Client as default };
package/package.json CHANGED
@@ -1,10 +1,22 @@
1
1
  {
2
2
  "name": "@leofcoin/endpoint-clients",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "exports": {
6
- "./ws": "./exports/ws.js",
7
- "./http": "./exports/http.js"
6
+ ".": {
7
+ "import": "./exports/index.js",
8
+ "types": "./exports/types/index.d.ts"
9
+ },
10
+ "./ws": {
11
+ "import": "./exports/ws.js",
12
+ "types": "./exports/types/ws.d.ts"
13
+ },
14
+ "./http": {
15
+ "import": "./exports/http.js",
16
+ "types": "./exports/types/http.d.ts"
17
+ },
18
+ "./ws.js": "./exports/ws.js",
19
+ "./http.js": "./exports/http.js"
8
20
  },
9
21
  "types": "./exports/types/index.d.ts",
10
22
  "type": "module",
package/src/http.ts CHANGED
@@ -1,16 +1,36 @@
1
- export default class {
1
+
2
+ // see https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods
3
+ type responseType = 'json' | 'text' | 'arrayBuffer' | 'clone' | 'formData' | 'blob';
4
+ type networkVersion = 'peach' | string;
5
+
6
+ export default class Client {
7
+ url: URL;
8
+ networkVersion: networkVersion;
2
9
  get isHttpClient() {
3
10
  return true
4
11
  }
12
+
5
13
  constructor(url, networkVersion) {
6
14
  this.url = url
7
15
  this.networkVersion = networkVersion
8
16
  }
9
17
 
10
- async _fetch(method, params) {
18
+ async _fetch(method, params?, type: responseType = 'json') {
11
19
  params = new URLSearchParams(params).toString()
12
20
  const response = await fetch(`${this.url}/${method}?${params}`)
13
- return response.json()
21
+ return response[type]()
22
+ }
23
+
24
+ async _fetchString(method: string, params?) {
25
+ return String(await this._fetch(method, params, 'text'))
26
+ }
27
+
28
+ async _fetchNumber(method: string, params?) {
29
+ return Number(await this._fetchString(method, params))
30
+ }
31
+
32
+ async _fetchBoolean(method: string, params?) {
33
+ return Boolean((await this._fetchString(method, params)) === 'true')
14
34
  }
15
35
 
16
36
  balances() {
@@ -22,7 +42,7 @@ export default class {
22
42
  }
23
43
 
24
44
  selectedAccount() {
25
- return this._fetch('selectedAccount')
45
+ return this._fetchString('selectedAccount')
26
46
  }
27
47
 
28
48
  selectAccount(address) {
@@ -34,7 +54,7 @@ export default class {
34
54
  }
35
55
 
36
56
  hasTransactionToHandle() {
37
- return this._fetch('hasTransactionToHandle')
57
+ return this._fetchBoolean('hasTransactionToHandle')
38
58
  }
39
59
 
40
60
  getBlock(index) {
@@ -48,7 +68,7 @@ export default class {
48
68
  return this._fetch('sendTransaction', transaction)
49
69
  }
50
70
  peerId() {
51
- return this._fetch('peerId')
71
+ return this._fetchString('peerId')
52
72
  }
53
73
  peers() {
54
74
  return this._fetch('peers')
@@ -64,34 +84,34 @@ export default class {
64
84
  return this._fetch('staticCall', {contract, method, params})
65
85
  }
66
86
  nativeBurns() {
67
- return this._fetch('nativeBurns')
87
+ return this._fetchNumber('nativeBurns')
68
88
  }
69
89
  contracts() {
70
90
  return this._fetch('contracts')
71
91
  }
72
92
  nativeMints() {
73
- return this._fetch('nativeMints')
93
+ return this._fetchNumber('nativeMints')
74
94
  }
75
95
  nativeToken() {
76
- return this._fetch('nativeToken')
96
+ return this._fetchString('nativeToken')
77
97
  }
78
98
  nativeTransfers() {
79
- return this._fetch('nativeTransfers')
99
+ return this._fetchNumber('nativeTransfers')
80
100
  }
81
101
  totalSize() {
82
- return this._fetch('totalSize')
102
+ return this._fetchNumber('totalSize')
83
103
  }
84
104
  totalTransactions() {
85
- return this._fetch('totalTransactions')
105
+ return this._fetchNumber('totalTransactions')
86
106
  }
87
107
  totalBlocks() {
88
- return this._fetch('totalBlocks')
108
+ return this._fetchNumber('totalBlocks')
89
109
  }
90
110
  nativeCalls() {
91
- return this._fetch('nativeCalls')
111
+ return this._fetchNumber('nativeCalls')
92
112
  }
93
113
  participating() {
94
- return this._fetch('participating')
114
+ return this._fetchBoolean('participating')
95
115
  }
96
116
  participate(address) {
97
117
  return this._fetch('participate', { address })
@@ -100,7 +120,16 @@ export default class {
100
120
  createContractAddress(owner, code, params) {
101
121
  return this._fetch('createContractAddress', { owner, code, params })
102
122
  }
123
+
103
124
  deployContract(code, params) {
104
125
  return this._fetch('deployContract', { code, params })
105
126
  }
127
+
128
+ network() {
129
+ return this._fetch('network')
130
+ }
131
+
132
+ networkStats(): { version: string; peers: {}[]; accounts: number; accountsHolding: number; } {
133
+ return this._fetch('networkStats')
134
+ }
106
135
  }
package/src/ws.ts CHANGED
@@ -114,4 +114,11 @@ export default class Client {
114
114
  deployContract(code: string, params: {}): {} {
115
115
  return this.request('deployContract', { code, params })
116
116
  }
117
+ network() {
118
+ return this.request('network')
119
+ }
120
+
121
+ networkStats(): { version: string; peers: {}[]; accounts: number; accountsHolding: number } {
122
+ return this.request('networkStats')
123
+ }
117
124
  }