@wishknish/knishio-client-js 0.5.2 → 0.6.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.
Files changed (138) hide show
  1. package/README.md +207 -274
  2. package/dist/client.iife.js +577 -0
  3. package/package.json +36 -79
  4. package/src/.babelrc +0 -22
  5. package/src/Atom.js +171 -132
  6. package/src/AtomMeta.js +76 -50
  7. package/src/AuthToken.js +37 -46
  8. package/src/KnishIOClient.js +932 -985
  9. package/src/Meta.js +15 -17
  10. package/src/Molecule.js +423 -494
  11. package/src/PolicyMeta.js +32 -41
  12. package/src/TokenUnit.js +30 -32
  13. package/src/Wallet.js +275 -265
  14. package/src/exception/AtomIndexException.js +4 -8
  15. package/src/exception/AtomsMissingException.js +4 -6
  16. package/src/exception/AuthorizationRejectedException.js +4 -5
  17. package/src/exception/BalanceInsufficientException.js +4 -8
  18. package/src/exception/BaseException.js +6 -8
  19. package/src/exception/BatchIdException.js +5 -7
  20. package/src/exception/CodeException.js +4 -8
  21. package/src/{libraries/ApolloLink/HttpLink.js → exception/DecryptionKeyException.js} +12 -15
  22. package/src/exception/InvalidResponseException.js +4 -5
  23. package/src/exception/MetaMissingException.js +4 -6
  24. package/src/exception/MolecularHashMismatchException.js +4 -6
  25. package/src/exception/MolecularHashMissingException.js +4 -5
  26. package/src/exception/NegativeAmountException.js +4 -5
  27. package/src/exception/PolicyInvalidException.js +4 -4
  28. package/src/exception/SignatureMalformedException.js +4 -5
  29. package/src/exception/SignatureMismatchException.js +4 -5
  30. package/src/exception/StackableUnitAmountException.js +4 -5
  31. package/src/exception/StackableUnitDecimalsException.js +4 -5
  32. package/src/exception/TransferBalanceException.js +4 -5
  33. package/src/exception/TransferMalformedException.js +4 -5
  34. package/src/exception/TransferMismatchedException.js +4 -5
  35. package/src/exception/TransferRemainderException.js +4 -5
  36. package/src/exception/TransferToSelfException.js +4 -5
  37. package/src/exception/TransferUnbalancedException.js +4 -5
  38. package/src/exception/UnauthenticatedException.js +4 -5
  39. package/src/{libraries/ApolloLink/AuthLink.js → exception/WalletCredentialException.js} +12 -40
  40. package/src/exception/WalletShadowException.js +4 -5
  41. package/src/exception/WrongTokenTypeException.js +4 -5
  42. package/src/exception/index.js +26 -26
  43. package/src/index.js +8 -10
  44. package/src/instance/Rules/Callback.js +91 -93
  45. package/src/instance/Rules/Condition.js +21 -23
  46. package/src/instance/Rules/Meta.js +13 -14
  47. package/src/instance/Rules/Rule.js +39 -43
  48. package/src/instance/Rules/exception/RuleArgumentException.js +4 -4
  49. package/src/libraries/CheckMolecule.js +253 -232
  50. package/src/libraries/Decimal.js +13 -17
  51. package/src/libraries/Dot.js +74 -48
  52. package/src/libraries/Hex.js +49 -54
  53. package/src/libraries/apollo/ApolloClientWrapper.js +143 -0
  54. package/src/libraries/apollo/CipherLink.js +116 -0
  55. package/src/libraries/apollo/Client.js +154 -0
  56. package/src/libraries/apollo/ErrorHandler.js +32 -0
  57. package/src/libraries/apollo/SubscriptionManager.js +60 -0
  58. package/src/libraries/apollo/operationUtils.js +26 -0
  59. package/src/libraries/array.js +50 -41
  60. package/src/libraries/crypto.js +20 -27
  61. package/src/libraries/strings.js +58 -91
  62. package/src/mutation/Mutation.js +44 -25
  63. package/src/mutation/MutationActiveSession.js +12 -12
  64. package/src/mutation/MutationClaimShadowWallet.js +15 -17
  65. package/src/mutation/MutationCreateIdentifier.js +11 -12
  66. package/src/mutation/MutationCreateMeta.js +11 -12
  67. package/src/mutation/MutationCreateRule.js +11 -12
  68. package/src/mutation/MutationCreateToken.js +18 -13
  69. package/src/mutation/MutationCreateWallet.js +9 -11
  70. package/src/mutation/MutationDepositBufferToken.js +7 -9
  71. package/src/mutation/MutationLinkIdentifier.js +12 -14
  72. package/src/mutation/MutationProposeMolecule.js +24 -25
  73. package/src/mutation/MutationRequestAuthorization.js +9 -10
  74. package/src/mutation/MutationRequestAuthorizationGuest.js +12 -15
  75. package/src/mutation/MutationRequestTokens.js +11 -14
  76. package/src/mutation/MutationTransferTokens.js +11 -14
  77. package/src/mutation/MutationWithdrawBufferToken.js +7 -10
  78. package/src/query/Query.js +62 -36
  79. package/src/query/QueryActiveSession.js +11 -13
  80. package/src/query/QueryAtom.js +75 -76
  81. package/src/query/QueryBalance.js +11 -12
  82. package/src/query/QueryBatch.js +17 -14
  83. package/src/query/QueryBatchHistory.js +16 -13
  84. package/src/query/QueryContinuId.js +13 -10
  85. package/src/query/QueryMetaType.js +45 -57
  86. package/src/query/QueryMetaTypeViaAtom.js +49 -57
  87. package/src/query/QueryPolicy.js +11 -12
  88. package/src/query/QueryToken.js +11 -13
  89. package/src/query/QueryUserActivity.js +11 -13
  90. package/src/query/QueryWalletBundle.js +15 -47
  91. package/src/query/QueryWalletList.js +15 -16
  92. package/src/response/Response.js +29 -34
  93. package/src/response/ResponseActiveSession.js +6 -6
  94. package/src/response/ResponseAtom.js +29 -30
  95. package/src/response/ResponseAuthorizationGuest.js +17 -18
  96. package/src/response/ResponseBalance.js +12 -13
  97. package/src/response/ResponseClaimShadowWallet.js +1 -1
  98. package/src/response/ResponseContinuId.js +21 -22
  99. package/src/response/ResponseCreateIdentifier.js +1 -1
  100. package/src/response/ResponseCreateMeta.js +1 -1
  101. package/src/response/ResponseCreateRule.js +1 -1
  102. package/src/response/ResponseCreateToken.js +1 -1
  103. package/src/response/ResponseCreateWallet.js +1 -1
  104. package/src/response/ResponseLinkIdentifier.js +9 -10
  105. package/src/response/ResponseMetaBatch.js +6 -8
  106. package/src/response/ResponseMetaType.js +19 -20
  107. package/src/response/ResponseMetaTypeViaAtom.js +19 -19
  108. package/src/response/ResponsePolicy.js +14 -15
  109. package/src/response/ResponseProposeMolecule.js +27 -30
  110. package/src/response/ResponseQueryActiveSession.js +20 -23
  111. package/src/response/ResponseQueryUserActivity.js +11 -12
  112. package/src/response/ResponseRequestAuthorization.js +11 -16
  113. package/src/response/ResponseRequestAuthorizationGuest.js +18 -21
  114. package/src/response/ResponseRequestTokens.js +1 -1
  115. package/src/response/ResponseTransferTokens.js +8 -9
  116. package/src/response/ResponseWalletBundle.js +16 -17
  117. package/src/response/ResponseWalletList.js +44 -47
  118. package/src/subscribe/ActiveSessionSubscribe.js +5 -6
  119. package/src/subscribe/ActiveWalletSubscribe.js +5 -6
  120. package/src/subscribe/CreateMoleculeSubscribe.js +5 -5
  121. package/src/subscribe/Subscribe.js +26 -26
  122. package/src/subscribe/WalletStatusSubscribe.js +5 -6
  123. package/src/versions/HashAtom.js +78 -0
  124. package/src/versions/Version4.js +34 -0
  125. package/src/versions/index.js +5 -0
  126. package/dist/client.umd.js +0 -453
  127. package/src/httpClient/ApolloClient.js +0 -245
  128. package/src/libraries/ApolloLink/CipherLink.js +0 -117
  129. package/src/libraries/ApolloLink/Client.js +0 -231
  130. package/src/libraries/ApolloLink/PusherLink.js +0 -234
  131. package/src/libraries/ApolloLink/handler.js +0 -106
  132. package/src/libraries/Base58.js +0 -71
  133. package/src/libraries/Base64.js +0 -40
  134. package/src/libraries/BaseX.js +0 -91
  135. package/src/libraries/Soda.js +0 -93
  136. package/src/query/QueryMetaInstance.js +0 -99
  137. package/src/test/Test.js +0 -670
  138. package/src/test/TestTokenUnit.js +0 -340
