@mentaproject/client 0.0.6 → 0.0.8
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/dist/utils/toJSON.js +12 -14
- package/package.json +1 -1
- package/test.ts +1 -1
package/dist/utils/toJSON.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
function isClassInstance(obj) {
|
|
2
|
-
if (
|
|
2
|
+
if (!obj)
|
|
3
|
+
return false;
|
|
4
|
+
const constructor = obj.constructor;
|
|
5
|
+
const nativeConstructors = [Array, Date, RegExp, Map, Set, Promise, Function, Number, String, Boolean, Error, Object];
|
|
6
|
+
if (nativeConstructors.includes(constructor)) {
|
|
3
7
|
return false;
|
|
4
8
|
}
|
|
5
|
-
|
|
6
|
-
// Attention: cela peut être sensible à la minification ou à des transpilers très spécifiques,
|
|
7
|
-
// mais c'est généralement fiable pour les classes définies avec le mot-clé `class`.
|
|
8
|
-
return obj.constructor.toString().startsWith('class ');
|
|
9
|
+
return true;
|
|
9
10
|
}
|
|
10
11
|
;
|
|
11
12
|
function convertBigintsToStrings(obj) {
|
|
@@ -25,22 +26,16 @@ function convertBigintsToStrings(obj) {
|
|
|
25
26
|
return obj;
|
|
26
27
|
}
|
|
27
28
|
export async function toJSON({ obj, depth = 0 }) {
|
|
28
|
-
|
|
29
|
-
const data = Object.assign({}, obj);
|
|
29
|
+
const data = {};
|
|
30
30
|
for (const key in obj) {
|
|
31
31
|
// skip class related properties
|
|
32
|
-
if (key === "toJSON" || key === "constructor" || key === "rpcClient")
|
|
33
|
-
delete data[key];
|
|
32
|
+
if (key === "toJSON" || key === "constructor" || key === "rpcClient")
|
|
34
33
|
continue;
|
|
35
|
-
}
|
|
36
|
-
;
|
|
37
34
|
const prop = obj[key];
|
|
38
35
|
if (typeof prop === "function") {
|
|
39
36
|
// do not fetch getters if depth is 0
|
|
40
|
-
if (depth === 0)
|
|
41
|
-
delete data[key];
|
|
37
|
+
if (depth === 0)
|
|
42
38
|
continue;
|
|
43
|
-
}
|
|
44
39
|
// fetch getters
|
|
45
40
|
const value = await obj[key]();
|
|
46
41
|
// if the value is a class instance, convert it to JSON recursively
|
|
@@ -48,6 +43,7 @@ export async function toJSON({ obj, depth = 0 }) {
|
|
|
48
43
|
data[key] = await toJSON({ obj: value, depth: depth - 1 });
|
|
49
44
|
continue;
|
|
50
45
|
}
|
|
46
|
+
;
|
|
51
47
|
data[key] = value;
|
|
52
48
|
continue;
|
|
53
49
|
}
|
|
@@ -63,7 +59,9 @@ export async function toJSON({ obj, depth = 0 }) {
|
|
|
63
59
|
;
|
|
64
60
|
// If depth is not 0, fetch the object
|
|
65
61
|
data[key] = await prop.toJSON({ depth: depth - 1 });
|
|
62
|
+
continue;
|
|
66
63
|
}
|
|
64
|
+
data[key] = prop;
|
|
67
65
|
}
|
|
68
66
|
;
|
|
69
67
|
return convertBigintsToStrings(data);
|
package/package.json
CHANGED
package/test.ts
CHANGED
|
@@ -15,7 +15,7 @@ const client = new MentaClient({
|
|
|
15
15
|
|
|
16
16
|
(async () => {
|
|
17
17
|
const firstTx = await client.transactions.get({ hash: "0xeae261aeada2db19fee5cc4dfd47d575ff2380a5fb6a4039685d5044fdc8e2bb" });
|
|
18
|
-
const json = await firstTx.toJSON(
|
|
18
|
+
const json = await firstTx.toJSON(3);
|
|
19
19
|
|
|
20
20
|
writeFileSync("./test.json", JSON.stringify(json, null, 2));
|
|
21
21
|
})()
|