-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjavascript_client.js
More file actions
409 lines (336 loc) · 11.1 KB
/
Copy pathjavascript_client.js
File metadata and controls
409 lines (336 loc) · 11.1 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* IntegrityX JavaScript/TypeScript Client Library
*
* A complete JavaScript client for the IntegrityX API with authentication,
* error handling, and retry logic.
*
* Usage:
* import { IntegrityXClient } from './integrityx_client';
*
* const client = new IntegrityXClient('http://localhost:8000', 'your_jwt_token');
* const result = await client.uploadDocument({ loan_id: 'LOAN-123', amount: 250000 });
* console.log(`Sealed with ETID: ${result.etid}`);
*/
class IntegrityXError extends Error {
constructor(message) {
super(message);
this.name = 'IntegrityXError';
}
}
class AuthenticationError extends IntegrityXError {
constructor(message) {
super(message);
this.name = 'AuthenticationError';
}
}
class ValidationError extends IntegrityXError {
constructor(message) {
super(message);
this.name = 'ValidationError';
}
}
class IntegrityXClient {
/**
* Create an IntegrityX API client.
*
* @param {string} baseUrl - Base URL of the IntegrityX API
* @param {string} token - Clerk JWT token for authentication
* @param {number} timeout - Request timeout in milliseconds (default: 30000)
* @param {number} maxRetries - Maximum number of retries (default: 3)
*/
constructor(baseUrl, token, timeout = 30000, maxRetries = 3) {
this.baseUrl = baseUrl.replace(/\/$/, '');
this.token = token;
this.timeout = timeout;
this.maxRetries = maxRetries;
}
/**
* Make HTTP request with retry logic.
* @private
*/
async _request(method, endpoint, data = null, requiresAuth = true) {
const url = `${this.baseUrl}${endpoint}`;
const headers = {
'Content-Type': 'application/json'
};
if (requiresAuth) {
headers['Authorization'] = `Bearer ${this.token}`;
}
for (let attempt = 0; attempt < this.maxRetries; attempt++) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
const response = await fetch(url, {
method,
headers,
body: data ? JSON.stringify(data) : undefined,
signal: controller.signal
});
clearTimeout(timeoutId);
if (response.status === 401) {
throw new AuthenticationError('Invalid or expired token');
}
if (response.status === 400) {
const errorData = await response.json();
throw new ValidationError(`Validation error: ${errorData.detail || 'Unknown error'}`);
}
if (!response.ok) {
throw new IntegrityXError(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
if (error.name === 'AbortError') {
if (attempt === this.maxRetries - 1) {
throw new IntegrityXError(`Request timeout after ${this.maxRetries} attempts`);
}
} else if (error instanceof IntegrityXError) {
throw error;
} else {
if (attempt === this.maxRetries - 1) {
throw new IntegrityXError(`Request failed: ${error.message}`);
}
}
// Exponential backoff
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 1000));
}
}
}
// ===========================
// Document Operations
// ===========================
/**
* Upload a single JSON document.
*
* @param {Object} document - Document data
* @param {Object} metadata - Optional metadata
* @returns {Promise<Object>} Response with ETID, hash, and blockchain transaction ID
*
* @example
* const result = await client.uploadDocument({
* loan_id: 'LOAN-12345',
* amount: 250000,
* borrower_name: 'John Doe'
* });
* console.log(`ETID: ${result.etid}`);
*/
async uploadDocument(document, metadata = null) {
const payload = { document };
if (metadata) {
payload.metadata = metadata;
}
return await this._request('POST', '/ingest-json', payload);
}
/**
* Retrieve a document by ETID.
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Document data with metadata
*/
async getDocument(etid) {
return await this._request('GET', `/document/${etid}`);
}
/**
* Delete a document (admin only).
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Deletion confirmation
*/
async deleteDocument(etid) {
return await this._request('DELETE', `/document/${etid}`);
}
// ===========================
// Verification
// ===========================
/**
* Verify a document (no auth required).
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Verification result with blockchain proof
*
* @example
* const result = await client.verifyDocument('ETID-20241028123456-ABC123');
* console.log(`Verified: ${result.verified}`);
*/
async verifyDocument(etid) {
return await this._request('GET', `/public/verify/${etid}`, null, false);
}
/**
* Verify multiple documents at once.
*
* @param {string[]} etids - List of document ETIDs
* @returns {Promise<Object>} Verification results for all documents
*/
async batchVerify(etids) {
return await this._request('POST', '/verify/batch', { etids });
}
// ===========================
// Attestations
// ===========================
/**
* Create an attestation for a document.
*
* @param {string} etid - Document ETID
* @param {string} role - Attestor role
* @param {string} status - Attestation status
* @param {string} comments - Optional comments
* @param {string} attestedBy - Email of attestor
* @returns {Promise<Object>} Attestation confirmation
*/
async createAttestation(etid, role, status, comments = null, attestedBy = null) {
const payload = { etid, role, status };
if (comments) payload.comments = comments;
if (attestedBy) payload.attested_by = attestedBy;
return await this._request('POST', '/attestations', payload);
}
/**
* Get all attestations for a document.
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} List of attestations
*/
async getAttestations(etid) {
return await this._request('GET', `/attestations/${etid}`);
}
// ===========================
// Provenance
// ===========================
/**
* Get complete provenance chain for a document.
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Complete chain of custody
*/
async getProvenance(etid) {
return await this._request('GET', `/provenance/${etid}`);
}
// ===========================
// Analytics
// ===========================
/**
* Get document statistics.
*
* @returns {Promise<Object>} Statistics and insights
*/
async getStats() {
return await this._request('GET', '/analytics/stats');
}
/**
* Get predictive analytics.
*
* @param {string} metric - Metric to predict
* @param {string} timeframe - Timeframe
* @returns {Promise<Object>} Prediction results
*/
async predict(metric, timeframe) {
return await this._request('POST', '/analytics/predictive', { metric, timeframe });
}
// ===========================
// AI Features
// ===========================
/**
* Detect anomalies in a document using AI.
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Anomaly detection results
*/
async detectAnomalies(etid) {
return await this._request('POST', '/ai/detect-anomalies', { etid });
}
/**
* Analyze document using NLP and entity extraction.
*
* @param {string} etid - Document ETID
* @returns {Promise<Object>} Document intelligence results
*/
async analyzeDocument(etid) {
return await this._request('POST', '/intelligence/analyze', { etid });
}
}
// ===========================
// Usage Examples
// ===========================
async function exampleUploadAndVerify() {
console.log('Example: Upload and verify a document\n');
const client = new IntegrityXClient('http://localhost:8000', 'your_jwt_token');
// Upload document
const document = {
loan_id: 'LOAN-12345',
borrower_name: 'John Doe',
amount: 250000,
interest_rate: 4.5,
term_months: 360
};
const result = await client.uploadDocument(document);
console.log(`✅ Document sealed with ETID: ${result.etid}`);
console.log(` Blockchain TX: ${result.walacor_txid}`);
// Verify document (no auth needed)
const verification = await client.verifyDocument(result.etid);
console.log(`✅ Document verified: ${verification.verified}`);
console.log(` Blockchain verified: ${verification.blockchain_verified}`);
}
async function exampleAttestations() {
console.log('\nExample: Create and retrieve attestations\n');
const client = new IntegrityXClient('http://localhost:8000', 'your_jwt_token');
const etid = 'ETID-20241028123456-ABC123';
// Create attestation
const attestation = await client.createAttestation(
etid,
'underwriter',
'approved',
'Loan application approved after thorough review',
'john.doe@company.com'
);
console.log(`✅ Attestation created: ${attestation.attestation_id}`);
// Get all attestations
const attestations = await client.getAttestations(etid);
console.log(`✅ Found ${attestations.attestations.length} attestations`);
}
async function exampleAnalytics() {
console.log('\nExample: Get analytics and predictions\n');
const client = new IntegrityXClient('http://localhost:8000', 'your_jwt_token');
// Get statistics
const stats = await client.getStats();
console.log(`📊 Total documents: ${stats.total_documents}`);
console.log(` Sealed today: ${stats.sealed_today}`);
console.log(` Average health score: ${stats.average_health_score}`);
// Get predictions
const prediction = await client.predict('document_volume', 'next_30_days');
console.log(`📈 Predicted volume trend: ${prediction.forecast[0].predicted_value}`);
}
async function exampleErrorHandling() {
console.log('\nExample: Proper error handling\n');
const client = new IntegrityXClient('http://localhost:8000', 'your_jwt_token');
try {
const result = await client.uploadDocument({ loan_id: 'LOAN-123' });
console.log(`✅ Success: ${result.etid}`);
} catch (error) {
if (error instanceof AuthenticationError) {
console.log(`❌ Authentication error: ${error.message}`);
// Handle token refresh
} else if (error instanceof ValidationError) {
console.log(`❌ Validation error: ${error.message}`);
// Handle invalid data
} else if (error instanceof IntegrityXError) {
console.log(`❌ API error: ${error.message}`);
// Handle other errors
}
}
}
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
IntegrityXClient,
IntegrityXError,
AuthenticationError,
ValidationError
};
}
// Run examples if executed directly
if (typeof require !== 'undefined' && require.main === module) {
(async () => {
await exampleUploadAndVerify();
await exampleAttestations();
await exampleAnalytics();
await exampleErrorHandling();
})().catch(console.error);
}