@robosystems/client 2.5.2 → 2.6.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/artifacts/InvestorClient.d.ts +7 -1
- package/artifacts/InvestorClient.js +24 -14
- package/artifacts/InvestorClient.ts +46 -27
- package/artifacts/LedgerClient.d.ts +23 -17
- package/artifacts/LedgerClient.js +116 -123
- package/artifacts/LedgerClient.ts +180 -242
- package/artifacts/OperationClient.js +24 -1
- package/artifacts/OperationClient.ts +24 -1
- package/artifacts/OperatorClient.d.ts +10 -0
- package/artifacts/OperatorClient.js +72 -17
- package/artifacts/OperatorClient.ts +79 -21
- package/artifacts/QueryClient.d.ts +2 -1
- package/artifacts/QueryClient.js +17 -58
- package/artifacts/QueryClient.ts +21 -68
- package/artifacts/SSEClient.d.ts +1 -0
- package/artifacts/SSEClient.js +56 -7
- package/artifacts/SSEClient.ts +57 -7
- package/artifacts/config.js +18 -4
- package/artifacts/config.ts +18 -4
- package/artifacts/graphql/client.d.ts +10 -0
- package/artifacts/graphql/client.js +29 -21
- package/artifacts/graphql/client.ts +34 -23
- package/artifacts/index.d.ts +2 -2
- package/artifacts/index.js +6 -6
- package/artifacts/index.ts +8 -8
- package/artifacts/rest.d.ts +22 -0
- package/artifacts/rest.js +27 -0
- package/artifacts/rest.ts +54 -0
- package/artifacts/retry.js +8 -3
- package/artifacts/retry.ts +8 -3
- package/package.json +9 -1
- package/sdk/sdk.gen.d.ts +2 -2
- package/sdk/sdk.gen.js +2 -2
- package/sdk/sdk.gen.ts +2 -2
- package/sdk/types.gen.d.ts +21 -13
- package/sdk/types.gen.ts +21 -13
- package/sdk.gen.d.ts +2 -2
- package/sdk.gen.js +2 -2
- package/sdk.gen.ts +2 -2
- package/types.gen.d.ts +21 -13
- package/types.gen.ts +21 -13
package/artifacts/QueryClient.ts
CHANGED
|
@@ -8,16 +8,19 @@
|
|
|
8
8
|
import { executeCypher } from '../sdk.gen'
|
|
9
9
|
import type { ExecuteCypherData } from '../types.gen'
|
|
10
10
|
import type { TokenProvider } from './graphql/client'
|
|
11
|
+
import { restCallOptions } from './rest'
|
|
11
12
|
import { EventType, SSEClient } from './SSEClient'
|
|
12
13
|
|
|
13
14
|
export interface QueryRequest {
|
|
14
15
|
query: string
|
|
15
16
|
parameters?: Record<string, any>
|
|
17
|
+
/** Server-side query timeout in whole seconds (the API accepts 1-300). */
|
|
16
18
|
timeout?: number
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export interface QueryOptions {
|
|
20
22
|
mode?: 'auto' | 'sync' | 'async' | 'stream'
|
|
23
|
+
/** Rows per streamed chunk (the API accepts 10-10000). */
|
|
21
24
|
chunkSize?: number
|
|
22
25
|
testMode?: boolean
|
|
23
26
|
maxWait?: number
|
|
@@ -73,16 +76,18 @@ export class QueryClient {
|
|
|
73
76
|
body: {
|
|
74
77
|
query: request.query,
|
|
75
78
|
parameters: request.parameters,
|
|
79
|
+
timeout: request.timeout,
|
|
76
80
|
},
|
|
77
81
|
query: {
|
|
78
82
|
mode: options.mode,
|
|
83
|
+
chunk_size: options.chunkSize,
|
|
79
84
|
test_mode: options.testMode,
|
|
80
85
|
},
|
|
81
86
|
// For streaming mode, don't parse response - get raw Response object
|
|
82
87
|
...(options.mode === 'stream' ? { parseAs: 'stream' as const } : {}),
|
|
83
88
|
}
|
|
84
89
|
|
|
85
|
-
const response = await executeCypher(data)
|
|
90
|
+
const response = await executeCypher({ ...(await restCallOptions(this.config)), ...data })
|
|
86
91
|
|
|
87
92
|
// Check for errors in the response (network errors, etc.)
|
|
88
93
|
if ('error' in response && response.error) {
|
|
@@ -255,76 +260,13 @@ export class QueryClient {
|
|
|
255
260
|
}
|
|
256
261
|
}
|
|
257
262
|
|
|
258
|
-
private async *streamQueryResults(
|
|
259
|
-
operationId: string,
|
|
260
|
-
options: QueryOptions
|
|
261
|
-
): AsyncIterableIterator<any> {
|
|
262
|
-
const buffer: any[] = []
|
|
263
|
-
let completed = false
|
|
264
|
-
let error: Error | null = null
|
|
265
|
-
|
|
266
|
-
// Set up SSE connection
|
|
267
|
-
this.sseClient = new SSEClient(this.config)
|
|
268
|
-
await this.sseClient.connect(operationId)
|
|
269
|
-
|
|
270
|
-
// Listen for data chunks
|
|
271
|
-
this.sseClient.on(EventType.DATA_CHUNK, (data) => {
|
|
272
|
-
if (Array.isArray(data.rows)) {
|
|
273
|
-
buffer.push(...data.rows)
|
|
274
|
-
} else if (data.data) {
|
|
275
|
-
buffer.push(...data.data)
|
|
276
|
-
}
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
// Listen for queue updates
|
|
280
|
-
this.sseClient.on(EventType.QUEUE_UPDATE, (data) => {
|
|
281
|
-
options.onQueueUpdate?.(data.position, data.estimated_wait_seconds)
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
// Listen for progress
|
|
285
|
-
this.sseClient.on(EventType.OPERATION_PROGRESS, (data) => {
|
|
286
|
-
options.onProgress?.(data.message)
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
// Listen for completion
|
|
290
|
-
this.sseClient.on(EventType.OPERATION_COMPLETED, (data) => {
|
|
291
|
-
if (data.result?.data) {
|
|
292
|
-
buffer.push(...data.result.data)
|
|
293
|
-
}
|
|
294
|
-
completed = true
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
// Listen for errors
|
|
298
|
-
this.sseClient.on(EventType.OPERATION_ERROR, (err) => {
|
|
299
|
-
error = new Error(err.message || err.error)
|
|
300
|
-
completed = true
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
// Yield buffered results
|
|
304
|
-
while (!completed || buffer.length > 0) {
|
|
305
|
-
if (error) throw error
|
|
306
|
-
|
|
307
|
-
if (buffer.length > 0) {
|
|
308
|
-
const chunk = buffer.splice(0, options.chunkSize || 100)
|
|
309
|
-
for (const item of chunk) {
|
|
310
|
-
yield item
|
|
311
|
-
}
|
|
312
|
-
} else if (!completed) {
|
|
313
|
-
// Wait for more data
|
|
314
|
-
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
this.sseClient.close()
|
|
319
|
-
this.sseClient = undefined
|
|
320
|
-
}
|
|
321
|
-
|
|
322
263
|
private async waitForQueryCompletion(
|
|
323
264
|
operationId: string,
|
|
324
265
|
options: QueryOptions
|
|
325
266
|
): Promise<QueryResult> {
|
|
326
267
|
return new Promise((resolve, reject) => {
|
|
327
268
|
const sseClient = new SSEClient(this.config)
|
|
269
|
+
this.sseClient = sseClient
|
|
328
270
|
|
|
329
271
|
sseClient
|
|
330
272
|
.connect(operationId)
|
|
@@ -351,18 +293,29 @@ export class QueryClient {
|
|
|
351
293
|
graph_id: queryResult.graph_id,
|
|
352
294
|
timestamp: queryResult.timestamp || new Date().toISOString(),
|
|
353
295
|
}
|
|
354
|
-
sseClient.close()
|
|
355
296
|
resolve(result)
|
|
297
|
+
sseClient.close()
|
|
356
298
|
})
|
|
357
299
|
|
|
358
300
|
sseClient.on(EventType.OPERATION_ERROR, (error) => {
|
|
359
|
-
sseClient.close()
|
|
360
301
|
reject(new Error(error.message || error.error))
|
|
302
|
+
sseClient.close()
|
|
361
303
|
})
|
|
362
304
|
|
|
363
305
|
sseClient.on(EventType.OPERATION_CANCELLED, () => {
|
|
364
|
-
sseClient.close()
|
|
365
306
|
reject(new Error('Query cancelled'))
|
|
307
|
+
sseClient.close()
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
sseClient.on('error', (error) => {
|
|
311
|
+
reject(new Error(error?.error || 'Query stream error'))
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
// A close before a terminal event (retries exhausted, a reconnect
|
|
315
|
+
// that failed before opening, close()) would otherwise leave this
|
|
316
|
+
// pending. Settle first in the handlers above: they close too.
|
|
317
|
+
sseClient.on('closed', () => {
|
|
318
|
+
reject(new Error('Query stream closed before the query completed'))
|
|
366
319
|
})
|
|
367
320
|
})
|
|
368
321
|
.catch(reject)
|
package/artifacts/SSEClient.d.ts
CHANGED
|
@@ -69,6 +69,7 @@ export declare class SSEClient {
|
|
|
69
69
|
private listeners;
|
|
70
70
|
constructor(config: SSEConfig);
|
|
71
71
|
connect(operationId: string, fromSequence?: number): Promise<void>;
|
|
72
|
+
private open;
|
|
72
73
|
/**
|
|
73
74
|
* Credential for one connect attempt: the `tokenProvider` result when one
|
|
74
75
|
* is configured (`null`/`undefined` connects unauthenticated), otherwise
|
package/artifacts/SSEClient.js
CHANGED
|
@@ -27,9 +27,15 @@ class SSEClient {
|
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
29
|
async connect(operationId, fromSequence = 0) {
|
|
30
|
+
return this.open(operationId, fromSequence, false);
|
|
31
|
+
}
|
|
32
|
+
async open(operationId, fromSequence, reconnect) {
|
|
30
33
|
// Resolved per attempt so a rotated JWT is picked up by every connect,
|
|
31
34
|
// not just the first one after construction.
|
|
32
35
|
const token = await this.resolveToken();
|
|
36
|
+
// A close() during the backoff or the token lookup ends a reconnect.
|
|
37
|
+
if (reconnect && this.closed)
|
|
38
|
+
return;
|
|
33
39
|
return new Promise((resolve, reject) => {
|
|
34
40
|
let url = `${this.config.baseUrl}/v1/operations/${operationId}/stream?from_sequence=${fromSequence}`;
|
|
35
41
|
// Add JWT token as query parameter if provided
|
|
@@ -38,9 +44,13 @@ class SSEClient {
|
|
|
38
44
|
if (token) {
|
|
39
45
|
url += `&token=${encodeURIComponent(token)}`;
|
|
40
46
|
}
|
|
41
|
-
|
|
47
|
+
// Never leave a previous source running beside its replacement: two
|
|
48
|
+
// live streams deliver every event twice.
|
|
49
|
+
this.eventSource?.close();
|
|
50
|
+
const eventSource = new EventSource(url, {
|
|
42
51
|
withCredentials: this.config.credentials === 'include',
|
|
43
52
|
});
|
|
53
|
+
this.eventSource = eventSource;
|
|
44
54
|
let opened = false;
|
|
45
55
|
const connectionTimeout = setTimeout(() => {
|
|
46
56
|
if (!opened) {
|
|
@@ -48,14 +58,30 @@ class SSEClient {
|
|
|
48
58
|
this.close();
|
|
49
59
|
}
|
|
50
60
|
}, 10000);
|
|
51
|
-
|
|
61
|
+
eventSource.onopen = () => {
|
|
52
62
|
opened = true;
|
|
53
63
|
clearTimeout(connectionTimeout);
|
|
54
64
|
this.reconnectAttempts = 0;
|
|
55
65
|
this.emit('connected', null);
|
|
56
66
|
resolve();
|
|
57
67
|
};
|
|
58
|
-
|
|
68
|
+
eventSource.onerror = (error) => {
|
|
69
|
+
if (this.eventSource !== eventSource)
|
|
70
|
+
return;
|
|
71
|
+
const serverError = readServerError(error);
|
|
72
|
+
if (serverError) {
|
|
73
|
+
// The server's own `event: error` — the per-user connection cap, an
|
|
74
|
+
// unknown or foreign operation, a failed stream — ends the stream
|
|
75
|
+
// for good. Reconnecting would only repeat it (and, since the
|
|
76
|
+
// stream opens first, reset the retry budget each time).
|
|
77
|
+
clearTimeout(connectionTimeout);
|
|
78
|
+
this.emit('error', serverError);
|
|
79
|
+
this.close();
|
|
80
|
+
if (!opened) {
|
|
81
|
+
reject(new Error(serverError.error));
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
59
85
|
if (!opened) {
|
|
60
86
|
// Failed before the stream ever opened. Browser EventSource doesn't
|
|
61
87
|
// expose the HTTP status, but a CLOSED readyState at this point
|
|
@@ -64,7 +90,7 @@ class SSEClient {
|
|
|
64
90
|
// waiting out the 10s connectionTimeout or spinning retries that
|
|
65
91
|
// can never satisfy this promise.
|
|
66
92
|
clearTimeout(connectionTimeout);
|
|
67
|
-
const nonRetryable =
|
|
93
|
+
const nonRetryable = eventSource.readyState === EventSource.CLOSED;
|
|
68
94
|
this.close();
|
|
69
95
|
reject(new Error(nonRetryable
|
|
70
96
|
? 'SSE connection failed before open (likely auth/not-found; EventSource hides the HTTP status)'
|
|
@@ -75,12 +101,12 @@ class SSEClient {
|
|
|
75
101
|
this.handleError(error, operationId, fromSequence);
|
|
76
102
|
}
|
|
77
103
|
};
|
|
78
|
-
|
|
104
|
+
eventSource.onmessage = (event) => {
|
|
79
105
|
this.handleMessage(event);
|
|
80
106
|
};
|
|
81
107
|
// Set up specific event listeners
|
|
82
108
|
Object.values(EventType).forEach((eventType) => {
|
|
83
|
-
|
|
109
|
+
eventSource.addEventListener(eventType, (event) => {
|
|
84
110
|
this.handleTypedEvent(eventType, event);
|
|
85
111
|
});
|
|
86
112
|
});
|
|
@@ -144,6 +170,9 @@ class SSEClient {
|
|
|
144
170
|
async handleError(error, operationId, fromSequence) {
|
|
145
171
|
if (this.closed)
|
|
146
172
|
return;
|
|
173
|
+
// Close the failed source now: a browser EventSource retries on its own,
|
|
174
|
+
// and that retry would run alongside the reconnect scheduled below.
|
|
175
|
+
this.eventSource?.close();
|
|
147
176
|
if (this.reconnectAttempts < this.config.maxRetries) {
|
|
148
177
|
this.reconnectAttempts++;
|
|
149
178
|
const delay = this.config.retryDelay * Math.pow(2, this.reconnectAttempts - 1);
|
|
@@ -153,8 +182,10 @@ class SSEClient {
|
|
|
153
182
|
lastEventId: this.lastEventId,
|
|
154
183
|
});
|
|
155
184
|
setTimeout(() => {
|
|
185
|
+
if (this.closed)
|
|
186
|
+
return;
|
|
156
187
|
const resumeFrom = this.lastEventId ? parseInt(this.lastEventId) + 1 : fromSequence;
|
|
157
|
-
this.
|
|
188
|
+
this.open(operationId, resumeFrom, true).catch(() => {
|
|
158
189
|
// Error handled in connect
|
|
159
190
|
});
|
|
160
191
|
}, delay);
|
|
@@ -196,3 +227,21 @@ class SSEClient {
|
|
|
196
227
|
}
|
|
197
228
|
}
|
|
198
229
|
exports.SSEClient = SSEClient;
|
|
230
|
+
/**
|
|
231
|
+
* The payload of a server-sent `event: error`, or null for a transport
|
|
232
|
+
* error. A named `error` event reaches `onerror` as a `MessageEvent`
|
|
233
|
+
* carrying the server's JSON (`{"error": "..."}`); a dropped connection
|
|
234
|
+
* arrives as a bare `Event` with no data.
|
|
235
|
+
*/
|
|
236
|
+
function readServerError(event) {
|
|
237
|
+
if (typeof event?.data !== 'string' || event.data === '')
|
|
238
|
+
return null;
|
|
239
|
+
try {
|
|
240
|
+
const data = JSON.parse(event.data);
|
|
241
|
+
const message = data?.error ?? data?.message ?? data?.detail;
|
|
242
|
+
return { ...(typeof data === 'object' ? data : {}), error: String(message ?? event.data) };
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
return { error: event.data };
|
|
246
|
+
}
|
|
247
|
+
}
|
package/artifacts/SSEClient.ts
CHANGED
|
@@ -85,10 +85,17 @@ export class SSEClient {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
async connect(operationId: string, fromSequence: number = 0): Promise<void> {
|
|
88
|
+
return this.open(operationId, fromSequence, false)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private async open(operationId: string, fromSequence: number, reconnect: boolean): Promise<void> {
|
|
88
92
|
// Resolved per attempt so a rotated JWT is picked up by every connect,
|
|
89
93
|
// not just the first one after construction.
|
|
90
94
|
const token = await this.resolveToken()
|
|
91
95
|
|
|
96
|
+
// A close() during the backoff or the token lookup ends a reconnect.
|
|
97
|
+
if (reconnect && this.closed) return
|
|
98
|
+
|
|
92
99
|
return new Promise((resolve, reject) => {
|
|
93
100
|
let url = `${this.config.baseUrl}/v1/operations/${operationId}/stream?from_sequence=${fromSequence}`
|
|
94
101
|
|
|
@@ -99,9 +106,13 @@ export class SSEClient {
|
|
|
99
106
|
url += `&token=${encodeURIComponent(token)}`
|
|
100
107
|
}
|
|
101
108
|
|
|
102
|
-
|
|
109
|
+
// Never leave a previous source running beside its replacement: two
|
|
110
|
+
// live streams deliver every event twice.
|
|
111
|
+
this.eventSource?.close()
|
|
112
|
+
const eventSource = new EventSource(url, {
|
|
103
113
|
withCredentials: this.config.credentials === 'include',
|
|
104
114
|
} as any)
|
|
115
|
+
this.eventSource = eventSource
|
|
105
116
|
|
|
106
117
|
let opened = false
|
|
107
118
|
|
|
@@ -112,7 +123,7 @@ export class SSEClient {
|
|
|
112
123
|
}
|
|
113
124
|
}, 10000)
|
|
114
125
|
|
|
115
|
-
|
|
126
|
+
eventSource.onopen = () => {
|
|
116
127
|
opened = true
|
|
117
128
|
clearTimeout(connectionTimeout)
|
|
118
129
|
this.reconnectAttempts = 0
|
|
@@ -120,7 +131,24 @@ export class SSEClient {
|
|
|
120
131
|
resolve()
|
|
121
132
|
}
|
|
122
133
|
|
|
123
|
-
|
|
134
|
+
eventSource.onerror = (error) => {
|
|
135
|
+
if (this.eventSource !== eventSource) return
|
|
136
|
+
|
|
137
|
+
const serverError = readServerError(error)
|
|
138
|
+
if (serverError) {
|
|
139
|
+
// The server's own `event: error` — the per-user connection cap, an
|
|
140
|
+
// unknown or foreign operation, a failed stream — ends the stream
|
|
141
|
+
// for good. Reconnecting would only repeat it (and, since the
|
|
142
|
+
// stream opens first, reset the retry budget each time).
|
|
143
|
+
clearTimeout(connectionTimeout)
|
|
144
|
+
this.emit('error', serverError)
|
|
145
|
+
this.close()
|
|
146
|
+
if (!opened) {
|
|
147
|
+
reject(new Error(serverError.error))
|
|
148
|
+
}
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
124
152
|
if (!opened) {
|
|
125
153
|
// Failed before the stream ever opened. Browser EventSource doesn't
|
|
126
154
|
// expose the HTTP status, but a CLOSED readyState at this point
|
|
@@ -129,7 +157,7 @@ export class SSEClient {
|
|
|
129
157
|
// waiting out the 10s connectionTimeout or spinning retries that
|
|
130
158
|
// can never satisfy this promise.
|
|
131
159
|
clearTimeout(connectionTimeout)
|
|
132
|
-
const nonRetryable =
|
|
160
|
+
const nonRetryable = eventSource.readyState === EventSource.CLOSED
|
|
133
161
|
this.close()
|
|
134
162
|
reject(
|
|
135
163
|
new Error(
|
|
@@ -145,13 +173,13 @@ export class SSEClient {
|
|
|
145
173
|
}
|
|
146
174
|
}
|
|
147
175
|
|
|
148
|
-
|
|
176
|
+
eventSource.onmessage = (event) => {
|
|
149
177
|
this.handleMessage(event)
|
|
150
178
|
}
|
|
151
179
|
|
|
152
180
|
// Set up specific event listeners
|
|
153
181
|
Object.values(EventType).forEach((eventType) => {
|
|
154
|
-
|
|
182
|
+
eventSource.addEventListener(eventType, (event: any) => {
|
|
155
183
|
this.handleTypedEvent(eventType, event)
|
|
156
184
|
})
|
|
157
185
|
})
|
|
@@ -223,6 +251,10 @@ export class SSEClient {
|
|
|
223
251
|
private async handleError(error: any, operationId: string, fromSequence: number): Promise<void> {
|
|
224
252
|
if (this.closed) return
|
|
225
253
|
|
|
254
|
+
// Close the failed source now: a browser EventSource retries on its own,
|
|
255
|
+
// and that retry would run alongside the reconnect scheduled below.
|
|
256
|
+
this.eventSource?.close()
|
|
257
|
+
|
|
226
258
|
if (this.reconnectAttempts < this.config.maxRetries!) {
|
|
227
259
|
this.reconnectAttempts++
|
|
228
260
|
const delay = this.config.retryDelay! * Math.pow(2, this.reconnectAttempts - 1)
|
|
@@ -234,8 +266,9 @@ export class SSEClient {
|
|
|
234
266
|
})
|
|
235
267
|
|
|
236
268
|
setTimeout(() => {
|
|
269
|
+
if (this.closed) return
|
|
237
270
|
const resumeFrom = this.lastEventId ? parseInt(this.lastEventId) + 1 : fromSequence
|
|
238
|
-
this.
|
|
271
|
+
this.open(operationId, resumeFrom, true).catch(() => {
|
|
239
272
|
// Error handled in connect
|
|
240
273
|
})
|
|
241
274
|
}, delay)
|
|
@@ -280,3 +313,20 @@ export class SSEClient {
|
|
|
280
313
|
return this.eventSource !== undefined && this.eventSource.readyState === EventSource.OPEN
|
|
281
314
|
}
|
|
282
315
|
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The payload of a server-sent `event: error`, or null for a transport
|
|
319
|
+
* error. A named `error` event reaches `onerror` as a `MessageEvent`
|
|
320
|
+
* carrying the server's JSON (`{"error": "..."}`); a dropped connection
|
|
321
|
+
* arrives as a bare `Event` with no data.
|
|
322
|
+
*/
|
|
323
|
+
function readServerError(event: any): { error: string; [key: string]: any } | null {
|
|
324
|
+
if (typeof event?.data !== 'string' || event.data === '') return null
|
|
325
|
+
try {
|
|
326
|
+
const data = JSON.parse(event.data)
|
|
327
|
+
const message = data?.error ?? data?.message ?? data?.detail
|
|
328
|
+
return { ...(typeof data === 'object' ? data : {}), error: String(message ?? event.data) }
|
|
329
|
+
} catch {
|
|
330
|
+
return { error: event.data }
|
|
331
|
+
}
|
|
332
|
+
}
|
package/artifacts/config.js
CHANGED
|
@@ -9,9 +9,23 @@ exports.isValidJWT = isValidJWT;
|
|
|
9
9
|
exports.extractTokenFromSDKClient = extractTokenFromSDKClient;
|
|
10
10
|
exports.configureWithJWT = configureWithJWT;
|
|
11
11
|
exports.getEnvironmentConfig = getEnvironmentConfig;
|
|
12
|
+
/**
|
|
13
|
+
* `NEXT_PUBLIC_API_URL`, or undefined where there is no `process` global (a
|
|
14
|
+
* browser bundle that does not inline env vars). The literal
|
|
15
|
+
* `process.env.NEXT_PUBLIC_API_URL` expression is kept so Next.js still
|
|
16
|
+
* inlines it at build time.
|
|
17
|
+
*/
|
|
18
|
+
function publicApiUrl() {
|
|
19
|
+
try {
|
|
20
|
+
return process.env.NEXT_PUBLIC_API_URL;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
12
26
|
// Default configuration
|
|
13
27
|
const defaultConfig = {
|
|
14
|
-
baseUrl:
|
|
28
|
+
baseUrl: publicApiUrl() || 'http://localhost:8000',
|
|
15
29
|
credentials: 'include',
|
|
16
30
|
token: undefined, // Will be set from environment or programmatically
|
|
17
31
|
timeout: 30000,
|
|
@@ -124,21 +138,21 @@ function configureWithJWT(token, config) {
|
|
|
124
138
|
function getEnvironmentConfig(env = 'development') {
|
|
125
139
|
const baseConfigs = {
|
|
126
140
|
production: {
|
|
127
|
-
baseUrl:
|
|
141
|
+
baseUrl: publicApiUrl() || 'https://api.robosystems.ai',
|
|
128
142
|
credentials: 'include',
|
|
129
143
|
timeout: 60000,
|
|
130
144
|
maxRetries: 5,
|
|
131
145
|
retryDelay: 2000,
|
|
132
146
|
},
|
|
133
147
|
staging: {
|
|
134
|
-
baseUrl:
|
|
148
|
+
baseUrl: publicApiUrl() || 'https://staging-api.robosystems.ai',
|
|
135
149
|
credentials: 'include',
|
|
136
150
|
timeout: 45000,
|
|
137
151
|
maxRetries: 3,
|
|
138
152
|
retryDelay: 1500,
|
|
139
153
|
},
|
|
140
154
|
development: {
|
|
141
|
-
baseUrl:
|
|
155
|
+
baseUrl: publicApiUrl() || 'http://localhost:8000',
|
|
142
156
|
credentials: 'include',
|
|
143
157
|
timeout: 30000,
|
|
144
158
|
maxRetries: 3,
|
package/artifacts/config.ts
CHANGED
|
@@ -35,9 +35,23 @@ export interface SDKClientConfig {
|
|
|
35
35
|
retryDelay?: number
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* `NEXT_PUBLIC_API_URL`, or undefined where there is no `process` global (a
|
|
40
|
+
* browser bundle that does not inline env vars). The literal
|
|
41
|
+
* `process.env.NEXT_PUBLIC_API_URL` expression is kept so Next.js still
|
|
42
|
+
* inlines it at build time.
|
|
43
|
+
*/
|
|
44
|
+
function publicApiUrl(): string | undefined {
|
|
45
|
+
try {
|
|
46
|
+
return process.env.NEXT_PUBLIC_API_URL
|
|
47
|
+
} catch {
|
|
48
|
+
return undefined
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
38
52
|
// Default configuration
|
|
39
53
|
const defaultConfig: SDKClientConfig = {
|
|
40
|
-
baseUrl:
|
|
54
|
+
baseUrl: publicApiUrl() || 'http://localhost:8000',
|
|
41
55
|
credentials: 'include',
|
|
42
56
|
token: undefined, // Will be set from environment or programmatically
|
|
43
57
|
timeout: 30000,
|
|
@@ -166,21 +180,21 @@ export function getEnvironmentConfig(
|
|
|
166
180
|
): SDKClientConfig {
|
|
167
181
|
const baseConfigs: Record<string, Partial<SDKClientConfig>> = {
|
|
168
182
|
production: {
|
|
169
|
-
baseUrl:
|
|
183
|
+
baseUrl: publicApiUrl() || 'https://api.robosystems.ai',
|
|
170
184
|
credentials: 'include',
|
|
171
185
|
timeout: 60000,
|
|
172
186
|
maxRetries: 5,
|
|
173
187
|
retryDelay: 2000,
|
|
174
188
|
},
|
|
175
189
|
staging: {
|
|
176
|
-
baseUrl:
|
|
190
|
+
baseUrl: publicApiUrl() || 'https://staging-api.robosystems.ai',
|
|
177
191
|
credentials: 'include',
|
|
178
192
|
timeout: 45000,
|
|
179
193
|
maxRetries: 3,
|
|
180
194
|
retryDelay: 1500,
|
|
181
195
|
},
|
|
182
196
|
development: {
|
|
183
|
-
baseUrl:
|
|
197
|
+
baseUrl: publicApiUrl() || 'http://localhost:8000',
|
|
184
198
|
credentials: 'include',
|
|
185
199
|
timeout: 30000,
|
|
186
200
|
maxRetries: 3,
|
|
@@ -102,6 +102,16 @@ export interface GraphQLClientConfig {
|
|
|
102
102
|
/** Base of the retry backoff, in milliseconds. */
|
|
103
103
|
retryDelay?: number;
|
|
104
104
|
}
|
|
105
|
+
/** The auth header for a credential — see {@link applyAuthHeader} for the rule. */
|
|
106
|
+
export declare function authHeaderFor(token: string): Record<string, string>;
|
|
107
|
+
/**
|
|
108
|
+
* The credential for one request: the `tokenProvider` result when one is
|
|
109
|
+
* configured (`null` means send unauthenticated), otherwise the static
|
|
110
|
+
* `token`. A throwing provider fails the request fast — the caller meant
|
|
111
|
+
* to authenticate, and an unauthenticated request would surface as a
|
|
112
|
+
* confusing 401 far from the real failure (matches the Python client).
|
|
113
|
+
*/
|
|
114
|
+
export declare function resolveCredential(config: Pick<GraphQLClientConfig, 'token' | 'tokenProvider'>): Promise<string | null>;
|
|
105
115
|
/**
|
|
106
116
|
* Build a new GraphQL client for the given graph. Prefer
|
|
107
117
|
* `GraphQLClientCache.get(graphId)` in facade code — this is the raw
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.GraphQLClientCache = exports.GraphQLError = exports.DEFAULT_GRAPHQL_TIMEOUT_MS = void 0;
|
|
5
5
|
exports.toGraphQLError = toGraphQLError;
|
|
6
|
+
exports.authHeaderFor = authHeaderFor;
|
|
7
|
+
exports.resolveCredential = resolveCredential;
|
|
6
8
|
exports.createGraphQLClient = createGraphQLClient;
|
|
7
9
|
/**
|
|
8
10
|
* GraphQL client factory used internally by the facade clients.
|
|
@@ -105,11 +107,33 @@ function createTimeoutFetch(timeoutMs) {
|
|
|
105
107
|
* sending a JWT as `X-API-Key` or an API key as Bearer both 401.
|
|
106
108
|
*/
|
|
107
109
|
function applyAuthHeader(headers, token) {
|
|
108
|
-
|
|
109
|
-
headers.set(
|
|
110
|
+
for (const [name, value] of Object.entries(authHeaderFor(token))) {
|
|
111
|
+
headers.set(name, value);
|
|
110
112
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
}
|
|
114
|
+
/** The auth header for a credential — see {@link applyAuthHeader} for the rule. */
|
|
115
|
+
function authHeaderFor(token) {
|
|
116
|
+
return token.startsWith('rfs') ? { 'X-API-Key': token } : { Authorization: `Bearer ${token}` };
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The credential for one request: the `tokenProvider` result when one is
|
|
120
|
+
* configured (`null` means send unauthenticated), otherwise the static
|
|
121
|
+
* `token`. A throwing provider fails the request fast — the caller meant
|
|
122
|
+
* to authenticate, and an unauthenticated request would surface as a
|
|
123
|
+
* confusing 401 far from the real failure (matches the Python client).
|
|
124
|
+
*/
|
|
125
|
+
async function resolveCredential(config) {
|
|
126
|
+
if (!config.tokenProvider) {
|
|
127
|
+
return config.token ?? null;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
return (await config.tokenProvider()) ?? null;
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
134
|
+
throw new Error(`RoboSystems SDK: tokenProvider threw while resolving the request credential (${detail}). ` +
|
|
135
|
+
'Fix the tokenProvider passed in the client config (or via setSDKClientConfig) so it ' +
|
|
136
|
+
'returns the current token, or null to send an unauthenticated (cookie-based) request.', { cause: err });
|
|
113
137
|
}
|
|
114
138
|
}
|
|
115
139
|
/**
|
|
@@ -143,23 +167,7 @@ function createGraphQLClient(config, graphId) {
|
|
|
143
167
|
credentials: config.credentials,
|
|
144
168
|
fetch: timeoutFetch,
|
|
145
169
|
requestMiddleware: async (request) => {
|
|
146
|
-
|
|
147
|
-
try {
|
|
148
|
-
token = await providerFn();
|
|
149
|
-
}
|
|
150
|
-
catch (err) {
|
|
151
|
-
// Fail fast — a throwing provider means the caller *intended*
|
|
152
|
-
// to authenticate but couldn't produce a credential. Sending
|
|
153
|
-
// the request unauthenticated would surface as a confusing
|
|
154
|
-
// 401 far from the real failure. (Matches the Python client,
|
|
155
|
-
// which raises when its credential is missing.) A provider
|
|
156
|
-
// that deliberately has no credential should return `null`
|
|
157
|
-
// instead — that still sends an unauthenticated request.
|
|
158
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
159
|
-
throw new Error(`RoboSystems SDK: tokenProvider threw while resolving the request credential (${detail}). ` +
|
|
160
|
-
'Fix the tokenProvider passed in the client config (or via setSDKClientConfig) so it ' +
|
|
161
|
-
'returns the current token, or null to send an unauthenticated (cookie-based) request.', { cause: err });
|
|
162
|
-
}
|
|
170
|
+
const token = await resolveCredential({ tokenProvider: providerFn });
|
|
163
171
|
if (!token) {
|
|
164
172
|
return request;
|
|
165
173
|
}
|