@wishknish/knishio-client-js 0.6.0 → 0.6.2
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/client.iife.js +41 -90
- package/package.json +19 -18
- package/src/AuthToken.js +1 -1
- package/src/KnishIOClient.js +8 -8
- package/src/Wallet.js +41 -3
- package/src/libraries/strings.js +4 -2
- package/src/libraries/urql/UrqlClientWrapper.js +166 -0
- package/src/mutation/MutationActiveSession.js +4 -4
- package/src/mutation/MutationLinkIdentifier.js +4 -4
- package/src/mutation/MutationProposeMolecule.js +4 -4
- package/src/mutation/MutationRequestAuthorizationGuest.js +4 -4
- package/src/query/Query.js +3 -3
- package/src/query/QueryActiveSession.js +4 -4
- package/src/query/QueryAtom.js +4 -4
- package/src/query/QueryBalance.js +4 -4
- package/src/query/QueryBatch.js +4 -4
- package/src/query/QueryBatchHistory.js +4 -4
- package/src/query/QueryContinuId.js +4 -4
- package/src/query/QueryMetaType.js +4 -4
- package/src/query/QueryMetaTypeViaAtom.js +4 -4
- package/src/query/QueryPolicy.js +4 -4
- package/src/query/QueryToken.js +4 -4
- package/src/query/QueryUserActivity.js +4 -4
- package/src/query/QueryWalletBundle.js +4 -4
- package/src/query/QueryWalletList.js +4 -4
- package/src/subscribe/ActiveSessionSubscribe.js +3 -3
- package/src/subscribe/ActiveWalletSubscribe.js +3 -3
- package/src/subscribe/CreateMoleculeSubscribe.js +3 -3
- package/src/subscribe/Subscribe.js +3 -3
- package/src/subscribe/WalletStatusSubscribe.js +3 -3
- package/src/types/index.js +2 -0
- package/src/libraries/apollo/ApolloClientWrapper.js +0 -143
- package/src/libraries/apollo/CipherLink.js +0 -116
- package/src/libraries/apollo/Client.js +0 -154
- package/src/libraries/apollo/ErrorHandler.js +0 -32
- package/src/libraries/apollo/SubscriptionManager.js +0 -60
- package/src/libraries/apollo/operationUtils.js +0 -26
|
@@ -46,16 +46,16 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
|
|
|
46
46
|
License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
47
47
|
*/
|
|
48
48
|
import Query from './Query'
|
|
49
|
-
import { gql } from '@apollo/client/core'
|
|
50
49
|
import ResponseMetaTypeViaAtom from '../response/ResponseMetaTypeViaAtom'
|
|
50
|
+
import { gql } from '@urql/core'
|
|
51
51
|
|
|
52
52
|
export default class QueryMetaTypeViaAtom extends Query {
|
|
53
53
|
/**
|
|
54
|
-
* @param {
|
|
54
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
55
55
|
* @param {KnishIOClient} knishIOClient
|
|
56
56
|
*/
|
|
57
|
-
constructor (
|
|
58
|
-
super(
|
|
57
|
+
constructor (graphQLClient, knishIOClient) {
|
|
58
|
+
super(graphQLClient, knishIOClient)
|
|
59
59
|
|
|
60
60
|
this.$__query = gql`query ($metaTypes: [String!], $metaIds: [String!], $values: [String!], $keys: [String!], $latest: Boolean, $filter: [MetaFilter!], $queryArgs: QueryArgs, $countBy: String, $atomValues: [String!] ) {
|
|
61
61
|
MetaTypeViaAtom(
|
package/src/query/QueryPolicy.js
CHANGED
|
@@ -47,16 +47,16 @@ License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
49
|
import Query from './Query'
|
|
50
|
-
import { gql } from '@apollo/client/core'
|
|
51
50
|
import ResponsePolicy from '../response/ResponsePolicy'
|
|
51
|
+
import { gql } from "@urql/core";
|
|
52
52
|
|
|
53
53
|
export default class QueryPolicy extends Query {
|
|
54
54
|
/**
|
|
55
|
-
* @param {
|
|
55
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
56
56
|
* @param {KnishIOClient} knishIOClient
|
|
57
57
|
*/
|
|
58
|
-
constructor (
|
|
59
|
-
super(
|
|
58
|
+
constructor (graphQLClient, knishIOClient) {
|
|
59
|
+
super(graphQLClient, knishIOClient)
|
|
60
60
|
|
|
61
61
|
this.$__query = gql`query( $metaType: String, $metaId: String, ) {
|
|
62
62
|
Policy( metaType: $metaType, metaId: $metaId ) {
|
package/src/query/QueryToken.js
CHANGED
|
@@ -46,19 +46,19 @@ Please visit https://github.com/WishKnish/KnishIO-Client-JS for information.
|
|
|
46
46
|
License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
47
47
|
*/
|
|
48
48
|
import Query from './Query'
|
|
49
|
-
import { gql } from '@apollo/client/core'
|
|
50
49
|
import Response from '../response/Response'
|
|
50
|
+
import { gql } from '@urql/core'
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Query for getting the token info
|
|
54
54
|
*/
|
|
55
55
|
export default class QueryToken extends Query {
|
|
56
56
|
/**
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
58
58
|
* @param {KnishIOClient} knishIOClient
|
|
59
59
|
*/
|
|
60
|
-
constructor (
|
|
61
|
-
super(
|
|
60
|
+
constructor (graphQLClient, knishIOClient) {
|
|
61
|
+
super(graphQLClient, knishIOClient)
|
|
62
62
|
|
|
63
63
|
this.$__query = gql`query( $slug: String, $slugs: [ String! ], $limit: Int, $order: String ) {
|
|
64
64
|
Token( slug: $slug, slugs: $slugs, limit: $limit, order: $order ) {
|
|
@@ -47,18 +47,18 @@ License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
|
47
47
|
*/
|
|
48
48
|
import Query from './Query'
|
|
49
49
|
import ResponseQueryUserActivity from '../response/ResponseQueryUserActivity'
|
|
50
|
-
import { gql } from '@
|
|
50
|
+
import { gql } from '@urql/core'
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Query for retrieving information about user activity
|
|
54
54
|
*/
|
|
55
55
|
export default class QueryUserActivity extends Query {
|
|
56
56
|
/**
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
58
58
|
* @param {KnishIOClient} knishIOClient
|
|
59
59
|
*/
|
|
60
|
-
constructor (
|
|
61
|
-
super(
|
|
60
|
+
constructor (graphQLClient, knishIOClient) {
|
|
61
|
+
super(graphQLClient, knishIOClient)
|
|
62
62
|
|
|
63
63
|
this.$__query = gql`query UserActivity (
|
|
64
64
|
$bundleHash:String,
|
|
@@ -47,18 +47,18 @@ License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
|
47
47
|
*/
|
|
48
48
|
import Query from './Query'
|
|
49
49
|
import ResponseWalletBundle from '../response/ResponseWalletBundle'
|
|
50
|
-
import { gql } from '@
|
|
50
|
+
import { gql } from '@urql/core'
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Query for retrieving information about Wallet Bundles
|
|
54
54
|
*/
|
|
55
55
|
export default class QueryWalletBundle extends Query {
|
|
56
56
|
/**
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
58
58
|
* @param {KnishIOClient} knishIOClient
|
|
59
59
|
*/
|
|
60
|
-
constructor (
|
|
61
|
-
super(
|
|
60
|
+
constructor (graphQLClient, knishIOClient) {
|
|
61
|
+
super(graphQLClient, knishIOClient)
|
|
62
62
|
|
|
63
63
|
this.$__query = gql`query( $bundleHashes: [ String! ] ) {
|
|
64
64
|
WalletBundle( bundleHashes: $bundleHashes ) {
|
|
@@ -47,18 +47,18 @@ License: https://github.com/WishKnish/KnishIO-Client-JS/blob/master/LICENSE
|
|
|
47
47
|
*/
|
|
48
48
|
import Query from './Query'
|
|
49
49
|
import ResponseWalletList from '../response/ResponseWalletList'
|
|
50
|
-
import { gql } from '@
|
|
50
|
+
import { gql } from '@urql/core'
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* Query for getting a list of Wallets
|
|
54
54
|
*/
|
|
55
55
|
export default class QueryWalletList extends Query {
|
|
56
56
|
/**
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
58
58
|
* @param {KnishIOClient} knishIOClient
|
|
59
59
|
*/
|
|
60
|
-
constructor (
|
|
61
|
-
super(
|
|
60
|
+
constructor (graphQLClient, knishIOClient) {
|
|
61
|
+
super(graphQLClient, knishIOClient)
|
|
62
62
|
|
|
63
63
|
this.$__query = gql`query( $bundleHash: String, $tokenSlug: String ) {
|
|
64
64
|
Wallet( bundleHash: $bundleHash, tokenSlug: $tokenSlug ) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import Subscribe from './Subscribe'
|
|
2
|
-
import { gql } from '@
|
|
2
|
+
import { gql } from '@urql/core'
|
|
3
3
|
|
|
4
4
|
export default class ActiveSessionSubscribe extends Subscribe {
|
|
5
|
-
constructor (
|
|
6
|
-
super(
|
|
5
|
+
constructor (graphQLClient) {
|
|
6
|
+
super(graphQLClient)
|
|
7
7
|
this.$__subscribe = gql`
|
|
8
8
|
subscription onActiveUser ( $metaType: String!, $metaId: String! ) {
|
|
9
9
|
ActiveUser( metaType: $metaType, metaId: $metaId ) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import Subscribe from './Subscribe'
|
|
2
|
-
import { gql } from '@
|
|
2
|
+
import { gql } from '@urql/core'
|
|
3
3
|
|
|
4
4
|
export default class ActiveWalletSubscribe extends Subscribe {
|
|
5
|
-
constructor (
|
|
6
|
-
super(
|
|
5
|
+
constructor (graphQLClient) {
|
|
6
|
+
super(graphQLClient)
|
|
7
7
|
this.$__subscribe = gql`
|
|
8
8
|
subscription onActiveWallet ( $bundle: String! ) {
|
|
9
9
|
ActiveWallet( bundle: $bundle ) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import Subscribe from './Subscribe'
|
|
2
|
-
import { gql } from '@
|
|
2
|
+
import { gql } from '@urql/core'
|
|
3
3
|
|
|
4
4
|
export default class CreateMoleculeSubscribe extends Subscribe {
|
|
5
|
-
constructor (
|
|
6
|
-
super(
|
|
5
|
+
constructor (graphQLClient) {
|
|
6
|
+
super(graphQLClient)
|
|
7
7
|
this.$__subscribe = gql`
|
|
8
8
|
subscription onCreateMolecule ( $bundle: String! ) {
|
|
9
9
|
CreateMolecule( bundle: $bundle ) {
|
|
@@ -3,10 +3,10 @@ import CodeException from '../exception/CodeException'
|
|
|
3
3
|
export default class Subscribe {
|
|
4
4
|
/**
|
|
5
5
|
*
|
|
6
|
-
* @param {
|
|
6
|
+
* @param {UrqlClientWrapper} graphQLClient
|
|
7
7
|
*/
|
|
8
|
-
constructor (
|
|
9
|
-
this.client =
|
|
8
|
+
constructor (graphQLClient) {
|
|
9
|
+
this.client = graphQLClient
|
|
10
10
|
this.$__variables = null
|
|
11
11
|
this.$__subscribe = null
|
|
12
12
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import Subscribe from './Subscribe'
|
|
2
|
-
import { gql } from '@
|
|
2
|
+
import { gql } from '@urql/core'
|
|
3
3
|
|
|
4
4
|
export default class WalletStatusSubscribe extends Subscribe {
|
|
5
|
-
constructor (
|
|
6
|
-
super(
|
|
5
|
+
constructor (graphQLClient) {
|
|
6
|
+
super(graphQLClient)
|
|
7
7
|
this.$__subscribe = gql`
|
|
8
8
|
subscription onWalletStatus ( $bundle: String!, $token: String! ) {
|
|
9
9
|
WalletStatus( bundle: $bundle, token: $token ) {
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import Client from './Client'
|
|
2
|
-
import SubscriptionManager from './SubscriptionManager'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* ApolloClient wrapper class
|
|
6
|
-
* Provides a high-level interface for interacting with the GraphQL server
|
|
7
|
-
*/
|
|
8
|
-
export default class ApolloClientWrapper {
|
|
9
|
-
/**
|
|
10
|
-
* @param {Object} config - Configuration object
|
|
11
|
-
* @param {string} config.serverUri - URI of the GraphQL server
|
|
12
|
-
* @param {Object|null} config.socket - WebSocket configuration (optional)
|
|
13
|
-
* @param {boolean} config.encrypt - Whether to use encryption (default: false)
|
|
14
|
-
*/
|
|
15
|
-
constructor ({ serverUri, socket = null, encrypt = false }) {
|
|
16
|
-
// Initialize the underlying Apollo Client
|
|
17
|
-
this.$__client = new Client({ serverUri, soketi: socket, encrypt })
|
|
18
|
-
|
|
19
|
-
// Initialize the subscription manager
|
|
20
|
-
this.$__subscriptionManager = new SubscriptionManager(this.$__client)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Set encryption for the client
|
|
25
|
-
* @param {boolean} encrypt - Whether to use encryption
|
|
26
|
-
*/
|
|
27
|
-
setEncryption (encrypt = false) {
|
|
28
|
-
const clientConfig = {
|
|
29
|
-
serverUri: this.$__client.getServerUri(),
|
|
30
|
-
soketi: {
|
|
31
|
-
socketUri: this.$__client.getSocketUri(),
|
|
32
|
-
appKey: 'knishio'
|
|
33
|
-
},
|
|
34
|
-
encrypt
|
|
35
|
-
}
|
|
36
|
-
this.$__client = new Client(clientConfig)
|
|
37
|
-
this.$__subscriptionManager.setClient(this.$__client)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Unsubscribe from a specific subscription
|
|
42
|
-
* @param {string} operationName - Name of the operation to unsubscribe from
|
|
43
|
-
*/
|
|
44
|
-
unsubscribe (operationName) {
|
|
45
|
-
this.$__subscriptionManager.unsubscribe(operationName)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Unsubscribe from all subscriptions
|
|
50
|
-
*/
|
|
51
|
-
unsubscribeAll () {
|
|
52
|
-
this.$__subscriptionManager.unsubscribeAll()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Disconnect the WebSocket
|
|
57
|
-
*/
|
|
58
|
-
socketDisconnect () {
|
|
59
|
-
this.$__client.socketDisconnect()
|
|
60
|
-
this.$__subscriptionManager.clear()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Create a new subscription
|
|
65
|
-
* @param {Object} request - Subscription request
|
|
66
|
-
* @param {Function} closure - Callback function for subscription updates
|
|
67
|
-
* @returns {Object} Subscription object
|
|
68
|
-
*/
|
|
69
|
-
subscribe (request, closure) {
|
|
70
|
-
return this.$__subscriptionManager.subscribe(request, closure)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Execute a query
|
|
75
|
-
* @param {Object} request - Query request
|
|
76
|
-
* @returns {Promise} Promise resolving to the query result
|
|
77
|
-
*/
|
|
78
|
-
async query (request) {
|
|
79
|
-
return this.$__client.query(request)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Execute a mutation
|
|
84
|
-
* @param {Object} request - Mutation request
|
|
85
|
-
* @returns {Promise} Promise resolving to the mutation result
|
|
86
|
-
*/
|
|
87
|
-
async mutate (request) {
|
|
88
|
-
return this.$__client.mutate(request)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Set authentication data for the client
|
|
93
|
-
* @param {Object} authData - Authentication data
|
|
94
|
-
* @param {string} authData.token - Authentication token
|
|
95
|
-
* @param {string} authData.pubkey - Public key
|
|
96
|
-
* @param {Object} authData.wallet - Wallet object
|
|
97
|
-
*/
|
|
98
|
-
setAuthData ({ token, pubkey, wallet }) {
|
|
99
|
-
this.$__client.setAuthData({ token, pubkey, wallet })
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Getter methods
|
|
103
|
-
getAuthToken () { return this.$__client.getAuthToken() }
|
|
104
|
-
|
|
105
|
-
getUri () {
|
|
106
|
-
return this.$__client ? this.$__client.getServerUri() : null
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
getSocketUri () { return this.$__client.getSocketUri() }
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Set the server URI
|
|
113
|
-
* @param {string} uri - New server URI
|
|
114
|
-
*/
|
|
115
|
-
setUri (uri) {
|
|
116
|
-
const clientConfig = {
|
|
117
|
-
serverUri: uri,
|
|
118
|
-
soketi: {
|
|
119
|
-
socketUri: this.$__client.getSocketUri(),
|
|
120
|
-
appKey: 'knishio'
|
|
121
|
-
},
|
|
122
|
-
encrypt: !!this.$__client.cipherLink
|
|
123
|
-
}
|
|
124
|
-
this.$__client = new Client(clientConfig)
|
|
125
|
-
this.$__subscriptionManager.setClient(this.$__client)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Set the WebSocket URI
|
|
130
|
-
* @param {Object} config - WebSocket configuration
|
|
131
|
-
* @param {string} config.socketUri - New WebSocket URI
|
|
132
|
-
* @param {string} config.appKey - Application key for the WebSocket
|
|
133
|
-
*/
|
|
134
|
-
setSocketUri ({ socketUri, appKey }) {
|
|
135
|
-
const clientConfig = {
|
|
136
|
-
serverUri: this.$__client.getServerUri(),
|
|
137
|
-
soketi: { socketUri, appKey },
|
|
138
|
-
encrypt: !!this.$__client.cipherLink
|
|
139
|
-
}
|
|
140
|
-
this.$__client = new Client(clientConfig)
|
|
141
|
-
this.$__subscriptionManager.setClient(this.$__client)
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { ApolloLink, gql } from '@apollo/client/core'
|
|
2
|
-
import { print } from 'graphql'
|
|
3
|
-
import { operationName, operationType } from './operationUtils'
|
|
4
|
-
import CodeException from '../../exception/CodeException'
|
|
5
|
-
import Dot from '../Dot'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Custom Apollo Link for handling encryption and decryption of GraphQL operations
|
|
9
|
-
*/
|
|
10
|
-
class CipherLink extends ApolloLink {
|
|
11
|
-
constructor () {
|
|
12
|
-
super()
|
|
13
|
-
this.__wallet = null
|
|
14
|
-
this.__pubkey = null
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Set the wallet for encryption
|
|
19
|
-
* @param {Object|null} wallet - Wallet object
|
|
20
|
-
*/
|
|
21
|
-
setWallet (wallet) {
|
|
22
|
-
this.__wallet = wallet
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Get the current wallet
|
|
27
|
-
* @returns {Object|null} Wallet object
|
|
28
|
-
*/
|
|
29
|
-
getWallet () {
|
|
30
|
-
return this.__wallet
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Set the public key for encryption
|
|
35
|
-
* @param {string|null} pubkey - Public key
|
|
36
|
-
*/
|
|
37
|
-
setPubKey (pubkey) {
|
|
38
|
-
this.__pubkey = pubkey
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Get the current public key
|
|
43
|
-
* @returns {string|null} Public key
|
|
44
|
-
*/
|
|
45
|
-
getPubKey () {
|
|
46
|
-
return this.__pubkey
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Handle the request and apply encryption if necessary
|
|
51
|
-
* @param {Object} operation - GraphQL operation
|
|
52
|
-
* @param {function} forward - Function to forward the operation
|
|
53
|
-
* @returns {Observable} Observable of the operation result
|
|
54
|
-
*/
|
|
55
|
-
request (operation, forward) {
|
|
56
|
-
const requestName = operationName(operation)
|
|
57
|
-
const requestType = operationType(operation)
|
|
58
|
-
const isMoleculeMutation = (requestType === 'mutation' && requestName === 'ProposeMolecule')
|
|
59
|
-
|
|
60
|
-
// Check if encryption should be skipped
|
|
61
|
-
const skipEncryption = [
|
|
62
|
-
(requestType === 'query' && ['__schema', 'ContinuId'].includes(requestName)),
|
|
63
|
-
(requestType === 'mutation' && requestName === 'AccessToken'),
|
|
64
|
-
(isMoleculeMutation && Dot.get(operation, 'variables.molecule.atoms.0.isotope') === 'U')
|
|
65
|
-
].some(condition => condition)
|
|
66
|
-
|
|
67
|
-
if (skipEncryption) {
|
|
68
|
-
return forward(operation)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const wallet = this.getWallet()
|
|
72
|
-
const pubKey = this.getPubKey()
|
|
73
|
-
|
|
74
|
-
if (!pubKey) {
|
|
75
|
-
throw new CodeException('CipherLink::request() - Node public key missing!')
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (!wallet) {
|
|
79
|
-
throw new CodeException('CipherLink::request() - Authorized wallet missing!')
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const cipher = {
|
|
83
|
-
query: print(operation.query),
|
|
84
|
-
variables: JSON.stringify(operation.variables)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Encrypt the operation
|
|
88
|
-
operation.operationName = null
|
|
89
|
-
operation.query = gql`query ($Hash: String!) { CipherHash(Hash: $Hash) { hash } }`
|
|
90
|
-
operation.variables = { Hash: JSON.stringify(wallet.encryptMessage(cipher, pubKey)) }
|
|
91
|
-
|
|
92
|
-
// Forward the encrypted operation and handle the response
|
|
93
|
-
return forward(operation).map(data => {
|
|
94
|
-
let response = data.data
|
|
95
|
-
|
|
96
|
-
if (response.data) {
|
|
97
|
-
response = response.data
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (response.CipherHash && response.CipherHash.hash) {
|
|
101
|
-
const encrypted = JSON.parse(response.CipherHash.hash)
|
|
102
|
-
const decryption = wallet.decryptMessage(encrypted)
|
|
103
|
-
|
|
104
|
-
if (decryption === null) {
|
|
105
|
-
throw new CodeException('CipherLink::request() - Unable to decrypt response!')
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return decryption
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return data
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export default CipherLink
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ApolloClient,
|
|
3
|
-
InMemoryCache,
|
|
4
|
-
from,
|
|
5
|
-
split
|
|
6
|
-
} from '@apollo/client/core'
|
|
7
|
-
import { onError } from '@apollo/client/link/error'
|
|
8
|
-
import { getMainDefinition } from '@apollo/client/utilities'
|
|
9
|
-
import { createHttpLink } from '@apollo/client/link/http'
|
|
10
|
-
import { setContext } from '@apollo/client/link/context'
|
|
11
|
-
import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
|
|
12
|
-
import { createClient } from 'graphql-ws'
|
|
13
|
-
import fetch from 'isomorphic-fetch'
|
|
14
|
-
import { errorHandler } from './errorHandler'
|
|
15
|
-
import CipherLink from './CipherLink'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Custom Apollo Client implementation
|
|
19
|
-
* Extends ApolloClient with additional functionality
|
|
20
|
-
*/
|
|
21
|
-
class Client extends ApolloClient {
|
|
22
|
-
/**
|
|
23
|
-
* @param {Object} config - Configuration object
|
|
24
|
-
* @param {string} config.serverUri - URI of the GraphQL server
|
|
25
|
-
* @param {Object|null} config.soketi - WebSocket configuration (optional)
|
|
26
|
-
* @param {boolean} config.encrypt - Whether to use encryption (default: false)
|
|
27
|
-
*/
|
|
28
|
-
constructor ({ serverUri, soketi = null, encrypt = false }) {
|
|
29
|
-
// Create HTTP link
|
|
30
|
-
const httpLink = createHttpLink({
|
|
31
|
-
uri: serverUri,
|
|
32
|
-
fetch
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
// Initialize empty auth token
|
|
36
|
-
const authToken = ''
|
|
37
|
-
|
|
38
|
-
// Create auth link to add token to headers
|
|
39
|
-
const authLink = setContext((_, { headers }) => ({
|
|
40
|
-
headers: {
|
|
41
|
-
...headers,
|
|
42
|
-
'X-Auth-Token': authToken
|
|
43
|
-
}
|
|
44
|
-
}))
|
|
45
|
-
|
|
46
|
-
// Create error handling link
|
|
47
|
-
const errorLink = onError(errorHandler)
|
|
48
|
-
|
|
49
|
-
// Combine links
|
|
50
|
-
let link = from([authLink, errorLink, httpLink])
|
|
51
|
-
|
|
52
|
-
// Add encryption link if enabled
|
|
53
|
-
let cipherLink
|
|
54
|
-
if (encrypt) {
|
|
55
|
-
cipherLink = new CipherLink()
|
|
56
|
-
link = from([cipherLink, link])
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Add WebSocket link if configured
|
|
60
|
-
let wsLink
|
|
61
|
-
if (soketi && soketi.socketUri) {
|
|
62
|
-
wsLink = new GraphQLWsLink(createClient({
|
|
63
|
-
url: soketi.socketUri,
|
|
64
|
-
connectionParams: () => ({
|
|
65
|
-
authToken
|
|
66
|
-
})
|
|
67
|
-
}))
|
|
68
|
-
|
|
69
|
-
// Split queries between WebSocket and HTTP
|
|
70
|
-
link = split(
|
|
71
|
-
({ query }) => {
|
|
72
|
-
const definition = getMainDefinition(query)
|
|
73
|
-
return (
|
|
74
|
-
definition.kind === 'OperationDefinition' &&
|
|
75
|
-
definition.operation === 'subscription'
|
|
76
|
-
)
|
|
77
|
-
},
|
|
78
|
-
wsLink,
|
|
79
|
-
link
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Call parent constructor with configured options
|
|
84
|
-
super({
|
|
85
|
-
link,
|
|
86
|
-
cache: new InMemoryCache(),
|
|
87
|
-
defaultOptions: {
|
|
88
|
-
watchQuery: {
|
|
89
|
-
fetchPolicy: 'no-cache',
|
|
90
|
-
errorPolicy: 'ignore'
|
|
91
|
-
},
|
|
92
|
-
query: {
|
|
93
|
-
fetchPolicy: 'no-cache',
|
|
94
|
-
errorPolicy: 'all'
|
|
95
|
-
},
|
|
96
|
-
mutate: {
|
|
97
|
-
fetchPolicy: 'no-cache',
|
|
98
|
-
errorPolicy: 'all'
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
// Store configuration and links
|
|
104
|
-
this.serverUri = serverUri
|
|
105
|
-
this.soketi = soketi
|
|
106
|
-
this.authToken = authToken
|
|
107
|
-
this.pubkey = null
|
|
108
|
-
this.wallet = null
|
|
109
|
-
this.cipherLink = cipherLink
|
|
110
|
-
this.wsLink = wsLink
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Set authentication data
|
|
115
|
-
* @param {Object} authData - Authentication data
|
|
116
|
-
* @param {string} authData.token - Auth token
|
|
117
|
-
* @param {string} authData.pubkey - Public key
|
|
118
|
-
* @param {Object} authData.wallet - Wallet object
|
|
119
|
-
*/
|
|
120
|
-
setAuthData ({ token, pubkey, wallet }) {
|
|
121
|
-
this.authToken = token
|
|
122
|
-
this.pubkey = pubkey
|
|
123
|
-
this.wallet = wallet
|
|
124
|
-
|
|
125
|
-
if (this.cipherLink) {
|
|
126
|
-
this.cipherLink.setWallet(wallet)
|
|
127
|
-
this.cipherLink.setPubKey(pubkey)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (this.wsLink) {
|
|
131
|
-
this.wsLink.client.connectionParams = () => ({
|
|
132
|
-
authToken: this.authToken
|
|
133
|
-
})
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Disconnect the WebSocket connection
|
|
139
|
-
*/
|
|
140
|
-
socketDisconnect () {
|
|
141
|
-
if (this.wsLink) {
|
|
142
|
-
this.wsLink.client.dispose()
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Getter methods
|
|
147
|
-
getAuthToken () { return this.authToken }
|
|
148
|
-
getPubKey () { return this.pubkey }
|
|
149
|
-
getWallet () { return this.wallet }
|
|
150
|
-
getServerUri () { return this.serverUri }
|
|
151
|
-
getSocketUri () { return this.soketi ? this.soketi.socketUri : null }
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export default Client
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Handle GraphQL and network errors
|
|
3
|
-
* @param {Object} errorInfo - Error information object
|
|
4
|
-
*/
|
|
5
|
-
export function errorHandler ({
|
|
6
|
-
graphQLErrors,
|
|
7
|
-
networkError,
|
|
8
|
-
operation,
|
|
9
|
-
forward,
|
|
10
|
-
response
|
|
11
|
-
}) {
|
|
12
|
-
if (graphQLErrors) {
|
|
13
|
-
graphQLErrors.forEach(({ message, debugMessage, locations, path }) =>
|
|
14
|
-
console.error(
|
|
15
|
-
`[GraphQL error]: ${message}`,
|
|
16
|
-
`\n Message : ${debugMessage}`,
|
|
17
|
-
`\n Path : ${path}`,
|
|
18
|
-
`\n Location: ${JSON.stringify(locations)}`
|
|
19
|
-
)
|
|
20
|
-
)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (networkError) {
|
|
24
|
-
const { name, statusCode, result = {} } = networkError
|
|
25
|
-
console.error(`[Network error]: ${name}, status code: ${statusCode}`)
|
|
26
|
-
// TODO: Implement automatic retry logic for network errors
|
|
27
|
-
// This could involve using an exponential backoff strategy
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// You can add custom error handling logic here
|
|
31
|
-
// For example, you could dispatch actions to update the app state on certain errors
|
|
32
|
-
}
|