@tchain/tcabci-read-js-client 1.3.13 → 2.0.0

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/errors.js CHANGED
@@ -1,27 +1,28 @@
1
- const ALREADY_CONNECTED = new Error("Already connected"),
2
- NOT_CONNECTED = new Error("Not connected"),
3
- ERR_NETWORK = new Error("Network Error"),
4
- INVALID_ARGUMENTS = new Error("Check your arguments"),
5
- INVALID_ARGUMENT_WITH_CS = (val) => {
6
- return new Error(INVALID_ARGUMENTS + " " + val)
7
- },
8
- NOT_SUBSCRIBED = new Error("Not subscribed"),
9
- ALREADY_SUBSCRIBED = new Error("Already subscribed"),
10
- ADDRESSES_IS_EMPTY = new Error("Addresses is empty"),
11
- BLOCK_NOT_FOUND = new Error("Block does not exist"),
12
- TRANSACTION_TYPE_NOT_VALID = new Error("Transaction type is not valid"),
13
- TRANSACTION_NOT_BROADCAST = new Error("Transaction can not be broadcast")
1
+ export const ALREADY_CONNECTED = new Error('Already connected'),
2
+ NOT_CONNECTED = new Error('Not connected'),
3
+ ERR_NETWORK = new Error('Network Error'),
4
+ INVALID_ARGUMENTS = new Error('Check your arguments'),
5
+ INVALID_ARGUMENT_WITH_CS = (val) => {
6
+ return new Error(INVALID_ARGUMENTS + ' ' + val)
7
+ },
8
+ NOT_SUBSCRIBED = new Error('Not subscribed'),
9
+ ALREADY_SUBSCRIBED = new Error('Already subscribed'),
10
+ ADDRESSES_IS_EMPTY = new Error('Addresses is empty'),
11
+ BLOCK_NOT_FOUND = new Error('Block does not exist'),
12
+ TRANSACTION_TYPE_NOT_VALID = new Error('Transaction type is not valid'),
13
+ TRANSACTION_NOT_BROADCAST = new Error('Transaction can not be broadcast')
14
14
 
