@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,722 @@
|
|
|
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
|
+
* Runtime Assertion Functions for KnishIO SDK
|
|
51
|
+
*
|
|
52
|
+
* Implements 2025 TypeScript patterns:
|
|
53
|
+
* - Assertion functions with 'asserts' keyword
|
|
54
|
+
* - Runtime type narrowing with compile-time guarantees
|
|
55
|
+
* - Enhanced error messages with context
|
|
56
|
+
* - Type-safe runtime validation
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
import type {
|
|
60
|
+
AtomIsotope,
|
|
61
|
+
KnishIOErrorType,
|
|
62
|
+
QueryType,
|
|
63
|
+
MutationType,
|
|
64
|
+
SubscriptionType
|
|
65
|
+
} from '../constants'
|
|
66
|
+
|
|
67
|
+
import type {
|
|
68
|
+
WalletAddress,
|
|
69
|
+
BundleHash,
|
|
70
|
+
TokenSlug,
|
|
71
|
+
Position,
|
|
72
|
+
MolecularHash,
|
|
73
|
+
CellSlug,
|
|
74
|
+
BatchId,
|
|
75
|
+
AtomParams,
|
|
76
|
+
WalletParams,
|
|
77
|
+
MoleculeParams,
|
|
78
|
+
ValueAtom,
|
|
79
|
+
MetaAtom,
|
|
80
|
+
ContinueAtom,
|
|
81
|
+
AnyAtom,
|
|
82
|
+
ErrorContext
|
|
83
|
+
} from './index'
|
|
84
|
+
|
|
85
|
+
import {
|
|
86
|
+
isAtomIsotope,
|
|
87
|
+
isKnishIOErrorType,
|
|
88
|
+
isQueryType,
|
|
89
|
+
isMutationType,
|
|
90
|
+
isSubscriptionType,
|
|
91
|
+
isWalletAddress,
|
|
92
|
+
isBundleHash,
|
|
93
|
+
isTokenSlug,
|
|
94
|
+
isPosition,
|
|
95
|
+
isMolecularHash,
|
|
96
|
+
isCellSlug,
|
|
97
|
+
isBatchId,
|
|
98
|
+
isAtomParams,
|
|
99
|
+
isWalletParams,
|
|
100
|
+
isMoleculeParams,
|
|
101
|
+
isValueAtom,
|
|
102
|
+
isMetaAtom,
|
|
103
|
+
isContinueAtom
|
|
104
|
+
} from './guards'
|
|
105
|
+
|
|
106
|
+
// =============================================================================
|
|
107
|
+
// ASSERTION ERROR CLASS
|
|
108
|
+
// =============================================================================
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Enhanced assertion error with context and type information
|
|
112
|
+
*/
|
|
113
|
+
export class AssertionError extends Error {
|
|
114
|
+
public readonly code = 'ASSERTION_ERROR'
|
|
115
|
+
public readonly context: ErrorContext
|
|
116
|
+
public readonly expectedType: string
|
|
117
|
+
public readonly actualValue: unknown
|
|
118
|
+
public readonly timestamp: number
|
|
119
|
+
|
|
120
|
+
constructor(
|
|
121
|
+
message: string,
|
|
122
|
+
expectedType: string,
|
|
123
|
+
actualValue: unknown,
|
|
124
|
+
context: ErrorContext = {}
|
|
125
|
+
) {
|
|
126
|
+
super(message)
|
|
127
|
+
this.name = 'AssertionError'
|
|
128
|
+
this.expectedType = expectedType
|
|
129
|
+
this.actualValue = actualValue
|
|
130
|
+
this.context = context
|
|
131
|
+
this.timestamp = Date.now()
|
|
132
|
+
|
|
133
|
+
if (Error.captureStackTrace) {
|
|
134
|
+
Error.captureStackTrace(this, AssertionError)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// =============================================================================
|
|
140
|
+
// ASSERTION UTILITY FUNCTIONS
|
|
141
|
+
// =============================================================================
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Create a detailed assertion error message
|
|
145
|
+
*/
|
|
146
|
+
function createAssertionMessage(
|
|
147
|
+
expectedType: string,
|
|
148
|
+
actualValue: unknown,
|
|
149
|
+
context?: string
|
|
150
|
+
): string {
|
|
151
|
+
const contextMsg = context ? ` in ${context}` : ''
|
|
152
|
+
const valueStr = typeof actualValue === 'string'
|
|
153
|
+
? `"${actualValue}"`
|
|
154
|
+
: String(actualValue)
|
|
155
|
+
|
|
156
|
+
return `Expected ${expectedType}${contextMsg}, but received ${typeof actualValue}: ${valueStr}`
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Generic assertion function factory
|
|
161
|
+
*/
|
|
162
|
+
function createAssertion<T>(
|
|
163
|
+
guard: (value: unknown) => value is T,
|
|
164
|
+
typeName: string
|
|
165
|
+
) {
|
|
166
|
+
return function assert(
|
|
167
|
+
value: unknown,
|
|
168
|
+
context?: string
|
|
169
|
+
): asserts value is T {
|
|
170
|
+
if (!guard(value)) {
|
|
171
|
+
throw new AssertionError(
|
|
172
|
+
createAssertionMessage(typeName, value, context),
|
|
173
|
+
typeName,
|
|
174
|
+
value,
|
|
175
|
+
{ operation: 'assertion', expected: typeName, actual: value }
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// =============================================================================
|
|
182
|
+
// ISOTOPE ASSERTIONS
|
|
183
|
+
// =============================================================================
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Assert value is a valid atom isotope
|
|
187
|
+
*/
|
|
188
|
+
export function assertAtomIsotope(
|
|
189
|
+
value: unknown,
|
|
190
|
+
context?: string
|
|
191
|
+
): asserts value is AtomIsotope {
|
|
192
|
+
if (!isAtomIsotope(value)) {
|
|
193
|
+
throw new AssertionError(
|
|
194
|
+
createAssertionMessage('AtomIsotope', value, context),
|
|
195
|
+
'AtomIsotope',
|
|
196
|
+
value,
|
|
197
|
+
{
|
|
198
|
+
operation: 'isotope_assertion',
|
|
199
|
+
expected: 'C|V|U|T|M|I|R|B|F',
|
|
200
|
+
actual: value,
|
|
201
|
+
field: context
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Specific isotope assertions for precise validation
|
|
209
|
+
*/
|
|
210
|
+
export function assertContinueIsotope(
|
|
211
|
+
value: unknown,
|
|
212
|
+
context?: string
|
|
213
|
+
): asserts value is 'C' {
|
|
214
|
+
if (value !== 'C') {
|
|
215
|
+
throw new AssertionError(
|
|
216
|
+
createAssertionMessage('Continue isotope (C)', value, context),
|
|
217
|
+
'C',
|
|
218
|
+
value,
|
|
219
|
+
{ operation: 'isotope_assertion', expected: 'C', actual: value }
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function assertValueIsotope(
|
|
225
|
+
value: unknown,
|
|
226
|
+
context?: string
|
|
227
|
+
): asserts value is 'V' {
|
|
228
|
+
if (value !== 'V') {
|
|
229
|
+
throw new AssertionError(
|
|
230
|
+
createAssertionMessage('Value isotope (V)', value, context),
|
|
231
|
+
'V',
|
|
232
|
+
value,
|
|
233
|
+
{ operation: 'isotope_assertion', expected: 'V', actual: value }
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function assertMetaIsotope(
|
|
239
|
+
value: unknown,
|
|
240
|
+
context?: string
|
|
241
|
+
): asserts value is 'M' {
|
|
242
|
+
if (value !== 'M') {
|
|
243
|
+
throw new AssertionError(
|
|
244
|
+
createAssertionMessage('Meta isotope (M)', value, context),
|
|
245
|
+
'M',
|
|
246
|
+
value,
|
|
247
|
+
{ operation: 'isotope_assertion', expected: 'M', actual: value }
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// =============================================================================
|
|
253
|
+
// ERROR TYPE ASSERTIONS
|
|
254
|
+
// =============================================================================
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Assert value is a valid KnishIO error type
|
|
258
|
+
*/
|
|
259
|
+
export function assertKnishIOErrorType(
|
|
260
|
+
value: unknown,
|
|
261
|
+
context?: string
|
|
262
|
+
): asserts value is KnishIOErrorType {
|
|
263
|
+
if (!isKnishIOErrorType(value)) {
|
|
264
|
+
throw new AssertionError(
|
|
265
|
+
createAssertionMessage('KnishIOErrorType', value, context),
|
|
266
|
+
'KnishIOErrorType',
|
|
267
|
+
value,
|
|
268
|
+
{
|
|
269
|
+
operation: 'error_type_assertion',
|
|
270
|
+
expected: 'Valid KnishIO error type',
|
|
271
|
+
actual: value
|
|
272
|
+
}
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// =============================================================================
|
|
278
|
+
// OPERATION TYPE ASSERTIONS
|
|
279
|
+
// =============================================================================
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Assert value is a valid query type
|
|
283
|
+
*/
|
|
284
|
+
export const assertQueryType = createAssertion(isQueryType, 'QueryType')
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Assert value is a valid mutation type
|
|
288
|
+
*/
|
|
289
|
+
export const assertMutationType = createAssertion(isMutationType, 'MutationType')
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Assert value is a valid subscription type
|
|
293
|
+
*/
|
|
294
|
+
export const assertSubscriptionType = createAssertion(isSubscriptionType, 'SubscriptionType')
|
|
295
|
+
|
|
296
|
+
// =============================================================================
|
|
297
|
+
// BRANDED TYPE ASSERTIONS
|
|
298
|
+
// =============================================================================
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Assert value is a valid wallet address
|
|
302
|
+
*/
|
|
303
|
+
export function assertWalletAddress(
|
|
304
|
+
value: unknown,
|
|
305
|
+
context?: string
|
|
306
|
+
): asserts value is WalletAddress {
|
|
307
|
+
if (!isWalletAddress(value)) {
|
|
308
|
+
throw new AssertionError(
|
|
309
|
+
createAssertionMessage('WalletAddress (64-character hex)', value, context),
|
|
310
|
+
'WalletAddress',
|
|
311
|
+
value,
|
|
312
|
+
{
|
|
313
|
+
operation: 'wallet_address_assertion',
|
|
314
|
+
expected: '64-character hexadecimal string',
|
|
315
|
+
actual: value,
|
|
316
|
+
field: context
|
|
317
|
+
}
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Assert value is a valid bundle hash
|
|
324
|
+
*/
|
|
325
|
+
export function assertBundleHash(
|
|
326
|
+
value: unknown,
|
|
327
|
+
context?: string
|
|
328
|
+
): asserts value is BundleHash {
|
|
329
|
+
if (!isBundleHash(value)) {
|
|
330
|
+
throw new AssertionError(
|
|
331
|
+
createAssertionMessage('BundleHash (64-character hex)', value, context),
|
|
332
|
+
'BundleHash',
|
|
333
|
+
value,
|
|
334
|
+
{
|
|
335
|
+
operation: 'bundle_hash_assertion',
|
|
336
|
+
expected: '64-character hexadecimal string',
|
|
337
|
+
actual: value,
|
|
338
|
+
field: context
|
|
339
|
+
}
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Assert value is a valid token slug
|
|
346
|
+
*/
|
|
347
|
+
export function assertTokenSlug(
|
|
348
|
+
value: unknown,
|
|
349
|
+
context?: string
|
|
350
|
+
): asserts value is TokenSlug {
|
|
351
|
+
if (!isTokenSlug(value)) {
|
|
352
|
+
throw new AssertionError(
|
|
353
|
+
createAssertionMessage('TokenSlug (uppercase alphanumeric)', value, context),
|
|
354
|
+
'TokenSlug',
|
|
355
|
+
value,
|
|
356
|
+
{
|
|
357
|
+
operation: 'token_slug_assertion',
|
|
358
|
+
expected: 'Uppercase alphanumeric string (1-32 chars)',
|
|
359
|
+
actual: value,
|
|
360
|
+
field: context
|
|
361
|
+
}
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Assert value is a valid position
|
|
368
|
+
*/
|
|
369
|
+
export const assertPosition = createAssertion(isPosition, 'Position')
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Assert value is a valid molecular hash
|
|
373
|
+
*/
|
|
374
|
+
export function assertMolecularHash(
|
|
375
|
+
value: unknown,
|
|
376
|
+
context?: string
|
|
377
|
+
): asserts value is MolecularHash {
|
|
378
|
+
if (!isMolecularHash(value)) {
|
|
379
|
+
throw new AssertionError(
|
|
380
|
+
createAssertionMessage('MolecularHash (base17 format)', value, context),
|
|
381
|
+
'MolecularHash',
|
|
382
|
+
value,
|
|
383
|
+
{
|
|
384
|
+
operation: 'molecular_hash_assertion',
|
|
385
|
+
expected: 'Base17 encoded string (32-128 chars)',
|
|
386
|
+
actual: value,
|
|
387
|
+
field: context
|
|
388
|
+
}
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Assert value is a valid cell slug
|
|
395
|
+
*/
|
|
396
|
+
export const assertCellSlug = createAssertion(isCellSlug, 'CellSlug')
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Assert value is a valid batch ID
|
|
400
|
+
*/
|
|
401
|
+
export function assertBatchId(
|
|
402
|
+
value: unknown,
|
|
403
|
+
context?: string
|
|
404
|
+
): asserts value is BatchId {
|
|
405
|
+
if (!isBatchId(value)) {
|
|
406
|
+
throw new AssertionError(
|
|
407
|
+
createAssertionMessage('BatchId (UUID format)', value, context),
|
|
408
|
+
'BatchId',
|
|
409
|
+
value,
|
|
410
|
+
{
|
|
411
|
+
operation: 'batch_id_assertion',
|
|
412
|
+
expected: 'UUID v4 format',
|
|
413
|
+
actual: value,
|
|
414
|
+
field: context
|
|
415
|
+
}
|
|
416
|
+
)
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// =============================================================================
|
|
421
|
+
// OBJECT STRUCTURE ASSERTIONS
|
|
422
|
+
// =============================================================================
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Assert value is valid atom parameters
|
|
426
|
+
*/
|
|
427
|
+
export function assertAtomParams(
|
|
428
|
+
value: unknown,
|
|
429
|
+
context?: string
|
|
430
|
+
): asserts value is AtomParams {
|
|
431
|
+
if (!isAtomParams(value)) {
|
|
432
|
+
throw new AssertionError(
|
|
433
|
+
createAssertionMessage('AtomParams object', value, context),
|
|
434
|
+
'AtomParams',
|
|
435
|
+
value,
|
|
436
|
+
{
|
|
437
|
+
operation: 'atom_params_assertion',
|
|
438
|
+
expected: 'Object with valid atom properties',
|
|
439
|
+
actual: value
|
|
440
|
+
}
|
|
441
|
+
)
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Assert value is valid wallet parameters
|
|
447
|
+
*/
|
|
448
|
+
export function assertWalletParams(
|
|
449
|
+
value: unknown,
|
|
450
|
+
context?: string
|
|
451
|
+
): asserts value is WalletParams {
|
|
452
|
+
if (!isWalletParams(value)) {
|
|
453
|
+
throw new AssertionError(
|
|
454
|
+
createAssertionMessage('WalletParams object', value, context),
|
|
455
|
+
'WalletParams',
|
|
456
|
+
value,
|
|
457
|
+
{
|
|
458
|
+
operation: 'wallet_params_assertion',
|
|
459
|
+
expected: 'Object with valid wallet properties',
|
|
460
|
+
actual: value
|
|
461
|
+
}
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Assert value is valid molecule parameters
|
|
468
|
+
*/
|
|
469
|
+
export function assertMoleculeParams(
|
|
470
|
+
value: unknown,
|
|
471
|
+
context?: string
|
|
472
|
+
): asserts value is MoleculeParams {
|
|
473
|
+
if (!isMoleculeParams(value)) {
|
|
474
|
+
throw new AssertionError(
|
|
475
|
+
createAssertionMessage('MoleculeParams object', value, context),
|
|
476
|
+
'MoleculeParams',
|
|
477
|
+
value,
|
|
478
|
+
{
|
|
479
|
+
operation: 'molecule_params_assertion',
|
|
480
|
+
expected: 'Object with valid molecule properties',
|
|
481
|
+
actual: value
|
|
482
|
+
}
|
|
483
|
+
)
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// =============================================================================
|
|
488
|
+
// ATOM TYPE DISCRIMINATION ASSERTIONS
|
|
489
|
+
// =============================================================================
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Assert atom is a value atom
|
|
493
|
+
*/
|
|
494
|
+
export function assertValueAtom(
|
|
495
|
+
value: unknown,
|
|
496
|
+
context?: string
|
|
497
|
+
): asserts value is ValueAtom {
|
|
498
|
+
if (!isValueAtom(value as AnyAtom)) {
|
|
499
|
+
throw new AssertionError(
|
|
500
|
+
createAssertionMessage('ValueAtom (isotope V with value)', value, context),
|
|
501
|
+
'ValueAtom',
|
|
502
|
+
value,
|
|
503
|
+
{
|
|
504
|
+
operation: 'value_atom_assertion',
|
|
505
|
+
expected: 'Atom with isotope V and value property',
|
|
506
|
+
actual: value
|
|
507
|
+
}
|
|
508
|
+
)
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Assert atom is a meta atom
|
|
514
|
+
*/
|
|
515
|
+
export function assertMetaAtom(
|
|
516
|
+
value: unknown,
|
|
517
|
+
context?: string
|
|
518
|
+
): asserts value is MetaAtom {
|
|
519
|
+
if (!isMetaAtom(value as AnyAtom)) {
|
|
520
|
+
throw new AssertionError(
|
|
521
|
+
createAssertionMessage('MetaAtom (isotope M with metadata)', value, context),
|
|
522
|
+
'MetaAtom',
|
|
523
|
+
value,
|
|
524
|
+
{
|
|
525
|
+
operation: 'meta_atom_assertion',
|
|
526
|
+
expected: 'Atom with isotope M, metaType, metaId, and meta properties',
|
|
527
|
+
actual: value
|
|
528
|
+
}
|
|
529
|
+
)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Assert atom is a continue atom
|
|
535
|
+
*/
|
|
536
|
+
export function assertContinueAtom(
|
|
537
|
+
value: unknown,
|
|
538
|
+
context?: string
|
|
539
|
+
): asserts value is ContinueAtom {
|
|
540
|
+
if (!isContinueAtom(value as AnyAtom)) {
|
|
541
|
+
throw new AssertionError(
|
|
542
|
+
createAssertionMessage('ContinueAtom (isotope C)', value, context),
|
|
543
|
+
'ContinueAtom',
|
|
544
|
+
value,
|
|
545
|
+
{
|
|
546
|
+
operation: 'continue_atom_assertion',
|
|
547
|
+
expected: 'Atom with isotope C',
|
|
548
|
+
actual: value
|
|
549
|
+
}
|
|
550
|
+
)
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// =============================================================================
|
|
555
|
+
// COMPLEX VALIDATION ASSERTIONS
|
|
556
|
+
// =============================================================================
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* Assert array contains only specific type
|
|
560
|
+
*/
|
|
561
|
+
export function assertArrayOf<T>(
|
|
562
|
+
value: unknown,
|
|
563
|
+
itemAssertion: (item: unknown, context?: string) => asserts item is T,
|
|
564
|
+
context?: string
|
|
565
|
+
): asserts value is T[] {
|
|
566
|
+
if (!Array.isArray(value)) {
|
|
567
|
+
throw new AssertionError(
|
|
568
|
+
createAssertionMessage('Array', value, context),
|
|
569
|
+
'Array',
|
|
570
|
+
value,
|
|
571
|
+
{ operation: 'array_assertion', expected: 'Array', actual: value }
|
|
572
|
+
)
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
value.forEach((item, index) => {
|
|
576
|
+
try {
|
|
577
|
+
itemAssertion(item, `${context}[${index}]`)
|
|
578
|
+
} catch (error) {
|
|
579
|
+
if (error instanceof AssertionError) {
|
|
580
|
+
throw new AssertionError(
|
|
581
|
+
`Array validation failed at index ${index}: ${error.message}`,
|
|
582
|
+
`Array<${error.expectedType}>`,
|
|
583
|
+
value,
|
|
584
|
+
{
|
|
585
|
+
operation: 'array_item_assertion',
|
|
586
|
+
expected: `Array<${error.expectedType}>`,
|
|
587
|
+
actual: value,
|
|
588
|
+
field: `${context}[${index}]`
|
|
589
|
+
}
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
throw error
|
|
593
|
+
}
|
|
594
|
+
})
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Assert value is non-null and non-undefined
|
|
599
|
+
*/
|
|
600
|
+
export function assertExists<T>(
|
|
601
|
+
value: T | null | undefined,
|
|
602
|
+
context?: string
|
|
603
|
+
): asserts value is T {
|
|
604
|
+
if (value === null || value === undefined) {
|
|
605
|
+
throw new AssertionError(
|
|
606
|
+
createAssertionMessage('non-null value', value, context),
|
|
607
|
+
'non-null',
|
|
608
|
+
value,
|
|
609
|
+
{
|
|
610
|
+
operation: 'existence_assertion',
|
|
611
|
+
expected: 'non-null and non-undefined value',
|
|
612
|
+
actual: value
|
|
613
|
+
}
|
|
614
|
+
)
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Assert value matches one of the allowed values
|
|
620
|
+
*/
|
|
621
|
+
export function assertOneOf<T extends readonly unknown[]>(
|
|
622
|
+
value: unknown,
|
|
623
|
+
allowedValues: T,
|
|
624
|
+
context?: string
|
|
625
|
+
): asserts value is T[number] {
|
|
626
|
+
if (!allowedValues.includes(value)) {
|
|
627
|
+
throw new AssertionError(
|
|
628
|
+
createAssertionMessage(`one of [${allowedValues.join(', ')}]`, value, context),
|
|
629
|
+
`OneOf<${allowedValues.join('|')}>`,
|
|
630
|
+
value,
|
|
631
|
+
{
|
|
632
|
+
operation: 'one_of_assertion',
|
|
633
|
+
expected: allowedValues.join('|'),
|
|
634
|
+
actual: value
|
|
635
|
+
}
|
|
636
|
+
)
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// =============================================================================
|
|
641
|
+
// GROUPED ASSERTIONS
|
|
642
|
+
// =============================================================================
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Grouped assertion functions for convenient access
|
|
646
|
+
*/
|
|
647
|
+
export const Assertions = {
|
|
648
|
+
// Isotope assertions
|
|
649
|
+
isotope: {
|
|
650
|
+
any: assertAtomIsotope,
|
|
651
|
+
continue: assertContinueIsotope,
|
|
652
|
+
value: assertValueIsotope,
|
|
653
|
+
meta: assertMetaIsotope
|
|
654
|
+
},
|
|
655
|
+
|
|
656
|
+
// Error type assertions
|
|
657
|
+
error: {
|
|
658
|
+
type: assertKnishIOErrorType
|
|
659
|
+
},
|
|
660
|
+
|
|
661
|
+
// Operation assertions
|
|
662
|
+
operation: {
|
|
663
|
+
query: assertQueryType,
|
|
664
|
+
mutation: assertMutationType,
|
|
665
|
+
subscription: assertSubscriptionType
|
|
666
|
+
},
|
|
667
|
+
|
|
668
|
+
// Branded type assertions
|
|
669
|
+
branded: {
|
|
670
|
+
walletAddress: assertWalletAddress,
|
|
671
|
+
bundleHash: assertBundleHash,
|
|
672
|
+
tokenSlug: assertTokenSlug,
|
|
673
|
+
position: assertPosition,
|
|
674
|
+
molecularHash: assertMolecularHash,
|
|
675
|
+
cellSlug: assertCellSlug,
|
|
676
|
+
batchId: assertBatchId
|
|
677
|
+
},
|
|
678
|
+
|
|
679
|
+
// Object assertions
|
|
680
|
+
object: {
|
|
681
|
+
atomParams: assertAtomParams,
|
|
682
|
+
walletParams: assertWalletParams,
|
|
683
|
+
moleculeParams: assertMoleculeParams
|
|
684
|
+
},
|
|
685
|
+
|
|
686
|
+
// Atom discrimination
|
|
687
|
+
atom: {
|
|
688
|
+
value: assertValueAtom,
|
|
689
|
+
meta: assertMetaAtom,
|
|
690
|
+
continue: assertContinueAtom
|
|
691
|
+
},
|
|
692
|
+
|
|
693
|
+
// Utility assertions
|
|
694
|
+
util: {
|
|
695
|
+
arrayOf: assertArrayOf,
|
|
696
|
+
exists: assertExists,
|
|
697
|
+
oneOf: assertOneOf
|
|
698
|
+
}
|
|
699
|
+
} as const
|
|
700
|
+
|
|
701
|
+
// =============================================================================
|
|
702
|
+
// ASSERTION FUNCTION FACTORY
|
|
703
|
+
// =============================================================================
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Create custom assertion function from a type guard
|
|
707
|
+
*/
|
|
708
|
+
export function createAssertionFrom<T>(
|
|
709
|
+
guard: (value: unknown) => value is T,
|
|
710
|
+
typeName: string
|
|
711
|
+
): (value: unknown, context?: string) => asserts value is T {
|
|
712
|
+
return (value: unknown, context?: string): asserts value is T => {
|
|
713
|
+
if (!guard(value)) {
|
|
714
|
+
throw new AssertionError(
|
|
715
|
+
createAssertionMessage(typeName, value, context),
|
|
716
|
+
typeName,
|
|
717
|
+
value,
|
|
718
|
+
{ operation: 'custom_assertion', expected: typeName, actual: value }
|
|
719
|
+
)
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|