@robosystems/client 0.1.12 → 0.1.13

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.
@@ -1,18 +1,20 @@
1
- 'use client'
2
-
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useQuery = useQuery;
5
+ exports.useStreamingQuery = useStreamingQuery;
6
+ exports.useOperation = useOperation;
7
+ exports.useMultipleOperations = useMultipleOperations;
8
+ exports.useSDKClients = useSDKClients;
3
9
  /**
4
10
  * React hooks for SDK extensions
5
11
  * Provides easy-to-use hooks for Next.js/React applications
6
12
  */
7
-
8
- import { useCallback, useEffect, useRef, useState } from 'react'
9
- import { client } from '../client.gen'
10
- import { getSDKExtensionsConfig } from './config'
11
- import { OperationProgress, OperationResult } from './OperationClient'
12
- import { OperationClient } from './OperationClient'
13
- import { QueryOptions, QueryResult } from './QueryClient'
14
- import { QueryClient } from './QueryClient'
15
-
13
+ const react_1 = require("react");
14
+ const client_gen_1 = require("../sdk/client.gen");
15
+ const config_1 = require("./config");
16
+ const OperationClient_1 = require("./OperationClient");
17
+ const QueryClient_1 = require("./QueryClient");
16
18
  /**
17
19
  * Hook for executing Cypher queries with loading states and error handling
18
20
  *
@@ -26,89 +28,69 @@ import { QueryClient } from './QueryClient'
26
28
  * }
27
29
  * ```
28
30
  */