@@ -45,18 +45,17 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
49
- import ResponseCreateWallet from '../response/ResponseCreateWallet';
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
+ import ResponseCreateWallet from '../response/ResponseCreateWallet'
50
50
 
51
51
  /**
52
52
  * Query for creating new Wallets
53
53
  */
54
54
  export default class MutationCreateWallet extends MutationProposeMolecule {
55
-
56
- fillMolecule ( newWallet ) {
57
- this.$__molecule.initWalletCreation( newWallet );
58
- this.$__molecule.sign( {} );
59
- this.$__molecule.check();
55
+ fillMolecule (wallet) {
56
+ this.$__molecule.initWalletCreation(wallet)
57
+ this.$__molecule.sign({})
58
+ this.$__molecule.check()
60
59
  }
61
60
 
62
61
  /**
@@ -65,11 +64,10 @@ export default class MutationCreateWallet extends MutationProposeMolecule {
65
64
  * @param {object} json
66
65
  * @return {ResponseCreateWallet}
67
66
  */
68
- createResponse ( json ) {
69
- return new ResponseCreateWallet( {
67
+ createResponse (json) {
68
+ return new ResponseCreateWallet({
70
69
  query: this,
71
70
  json
72
- } );
71
+ })
73
72
  }
74
-
75
73
  }
@@ -45,29 +45,27 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
49
 
50
50
  /**
51
51
  *
52
52
  */
53
53
  export default class MutationDepositBufferToken extends MutationProposeMolecule {
54
-
55
54
  /**
56
55
  * Fills the Molecule with provided wallet and amount data
57
56
  *
58
57
  * @param amount
59
58
  * @param tradeRates
60
59
  */
61
- fillMolecule ( {
60
+ fillMolecule ({
62
61
  amount,
63
62
  tradeRates
64
- } ) {
65
- this.$__molecule.initDepositBuffer( {
63
+ }) {
64
+ this.$__molecule.initDepositBuffer({
66
65
  amount,
67
66
  tradeRates
68
- } );
69
- this.$__molecule.sign( {} );
70
- this.$__molecule.check( this.$__molecule.sourceWallet );
67
+ })
68
+ this.$__molecule.sign({})
69
+ this.$__molecule.check(this.$__molecule.sourceWallet)
71
70
  }
72
-
73
71
  }
@@ -45,22 +45,21 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import Mutation from '../mutation/Mutation';
49
- import ResponseLinkIdentifier from '../response/ResponseLinkIdentifier';
50
- import { gql } from '@apollo/client/core';
48
+ import Mutation from '../mutation/Mutation'
49
+ import ResponseLinkIdentifier from '../response/ResponseLinkIdentifier'
50
+ import { gql } from '@apollo/client/core'
51
51
 
52
52
  /**
53
53
  * Query for linking an Identifier to a Wallet Bundle
54
54
  */
55
55
  export default class MutationLinkIdentifier extends Mutation {
56
-
57
56
  /**
58
- * Class constructor
59
- *
60
- * @param apolloClient
57
+ * @param {ApolloClientWrapper} apolloClient
58
+ * @param {KnishIOClient} knishIOClient
61
59
  */
62
- constructor ( apolloClient ) {
63
- super( apolloClient );
60
+ constructor (apolloClient, knishIOClient) {
61
+ super(apolloClient, knishIOClient)
62
+
64
63
  this.$__query = gql`mutation( $bundle: String!, $type: String!, $content: String! ) {
65
64
  LinkIdentifier( bundle: $bundle, type: $type, content: $content ) {
66
65
  type,
@@ -69,7 +68,7 @@ export default class MutationLinkIdentifier extends Mutation {
69
68
  set,
70
69
  message
71
70
  }
72
- }`;
71
+ }`
73
72
  }
74
73
 
75
74
  /**
@@ -78,11 +77,10 @@ export default class MutationLinkIdentifier extends Mutation {
78
77
  * @param {object} json
79
78
  * @return {ResponseLinkIdentifier}
80
79
  */
81
- createResponse ( json ) {
82
- return new ResponseLinkIdentifier( {
80
+ createResponse (json) {
81
+ return new ResponseLinkIdentifier({
83
82
  query: this,
84
83
  json
85
- } );
84
+ })
86
85
  }
87
-
88
86
  }
@@ -45,21 +45,21 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import ResponseProposeMolecule from '../response/ResponseProposeMolecule';
49
- import Mutation from './Mutation';
50
- import { gql } from '@apollo/client/core';
51
-
48
+ import ResponseProposeMolecule from '../response/ResponseProposeMolecule'
49
+ import Mutation from './Mutation'
50
+ import { gql } from '@apollo/client/core'
52
51
 
53
52
  export default class MutationProposeMolecule extends Mutation {
54
53
  /**
55
- *
56
- * @param {ApolloClient} apolloClient
54
+ * @param {ApolloClientWrapper} apolloClient
55
+ * @param {KnishIOClient} knishIOClient
57
56
  * @param molecule
58
57
  */
59
- constructor ( apolloClient, molecule = null ) {
60
- super( apolloClient );
61
- this.$__molecule = molecule;
62
- this.$__remainderWallet = null;
58
+ constructor (apolloClient, knishIOClient, molecule) {
59
+ super(apolloClient, knishIOClient)
60
+
61
+ this.$__molecule = molecule
62
+ this.$__remainderWallet = null
63
63
  this.$__query = gql`mutation( $molecule: MoleculeInput! ) {
64
64
  ProposeMolecule( molecule: $molecule ) {
65
65
  molecularHash,
@@ -73,7 +73,7 @@ export default class MutationProposeMolecule extends Mutation {
73
73
  processedAt,
74
74
  broadcastedAt,
75
75
  }
76
- }`;
76
+ }`
77
77
  }
78
78
 
79
79
  /**
@@ -82,10 +82,10 @@ export default class MutationProposeMolecule extends Mutation {
82
82
  * @param {object} variables
83
83
  * @return {object}
84
84
  */
85
- compiledVariables ( variables ) {
86
- const _variables = super.compiledVariables( variables );
85
+ compiledVariables (variables) {
86
+ const _variables = super.compiledVariables(variables)
87
87
 
88
- return { ..._variables, ...{ molecule: this.molecule() } };
88
+ return { ..._variables, ...{ molecule: this.molecule() } }
89
89
  }
90
90
 
91
91
  /**
@@ -94,11 +94,11 @@ export default class MutationProposeMolecule extends Mutation {
94
94
  * @param {object} json
95
95
  * @return {ResponseProposeMolecule}
96
96
  */
97
- createResponse ( json ) {
98
- return new ResponseProposeMolecule( {
97
+ createResponse (json) {
98
+ return new ResponseProposeMolecule({
99
99
  query: this,
100
100
  json
101
- } );
101
+ })
102
102
  }
103
103
 
104
104
  /**
@@ -107,13 +107,13 @@ export default class MutationProposeMolecule extends Mutation {
107
107
  * @param {object} variables
108
108
  * @return {Promise}
109
109
  */
110
- async execute ( { variables = null } ) {
111
- variables = variables || {};
112
- variables.molecule = this.molecule();
110
+ async execute ({ variables = null }) {
111
+ variables = variables || {}
112
+ variables.molecule = this.molecule()
113
113
 
114
- return super.execute( {
114
+ return super.execute({
115
115
  variables
116
- } );
116
+ })
117
117
  }
118
118
 
119
119
  /**
@@ -122,7 +122,7 @@ export default class MutationProposeMolecule extends Mutation {
122
122
  * @return {null}
123
123
  */
124
124
  remainderWallet () {
125
- return this.$__remainderWallet;
125
+ return this.$__remainderWallet
126
126
  }
127
127
 
128
128
  /**
@@ -131,7 +131,6 @@ export default class MutationProposeMolecule extends Mutation {
131
131
  * @return {Molecule}
132
132
  */
133
133
  molecule () {
134
- return this.$__molecule;
134
+ return this.$__molecule
135
135
  }
136
-
137
136
  }
@@ -45,8 +45,8 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
49
- import ResponseRequestAuthorization from '../response/ResponseRequestAuthorization';
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
+ import ResponseRequestAuthorization from '../response/ResponseRequestAuthorization'
50
50
 
51
51
  /**
52
52
  * Query for requesting an authorization token from the node
@@ -56,10 +56,10 @@ export default class MutationRequestAuthorization extends MutationProposeMolecul
56
56
  *
57
57
  * @param {object} meta
58
58
  */
59
- fillMolecule ( { meta } ) {
60
- this.$__molecule.initAuthorization( { meta } );
61
- this.$__molecule.sign( {} );
62
- this.$__molecule.check();
59
+ fillMolecule ({ meta }) {
60
+ this.$__molecule.initAuthorization({ meta })
61
+ this.$__molecule.sign({})
62
+ this.$__molecule.check()
63
63
  }
64
64
 
65
65
  /**
@@ -68,11 +68,10 @@ export default class MutationRequestAuthorization extends MutationProposeMolecul
68
68
  * @param {object} json
69
69
  * @return {ResponseRequestAuthorization}
70
70
  */
71
- createResponse ( json ) {
72
- return new ResponseRequestAuthorization( {
71
+ createResponse (json) {
72
+ return new ResponseRequestAuthorization({
73
73
  query: this,
74
74
  json
75
- } );
75
+ })
76
76
  }
77
-
78
77
  }
@@ -45,40 +45,37 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import { gql } from '@apollo/client/core';
49
- import Mutation from '../mutation/Mutation';
50
- import ResponseRequestAuthorizationGuest from '../response/ResponseRequestAuthorizationGuest';
51
-
48
+ import { gql } from '@apollo/client/core'
49
+ import Mutation from '../mutation/Mutation'
50
+ import ResponseRequestAuthorizationGuest from '../response/ResponseRequestAuthorizationGuest'
52
51
 
53
52
  export default class MutationRequestAuthorizationGuest extends Mutation {
54
53
  /**
55
- * Class constructor
56
- *
57
- * @param apolloClient
54
+ * @param {ApolloClientWrapper} apolloClient
55
+ * @param {KnishIOClient} knishIOClient
58
56
  */
59
- constructor ( apolloClient ) {
60
- super( apolloClient );
57
+ constructor (apolloClient, knishIOClient) {
58
+ super(apolloClient, knishIOClient)
59
+
61
60
  this.$__query = gql`mutation( $cellSlug: String, $pubkey: String, $encrypt: Boolean ) {
62
61
  AccessToken( cellSlug: $cellSlug, pubkey: $pubkey, encrypt: $encrypt ) {
63
62
  token,
64
63
  pubkey,
65
64
  expiresAt
66
65
  }
67
- }`;
66
+ }`
68
67
  }
69
68
 
70
-
71
69
  /**
72
70
  * Returns a Response object
73
71
  *
74
72
  * @param {object} json
75
73
  * @return {ResponseRequestAuthorizationGuest}
76
74
  */
77
- createResponse ( json ) {
78
- return new ResponseRequestAuthorizationGuest( {
75
+ createResponse (json) {
76
+ return new ResponseRequestAuthorizationGuest({
79
77
  query: this,
80
78
  json
81
- } );
79
+ })
82
80
  }
83
-
84
81
  }
@@ -45,9 +45,8 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
49
- import ResponseRequestTokens from '../response/ResponseRequestTokens';
50
-
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
+ import ResponseRequestTokens from '../response/ResponseRequestTokens'
51
50
 
52
51
  export default class MutationRequestTokens extends MutationProposeMolecule {
53
52
  /**
@@ -60,25 +59,24 @@ export default class MutationRequestTokens extends MutationProposeMolecule {
60
59
  * @param {object} meta
61
60
  * @param {string|null} batchId
62
61
  */
63
- fillMolecule ( {
62
+ fillMolecule ({
64
63
  token,
65
64
  amount,
66
65
  metaType,
67
66
  metaId,
68
67
  meta = null,
69
68
  batchId = null
70
- } ) {
71
-
72
- this.$__molecule.initTokenRequest( {
69
+ }) {
70
+ this.$__molecule.initTokenRequest({
73
71
  token,
74
72
  amount,
75
73
  metaType,
76
74
  metaId,
77
75
  meta: meta || {},
78
76
  batchId
79
- } );
80
- this.$__molecule.sign( {} );
81
- this.$__molecule.check();
77
+ })
78
+ this.$__molecule.sign({})
79
+ this.$__molecule.check()
82
80
  }
83
81
 
84
82
  /**
@@ -87,11 +85,10 @@ export default class MutationRequestTokens extends MutationProposeMolecule {
87
85
  * @param {object} json
88
86
  * @return {ResponseRequestTokens}
89
87
  */
90
- createResponse ( json ) {
91
- return new ResponseRequestTokens( {
88
+ createResponse (json) {
89
+ return new ResponseRequestTokens({
92
90
  query: this,
93
91
  json
94
- } );
92
+ })
95
93
  }
96
-
97
94
  }
@@ -45,31 +45,29 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
49
- import ResponseTransferTokens from '../response/ResponseTransferTokens';
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
+ import ResponseTransferTokens from '../response/ResponseTransferTokens'
50
50
 
51
51
  /**
52
52
  * Query for moving tokens between wallets
53
53
  */
54
54
  export default class MutationTransferTokens extends MutationProposeMolecule {
55
-
56
55
  /**
57
56
  * Fills the Molecule with provided wallet and amount data
58
57
  *
59
58
  * @param recipientWallet
60
59
  * @param amount
61
60
  */
62
- fillMolecule ( {
61
+ fillMolecule ({
63
62
  recipientWallet,
64
63
  amount
65
- } ) {
66
-
67
- this.$__molecule.initValue( {
64
+ }) {
65
+ this.$__molecule.initValue({
68
66
  recipientWallet,
69
67
  amount
70
- } );
71
- this.$__molecule.sign( {} );
72
- this.$__molecule.check( this.$__molecule.sourceWallet );
68
+ })
69
+ this.$__molecule.sign({})
70
+ this.$__molecule.check(this.$__molecule.sourceWallet)
73
71
  }
74
72
 
75
73
  /**
@@ -78,11 +76,10 @@ export default class MutationTransferTokens extends MutationProposeMolecule {
78
76
  * @param {object} json
79
77
  * @return {ResponseTransferTokens}
80
78
  */
81
- createResponse ( json ) {
82
- return new ResponseTransferTokens( {
79
+ createResponse (json) {
80
+ return new ResponseTransferTokens({
83
81
  query: this,
84
82
  json
85
- } );
83
+ })
86
84
  }
87
-
88
85
  }
@@ -45,29 +45,26 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import MutationProposeMolecule from './MutationProposeMolecule';
48
+ import MutationProposeMolecule from './MutationProposeMolecule'
49
49
 
50
50
  /**
51
51
  *
52
52
  */
53
53
  export default class MutationWithdrawBufferToken extends MutationProposeMolecule {
54
-
55
54
  /**
56
55
  *
57
56
  * @param recipients
58
57
  * @param signingWallet
59
58
  */
60
- fillMolecule ( {
59
+ fillMolecule ({
61
60
  recipients,
62
61
  signingWallet
63
- } ) {
64
-
65
- this.$__molecule.initWithdrawBuffer( {
62
+ }) {
63
+ this.$__molecule.initWithdrawBuffer({
66
64
  recipients,
67
65
  signingWallet
68
- } );
69
- this.$__molecule.sign( {} );
70
- this.$__molecule.check( this.$__molecule.sourceWallet );
66
+ })
67
+ this.$__molecule.sign({})
68
+ this.$__molecule.check(this.$__molecule.sourceWallet)
71
69
  }
72
-
73
70
  }
@@ -45,28 +45,30 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
45
45
 
46
46
  License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
47
47
  */
48
- import CodeException from '../exception/CodeException';
49
- import Response from '../response/Response';
48
+ import CodeException from '../exception/CodeException'
49
+ import Response from '../response/Response'
50
50
 
51
51
  export default class Query {
52
52
  /**
53
- *
54
- * @param {ApolloClient} apolloClient
53
+ * @param {ApolloClientWrapper} apolloClient
54
+ * @param {KnishIOClient} knishIOClient
55
55
  */
56
- constructor ( apolloClient ) {
57
- this.client = apolloClient;
58
- this.$__variables = null;
59
- this.$__query = null;
56
+ constructor (apolloClient, knishIOClient) {
57
+ this.client = apolloClient
58
+ this.knishIOClient = knishIOClient
59
+ this.$__variables = null
60
+ this.$__query = null
61
+ this.$__response = null
62
+ this.$__request = null
60
63
  }
61
64
 
62
-
63
65
  /**
64
66
  * Return a response object
65
67
  * Used at KnishIOClient::createMolecule => sets the source wallet from the remainder one stored in response object
66
68
  * @return {Response}
67
69
  */
68
70
  response () {
69
- return this.$__response;
71
+ return this.$__response
70
72
  }
71
73
 
72
74
  /**
@@ -75,8 +77,8 @@ export default class Query {
75
77
  * @param response
76
78
  * @return {Promise<Response>}
77
79
  */
78
- async createResponseRaw ( response ) {
79
- return this.createResponse( response );
80
+ async createResponseRaw (response) {
81
+ return this.createResponse(response)
80
82
  }
81
83
 
82
84
  /**
@@ -85,11 +87,11 @@ export default class Query {
85
87
  * @param {object} json
86
88
  * @return {Response}
87
89
  */
88
- createResponse ( json ) {
89
- return new Response( {
90
+ createResponse (json) {
91
+ return new Response({
90
92
  query: this,
91
93
  json
92
- } );
94
+ })
93
95
  }
94
96
 
95
97
  /**
@@ -98,43 +100,62 @@ export default class Query {
98
100
  * @param {{}} variables
99
101
  * @returns {{variables: (Object|null), query: null}}
100
102
  */
101
- createQuery ( { variables = null } ) {
102
- this.$__variables = this.compiledVariables( variables );
103
+ createQuery ({ variables = null }) {
104
+ this.$__variables = this.compiledVariables(variables)
103
105
 
104
106
  // Uri is a required parameter
105
- let uri = this.uri();
107
+ const uri = this.uri()
106
108
 
107
- if ( !uri ) {
108
- throw new CodeException( 'Query::createQuery() - Node URI was not initialized for this client instance!' );
109
+ if (!uri) {
110
+ throw new CodeException('Query::createQuery() - Node URI was not initialized for this client instance!')
109
111
  }
110
112
 
111
- if ( this.$__query === null ) {
112
- throw new CodeException( 'Query::createQuery() - GraphQL subscription was not initialized!' );
113
+ if (this.$__query === null) {
114
+ throw new CodeException('Query::createQuery() - GraphQL subscription was not initialized!')
113
115
  }
114
116
 
115
117
  return {
116
118
  query: this.$__query,
117
119
  variables: this.variables()
118
- };
120
+ }
119
121
  }
120
122
 
121
123
  /**
122
124
  * Sends the Query to a Knish.IO node and returns the Response
123
125
  *
124
126
  * @param {object} variables
127
+ * @param {object} context
125
128
  * @return {Promise<Response>}
126
129
  */
127
- async execute ( { variables = null } ) {
128
-
129
- this.$__request = this.createQuery( {
130
- variables
131
- } );
132
-
133
- let response = await this.client.query( this.$__request );
130
+ async execute ({ variables = null, context = {} }) {
131
+ this.$__request = this.createQuery({ variables })
134
132
 
135
- this.$__response = await this.createResponseRaw( response );
133
+ const mergedContext = {
134
+ ...context,
135
+ ...this.createQueryContext()
136
+ }
136
137
 
137
- return this.$__response;
138
+ try {
139
+ const response = await this.client.query({
140
+ ...this.$__request,
141
+ context: mergedContext
142
+ })
143
+
144
+ this.$__response = await this.createResponseRaw(response)
145
+
146
+ return this.$__response
147
+ } catch (error) {
148
+ if (error.name === 'AbortError') {
149
+ this.knishIOClient.log('warn', 'Query was cancelled')
150
+ // You might want to create a custom response for cancelled queries
151
+ return new Response({
152
+ query: this,
153
+ json: { data: null, errors: [{ message: 'Query was cancelled' }] }
154
+ })
155
+ } else {
156
+ throw error
157
+ }
158
+ }
138
159
  }
139
160
 
140
161
  /**
@@ -143,8 +164,8 @@ export default class Query {
143
164
  * @param {object} variables
144
165
  * @return {object}
145
166
  */
146
- compiledVariables ( variables = null ) {
147
- return variables || {};
167
+ compiledVariables (variables = null) {
168
+ return variables || {}
148
169
  }
149
170
 
150
171
  /**
@@ -153,7 +174,7 @@ export default class Query {
153
174
  * @return {string}
154
175
  */
155
176
  uri () {
156
- return this.client.getUri();
177
+ return this.client.getUri()
157
178
  }
158
179
 
159
180
  /**
@@ -162,6 +183,11 @@ export default class Query {
162
183
  * @return {object|null}
163
184
  */
164
185
  variables () {
165
- return this.$__variables;
186
+ return this.$__variables
187
+ }
188
+
189
+ createQueryContext () {
190
+ // Override this method in subclasses if needed
191
+ return {}
166
192
  }
167
193
  }