@wishknish/knishio-client-ts 0.7.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/LICENSE +674 -0
- package/README.md +425 -0
- package/dist/index.cjs +9961 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.iife.js +32241 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.js +9853 -0
- package/dist/index.js.map +1 -0
- package/package.json +113 -0
- package/src/AuthToken.ts +214 -0
- package/src/KnishIOClient.ts +2020 -0
- package/src/constants.ts +398 -0
- package/src/core/Atom.ts +646 -0
- package/src/core/AtomMeta.ts +278 -0
- package/src/core/Meta.ts +428 -0
- package/src/core/Molecule.ts +825 -0
- package/src/core/PolicyMeta.ts +130 -0
- package/src/core/TokenUnit.ts +148 -0
- package/src/core/Wallet.ts +467 -0
- package/src/exception/AtomIndexException.ts +97 -0
- package/src/exception/AtomsMissingException.ts +109 -0
- package/src/exception/AuthorizationRejectedException.ts +63 -0
- package/src/exception/BalanceInsufficientException.ts +58 -0
- package/src/exception/BaseException.ts +275 -0
- package/src/exception/BatchIdException.ts +58 -0
- package/src/exception/CodeException.ts +119 -0
- package/src/exception/DecryptionKeyException.ts +65 -0
- package/src/exception/InvalidResponseException.ts +112 -0
- package/src/exception/MetaMissingException.ts +58 -0
- package/src/exception/MolecularHashMismatchException.ts +115 -0
- package/src/exception/MolecularHashMissingException.ts +58 -0
- package/src/exception/NegativeAmountException.ts +58 -0
- package/src/exception/PolicyInvalidException.ts +58 -0
- package/src/exception/SignatureMalformedException.ts +58 -0
- package/src/exception/SignatureMismatchException.ts +98 -0
- package/src/exception/StackableUnitAmountException.ts +65 -0
- package/src/exception/StackableUnitDecimalsException.ts +65 -0
- package/src/exception/TransferBalanceException.ts +98 -0
- package/src/exception/TransferMalformedException.ts +58 -0
- package/src/exception/TransferMismatchedException.ts +58 -0
- package/src/exception/TransferRemainderException.ts +58 -0
- package/src/exception/TransferToSelfException.ts +58 -0
- package/src/exception/TransferUnbalancedException.ts +58 -0
- package/src/exception/UnauthenticatedException.ts +147 -0
- package/src/exception/WalletCredentialException.ts +108 -0
- package/src/exception/WalletShadowException.ts +65 -0
- package/src/exception/WrongTokenTypeException.ts +58 -0
- package/src/exception/index.ts +272 -0
- package/src/index.ts +512 -0
- package/src/instance/rules/Callback.ts +257 -0
- package/src/instance/rules/Condition.ts +96 -0
- package/src/instance/rules/Meta.ts +163 -0
- package/src/instance/rules/Rule.ts +29 -0
- package/src/instance/rules/exception/RuleArgumentException.ts +65 -0
- package/src/libraries/CheckMolecule.ts +581 -0
- package/src/libraries/Decimal.ts +94 -0
- package/src/libraries/Dot.ts +202 -0
- package/src/libraries/GraphQLClient.ts +276 -0
- package/src/libraries/Hex.ts +155 -0
- package/src/libraries/UrqlClientWrapper.ts +336 -0
- package/src/libraries/array.ts +91 -0
- package/src/libraries/crypto.ts +816 -0
- package/src/libraries/strings.ts +458 -0
- package/src/mutation/Mutation.ts +103 -0
- package/src/mutation/MutationActiveSession.ts +108 -0
- package/src/mutation/MutationClaimShadowWallet.ts +91 -0
- package/src/mutation/MutationCreateIdentifier.ts +86 -0
- package/src/mutation/MutationCreateMeta.ts +92 -0
- package/src/mutation/MutationCreateRule.ts +90 -0
- package/src/mutation/MutationCreateToken.ts +92 -0
- package/src/mutation/MutationCreateWallet.ts +78 -0
- package/src/mutation/MutationDepositBufferToken.ts +72 -0
- package/src/mutation/MutationLinkIdentifier.ts +87 -0
- package/src/mutation/MutationProposeMolecule.ts +147 -0
- package/src/mutation/MutationRequestAuthorization.ts +77 -0
- package/src/mutation/MutationRequestAuthorizationGuest.ts +85 -0
- package/src/mutation/MutationRequestTokens.ts +98 -0
- package/src/mutation/MutationTransferTokens.ts +87 -0
- package/src/mutation/MutationWithdrawBufferToken.ts +73 -0
- package/src/query/Query.ts +187 -0
- package/src/query/QueryActiveSession.ts +89 -0
- package/src/query/QueryAtom.ts +275 -0
- package/src/query/QueryBalance.ts +102 -0
- package/src/query/QueryBatch.ts +145 -0
- package/src/query/QueryBatchHistory.ts +86 -0
- package/src/query/QueryContinuId.ts +91 -0
- package/src/query/QueryMetaType.ts +177 -0
- package/src/query/QueryMetaTypeViaAtom.ts +199 -0
- package/src/query/QueryPolicy.ts +91 -0
- package/src/query/QueryToken.ts +90 -0
- package/src/query/QueryUserActivity.ts +153 -0
- package/src/query/QueryWalletBundle.ts +92 -0
- package/src/query/QueryWalletList.ts +106 -0
- package/src/response/EnhancedResponse.ts +345 -0
- package/src/response/Response.ts +254 -0
- package/src/response/ResponseActiveSession.ts +72 -0
- package/src/response/ResponseAtom.ts +132 -0
- package/src/response/ResponseAuthorizationGuest.ts +119 -0
- package/src/response/ResponseBalance.ts +153 -0
- package/src/response/ResponseClaimShadowWallet.ts +56 -0
- package/src/response/ResponseContinuId.ts +101 -0
- package/src/response/ResponseCreateIdentifier.ts +56 -0
- package/src/response/ResponseCreateMeta.ts +58 -0
- package/src/response/ResponseCreateRule.ts +56 -0
- package/src/response/ResponseCreateToken.ts +58 -0
- package/src/response/ResponseCreateWallet.ts +58 -0
- package/src/response/ResponseLinkIdentifier.ts +87 -0
- package/src/response/ResponseMetaBatch.ts +72 -0
- package/src/response/ResponseMetaType.ts +108 -0
- package/src/response/ResponseMetaTypeViaAtom.ts +108 -0
- package/src/response/ResponsePolicy.ts +90 -0
- package/src/response/ResponseProposeMolecule.ts +155 -0
- package/src/response/ResponseQueryActiveSession.ts +105 -0
- package/src/response/ResponseQueryUserActivity.ts +89 -0
- package/src/response/ResponseRequestAuthorization.ts +100 -0
- package/src/response/ResponseRequestAuthorizationGuest.ts +133 -0
- package/src/response/ResponseRequestTokens.ts +58 -0
- package/src/response/ResponseTransferTokens.ts +72 -0
- package/src/response/ResponseWalletBundle.ts +95 -0
- package/src/response/ResponseWalletList.ts +165 -0
- package/src/schemas/index.ts +457 -0
- package/src/subscribe/ActiveSessionSubscribe.ts +72 -0
- package/src/subscribe/ActiveWalletSubscribe.ts +99 -0
- package/src/subscribe/CreateMoleculeSubscribe.ts +106 -0
- package/src/subscribe/Subscribe.ts +182 -0
- package/src/subscribe/WalletStatusSubscribe.ts +70 -0
- package/src/subscribe/index.ts +59 -0
- package/src/types/assertions.ts +722 -0
- package/src/types/client.ts +567 -0
- package/src/types/crypto.ts +541 -0
- package/src/types/graphql.ts +630 -0
- package/src/types/guards.ts +659 -0
- package/src/types/index.ts +614 -0
- package/src/types/response.ts +133 -0
- package/src/types/template-literals.ts +382 -0
- package/src/validation/UNIVERSAL_CONFIGURATION_INTERFACES.ts +580 -0
- package/src/validation/ValidationService.ts +607 -0
- package/src/validation/schemas.ts +447 -0
- package/src/versions/HashAtom.ts +170 -0
- package/src/versions/Version4.ts +120 -0
- package/src/versions/index.ts +80 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
/*
|
|
2
|
+
(
|
|
3
|
+
(/(
|
|
4
|
+
(//(
|
|
5
|
+
(///(
|
|
6
|
+
(/////(
|
|
7
|
+
(//////( )
|
|
8
|
+
(////////( (/)
|
|
9
|
+
(////////( (///)
|
|
10
|
+
(//////////( (////)
|
|
11
|
+
(//////////( (//////)
|
|
12
|
+
(////////////( (///////)
|
|
13
|
+
(/////////////( (/////////)
|
|
14
|
+
(//////////////( (///////////)
|
|
15
|
+
(///////////////( (/////////////)
|
|
16
|
+
(////////////////( (//////////////)
|
|
17
|
+
((((((((((((((((((( (((((((((((((((
|
|
18
|
+
((((((((((((((((((( ((((((((((((((
|
|
19
|
+
((((((((((((((((((( ((((((((((((((
|
|
20
|
+
(((((((((((((((((((( (((((((((((((
|
|
21
|
+
(((((((((((((((((((( ((((((((((((
|
|
22
|
+
((((((((((((((((((( ((((((((((((
|
|
23
|
+
((((((((((((((((((( ((((((((((
|
|
24
|
+
((((((((((((((((((/ (((((((((
|
|
25
|
+
(((((((((((((((((( ((((((((
|
|
26
|
+
((((((((((((((((( (((((((
|
|
27
|
+
(((((((((((((((((( (((((
|
|
28
|
+
################# ##
|
|
29
|
+
################ #
|
|
30
|
+
################# ##
|
|
31
|
+
%################ ###
|
|
32
|
+
###############( ####
|
|
33
|
+
############### ####
|
|
34
|
+
############### ######
|
|
35
|
+
%#############( (#######
|
|
36
|
+
%############# #########
|
|
37
|
+
############( ##########
|
|
38
|
+
########### #############
|
|
39
|
+
######### ##############
|
|
40
|
+
%######
|
|
41
|
+
|
|
42
|
+
Powered by Knish.IO: Connecting a Decentralized World
|
|
43
|
+
|
|
44
|
+
Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* String utility functions for KnishIO TypeScript SDK
|
|
51
|
+
* Maintains compatibility with JavaScript SDK string operations
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
// =============================================================================
|
|
55
|
+
// STRING CHUNKING AND MANIPULATION
|
|
56
|
+
// =============================================================================
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Split string into chunks of specified size
|
|
60
|
+
* MUST match JavaScript SDK behavior exactly
|
|
61
|
+
*/
|
|
62
|
+
export function chunkSubstr(str: string, size: number): string[] {
|
|
63
|
+
if (!str || size <= 0) {
|
|
64
|
+
return []
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const chunks: string[] = []
|
|
68
|
+
for (let i = 0; i < str.length; i += size) {
|
|
69
|
+
chunks.push(str.substring(i, i + size))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return chunks
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Generate cryptographically secure random string
|
|
77
|
+
* Uses crypto.getRandomValues when available, falls back to Math.random
|
|
78
|
+
*/
|
|
79
|
+
export function randomString(
|
|
80
|
+
length = 32,
|
|
81
|
+
alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
82
|
+
): string {
|
|
83
|
+
if (length <= 0) return ''
|
|
84
|
+
|
|
85
|
+
let result = ''
|
|
86
|
+
|
|
87
|
+
// Use crypto.getRandomValues if available (browser/Node.js crypto)
|
|
88
|
+
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
|
89
|
+
const randomValues = new Uint8Array(length)
|
|
90
|
+
crypto.getRandomValues(randomValues)
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < length; i++) {
|
|
93
|
+
result += alphabet.charAt(randomValues[i] % alphabet.length)
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
// Fallback to Math.random (less secure but compatible)
|
|
97
|
+
for (let i = 0; i < length; i++) {
|
|
98
|
+
result += alphabet.charAt(Math.floor(Math.random() * alphabet.length))
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return result
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// =============================================================================
|
|
106
|
+
// BASE CONVERSION UTILITIES
|
|
107
|
+
// =============================================================================
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Convert between different character sets/bases
|
|
111
|
+
* Ported from JavaScript SDK for cross-platform compatibility
|
|
112
|
+
*/
|
|
113
|
+
export function charsetBaseConvert(
|
|
114
|
+
src: string,
|
|
115
|
+
fromBase: number,
|
|
116
|
+
toBase: number,
|
|
117
|
+
srcSymbolTable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
118
|
+
destSymbolTable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
119
|
+
): string | number | boolean {
|
|
120
|
+
if (!src || fromBase < 2 || toBase < 2 || fromBase > srcSymbolTable.length || toBase > destSymbolTable.length) {
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// Convert from source base to decimal
|
|
126
|
+
let decimalValue = 0
|
|
127
|
+
const srcLength = src.length
|
|
128
|
+
|
|
129
|
+
for (let i = 0; i < srcLength; i++) {
|
|
130
|
+
const char = src.charAt(srcLength - 1 - i)
|
|
131
|
+
const charIndex = srcSymbolTable.indexOf(char)
|
|
132
|
+
|
|
133
|
+
if (charIndex === -1) {
|
|
134
|
+
return false // Invalid character for source base
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
decimalValue += charIndex * Math.pow(fromBase, i)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Convert from decimal to destination base
|
|
141
|
+
if (decimalValue === 0) {
|
|
142
|
+
return destSymbolTable.charAt(0)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let result = ''
|
|
146
|
+
while (decimalValue > 0) {
|
|
147
|
+
const remainder = decimalValue % toBase
|
|
148
|
+
result = destSymbolTable.charAt(remainder) + result
|
|
149
|
+
decimalValue = Math.floor(decimalValue / toBase)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return result
|
|
153
|
+
|
|
154
|
+
} catch (error) {
|
|
155
|
+
return false
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// =============================================================================
|
|
160
|
+
// HEX AND BINARY UTILITIES
|
|
161
|
+
// =============================================================================
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Convert buffer/byte array to hex string
|
|
165
|
+
* Maintains compatibility with JavaScript SDK
|
|
166
|
+
*/
|
|
167
|
+
export function bufferToHexString(byteArray: Uint8Array | ArrayBuffer | number[]): string {
|
|
168
|
+
let bytes: Uint8Array
|
|
169
|
+
|
|
170
|
+
if (byteArray instanceof ArrayBuffer) {
|
|
171
|
+
bytes = new Uint8Array(byteArray)
|
|
172
|
+
} else if (Array.isArray(byteArray)) {
|
|
173
|
+
bytes = new Uint8Array(byteArray)
|
|
174
|
+
} else {
|
|
175
|
+
bytes = byteArray
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return Array.from(bytes)
|
|
179
|
+
.map(byte => byte.toString(16).padStart(2, '0'))
|
|
180
|
+
.join('')
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Convert hex string to buffer/byte array
|
|
185
|
+
* Maintains compatibility with JavaScript SDK
|
|
186
|
+
*/
|
|
187
|
+
export function hexStringToBuffer(hexString: string): Uint8Array {
|
|
188
|
+
if (!hexString || hexString.length % 2 !== 0) {
|
|
189
|
+
throw new Error('Invalid hex string')
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const cleanHex = hexString.replace(/^0x/i, '').toLowerCase()
|
|
193
|
+
const bytes = new Uint8Array(cleanHex.length / 2)
|
|
194
|
+
|
|
195
|
+
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
196
|
+
const byteValue = parseInt(cleanHex.substring(i, i + 2), 16)
|
|
197
|
+
if (isNaN(byteValue)) {
|
|
198
|
+
throw new Error(`Invalid hex characters at position ${i}: ${cleanHex.substring(i, i + 2)}`)
|
|
199
|
+
}
|
|
200
|
+
bytes[i / 2] = byteValue
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return bytes
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// =============================================================================
|
|
207
|
+
// BASE64 CONVERSION UTILITIES
|
|
208
|
+
// =============================================================================
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Convert hex string to base64
|
|
212
|
+
* Uses native btoa when available, falls back to manual conversion
|
|
213
|
+
*/
|
|
214
|
+
export function hexToBase64(hexString: string): string {
|
|
215
|
+
if (!hexString) return ''
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
const cleanHex = hexString.replace(/^0x/i, '')
|
|
219
|
+
|
|
220
|
+
// Convert hex to bytes
|
|
221
|
+
const bytes = hexStringToBuffer(cleanHex)
|
|
222
|
+
|
|
223
|
+
// Convert to base64
|
|
224
|
+
if (typeof btoa !== 'undefined') {
|
|
225
|
+
// Browser environment
|
|
226
|
+
return btoa(String.fromCharCode(...bytes))
|
|
227
|
+
} else {
|
|
228
|
+
// Node.js environment or manual conversion
|
|
229
|
+
return Buffer.from(bytes).toString('base64')
|
|
230
|
+
}
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throw new Error(`Failed to convert hex to base64: ${error instanceof Error ? error.message : String(error)}`)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Convert base64 string to hex
|
|
238
|
+
* Uses native atob when available, falls back to manual conversion
|
|
239
|
+
*/
|
|
240
|
+
export function base64ToHex(base64String: string): string {
|
|
241
|
+
if (!base64String) return ''
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
let bytes: Uint8Array
|
|
245
|
+
|
|
246
|
+
if (typeof atob !== 'undefined') {
|
|
247
|
+
// Browser environment
|
|
248
|
+
const binaryString = atob(base64String)
|
|
249
|
+
bytes = new Uint8Array(binaryString.length)
|
|
250
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
251
|
+
bytes[i] = binaryString.charCodeAt(i)
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
// Node.js environment
|
|
255
|
+
bytes = new Uint8Array(Buffer.from(base64String, 'base64'))
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return bufferToHexString(bytes)
|
|
259
|
+
|
|
260
|
+
} catch (error) {
|
|
261
|
+
throw new Error(`Failed to convert base64 to hex: ${error instanceof Error ? error.message : String(error)}`)
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// =============================================================================
|
|
266
|
+
// VALIDATION UTILITIES
|
|
267
|
+
// =============================================================================
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Check if string is valid hexadecimal
|
|
271
|
+
*/
|
|
272
|
+
export function isHex(str: string): boolean {
|
|
273
|
+
if (!str || typeof str !== 'string') return false
|
|
274
|
+
|
|
275
|
+
const cleanStr = str.replace(/^0x/i, '')
|
|
276
|
+
return /^[0-9a-fA-F]+$/.test(cleanStr)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Check if string is numeric
|
|
281
|
+
*/
|
|
282
|
+
export function isNumeric(str: string): boolean {
|
|
283
|
+
if (!str || typeof str !== 'string') return false
|
|
284
|
+
|
|
285
|
+
return /^-?\d+\.?\d*$/.test(str)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Check if string is valid base64
|
|
290
|
+
*/
|
|
291
|
+
export function isBase64(str: string): boolean {
|
|
292
|
+
if (!str || typeof str !== 'string') return false
|
|
293
|
+
|
|
294
|
+
try {
|
|
295
|
+
return btoa ? str === btoa(atob(str)) : Buffer.from(str, 'base64').toString('base64') === str
|
|
296
|
+
} catch {
|
|
297
|
+
return false
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// =============================================================================
|
|
302
|
+
// STRING FORMATTING UTILITIES
|
|
303
|
+
// =============================================================================
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Convert string to camelCase
|
|
307
|
+
*/
|
|
308
|
+
export function toCamelCase(str: string): string {
|
|
309
|
+
if (!str) return ''
|
|
310
|
+
|
|
311
|
+
return str
|
|
312
|
+
.toLowerCase()
|
|
313
|
+
.replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase())
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Convert string to snake_case
|
|
318
|
+
*/
|
|
319
|
+
export function toSnakeCase(str: string): string {
|
|
320
|
+
if (!str) return ''
|
|
321
|
+
|
|
322
|
+
return str
|
|
323
|
+
.replace(/([A-Z])/g, '_$1')
|
|
324
|
+
.replace(/^_/, '')
|
|
325
|
+
.toLowerCase()
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Convert string to kebab-case
|
|
330
|
+
*/
|
|
331
|
+
export function toKebabCase(str: string): string {
|
|
332
|
+
if (!str) return ''
|
|
333
|
+
|
|
334
|
+
return str
|
|
335
|
+
.replace(/([A-Z])/g, '-$1')
|
|
336
|
+
.replace(/^-/, '')
|
|
337
|
+
.toLowerCase()
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Capitalize first letter of string
|
|
342
|
+
*/
|
|
343
|
+
export function capitalize(str: string): string {
|
|
344
|
+
if (!str) return ''
|
|
345
|
+
|
|
346
|
+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Convert string to title case
|
|
351
|
+
*/
|
|
352
|
+
export function toTitleCase(str: string): string {
|
|
353
|
+
if (!str) return ''
|
|
354
|
+
|
|
355
|
+
return str
|
|
356
|
+
.split(/\s+/)
|
|
357
|
+
.map(word => capitalize(word))
|
|
358
|
+
.join(' ')
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// =============================================================================
|
|
362
|
+
// STRING PADDING AND ALIGNMENT
|
|
363
|
+
// =============================================================================
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Pad string to left with specified character
|
|
367
|
+
*/
|
|
368
|
+
export function padLeft(str: string, length: number, padChar = ' '): string {
|
|
369
|
+
if (!str) str = ''
|
|
370
|
+
|
|
371
|
+
while (str.length < length) {
|
|
372
|
+
str = padChar + str
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return str
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Pad string to right with specified character
|
|
380
|
+
*/
|
|
381
|
+
export function padRight(str: string, length: number, padChar = ' '): string {
|
|
382
|
+
if (!str) str = ''
|
|
383
|
+
|
|
384
|
+
while (str.length < length) {
|
|
385
|
+
str = str + padChar
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return str
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Truncate string to specified length with ellipsis
|
|
393
|
+
*/
|
|
394
|
+
export function truncate(str: string, length: number, ellipsis = '...'): string {
|
|
395
|
+
if (!str || str.length <= length) return str
|
|
396
|
+
|
|
397
|
+
return str.substring(0, length - ellipsis.length) + ellipsis
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// =============================================================================
|
|
401
|
+
// UNICODE AND ENCODING UTILITIES
|
|
402
|
+
// =============================================================================
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Escape string for safe usage in various contexts
|
|
406
|
+
*/
|
|
407
|
+
export function escapeString(str: string): string {
|
|
408
|
+
if (!str) return ''
|
|
409
|
+
|
|
410
|
+
return str
|
|
411
|
+
.replace(/\\/g, '\\\\')
|
|
412
|
+
.replace(/"/g, '\\"')
|
|
413
|
+
.replace(/'/g, "\\'")
|
|
414
|
+
.replace(/\n/g, '\\n')
|
|
415
|
+
.replace(/\r/g, '\\r')
|
|
416
|
+
.replace(/\t/g, '\\t')
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Unescape string
|
|
421
|
+
*/
|
|
422
|
+
export function unescapeString(str: string): string {
|
|
423
|
+
if (!str) return ''
|
|
424
|
+
|
|
425
|
+
return str
|
|
426
|
+
.replace(/\\n/g, '\n')
|
|
427
|
+
.replace(/\\r/g, '\r')
|
|
428
|
+
.replace(/\\t/g, '\t')
|
|
429
|
+
.replace(/\\'/g, "'")
|
|
430
|
+
.replace(/\\"/g, '"')
|
|
431
|
+
.replace(/\\\\/g, '\\')
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// =============================================================================
|
|
435
|
+
// STRING PROTOTYPE EXTENSIONS (FOR COMPATIBILITY)
|
|
436
|
+
// =============================================================================
|
|
437
|
+
|
|
438
|
+
declare global {
|
|
439
|
+
interface String {
|
|
440
|
+
toCamelCase(): string
|
|
441
|
+
toSnakeCase(): string
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// Add methods to String prototype for compatibility with JS SDK
|
|
446
|
+
if (typeof String.prototype.toCamelCase === 'undefined') {
|
|
447
|
+
String.prototype.toCamelCase = function(): string {
|
|
448
|
+
return toCamelCase(this.toString())
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (typeof String.prototype.toSnakeCase === 'undefined') {
|
|
453
|
+
String.prototype.toSnakeCase = function(): string {
|
|
454
|
+
return toSnakeCase(this.toString())
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// All functions are already exported individually above
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
(
|
|
3
|
+
(/(
|
|
4
|
+
(//(
|
|
5
|
+
(///(
|
|
6
|
+
(/////(
|
|
7
|
+
(//////( )
|
|
8
|
+
(////////( (/)
|
|
9
|
+
(////////( (///)
|
|
10
|
+
(//////////( (////)
|
|
11
|
+
(//////////( (//////)
|
|
12
|
+
(////////////( (///////)
|
|
13
|
+
(/////////////( (/////////)
|
|
14
|
+
(//////////////( (///////////)
|
|
15
|
+
(///////////////( (/////////////)
|
|
16
|
+
(////////////////( (//////////////)
|
|
17
|
+
((((((((((((((((((( (((((((((((((((
|
|
18
|
+
((((((((((((((((((( ((((((((((((((
|
|
19
|
+
((((((((((((((((((( ((((((((((((((
|
|
20
|
+
(((((((((((((((((((( (((((((((((((
|
|
21
|
+
(((((((((((((((((((( ((((((((((((
|
|
22
|
+
((((((((((((((((((( ((((((((((((
|
|
23
|
+
((((((((((((((((((( ((((((((((
|
|
24
|
+
((((((((((((((((((/ (((((((((
|
|
25
|
+
(((((((((((((((((( ((((((((
|
|
26
|
+
((((((((((((((((( (((((((
|
|
27
|
+
(((((((((((((((((( (((((
|
|
28
|
+
################# ##
|
|
29
|
+
################ #
|
|
30
|
+
################# ##
|
|
31
|
+
%################ ###
|
|
32
|
+
###############( ####
|
|
33
|
+
############### ####
|
|
34
|
+
############### ######
|
|
35
|
+
%#############( (#######
|
|
36
|
+
%############# #########
|
|
37
|
+
############( ##########
|
|
38
|
+
########### #############
|
|
39
|
+
######### ##############
|
|
40
|
+
%######
|
|
41
|
+
|
|
42
|
+
Powered by Knish.IO: Connecting a Decentralized World
|
|
43
|
+
|
|
44
|
+
Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import Query from '@/query/Query'
|
|
50
|
+
import type Response from '@/response/Response'
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Base class used to construct various GraphQL mutations
|
|
54
|
+
*/
|
|
55
|
+
export default class Mutation extends Query {
|
|
56
|
+
/**
|
|
57
|
+
* Creates a new Request for the given parameters
|
|
58
|
+
*/
|
|
59
|
+
override createQuery({ variables = null }: { variables?: Record<string, any> | null }): any {
|
|
60
|
+
const request = super.createQuery({ variables })
|
|
61
|
+
request.mutation = request.query
|
|
62
|
+
delete request.query
|
|
63
|
+
return request
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Sends the Mutation to a Knish.IO node and returns the Response
|
|
68
|
+
*/
|
|
69
|
+
override async execute({
|
|
70
|
+
variables = {},
|
|
71
|
+
context = {}
|
|
72
|
+
}: {
|
|
73
|
+
variables?: Record<string, any>
|
|
74
|
+
context?: Record<string, any>
|
|
75
|
+
} = {}): Promise<Response | null> {
|
|
76
|
+
this.$__request = this.createQuery({ variables })
|
|
77
|
+
|
|
78
|
+
const mergedContext = {
|
|
79
|
+
...context,
|
|
80
|
+
...this.createQueryContext()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const mutationParams = {
|
|
85
|
+
...this.$__request,
|
|
86
|
+
context: mergedContext
|
|
87
|
+
}
|
|
88
|
+
const response = await this.client.mutate(mutationParams)
|
|
89
|
+
this.$__response = await this.createResponseRaw(response)
|
|
90
|
+
return this.$__response
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if ((error as any).name === 'AbortError') {
|
|
93
|
+
this.knishIOClient.log('warn', 'Mutation was cancelled')
|
|
94
|
+
const Response = require('@/response/Response').default
|
|
95
|
+
return new Response({
|
|
96
|
+
query: this,
|
|
97
|
+
json: { data: null, errors: [{ message: 'Mutation was cancelled' }] }
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
throw error
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
(
|
|
3
|
+
(/(
|
|
4
|
+
(//(
|
|
5
|
+
(///(
|
|
6
|
+
(/////(
|
|
7
|
+
(//////( )
|
|
8
|
+
(////////( (/)
|
|
9
|
+
(////////( (///)
|
|
10
|
+
(//////////( (////)
|
|
11
|
+
(//////////( (//////)
|
|
12
|
+
(////////////( (///////)
|
|
13
|
+
(/////////////( (/////////)
|
|
14
|
+
(//////////////( (///////////)
|
|
15
|
+
(///////////////( (/////////////)
|
|
16
|
+
(////////////////( (//////////////)
|
|
17
|
+
((((((((((((((((((( (((((((((((((((
|
|
18
|
+
((((((((((((((((((( ((((((((((((((
|
|
19
|
+
((((((((((((((((((( ((((((((((((((
|
|
20
|
+
(((((((((((((((((((( (((((((((((((
|
|
21
|
+
(((((((((((((((((((( ((((((((((((
|
|
22
|
+
((((((((((((((((((( ((((((((((((
|
|
23
|
+
((((((((((((((((((( ((((((((((
|
|
24
|
+
((((((((((((((((((/ (((((((((
|
|
25
|
+
(((((((((((((((((( ((((((((
|
|
26
|
+
((((((((((((((((( (((((((
|
|
27
|
+
(((((((((((((((((( (((((
|
|
28
|
+
################# ##
|
|
29
|
+
################ #
|
|
30
|
+
################# ##
|
|
31
|
+
%################ ###
|
|
32
|
+
###############( ####
|
|
33
|
+
############### ####
|
|
34
|
+
############### ######
|
|
35
|
+
%#############( (#######
|
|
36
|
+
%############# #########
|
|
37
|
+
############( ##########
|
|
38
|
+
########### #############
|
|
39
|
+
######### ##############
|
|
40
|
+
%######
|
|
41
|
+
|
|
42
|
+
Powered by Knish.IO: Connecting a Decentralized World
|
|
43
|
+
|
|
44
|
+
Please visit https://github.com/WishKnish/KnishIO-Client-TS for information.
|
|
45
|
+
|
|
46
|
+
License: https://github.com/WishKnish/KnishIO-Client-TS/blob/master/LICENSE
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import Mutation from './Mutation'
|
|
50
|
+
import ResponseActiveSession from '@/response/ResponseActiveSession'
|
|
51
|
+
import { gql } from '@urql/core'
|
|
52
|
+
import type { GraphQLClient as IGraphQLClient } from '@/types'
|
|
53
|
+
import type KnishIOClient from '@/KnishIOClient'
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Mutation for declaring an active User Session with a given MetaAsset
|
|
57
|
+
*/
|
|
58
|
+
export default class MutationActiveSession extends Mutation {
|
|
59
|
+
/**
|
|
60
|
+
* Constructor
|
|
61
|
+
*/
|
|
62
|
+
constructor(graphQLClient: IGraphQLClient, knishIOClient: KnishIOClient) {
|
|
63
|
+
super(graphQLClient, knishIOClient)
|
|
64
|
+
|
|
65
|
+
this.$__query = gql`
|
|
66
|
+
mutation(
|
|
67
|
+
$bundleHash: String!
|
|
68
|
+
$metaType: String!
|
|
69
|
+
$metaId: String!
|
|
70
|
+
$ipAddress: String
|
|
71
|
+
$browser: String
|
|
72
|
+
$osCpu: String
|
|
73
|
+
$resolution: String
|
|
74
|
+
$timeZone: String
|
|
75
|
+
$json: String
|
|
76
|
+
) {
|
|
77
|
+
ActiveSession(
|
|
78
|
+
bundleHash: $bundleHash
|
|
79
|
+
metaType: $metaType
|
|
80
|
+
metaId: $metaId
|
|
81
|
+
ipAddress: $ipAddress
|
|
82
|
+
browser: $browser
|
|
83
|
+
osCpu: $osCpu
|
|
84
|
+
resolution: $resolution
|
|
85
|
+
timeZone: $timeZone
|
|
86
|
+
json: $json
|
|
87
|
+
) {
|
|
88
|
+
bundleHash
|
|
89
|
+
metaType
|
|
90
|
+
metaId
|
|
91
|
+
jsonData
|
|
92
|
+
createdAt
|
|
93
|
+
updatedAt
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Builds a Response object out of a JSON string
|
|
101
|
+
*/
|
|
102
|
+
override createResponse(json: Record<string, any>): ResponseActiveSession {
|
|
103
|
+
return new ResponseActiveSession({
|
|
104
|
+
query: this,
|
|
105
|
+
json
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
}
|