@seaverse/dataservice 1.6.2 → 1.8.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/README.md +82 -15
- package/dist/index.d.mts +35 -12
- package/dist/index.d.ts +35 -12
- package/dist/index.js +58 -7
- package/dist/index.mjs +56 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ This package uses a **factory pattern** for client creation. Here's what it expo
|
|
|
24
24
|
**Functions:**
|
|
25
25
|
- `createClient(config)` - Factory function to create a client instance (async)
|
|
26
26
|
- `debugSetToken(token)` - Set debug token for testing (call before createClient)
|
|
27
|
+
- `setAppId(appId)` - **[Deprecated]** Set application ID manually (not recommended)
|
|
27
28
|
|
|
28
29
|
**Types (TypeScript):**
|
|
29
30
|
- `DataServiceClient` - Type of the client instance (**not a constructor**)
|
|
@@ -55,10 +56,13 @@ const client = new DataServiceClient(); // This will fail!
|
|
|
55
56
|
|
|
56
57
|
The SDK uses a **factory function pattern**. Always use `createClient()`:
|
|
57
58
|
|
|
59
|
+
**Important:** The SDK automatically obtains the service host from the parent page via PostMessage (500ms timeout). If the fetch fails, it defaults to `https://dataservice-api.seaverse.ai`. No configuration is needed.
|
|
60
|
+
|
|
58
61
|
```typescript
|
|
59
62
|
import { createClient, debugSetToken } from '@seaverse/dataservice';
|
|
60
63
|
|
|
61
|
-
// Production: Auto-fetch token from parent page (when running in iframe)
|
|
64
|
+
// Production: Auto-fetch token and serviceHost from parent page (when running in iframe)
|
|
65
|
+
// Falls back to https://dataservice-api.seaverse.ai if fetch fails
|
|
62
66
|
const client = await createClient({});
|
|
63
67
|
|
|
64
68
|
// Development/Testing: Use debug token
|
|
@@ -134,6 +138,27 @@ Access the extracted ID:
|
|
|
134
138
|
console.log(client.appId); // "app_8e5e867e-user_f4ed2364"
|
|
135
139
|
```
|
|
136
140
|
|
|
141
|
+
**Manual Override (Not Recommended):**
|
|
142
|
+
|
|
143
|
+
For special cases where auto-extraction doesn't work, you can manually set the appId using `setAppId()`:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { setAppId, createClient } from '@seaverse/dataservice';
|
|
147
|
+
|
|
148
|
+
// Set manual appId before creating client (highest priority)
|
|
149
|
+
setAppId('my-custom-app-id');
|
|
150
|
+
|
|
151
|
+
// Client will use manual appId instead of auto-extraction
|
|
152
|
+
const client = await createClient({});
|
|
153
|
+
console.log(client.appId); // "my-custom-app-id"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
⚠️ **Note:** `setAppId()` is deprecated and not recommended for normal use. The SDK automatically extracts appId from the URL, which is the recommended approach. Only use this for special cases where auto-extraction doesn't work.
|
|
157
|
+
|
|
158
|
+
**AppId Priority (from highest to lowest):**
|
|
159
|
+
1. `setAppId()` - Manual override (not recommended)
|
|
160
|
+
2. URL extraction (browser) / Environment variable (Node.js) - Automatic and recommended
|
|
161
|
+
|
|
137
162
|
### Data Tables
|
|
138
163
|
|
|
139
164
|
The SDK provides access to different data tables with different permission scopes:
|
|
@@ -308,7 +333,6 @@ interface DataRecord<T> {
|
|
|
308
333
|
import { createClient } from '@seaverse/dataservice';
|
|
309
334
|
|
|
310
335
|
const client = await createClient({
|
|
311
|
-
url?: string; // PostgREST API URL (default: https://dataservice-api.seaverse.ai)
|
|
312
336
|
options?: {
|
|
313
337
|
timeout?: number; // Request timeout in ms (default: 30000)
|
|
314
338
|
tokenFetchTimeout?: number; // Token fetch timeout in ms (default: 5000)
|
|
@@ -317,6 +341,14 @@ const client = await createClient({
|
|
|
317
341
|
});
|
|
318
342
|
```
|
|
319
343
|
|
|
344
|
+
**ServiceHost Auto-Detection:**
|
|
345
|
+
|
|
346
|
+
The SDK automatically fetches the service host from the parent page via PostMessage:
|
|
347
|
+
- **Timeout**: 500ms (hardcoded, not configurable)
|
|
348
|
+
- **Protocol**: Sends `{ type: 'seaverse:get_service_host', payload: { serviceName: 'dataservice' } }`
|
|
349
|
+
- **Fallback**: If fetch fails, defaults to `https://dataservice-api.seaverse.ai`
|
|
350
|
+
- **No user configuration**: ServiceHost is managed internally to prevent request failures
|
|
351
|
+
|
|
320
352
|
**Token Authentication Priority:**
|
|
321
353
|
|
|
322
354
|
The SDK uses the following priority order for obtaining authentication tokens:
|
|
@@ -325,18 +357,31 @@ The SDK uses the following priority order for obtaining authentication tokens:
|
|
|
325
357
|
2. **Parent Page Token** - Auto-fetched via PostMessage when in iframe
|
|
326
358
|
3. **No Token** - Client created without authentication (API calls will fail with 401)
|
|
327
359
|
|
|
360
|
+
**AppId Configuration Priority:**
|
|
361
|
+
|
|
362
|
+
The SDK uses the following priority order for determining the application ID:
|
|
363
|
+
|
|
364
|
+
1. **Manual AppId (Highest Priority)** - Set via `setAppId()` (not recommended, deprecated)
|
|
365
|
+
2. **Auto-extraction** - Extracted from URL (browser) or environment variable (Node.js)
|
|
366
|
+
|
|
328
367
|
**Production Usage (Auto-fetch from parent):**
|
|
329
368
|
|
|
330
|
-
The SDK automatically fetches the authentication token from the parent page via PostMessage when running in an iframe:
|
|
369
|
+
The SDK automatically fetches the authentication token and service host from the parent page via PostMessage when running in an iframe:
|
|
331
370
|
|
|
332
371
|
```typescript
|
|
333
|
-
// No
|
|
372
|
+
// No configuration needed - auto-fetches token and serviceHost from parent
|
|
334
373
|
const client = await createClient({});
|
|
335
374
|
|
|
336
375
|
// Parent page should respond to PostMessage:
|
|
376
|
+
// Token request:
|
|
337
377
|
// Send: { type: 'seaverse:get_token' }
|
|
338
378
|
// Receive: { type: 'seaverse:token', payload: { accessToken: string, expiresIn: number } }
|
|
339
379
|
// Error: { type: 'seaverse:error', error: string }
|
|
380
|
+
|
|
381
|
+
// ServiceHost request:
|
|
382
|
+
// Send: { type: 'seaverse:get_service_host', payload: { serviceName: 'dataservice' } }
|
|
383
|
+
// Receive: { type: 'seaverse:service_host', payload: { serviceHost: string } }
|
|
384
|
+
// Error: { type: 'seaverse:error', error: string }
|
|
340
385
|
```
|
|
341
386
|
|
|
342
387
|
**Development/Testing (Debug Token):**
|
|
@@ -353,12 +398,43 @@ debugSetToken('your-test-token');
|
|
|
353
398
|
const client = await createClient({});
|
|
354
399
|
```
|
|
355
400
|
|
|
401
|
+
**Manual AppId Override (Not Recommended):**
|
|
402
|
+
|
|
403
|
+
For special cases, you can force a specific appId using `setAppId()`, which will override auto-extraction:
|
|
404
|
+
|
|
405
|
+
```typescript
|
|
406
|
+
import { setAppId, createClient } from '@seaverse/dataservice';
|
|
407
|
+
|
|
408
|
+
// Current URL: https://app_auto_extracted.app.seaverse.ai
|
|
409
|
+
// Without setAppId, client.appId would be "app_auto_extracted"
|
|
410
|
+
|
|
411
|
+
// Force specific appId (overrides auto-extraction)
|
|
412
|
+
setAppId('my-custom-app-id');
|
|
413
|
+
|
|
414
|
+
const client = await createClient({});
|
|
415
|
+
console.log(client.appId); // "my-custom-app-id" (not "app_auto_extracted")
|
|
416
|
+
|
|
417
|
+
// All operations will use the manual appId
|
|
418
|
+
await client.userData.collection('test').insert({ foo: 'bar' });
|
|
419
|
+
// ↑ This record will be stored with app_id = "my-custom-app-id"
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
⚠️ **Warning:** `setAppId()` is deprecated. Only use it when:
|
|
423
|
+
- Testing in environments where URL-based extraction doesn't work
|
|
424
|
+
- Debugging issues with specific appIds
|
|
425
|
+
- Working around temporary limitations
|
|
426
|
+
|
|
427
|
+
In production, always rely on automatic appId extraction from the URL.
|
|
428
|
+
|
|
356
429
|
**Graceful Degradation:**
|
|
357
430
|
|
|
358
|
-
If token fetching fails
|
|
431
|
+
If token or serviceHost fetching fails, the SDK gracefully handles the failure:
|
|
432
|
+
|
|
433
|
+
- **Token fetch failure**: Creates client without authentication (API calls will return 401)
|
|
434
|
+
- **ServiceHost fetch failure**: Falls back to default `https://dataservice-api.seaverse.ai`
|
|
359
435
|
|
|
360
436
|
```typescript
|
|
361
|
-
// Client creation never throws, even if
|
|
437
|
+
// Client creation never throws, even if fetch fails
|
|
362
438
|
const client = await createClient({});
|
|
363
439
|
|
|
364
440
|
try {
|
|
@@ -372,15 +448,6 @@ try {
|
|
|
372
448
|
}
|
|
373
449
|
```
|
|
374
450
|
|
|
375
|
-
**Custom Endpoint:**
|
|
376
|
-
|
|
377
|
-
```typescript
|
|
378
|
-
// Use custom API endpoint
|
|
379
|
-
const client = await createClient({
|
|
380
|
-
url: 'https://your-custom-api.example.com',
|
|
381
|
-
});
|
|
382
|
-
```
|
|
383
|
-
|
|
384
451
|
### DataTable Methods
|
|
385
452
|
|
|
386
453
|
```typescript
|
package/dist/index.d.mts
CHANGED
|
@@ -12,13 +12,13 @@
|
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
|
14
14
|
* const config: ClientConfig = {
|
|
15
|
-
*
|
|
15
|
+
* options: {
|
|
16
|
+
* timeout: 10000,
|
|
17
|
+
* }
|
|
16
18
|
* };
|
|
17
19
|
* ```
|
|
18
20
|
*/
|
|
19
21
|
interface ClientConfig {
|
|
20
|
-
/** PostgREST API base URL (default: https://dataservice-api.seaverse.ai) */
|
|
21
|
-
url?: string;
|
|
22
22
|
/** Optional configuration */
|
|
23
23
|
options?: {
|
|
24
24
|
/** Custom fetch implementation (useful for Node.js < 18) */
|
|
@@ -278,24 +278,47 @@ interface DataServiceClient {
|
|
|
278
278
|
* ```
|
|
279
279
|
*/
|
|
280
280
|
declare function debugSetToken(token: string): void;
|
|
281
|
+
/**
|
|
282
|
+
* Set application ID manually (not recommended)
|
|
283
|
+
*
|
|
284
|
+
* @deprecated This function is not recommended for normal use.
|
|
285
|
+
* The SDK automatically extracts appId from the URL, which is the recommended approach.
|
|
286
|
+
* Only use this for special cases where auto-extraction doesn't work.
|
|
287
|
+
*
|
|
288
|
+
* When set, this appId will take highest priority over auto-extracted appId.
|
|
289
|
+
*
|
|
290
|
+
* @param appId The application ID to use
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* import { setAppId, createClient } from '@seaverse/dataservice';
|
|
295
|
+
*
|
|
296
|
+
* // Set manual appId before creating client (highest priority)
|
|
297
|
+
* setAppId('my-custom-app-id');
|
|
298
|
+
*
|
|
299
|
+
* // Client will use manual appId instead of auto-extraction
|
|
300
|
+
* const client = await createClient({});
|
|
301
|
+
* console.log(client.appId); // "my-custom-app-id"
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
declare function setAppId(appId: string): void;
|
|
281
305
|
/**
|
|
282
306
|
* Create a new Data Service client
|
|
283
307
|
*
|
|
308
|
+
* The SDK automatically obtains the service host from the parent page via PostMessage (500ms timeout).
|
|
309
|
+
* If the fetch fails, it falls back to the default host: https://dataservice-api.seaverse.ai
|
|
310
|
+
*
|
|
284
311
|
* AI-friendly: Single entry point with clear configuration
|
|
285
312
|
*
|
|
286
|
-
* @param config - Client configuration
|
|
313
|
+
* @param config - Client configuration (optional)
|
|
287
314
|
* @returns DataServiceClient instance
|
|
288
315
|
*
|
|
289
316
|
* @example
|
|
290
317
|
* ```typescript
|
|
291
|
-
* // Auto-fetch token from parent page (iframe)
|
|
318
|
+
* // Auto-fetch token and serviceHost from parent page (iframe)
|
|
319
|
+
* // ServiceHost fetch: 500ms timeout, falls back to https://dataservice-api.seaverse.ai
|
|
292
320
|
* const client = await createClient({});
|
|
293
321
|
*
|
|
294
|
-
* // Custom endpoint
|
|
295
|
-
* const client = await createClient({
|
|
296
|
-
* url: 'https://your-postgrest-api.example.com',
|
|
297
|
-
* });
|
|
298
|
-
*
|
|
299
322
|
* // For development/testing, use debugSetToken
|
|
300
323
|
* import { debugSetToken, createClient } from '@seaverse/dataservice';
|
|
301
324
|
* debugSetToken('your-test-token');
|
|
@@ -354,6 +377,6 @@ declare function createClient(config?: ClientConfig): Promise<DataServiceClient>
|
|
|
354
377
|
* ```
|
|
355
378
|
*/
|
|
356
379
|
|
|
357
|
-
declare const VERSION = "1.
|
|
380
|
+
declare const VERSION = "1.8.0";
|
|
358
381
|
|
|
359
|
-
export { type APIError, type ClientConfig, type Collection, type DataRecord, type DataServiceClient, DataServiceError, type DataTable, type OrderOptions, type QueryBuilder, type QueryFilter, type UserDataStats, VERSION, createClient, debugSetToken };
|
|
382
|
+
export { type APIError, type ClientConfig, type Collection, type DataRecord, type DataServiceClient, DataServiceError, type DataTable, type OrderOptions, type QueryBuilder, type QueryFilter, type UserDataStats, VERSION, createClient, debugSetToken, setAppId };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,13 +12,13 @@
|
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
|
14
14
|
* const config: ClientConfig = {
|
|
15
|
-
*
|
|
15
|
+
* options: {
|
|
16
|
+
* timeout: 10000,
|
|
17
|
+
* }
|
|
16
18
|
* };
|
|
17
19
|
* ```
|
|
18
20
|
*/
|
|
19
21
|
interface ClientConfig {
|
|
20
|
-
/** PostgREST API base URL (default: https://dataservice-api.seaverse.ai) */
|
|
21
|
-
url?: string;
|
|
22
22
|
/** Optional configuration */
|
|
23
23
|
options?: {
|
|
24
24
|
/** Custom fetch implementation (useful for Node.js < 18) */
|
|
@@ -278,24 +278,47 @@ interface DataServiceClient {
|
|
|
278
278
|
* ```
|
|
279
279
|
*/
|
|
280
280
|
declare function debugSetToken(token: string): void;
|
|
281
|
+
/**
|
|
282
|
+
* Set application ID manually (not recommended)
|
|
283
|
+
*
|
|
284
|
+
* @deprecated This function is not recommended for normal use.
|
|
285
|
+
* The SDK automatically extracts appId from the URL, which is the recommended approach.
|
|
286
|
+
* Only use this for special cases where auto-extraction doesn't work.
|
|
287
|
+
*
|
|
288
|
+
* When set, this appId will take highest priority over auto-extracted appId.
|
|
289
|
+
*
|
|
290
|
+
* @param appId The application ID to use
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```typescript
|
|
294
|
+
* import { setAppId, createClient } from '@seaverse/dataservice';
|
|
295
|
+
*
|
|
296
|
+
* // Set manual appId before creating client (highest priority)
|
|
297
|
+
* setAppId('my-custom-app-id');
|
|
298
|
+
*
|
|
299
|
+
* // Client will use manual appId instead of auto-extraction
|
|
300
|
+
* const client = await createClient({});
|
|
301
|
+
* console.log(client.appId); // "my-custom-app-id"
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
declare function setAppId(appId: string): void;
|
|
281
305
|
/**
|
|
282
306
|
* Create a new Data Service client
|
|
283
307
|
*
|
|
308
|
+
* The SDK automatically obtains the service host from the parent page via PostMessage (500ms timeout).
|
|
309
|
+
* If the fetch fails, it falls back to the default host: https://dataservice-api.seaverse.ai
|
|
310
|
+
*
|
|
284
311
|
* AI-friendly: Single entry point with clear configuration
|
|
285
312
|
*
|
|
286
|
-
* @param config - Client configuration
|
|
313
|
+
* @param config - Client configuration (optional)
|
|
287
314
|
* @returns DataServiceClient instance
|
|
288
315
|
*
|
|
289
316
|
* @example
|
|
290
317
|
* ```typescript
|
|
291
|
-
* // Auto-fetch token from parent page (iframe)
|
|
318
|
+
* // Auto-fetch token and serviceHost from parent page (iframe)
|
|
319
|
+
* // ServiceHost fetch: 500ms timeout, falls back to https://dataservice-api.seaverse.ai
|
|
292
320
|
* const client = await createClient({});
|
|
293
321
|
*
|
|
294
|
-
* // Custom endpoint
|
|
295
|
-
* const client = await createClient({
|
|
296
|
-
* url: 'https://your-postgrest-api.example.com',
|
|
297
|
-
* });
|
|
298
|
-
*
|
|
299
322
|
* // For development/testing, use debugSetToken
|
|
300
323
|
* import { debugSetToken, createClient } from '@seaverse/dataservice';
|
|
301
324
|
* debugSetToken('your-test-token');
|
|
@@ -354,6 +377,6 @@ declare function createClient(config?: ClientConfig): Promise<DataServiceClient>
|
|
|
354
377
|
* ```
|
|
355
378
|
*/
|
|
356
379
|
|
|
357
|
-
declare const VERSION = "1.
|
|
380
|
+
declare const VERSION = "1.8.0";
|
|
358
381
|
|
|
359
|
-
export { type APIError, type ClientConfig, type Collection, type DataRecord, type DataServiceClient, DataServiceError, type DataTable, type OrderOptions, type QueryBuilder, type QueryFilter, type UserDataStats, VERSION, createClient, debugSetToken };
|
|
382
|
+
export { type APIError, type ClientConfig, type Collection, type DataRecord, type DataServiceClient, DataServiceError, type DataTable, type OrderOptions, type QueryBuilder, type QueryFilter, type UserDataStats, VERSION, createClient, debugSetToken, setAppId };
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,8 @@ __export(src_exports, {
|
|
|
23
23
|
DataServiceError: () => DataServiceError,
|
|
24
24
|
VERSION: () => VERSION,
|
|
25
25
|
createClient: () => createClient,
|
|
26
|
-
debugSetToken: () => debugSetToken
|
|
26
|
+
debugSetToken: () => debugSetToken,
|
|
27
|
+
setAppId: () => setAppId
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(src_exports);
|
|
29
30
|
|
|
@@ -41,9 +42,13 @@ var DataServiceError = class extends Error {
|
|
|
41
42
|
|
|
42
43
|
// src/client.ts
|
|
43
44
|
var debugToken = null;
|
|
45
|
+
var manualAppId = null;
|
|
44
46
|
function debugSetToken(token) {
|
|
45
47
|
debugToken = token;
|
|
46
48
|
}
|
|
49
|
+
function setAppId(appId) {
|
|
50
|
+
manualAppId = appId;
|
|
51
|
+
}
|
|
47
52
|
function isInIframe() {
|
|
48
53
|
try {
|
|
49
54
|
return typeof globalThis !== "undefined" && "window" in globalThis && globalThis.window.self !== globalThis.window.top;
|
|
@@ -88,6 +93,43 @@ async function getTokenFromParent(timeout = 5e3) {
|
|
|
88
93
|
}
|
|
89
94
|
});
|
|
90
95
|
}
|
|
96
|
+
async function getServiceHostFromParent(timeout = 500, serviceName = "dataservice") {
|
|
97
|
+
if (!isInIframe()) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
const messageHandler = (event) => {
|
|
102
|
+
if (event.data && event.data.type === "seaverse:service_host") {
|
|
103
|
+
cleanup();
|
|
104
|
+
const serviceHost = event.data.payload?.serviceHost;
|
|
105
|
+
resolve(serviceHost || null);
|
|
106
|
+
} else if (event.data && event.data.type === "seaverse:error") {
|
|
107
|
+
cleanup();
|
|
108
|
+
console.warn("[SeaVerse DataService SDK] Error getting serviceHost from parent:", event.data.error);
|
|
109
|
+
resolve(null);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const timeoutId = setTimeout(() => {
|
|
113
|
+
cleanup();
|
|
114
|
+
resolve(null);
|
|
115
|
+
}, timeout);
|
|
116
|
+
const cleanup = () => {
|
|
117
|
+
clearTimeout(timeoutId);
|
|
118
|
+
globalThis.window.removeEventListener("message", messageHandler);
|
|
119
|
+
};
|
|
120
|
+
globalThis.window.addEventListener("message", messageHandler);
|
|
121
|
+
try {
|
|
122
|
+
globalThis.window.parent.postMessage(
|
|
123
|
+
{ type: "seaverse:get_service_host", payload: { serviceName } },
|
|
124
|
+
"*"
|
|
125
|
+
// Allow any origin, supports cross-domain scenarios
|
|
126
|
+
);
|
|
127
|
+
} catch (e) {
|
|
128
|
+
cleanup();
|
|
129
|
+
resolve(null);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
91
133
|
function extractAppId() {
|
|
92
134
|
if (typeof globalThis !== "undefined" && "location" in globalThis) {
|
|
93
135
|
const location = globalThis.location;
|
|
@@ -106,8 +148,9 @@ var HTTPClient = class {
|
|
|
106
148
|
fetchFn;
|
|
107
149
|
timeout;
|
|
108
150
|
tokenPromise = null;
|
|
109
|
-
constructor(config, token) {
|
|
110
|
-
|
|
151
|
+
constructor(config, token, serviceHost) {
|
|
152
|
+
const defaultHost = "https://dataservice-api.seaverse.ai";
|
|
153
|
+
this.baseUrl = (serviceHost || defaultHost).replace(/\/$/, "");
|
|
111
154
|
const customFetch = config.options?.fetch;
|
|
112
155
|
this.fetchFn = customFetch ? customFetch.bind(customFetch) : globalThis.fetch.bind(globalThis);
|
|
113
156
|
this.timeout = config.options?.timeout || 3e4;
|
|
@@ -408,6 +451,7 @@ var DataServiceClientImpl = class {
|
|
|
408
451
|
};
|
|
409
452
|
async function createClient(config = {}) {
|
|
410
453
|
let token = null;
|
|
454
|
+
let serviceHost = null;
|
|
411
455
|
if (debugToken) {
|
|
412
456
|
token = debugToken;
|
|
413
457
|
} else {
|
|
@@ -419,17 +463,24 @@ async function createClient(config = {}) {
|
|
|
419
463
|
token = null;
|
|
420
464
|
}
|
|
421
465
|
}
|
|
422
|
-
|
|
423
|
-
|
|
466
|
+
try {
|
|
467
|
+
serviceHost = await getServiceHostFromParent(500, "dataservice");
|
|
468
|
+
} catch (error) {
|
|
469
|
+
console.warn("[SeaVerse DataService SDK] Failed to fetch serviceHost:", error);
|
|
470
|
+
serviceHost = null;
|
|
471
|
+
}
|
|
472
|
+
const httpClient = new HTTPClient(config, token || void 0, serviceHost || void 0);
|
|
473
|
+
const appId = manualAppId || extractAppId();
|
|
424
474
|
return new DataServiceClientImpl(httpClient, appId);
|
|
425
475
|
}
|
|
426
476
|
|
|
427
477
|
// src/index.ts
|
|
428
|
-
var VERSION = "1.
|
|
478
|
+
var VERSION = "1.8.0";
|
|
429
479
|
// Annotate the CommonJS export names for ESM import in node:
|
|
430
480
|
0 && (module.exports = {
|
|
431
481
|
DataServiceError,
|
|
432
482
|
VERSION,
|
|
433
483
|
createClient,
|
|
434
|
-
debugSetToken
|
|
484
|
+
debugSetToken,
|
|
485
|
+
setAppId
|
|
435
486
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -12,9 +12,13 @@ var DataServiceError = class extends Error {
|
|
|
12
12
|
|
|
13
13
|
// src/client.ts
|
|
14
14
|
var debugToken = null;
|
|
15
|
+
var manualAppId = null;
|
|
15
16
|
function debugSetToken(token) {
|
|
16
17
|
debugToken = token;
|
|
17
18
|
}
|
|
19
|
+
function setAppId(appId) {
|
|
20
|
+
manualAppId = appId;
|
|
21
|
+
}
|
|
18
22
|
function isInIframe() {
|
|
19
23
|
try {
|
|
20
24
|
return typeof globalThis !== "undefined" && "window" in globalThis && globalThis.window.self !== globalThis.window.top;
|
|
@@ -59,6 +63,43 @@ async function getTokenFromParent(timeout = 5e3) {
|
|
|
59
63
|
}
|
|
60
64
|
});
|
|
61
65
|
}
|
|
66
|
+
async function getServiceHostFromParent(timeout = 500, serviceName = "dataservice") {
|
|
67
|
+
if (!isInIframe()) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
const messageHandler = (event) => {
|
|
72
|
+
if (event.data && event.data.type === "seaverse:service_host") {
|
|
73
|
+
cleanup();
|
|
74
|
+
const serviceHost = event.data.payload?.serviceHost;
|
|
75
|
+
resolve(serviceHost || null);
|
|
76
|
+
} else if (event.data && event.data.type === "seaverse:error") {
|
|
77
|
+
cleanup();
|
|
78
|
+
console.warn("[SeaVerse DataService SDK] Error getting serviceHost from parent:", event.data.error);
|
|
79
|
+
resolve(null);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const timeoutId = setTimeout(() => {
|
|
83
|
+
cleanup();
|
|
84
|
+
resolve(null);
|
|
85
|
+
}, timeout);
|
|
86
|
+
const cleanup = () => {
|
|
87
|
+
clearTimeout(timeoutId);
|
|
88
|
+
globalThis.window.removeEventListener("message", messageHandler);
|
|
89
|
+
};
|
|
90
|
+
globalThis.window.addEventListener("message", messageHandler);
|
|
91
|
+
try {
|
|
92
|
+
globalThis.window.parent.postMessage(
|
|
93
|
+
{ type: "seaverse:get_service_host", payload: { serviceName } },
|
|
94
|
+
"*"
|
|
95
|
+
// Allow any origin, supports cross-domain scenarios
|
|
96
|
+
);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
cleanup();
|
|
99
|
+
resolve(null);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
62
103
|
function extractAppId() {
|
|
63
104
|
if (typeof globalThis !== "undefined" && "location" in globalThis) {
|
|
64
105
|
const location = globalThis.location;
|
|
@@ -77,8 +118,9 @@ var HTTPClient = class {
|
|
|
77
118
|
fetchFn;
|
|
78
119
|
timeout;
|
|
79
120
|
tokenPromise = null;
|
|
80
|
-
constructor(config, token) {
|
|
81
|
-
|
|
121
|
+
constructor(config, token, serviceHost) {
|
|
122
|
+
const defaultHost = "https://dataservice-api.seaverse.ai";
|
|
123
|
+
this.baseUrl = (serviceHost || defaultHost).replace(/\/$/, "");
|
|
82
124
|
const customFetch = config.options?.fetch;
|
|
83
125
|
this.fetchFn = customFetch ? customFetch.bind(customFetch) : globalThis.fetch.bind(globalThis);
|
|
84
126
|
this.timeout = config.options?.timeout || 3e4;
|
|
@@ -379,6 +421,7 @@ var DataServiceClientImpl = class {
|
|
|
379
421
|
};
|
|
380
422
|
async function createClient(config = {}) {
|
|
381
423
|
let token = null;
|
|
424
|
+
let serviceHost = null;
|
|
382
425
|
if (debugToken) {
|
|
383
426
|
token = debugToken;
|
|
384
427
|
} else {
|
|
@@ -390,16 +433,23 @@ async function createClient(config = {}) {
|
|
|
390
433
|
token = null;
|
|
391
434
|
}
|
|
392
435
|
}
|
|
393
|
-
|
|
394
|
-
|
|
436
|
+
try {
|
|
437
|
+
serviceHost = await getServiceHostFromParent(500, "dataservice");
|
|
438
|
+
} catch (error) {
|
|
439
|
+
console.warn("[SeaVerse DataService SDK] Failed to fetch serviceHost:", error);
|
|
440
|
+
serviceHost = null;
|
|
441
|
+
}
|
|
442
|
+
const httpClient = new HTTPClient(config, token || void 0, serviceHost || void 0);
|
|
443
|
+
const appId = manualAppId || extractAppId();
|
|
395
444
|
return new DataServiceClientImpl(httpClient, appId);
|
|
396
445
|
}
|
|
397
446
|
|
|
398
447
|
// src/index.ts
|
|
399
|
-
var VERSION = "1.
|
|
448
|
+
var VERSION = "1.8.0";
|
|
400
449
|
export {
|
|
401
450
|
DataServiceError,
|
|
402
451
|
VERSION,
|
|
403
452
|
createClient,
|
|
404
|
-
debugSetToken
|
|
453
|
+
debugSetToken,
|
|
454
|
+
setAppId
|
|
405
455
|
};
|