15
- module.exports = {
16
- ALREADY_CONNECTED,
17
- NOT_CONNECTED,
18
- ERR_NETWORK,
19
- INVALID_ARGUMENTS,
20
- INVALID_ARGUMENT_WITH_CS,
21
- NOT_SUBSCRIBED,
22
- ALREADY_SUBSCRIBED,
23
- ADDRESSES_IS_EMPTY,
24
- BLOCK_NOT_FOUND,
25
- TRANSACTION_TYPE_NOT_VALID,
26
- TRANSACTION_NOT_BROADCAST,
15
+ export class FetchError extends Error {
16
+ message = ''
17
+ code = -1
18
+ response = {}
19
+
20
+ constructor(props, code = -1) {
21
+ super(props)
22
+ this.code = code
23
+ this.response = {
24
+ status: code,
25
+ statusText: Array.isArray(props) ? props[0] : props,
26
+ }
27
+ }
27
28
  }
package/message.js CHANGED
@@ -1,55 +1,59 @@
1
- const { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } = require("./errors")
2
- const { MESSAGE_TYPE, TX_TYPE} = require("./constants")
3
- class Message {
4
- #is_web = false
5
- #type = ""
6
- #addrs = []
7
- #txTypes = []
8
- constructor(isWeb, type, addrs, txTypes = []) {
9
- this.#is_web = isWeb
10
- this.#type = type
11
- this.#addrs = addrs
12
- this.#txTypes = txTypes
1
+ import { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
+ import { MESSAGE_TYPE, TX_TYPE } from './constants.js'
3
+
4
+ export default class Message {
5
+ #is_web = false
6
+ #type = ''
7
+ #addrs = []
8
+ #txTypes = []
9
+ constructor(isWeb, type, addrs, txTypes = []) {
10
+ this.#is_web = isWeb
11
+ this.#type = type
12
+ this.#addrs = addrs
13
+ this.#txTypes = txTypes
14
+ }
15
+
16
+ ToJSONString() {
17
+ this.#validate()
18
+
19
+ return JSON.stringify({
20
+ is_web: this.#is_web,
21
+ type: this.#type,
22
+ addrs: this.#addrs,
23
+ ...(this.#txTypes.length > 0 ? { tx_types: this.#txTypes } : {}),
24
+ })
25
+ }
26
+
27
+ #validate() {
28
+ if (typeof this.#is_web !== 'boolean') {
29
+ throw INVALID_ARGUMENT_WITH_CS('is_web')
13
30
  }
14
31
 
15
- ToJSONString() {
16
- this.#validate()
17
- return JSON.stringify({
18
- "is_web": this.#is_web,
19
- "type": this.#type,
20
- "addrs": this.#addrs,
21
- ...(this.#txTypes.length > 0 ? { "tx_types": this.#txTypes } : {})
22
- })
32
+ if (typeof this.#type !== 'string') {
33
+ throw INVALID_ARGUMENT_WITH_CS('type')
23
34
  }
24
35
 
25
- #validate() {
26
- if (typeof this.#is_web !== "boolean") {
27
- throw INVALID_ARGUMENT_WITH_CS("is_web")
28
- }
36
+ if (!Array.isArray(this.#addrs)) {
37
+ throw INVALID_ARGUMENT_WITH_CS('addrs')
38
+ }
29
39
 
30
- if (typeof this.#type !== "string") {
31
- throw INVALID_ARGUMENT_WITH_CS("type")
32
- }
40
+ if (
41
+ [MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) ===
42
+ -1
43
+ ) {
44
+ throw INVALID_ARGUMENTS
45
+ }
33
46
 
34
- if (!Array.isArray(this.#addrs)) {
35
- throw INVALID_ARGUMENT_WITH_CS("addrs")
36
- }
47
+ if (this.#txTypes.length) {
48
+ let vals = Object.values(TX_TYPE)
37
49
 
38
- if ([MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) === -1) {
39
- throw INVALID_ARGUMENTS
50
+ for (let i = 0; i < this.#txTypes.length; i++) {
51
+ if (vals.indexOf(this.#txTypes[i]) === -1) {
52
+ throw INVALID_ARGUMENT_WITH_CS('tx_types')
40
53
  }
54
+ }
41
55
 
42
- if (this.#txTypes.length) {
43
- let vals = Object.values(TX_TYPE)
44
- for (let i = 0; i < this.#txTypes.length; i++) {
45
- if (vals.indexOf(this.#txTypes[i]) === -1) {
46
- throw INVALID_ARGUMENT_WITH_CS('tx_types')
47
- }
48
- }
49
-
50
- vals = []
51
- }
56
+ vals = []
52
57
  }
58
+ }
53
59
  }
54
-
55
- module.exports = Message
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "1.3.13",
3
+ "version": "2.0.0",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
- "type": "commonjs",
6
+ "type": "module",
7
7
  "scripts": {
8
8
  "test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha --timeout 5000",
9
- "test:coverage": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=text mocha --timeout 5000"
9
+ "test:coverage": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=text mocha --timeout 5000",
10
+ "lint": "eslint --ext .js ./",
11
+ "lint-fix": "eslint --fix --ext .js ./",
12
+ "lint-fix-file": "eslint --fix --ext .js",
13
+ "format": "prettier --write \"**/*.{js,md,json}\" --ignore-path .gitignore"
10
14
  },
11
15
  "repository": {
12
16
  "type": "git",
@@ -20,11 +24,17 @@
20
24
  "homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
21
25
  "devDependencies": {
22
26
  "mocha": "^10.2.0",
23
- "nyc": "^15.1.0",
24
- "unit.js": "^2.1.1"
27
+ "nyc": "^17.0.0",
28
+ "unit.js": "^2.1.1",
29
+ "eslint": "^8.56.0",
30
+ "eslint-config-prettier": "^9.1.0",
31
+ "eslint-plugin-import": "^2.29.1",
32
+ "eslint-plugin-node": "^11.0.0",
33
+ "eslint-plugin-promise": "^6.1.1",
34
+ "prettier": "^3.2.4"
25
35
  },
26
36
  "dependencies": {
27
- "axios": "^1.7.2",
37
+ "node-fetch": "^3.3.2",
28
38
  "reconnecting-websocket": "^4.4.0",
29
39
  "ws": "^8.18.0"
30
40
  }
package/transaction.js CHANGED
@@ -1,108 +1,106 @@
1
- const {INVALID_ARGUMENT_WITH_CS} = require("./errors");
1
+ import { INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
2
 
3
- class Transaction {
4
- #id
5
- #block_id
6
- #height
7
- #identifier
8
- #version
9
- #typ
10
- #sender_addr
11
- #recipient_addr
12
- #data
13
- #sign
14
- #fee
15
- #hash
16
- #inserted_at
3
+ export default class Transaction {
4
+ #id
5
+ #block_id
6
+ #height
7
+ #identifier
8
+ #version
9
+ #typ
10
+ #sender_addr
11
+ #recipient_addr
12
+ #data
13
+ #sign
14
+ #fee
15
+ #hash
16
+ #inserted_at
17
17
 
18
- constructor({
19
- id,
20
- block_id,
21
- height,
22
- identifier,
23
- version,
24
- typ,
25
- sender_addr,
26
- recipient_addr,
27
- data,
28
- sign,
29
- fee,
30
- hash,
31
- inserted_at
32
- }) {
18
+ constructor({
19
+ id,
20
+ block_id,
21
+ height,
22
+ identifier,
23
+ version,
24
+ typ,
25
+ sender_addr,
26
+ recipient_addr,
27
+ data,
28
+ sign,
29
+ fee,
30
+ hash,
31
+ inserted_at,
32
+ }) {
33
+ this.#id = id
34
+ this.#block_id = block_id
35
+ this.#height = height
36
+ this.#identifier = identifier
37
+ this.#version = version
38
+ this.#typ = typ
39
+ this.#sender_addr = sender_addr
40
+ this.#recipient_addr = recipient_addr
41
+ this.#data = data
42
+ this.#sign = sign
43
+ this.#fee = fee
44
+ this.#hash = hash
45
+ this.#inserted_at = inserted_at
46
+ }
33
47
 
34
- this.#id = id
35
- this.#block_id = block_id
36
- this.#height = height
37
- this.#identifier = identifier
38
- this.#version = version
39
- this.#typ = typ
40
- this.#sender_addr = sender_addr
41
- this.#recipient_addr = recipient_addr
42
- this.#data = data
43
- this.#sign = sign
44
- this.#fee = fee
45
- this.#hash = hash
46
- this.#inserted_at = inserted_at
47
- }
48
+ ToJSONString() {
49
+ this.#validate()
48
50
 
49
- ToJSONString() {
50
- this.#validate()
51
- return JSON.stringify({
52
- "id": this.#id,
53
- "block_id": this.#block_id,
54
- "height": this.#height,
55
- "identifier": this.#identifier,
56
- "version": this.#version,
57
- "typ": this.#typ,
58
- "sender_addr": this.#sender_addr,
59
- "recipient_addr": this.#recipient_addr,
60
- "data": this.#data,
61
- "sign": this.#sign,
62
- "fee": this.#fee,
63
- "hash": this.#hash,
64
- "inserted_at": this.#inserted_at
65
- })
66
- }
51
+ return JSON.stringify({
52
+ id: this.#id,
53
+ block_id: this.#block_id,
54
+ height: this.#height,
55
+ identifier: this.#identifier,
56
+ version: this.#version,
57
+ typ: this.#typ,
58
+ sender_addr: this.#sender_addr,
59
+ recipient_addr: this.#recipient_addr,
60
+ data: this.#data,
61
+ sign: this.#sign,
62
+ fee: this.#fee,
63
+ hash: this.#hash,
64
+ inserted_at: this.#inserted_at,
65
+ })
66
+ }
67
67
 
68
- #validate() {
69
- if (typeof this.#id !== "number") {
70
- throw INVALID_ARGUMENT_WITH_CS("id")
71
- }
72
- if (typeof this.#block_id !== "number") {
73
- throw INVALID_ARGUMENT_WITH_CS("blockID")
74
- }
75
- if (typeof this.#height !== "number") {
76
- throw INVALID_ARGUMENT_WITH_CS("height")
77
- }
78
- if (typeof this.#identifier !== "string") {
79
- throw INVALID_ARGUMENT_WITH_CS("identifier")
80
- }
81
- if (typeof this.#version !== "number") {
82
- throw INVALID_ARGUMENT_WITH_CS("version")
83
- }
84
- if (typeof this.#typ !== "string") {
85
- throw INVALID_ARGUMENT_WITH_CS("typ")
86
- }
87
- if (typeof this.#sender_addr !== "string") {
88
- throw INVALID_ARGUMENT_WITH_CS("senderAddr")
89
- }
90
- if (typeof this.#recipient_addr !== "string") {
91
- throw INVALID_ARGUMENT_WITH_CS("recipientAddr")
92
- }
93
- if (typeof this.#data !== "object") {
94
- throw INVALID_ARGUMENT_WITH_CS("data")
95
- }
96
- if (typeof this.#sign !== "object") {
97
- throw INVALID_ARGUMENT_WITH_CS("sign")
98
- }
99
- if (typeof this.#fee !== "number") {
100
- throw INVALID_ARGUMENT_WITH_CS("fee")
101
- }
102
- if (typeof this.#inserted_at !== "string") {
103
- throw INVALID_ARGUMENT_WITH_CS("insertedAt")
104
- }
68
+ #validate() {
69
+ if (typeof this.#id !== 'number') {
70
+ throw INVALID_ARGUMENT_WITH_CS('id')
71
+ }
72
+ if (typeof this.#block_id !== 'number') {
73
+ throw INVALID_ARGUMENT_WITH_CS('blockID')
74
+ }
75
+ if (typeof this.#height !== 'number') {
76
+ throw INVALID_ARGUMENT_WITH_CS('height')
77
+ }
78
+ if (typeof this.#identifier !== 'string') {
79
+ throw INVALID_ARGUMENT_WITH_CS('identifier')
80
+ }
81
+ if (typeof this.#version !== 'number') {
82
+ throw INVALID_ARGUMENT_WITH_CS('version')
83
+ }
84
+ if (typeof this.#typ !== 'string') {
85
+ throw INVALID_ARGUMENT_WITH_CS('typ')
105
86
  }
87
+ if (typeof this.#sender_addr !== 'string') {
88
+ throw INVALID_ARGUMENT_WITH_CS('senderAddr')
89
+ }
90
+ if (typeof this.#recipient_addr !== 'string') {
91
+ throw INVALID_ARGUMENT_WITH_CS('recipientAddr')
92
+ }
93
+ if (typeof this.#data !== 'object') {
94
+ throw INVALID_ARGUMENT_WITH_CS('data')
95
+ }
96
+ if (typeof this.#sign !== 'object') {
97
+ throw INVALID_ARGUMENT_WITH_CS('sign')
98
+ }
99
+ if (typeof this.#fee !== 'number') {
100
+ throw INVALID_ARGUMENT_WITH_CS('fee')
101
+ }
102
+ if (typeof this.#inserted_at !== 'string') {
103
+ throw INVALID_ARGUMENT_WITH_CS('insertedAt')
104
+ }
105
+ }
106
106
  }
107
-
108
- module.exports = Transaction
package/util.js CHANGED
@@ -1,21 +1,20 @@
1
- const {CHARACTERS} = require("./constants");
1
+ import { CHARACTERS } from './constants.js'
2
2
 
3
- const randomString = (length) => {
4
- let result = '';
5
- const charactersLength = CHARACTERS.length;
6
- let counter = 0;
7
- while (counter < length) {
8
- result += CHARACTERS.charAt(Math.floor(Math.random() * charactersLength));
9
- counter += 1;
10
- }
11
- return result;
12
- }
3
+ export const randomString = (length) => {
4
+ let result = ''
5
+
6
+ const charactersLength = CHARACTERS.length
7
+
8
+ let counter = 0
13
9
 
14
- const toJSON = (data) => {
15
- return JSON.parse(data)
10
+ while (counter < length) {
11
+ result += CHARACTERS.charAt(Math.floor(Math.random() * charactersLength))
12
+ counter += 1
13
+ }
14
+
15
+ return result
16
16
  }
17
17
 
18
- module.exports = {
19
- randomString,
20
- toJSON
21
- }
18
+ export const toJSON = (data) => {
19
+ return JSON.parse(data)
20
+ }