29
- export function useQuery(graphId) {
30
- const [loading, setLoading] = useState(false)
31
- const [error, setError] = useState(null)
32
- const [data, setData] = useState(null)
33
- const [queuePosition, setQueuePosition] = useState(null)
34
- const clientRef = useRef(null)
35
-
36
- // Initialize client
37
- useEffect(() => {
38
- const sdkConfig = getSDKExtensionsConfig()
39
- const clientConfig = client.getConfig()
40
- clientRef.current = new QueryClient({
41
- baseUrl.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
42
- credentials.credentials,
43
- headers.headers,
44
- })
45
-
46
- return () => {
47
- clientRef.current?.close()
48
- }
49
- }, [])
50
-
51
- const execute = useCallback(
52
- async (
53
- query,
54
- parameters?,
55
- options?
56
- ) => {
57
- if (!clientRef.current) return null
58
-
59
- setLoading(true)
60
- setError(null)
61
- setData(null)
62
- setQueuePosition(null)
63
-
64
- try {
65
- const result = (await clientRef.current.executeQuery(
66
- graphId,
67
- { query, parameters },
68
- {
69
- ...options,
70
- onQueueUpdate: (position) => {
71
- setQueuePosition(position)
72
- },
73
- onProgress: () => {
74
- setQueuePosition(null) // Clear queue position when executing
75
- },
76
- }
77
- ))
78
-
79
- setData(result)
80
- return result
81
- } catch (err) {
82
- const error = err
83
- setError(error)
84
- return null
85
- } finally {
86
- setLoading(false)
87
- setQueuePosition(null)
88
- }
89
- },
90
- [graphId]
91
- )
92
-
93
- // Simple query method that returns just the data
94
- const query = useCallback(
95
- async (cypher, parameters?) => {
96
- const result = await execute(cypher, parameters)
97
- return result?.data || []
98
- },
99
- [execute]
100
- )
101
-
102
- return {
103
- execute,
104
- query,
105
- loading,
106
- error,
107
- data,
108
- queuePosition,
109
- }
31
+ function useQuery(graphId) {
32
+ const [loading, setLoading] = (0, react_1.useState)(false);
33
+ const [error, setError] = (0, react_1.useState)(null);
34
+ const [data, setData] = (0, react_1.useState)(null);
35
+ const [queuePosition, setQueuePosition] = (0, react_1.useState)(null);
36
+ const clientRef = (0, react_1.useRef)(null);
37
+ // Initialize client
38
+ (0, react_1.useEffect)(() => {
39
+ const sdkConfig = (0, config_1.getSDKExtensionsConfig)();
40
+ const clientConfig = client_gen_1.client.getConfig();
41
+ clientRef.current = new QueryClient_1.QueryClient({
42
+ baseUrl: sdkConfig.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
43
+ credentials: sdkConfig.credentials,
44
+ headers: sdkConfig.headers,
45
+ });
46
+ return () => {
47
+ clientRef.current?.close();
48
+ };
49
+ }, []);
50
+ const execute = (0, react_1.useCallback)(async (query, parameters, options) => {
51
+ if (!clientRef.current)
52
+ return null;
53
+ setLoading(true);
54
+ setError(null);
55
+ setData(null);
56
+ setQueuePosition(null);
57
+ try {
58
+ const result = (await clientRef.current.executeQuery(graphId, { query, parameters }, {
59
+ ...options,
60
+ onQueueUpdate: (position) => {
61
+ setQueuePosition(position);
62
+ },
63
+ onProgress: () => {
64
+ setQueuePosition(null); // Clear queue position when executing
65
+ },
66
+ }));
67
+ setData(result);
68
+ return result;
69
+ }
70
+ catch (err) {
71
+ const error = err;
72
+ setError(error);
73
+ return null;
74
+ }
75
+ finally {
76
+ setLoading(false);
77
+ setQueuePosition(null);
78
+ }
79
+ }, [graphId]);
80
+ // Simple query method that returns just the data
81
+ const query = (0, react_1.useCallback)(async (cypher, parameters) => {
82
+ const result = await execute(cypher, parameters);
83
+ return result?.data || [];
84
+ }, [execute]);
85
+ return {
86
+ execute,
87
+ query,
88
+ loading,
89
+ error,
90
+ data,
91
+ queuePosition,
92
+ };
110
93
  }
111
-
112
94
  /**
113
95
  * Hook for streaming large query results
114
96
  *
@@ -124,90 +106,74 @@ export function useQuery(graphId) {
124
106
  * }
125
107
  * ```
126
108
  */
127
- export function useStreamingQuery(graphId) {
128
- const [isStreaming, setIsStreaming] = useState(false)
129
- const [error, setError] = useState(null)
130
- const [rowsReceived, setRowsReceived] = useState(0)
131
- const clientRef = useRef(null)
132
-
133
- useEffect(() => {
134
- const sdkConfig = getSDKExtensionsConfig()
135
- const clientConfig = client.getConfig()
136
- clientRef.current = new QueryClient({
137
- baseUrl.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
138
- credentials.credentials,
139
- headers.headers,
140
- })
141
-
142
- return () => {
143
- clientRef.current?.close()
144
- }
145
- }, [])
146
-
147
- const stream = useCallback(
148
- async function* (
149
- query,
150
- parameters?,
151
- chunkSize = 100
152
- ) {
153
- if (!clientRef.current) return
154
-
155
- setIsStreaming(true)
156
- setError(null)
157
- setRowsReceived(0)
158
-
159
- try {
160
- const iterator = clientRef.current.streamQuery(graphId, query, parameters, chunkSize)
161
-
162
- let buffer = []
163
- let count = 0
164
-
165
- for await (const row of iterator) {
166
- buffer.push(row)
167
- count++
168
-
169
- if (buffer.length >= chunkSize) {
170
- setRowsReceived(count)
171
- yield buffer
172
- buffer = []
173
- }
109
+ function useStreamingQuery(graphId) {
110
+ const [isStreaming, setIsStreaming] = (0, react_1.useState)(false);
111
+ const [error, setError] = (0, react_1.useState)(null);
112
+ const [rowsReceived, setRowsReceived] = (0, react_1.useState)(0);
113
+ const clientRef = (0, react_1.useRef)(null);
114
+ (0, react_1.useEffect)(() => {
115
+ const sdkConfig = (0, config_1.getSDKExtensionsConfig)();
116
+ const clientConfig = client_gen_1.client.getConfig();
117
+ clientRef.current = new QueryClient_1.QueryClient({
118
+ baseUrl: sdkConfig.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
119
+ credentials: sdkConfig.credentials,
120
+ headers: sdkConfig.headers,
121
+ });
122
+ return () => {
123
+ clientRef.current?.close();
124
+ };
125
+ }, []);
126
+ const stream = (0, react_1.useCallback)(async function* (query, parameters, chunkSize = 100) {
127
+ if (!clientRef.current)
128
+ return;
129
+ setIsStreaming(true);
130
+ setError(null);
131
+ setRowsReceived(0);
132
+ try {
133
+ const iterator = clientRef.current.streamQuery(graphId, query, parameters, chunkSize);
134
+ let buffer = [];
135
+ let count = 0;
136
+ for await (const row of iterator) {
137
+ buffer.push(row);
138
+ count++;
139
+ if (buffer.length >= chunkSize) {
140
+ setRowsReceived(count);
141
+ yield buffer;
142
+ buffer = [];
143
+ }
144
+ }
145
+ // Yield any remaining items
146
+ if (buffer.length > 0) {
147
+ setRowsReceived(count);
148
+ yield buffer;
149
+ }
174
150
  }
175
-
176
- // Yield any remaining items
177
- if (buffer.length > 0) {
178
- setRowsReceived(count)
179
- yield buffer
151
+ catch (err) {
152
+ setError(err);
153
+ throw err;
180
154
  }
181
- } catch (err) {
182
- setError(err)
183
- throw err
184
- } finally {
185
- setIsStreaming(false)
186
- }
187
- },
188
- [graphId]
189
- )
190
-
191
- const cancel = useCallback(() => {
192
- clientRef.current?.close()
193
- setIsStreaming(false)
194
- }, [])
195
-
196
- return {
197
- stream,
198
- isStreaming,
199
- error,
200
- rowsReceived,
201
- cancel,
202
- }
155
+ finally {
156
+ setIsStreaming(false);
157
+ }
158
+ }, [graphId]);
159
+ const cancel = (0, react_1.useCallback)(() => {
160
+ clientRef.current?.close();
161
+ setIsStreaming(false);
162
+ }, []);
163
+ return {
164
+ stream,
165
+ isStreaming,
166
+ error,
167
+ rowsReceived,
168
+ cancel,
169
+ };
203
170
  }
204
-
205
171
  /**
206
172
  * Hook for monitoring long-running operations
207
173
  *
208
174
  * @example
209
175
  * ```tsx
210
- * const { monitor, status, progress, error, result } = useOperation()
176
+ * const { monitor, status, progress, error, result } = useOperation<BackupResult>()
211
177
  *
212
178
  * const handleBackup = async () => {
213
179
  * const { operation_id } = await createBackup({ ... })
@@ -216,98 +182,86 @@ export function useStreamingQuery(graphId) {
216
182
  * }
217
183
  * ```
218
184
  */
219
- export function useOperation(operationId?) {
220
- const [status, setStatus] = useState('idle')
221
- const [progress, setProgress] = useState(null)
222
- const [error, setError] = useState(null)
223
- const [result, setResult] = useState | null>(null)
224
- const clientRef = useRef(null)
225
-
226
- useEffect(() => {
227
- const sdkConfig = getSDKExtensionsConfig()
228
- const clientConfig = client.getConfig()
229
- clientRef.current = new OperationClient({
230
- baseUrl.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
231
- credentials.credentials,
232
- maxRetries.maxRetries,
233
- retryDelay.retryDelay,
234
- })
235
-
236
- return () => {
237
- clientRef.current?.closeAll()
238
- }
239
- }, [])
240
-
241
- const monitor = useCallback(
242
- async (id, timeout?) | null> => {
243
- if (!clientRef.current) return null
244
-
245
- setStatus('running')
246
- setError(null)
247
- setResult(null)
248
- setProgress(null)
249
-
250
- try {
251
- const opResult = await clientRef.current.monitorOperation(id, {
252
- onProgress: (p) => {
253
- setProgress(p)
254
- },
255
- onQueueUpdate: (position, estimatedWait) => {
256
- setProgress({
257
- message: `Queue position: ${position}`,
258
- progressPercent,
259
- details,
260
- })
261
- },
262
- timeout,
263
- })
264
-
265
- setResult(opResult)
266
- setStatus(opResult.success ? 'completed' )
267
-
268
- if (!opResult.success && opResult.error) {
269
- setError(new Error(opResult.error))
185
+ function useOperation(operationId) {
186
+ const [status, setStatus] = (0, react_1.useState)('idle');
187
+ const [progress, setProgress] = (0, react_1.useState)(null);
188
+ const [error, setError] = (0, react_1.useState)(null);
189
+ const [result, setResult] = (0, react_1.useState)(null);
190
+ const clientRef = (0, react_1.useRef)(null);
191
+ (0, react_1.useEffect)(() => {
192
+ const sdkConfig = (0, config_1.getSDKExtensionsConfig)();
193
+ const clientConfig = client_gen_1.client.getConfig();
194
+ clientRef.current = new OperationClient_1.OperationClient({
195
+ baseUrl: sdkConfig.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
196
+ credentials: sdkConfig.credentials,
197
+ maxRetries: sdkConfig.maxRetries,
198
+ retryDelay: sdkConfig.retryDelay,
199
+ });
200
+ return () => {
201
+ clientRef.current?.closeAll();
202
+ };
203
+ }, []);
204
+ const monitor = (0, react_1.useCallback)(async (id, timeout) => {
205
+ if (!clientRef.current)
206
+ return null;
207
+ setStatus('running');
208
+ setError(null);
209
+ setResult(null);
210
+ setProgress(null);
211
+ try {
212
+ const opResult = await clientRef.current.monitorOperation(id, {
213
+ onProgress: (p) => {
214
+ setProgress(p);
215
+ },
216
+ onQueueUpdate: (position, estimatedWait) => {
217
+ setProgress({
218
+ message: `Queue position: ${position}`,
219
+ progressPercent: 0,
220
+ details: { position, estimatedWait },
221
+ });
222
+ },
223
+ timeout,
224
+ });
225
+ setResult(opResult);
226
+ setStatus(opResult.success ? 'completed' : 'error');
227
+ if (!opResult.success && opResult.error) {
228
+ setError(new Error(opResult.error));
229
+ }
230
+ return opResult;
231
+ }
232
+ catch (err) {
233
+ const error = err;
234
+ setError(error);
235
+ setStatus('error');
236
+ return null;
270
237
  }
271
-
272
- return opResult
273
- } catch (err) {
274
- const error = err
275
- setError(error)
276
- setStatus('error')
277
- return null
278
- }
279
- },
280
- []
281
- )
282
-
283
- const cancel = useCallback(async (id) => {
284
- if (!clientRef.current) return
285
-
286
- try {
287
- await clientRef.current.cancelOperation(id)
288
- setStatus('idle')
289
- } catch (err) {
290
- setError(err)
291
- }
292
- }, [])
293
-
294
- // Auto-monitor if operationId is provided
295
- useEffect(() => {
296
- if (operationId && status === 'idle') {
297
- monitor(operationId)
298
- }
299
- }, [operationId, monitor, status])
300
-
301
- return {
302
- monitor,
303
- cancel,
304
- status,
305
- progress,
306
- error,
307
- result,
308
- }
238
+ }, []);
239
+ const cancel = (0, react_1.useCallback)(async (id) => {
240
+ if (!clientRef.current)
241
+ return;
242
+ try {
243
+ await clientRef.current.cancelOperation(id);
244
+ setStatus('idle');
245
+ }
246
+ catch (err) {
247
+ setError(err);
248
+ }
249
+ }, []);
250
+ // Auto-monitor if operationId is provided
251
+ (0, react_1.useEffect)(() => {
252
+ if (operationId && status === 'idle') {
253
+ monitor(operationId);
254
+ }
255
+ }, [operationId, monitor, status]);
256
+ return {
257
+ monitor,
258
+ cancel,
259
+ status,
260
+ progress,
261
+ error,
262
+ result,
263
+ };
309
264
  }
310
-
311
265
  /**
312
266
  * Hook for monitoring multiple operations concurrently
313
267
  *
@@ -325,71 +279,58 @@ export function useOperation(operationId?) {
325
279
  * }
326
280
  * ```
327
281
  */
328
- export function useMultipleOperations() {
329
- const [results, setResults] = useState>>(new Map())
330
- const [loading, setLoading] = useState(false)
331
- const [errors, setErrors] = useState>(new Map())
332
- const clientRef = useRef(null)
333
-
334
- useEffect(() => {
335
- const sdkConfig = getSDKExtensionsConfig()
336
- const clientConfig = client.getConfig()
337
- clientRef.current = new OperationClient({
338
- baseUrl.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
339
- credentials.credentials,
340
- maxRetries.maxRetries,
341
- retryDelay.retryDelay,
342
- })
343
-
344
- return () => {
345
- clientRef.current?.closeAll()
346
- }
347
- }, [])
348
-
349
- const monitorAll = useCallback(
350
- async (operationIds)>> => {
351
- if (!clientRef.current) return new Map()
352
-
353
- setLoading(true)
354
- setResults(new Map())
355
- setErrors(new Map())
356
-
357
- try {
358
- const allResults = await clientRef.current.monitorMultiple(operationIds)
359
- setResults(allResults)
360
-
361
- // Extract any errors
362
- const newErrors = new Map()
363
- allResults.forEach((result, id) => {
364
- if (!result.success && result.error) {
365
- newErrors.set(id, new Error(result.error))
366
- }
367
- })
368
- setErrors(newErrors)
369
-
370
- return allResults
371
- } finally {
372
- setLoading(false)
373
- }
374
- },
375
- []
376
- )
377
-
378
- const allCompleted =
379
- results.size > 0 && Array.from(results.values()).every((r) => r.success || r.error)
380
-
381
- const hasErrors = errors.size > 0
382
-
383
- return {
384
- monitorAll,
385
- results,
386
- errors,
387
- loading,
388
- allCompleted,
389
- hasErrors,
390
- }
282
+ function useMultipleOperations() {
283
+ const [results, setResults] = (0, react_1.useState)(new Map());
284
+ const [loading, setLoading] = (0, react_1.useState)(false);
285
+ const [errors, setErrors] = (0, react_1.useState)(new Map());
286
+ const clientRef = (0, react_1.useRef)(null);
287
+ (0, react_1.useEffect)(() => {
288
+ const sdkConfig = (0, config_1.getSDKExtensionsConfig)();
289
+ const clientConfig = client_gen_1.client.getConfig();
290
+ clientRef.current = new OperationClient_1.OperationClient({
291
+ baseUrl: sdkConfig.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
292
+ credentials: sdkConfig.credentials,
293
+ maxRetries: sdkConfig.maxRetries,
294
+ retryDelay: sdkConfig.retryDelay,
295
+ });
296
+ return () => {
297
+ clientRef.current?.closeAll();
298
+ };
299
+ }, []);
300
+ const monitorAll = (0, react_1.useCallback)(async (operationIds) => {
301
+ if (!clientRef.current)
302
+ return new Map();
303
+ setLoading(true);
304
+ setResults(new Map());
305
+ setErrors(new Map());
306
+ try {
307
+ const allResults = await clientRef.current.monitorMultiple(operationIds);
308
+ setResults(allResults);
309
+ // Extract any errors
310
+ const newErrors = new Map();
311
+ allResults.forEach((result, id) => {
312
+ if (!result.success && result.error) {
313
+ newErrors.set(id, new Error(result.error));
314
+ }
315
+ });
316
+ setErrors(newErrors);
317
+ return allResults;
318
+ }
319
+ finally {
320
+ setLoading(false);
321
+ }
322
+ }, []);
323
+ const allCompleted = results.size > 0 && Array.from(results.values()).every((r) => r.success || r.error);
324
+ const hasErrors = errors.size > 0;
325
+ return {
326
+ monitorAll,
327
+ results,
328
+ errors,
329
+ loading,
330
+ allCompleted,
331
+ hasErrors,
332
+ };
391
333
  }
392
-
393
334
  /**
394
335
  * Hook that provides access to all SDK extension clients
395
336
  * Useful when you need direct access to the underlying clients
@@ -402,34 +343,29 @@ export function useMultipleOperations() {
402
343
  * const result = await clients.query.query('graph_123', 'MATCH (n) RETURN n')
403
344
  * ```
404
345
  */
405
- export function useSDKClients() {
406
- const [clients, setClients] = useState({
407
- query,
408
- operations,
409
- })
410
-
411
- useEffect(() => {
412
- const sdkConfig = getSDKExtensionsConfig()
413
- const clientConfig = client.getConfig()
414
- const baseConfig = {
415
- baseUrl.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
416
- credentials.credentials,
417
- headers.headers,
418
- }
419
-
420
- const queryClient = new QueryClient(baseConfig)
421
- const operationsClient = new OperationClient(baseConfig)
422
-
423
- setClients({
424
- query,
425
- operations,
426
- })
427
-
428
- return () => {
429
- queryClient.close()
430
- operationsClient.closeAll()
431
- }
432
- }, [])
433
-
434
- return clients
346
+ function useSDKClients() {
347
+ const [clients, setClients] = (0, react_1.useState)({
348
+ query: null,
349
+ operations: null,
350
+ });
351
+ (0, react_1.useEffect)(() => {
352
+ const sdkConfig = (0, config_1.getSDKExtensionsConfig)();
353
+ const clientConfig = client_gen_1.client.getConfig();
354
+ const baseConfig = {
355
+ baseUrl: sdkConfig.baseUrl || clientConfig.baseUrl || 'http://localhost:8000',
356
+ credentials: sdkConfig.credentials,
357
+ headers: sdkConfig.headers,
358
+ };
359
+ const queryClient = new QueryClient_1.QueryClient(baseConfig);
360
+ const operationsClient = new OperationClient_1.OperationClient(baseConfig);
361
+ setClients({
362
+ query: queryClient,
363
+ operations: operationsClient,
364
+ });
365
+ return () => {
366
+ queryClient.close();
367
+ operationsClient.closeAll();
368
+ };
369
+ }, []);
370
+ return clients;
435
371
  }