@seaverse/dataservice 1.7.0 → 1.8.1
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 +57 -0
- package/dist/index.d.mts +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +62 -15
- package/dist/index.mjs +57 -11
- 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**)
|
|
@@ -137,6 +138,27 @@ Access the extracted ID:
|
|
|
137
138
|
console.log(client.appId); // "app_8e5e867e-user_f4ed2364"
|
|
138
139
|
```
|
|
139
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
|
+
|
|
140
162
|
### Data Tables
|
|
141
163
|
|
|
142
164
|
The SDK provides access to different data tables with different permission scopes:
|
|
@@ -335,6 +357,13 @@ The SDK uses the following priority order for obtaining authentication tokens:
|
|
|
335
357
|
2. **Parent Page Token** - Auto-fetched via PostMessage when in iframe
|
|
336
358
|
3. **No Token** - Client created without authentication (API calls will fail with 401)
|
|
337
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
|
+
|
|
338
367
|
**Production Usage (Auto-fetch from parent):**
|
|
339
368
|
|
|
340
369
|
The SDK automatically fetches the authentication token and service host from the parent page via PostMessage when running in an iframe:
|
|
@@ -369,6 +398,34 @@ debugSetToken('your-test-token');
|
|
|
369
398
|
const client = await createClient({});
|
|
370
399
|
```
|
|
371
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
|
+
|
|
372
429
|
**Graceful Degradation:**
|
|
373
430
|
|
|
374
431
|
If token or serviceHost fetching fails, the SDK gracefully handles the failure:
|
package/dist/index.d.mts
CHANGED
|
@@ -278,6 +278,30 @@ 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
|
*
|
|
@@ -353,6 +377,6 @@ declare function createClient(config?: ClientConfig): Promise<DataServiceClient>
|
|
|
353
377
|
* ```
|
|
354
378
|
*/
|
|
355
379
|
|
|
356
|
-
declare const VERSION = "1.
|
|
380
|
+
declare const VERSION = "1.8.0";
|
|
357
381
|
|
|
358
|
-
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
|
@@ -278,6 +278,30 @@ 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
|
*
|
|
@@ -353,6 +377,6 @@ declare function createClient(config?: ClientConfig): Promise<DataServiceClient>
|
|
|
353
377
|
* ```
|
|
354
378
|
*/
|
|
355
379
|
|
|
356
|
-
declare const VERSION = "1.
|
|
380
|
+
declare const VERSION = "1.8.0";
|
|
357
381
|
|
|
358
|
-
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
|
@@ -18,14 +18,15 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_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
|
-
module.exports = __toCommonJS(
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
30
|
|
|
30
31
|
// src/types.ts
|
|
31
32
|
var DataServiceError = class extends Error {
|
|
@@ -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;
|
|
@@ -125,6 +130,42 @@ async function getServiceHostFromParent(timeout = 500, serviceName = "dataservic
|
|
|
125
130
|
}
|
|
126
131
|
});
|
|
127
132
|
}
|
|
133
|
+
async function getAppIdFromParent(timeout = 500) {
|
|
134
|
+
if (!isInIframe()) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return new Promise((resolve) => {
|
|
138
|
+
const messageHandler = (event) => {
|
|
139
|
+
if (event.data && event.data.type === "seaverse:appId") {
|
|
140
|
+
cleanup();
|
|
141
|
+
const appId = event.data.payload?.appId;
|
|
142
|
+
resolve(appId || null);
|
|
143
|
+
} else if (event.data && event.data.type === "seaverse:error") {
|
|
144
|
+
cleanup();
|
|
145
|
+
console.warn("[SeaVerse DataService SDK] Error getting appId from parent:", event.data.error);
|
|
146
|
+
resolve(null);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const timeoutId = setTimeout(() => {
|
|
150
|
+
cleanup();
|
|
151
|
+
resolve(null);
|
|
152
|
+
}, timeout);
|
|
153
|
+
const cleanup = () => {
|
|
154
|
+
clearTimeout(timeoutId);
|
|
155
|
+
globalThis.window.removeEventListener("message", messageHandler);
|
|
156
|
+
};
|
|
157
|
+
globalThis.window.addEventListener("message", messageHandler);
|
|
158
|
+
try {
|
|
159
|
+
globalThis.window.parent.postMessage(
|
|
160
|
+
{ type: "seaverse:get_appId" },
|
|
161
|
+
"*"
|
|
162
|
+
);
|
|
163
|
+
} catch (e) {
|
|
164
|
+
cleanup();
|
|
165
|
+
resolve(null);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
128
169
|
function extractAppId() {
|
|
129
170
|
if (typeof globalThis !== "undefined" && "location" in globalThis) {
|
|
130
171
|
const location = globalThis.location;
|
|
@@ -283,8 +324,7 @@ var QueryBuilderImpl = class {
|
|
|
283
324
|
}
|
|
284
325
|
order(field, options = {}) {
|
|
285
326
|
let orderStr = field;
|
|
286
|
-
if (options.descending)
|
|
287
|
-
orderStr += ".desc";
|
|
327
|
+
if (options.descending) orderStr += ".desc";
|
|
288
328
|
if (options.nullsFirst !== void 0) {
|
|
289
329
|
orderStr += options.nullsFirst ? ".nullsfirst" : ".nullslast";
|
|
290
330
|
}
|
|
@@ -305,12 +345,9 @@ var QueryBuilderImpl = class {
|
|
|
305
345
|
collection_name: `eq.${this.collectionName}`,
|
|
306
346
|
...Object.fromEntries(this.filters.map((f, i) => [`filter${i}`, f]))
|
|
307
347
|
};
|
|
308
|
-
if (this.ordering)
|
|
309
|
-
|
|
310
|
-
if (this.
|
|
311
|
-
params.limit = String(this.limitValue);
|
|
312
|
-
if (this.offsetValue !== null)
|
|
313
|
-
params.offset = String(this.offsetValue);
|
|
348
|
+
if (this.ordering) params.order = this.ordering;
|
|
349
|
+
if (this.limitValue !== null) params.limit = String(this.limitValue);
|
|
350
|
+
if (this.offsetValue !== null) params.offset = String(this.offsetValue);
|
|
314
351
|
return this.client.get(this.tablePath, params);
|
|
315
352
|
}
|
|
316
353
|
async count() {
|
|
@@ -465,16 +502,26 @@ async function createClient(config = {}) {
|
|
|
465
502
|
serviceHost = null;
|
|
466
503
|
}
|
|
467
504
|
const httpClient = new HTTPClient(config, token || void 0, serviceHost || void 0);
|
|
468
|
-
|
|
505
|
+
let parentAppId = null;
|
|
506
|
+
if (!manualAppId) {
|
|
507
|
+
try {
|
|
508
|
+
parentAppId = await getAppIdFromParent(500);
|
|
509
|
+
} catch (error) {
|
|
510
|
+
console.warn("[SeaVerse DataService SDK] Failed to fetch appId:", error);
|
|
511
|
+
parentAppId = null;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
const appId = manualAppId || parentAppId || extractAppId();
|
|
469
515
|
return new DataServiceClientImpl(httpClient, appId);
|
|
470
516
|
}
|
|
471
517
|
|
|
472
518
|
// src/index.ts
|
|
473
|
-
var VERSION = "1.
|
|
519
|
+
var VERSION = "1.8.0";
|
|
474
520
|
// Annotate the CommonJS export names for ESM import in node:
|
|
475
521
|
0 && (module.exports = {
|
|
476
522
|
DataServiceError,
|
|
477
523
|
VERSION,
|
|
478
524
|
createClient,
|
|
479
|
-
debugSetToken
|
|
525
|
+
debugSetToken,
|
|
526
|
+
setAppId
|
|
480
527
|
});
|
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;
|
|
@@ -96,6 +100,42 @@ async function getServiceHostFromParent(timeout = 500, serviceName = "dataservic
|
|
|
96
100
|
}
|
|
97
101
|
});
|
|
98
102
|
}
|
|
103
|
+
async function getAppIdFromParent(timeout = 500) {
|
|
104
|
+
if (!isInIframe()) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return new Promise((resolve) => {
|
|
108
|
+
const messageHandler = (event) => {
|
|
109
|
+
if (event.data && event.data.type === "seaverse:appId") {
|
|
110
|
+
cleanup();
|
|
111
|
+
const appId = event.data.payload?.appId;
|
|
112
|
+
resolve(appId || null);
|
|
113
|
+
} else if (event.data && event.data.type === "seaverse:error") {
|
|
114
|
+
cleanup();
|
|
115
|
+
console.warn("[SeaVerse DataService SDK] Error getting appId from parent:", event.data.error);
|
|
116
|
+
resolve(null);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const timeoutId = setTimeout(() => {
|
|
120
|
+
cleanup();
|
|
121
|
+
resolve(null);
|
|
122
|
+
}, timeout);
|
|
123
|
+
const cleanup = () => {
|
|
124
|
+
clearTimeout(timeoutId);
|
|
125
|
+
globalThis.window.removeEventListener("message", messageHandler);
|
|
126
|
+
};
|
|
127
|
+
globalThis.window.addEventListener("message", messageHandler);
|
|
128
|
+
try {
|
|
129
|
+
globalThis.window.parent.postMessage(
|
|
130
|
+
{ type: "seaverse:get_appId" },
|
|
131
|
+
"*"
|
|
132
|
+
);
|
|
133
|
+
} catch (e) {
|
|
134
|
+
cleanup();
|
|
135
|
+
resolve(null);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
99
139
|
function extractAppId() {
|
|
100
140
|
if (typeof globalThis !== "undefined" && "location" in globalThis) {
|
|
101
141
|
const location = globalThis.location;
|
|
@@ -254,8 +294,7 @@ var QueryBuilderImpl = class {
|
|
|
254
294
|
}
|
|
255
295
|
order(field, options = {}) {
|
|
256
296
|
let orderStr = field;
|
|
257
|
-
if (options.descending)
|
|
258
|
-
orderStr += ".desc";
|
|
297
|
+
if (options.descending) orderStr += ".desc";
|
|
259
298
|
if (options.nullsFirst !== void 0) {
|
|
260
299
|
orderStr += options.nullsFirst ? ".nullsfirst" : ".nullslast";
|
|
261
300
|
}
|
|
@@ -276,12 +315,9 @@ var QueryBuilderImpl = class {
|
|
|
276
315
|
collection_name: `eq.${this.collectionName}`,
|
|
277
316
|
...Object.fromEntries(this.filters.map((f, i) => [`filter${i}`, f]))
|
|
278
317
|
};
|
|
279
|
-
if (this.ordering)
|
|
280
|
-
|
|
281
|
-
if (this.
|
|
282
|
-
params.limit = String(this.limitValue);
|
|
283
|
-
if (this.offsetValue !== null)
|
|
284
|
-
params.offset = String(this.offsetValue);
|
|
318
|
+
if (this.ordering) params.order = this.ordering;
|
|
319
|
+
if (this.limitValue !== null) params.limit = String(this.limitValue);
|
|
320
|
+
if (this.offsetValue !== null) params.offset = String(this.offsetValue);
|
|
285
321
|
return this.client.get(this.tablePath, params);
|
|
286
322
|
}
|
|
287
323
|
async count() {
|
|
@@ -436,15 +472,25 @@ async function createClient(config = {}) {
|
|
|
436
472
|
serviceHost = null;
|
|
437
473
|
}
|
|
438
474
|
const httpClient = new HTTPClient(config, token || void 0, serviceHost || void 0);
|
|
439
|
-
|
|
475
|
+
let parentAppId = null;
|
|
476
|
+
if (!manualAppId) {
|
|
477
|
+
try {
|
|
478
|
+
parentAppId = await getAppIdFromParent(500);
|
|
479
|
+
} catch (error) {
|
|
480
|
+
console.warn("[SeaVerse DataService SDK] Failed to fetch appId:", error);
|
|
481
|
+
parentAppId = null;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const appId = manualAppId || parentAppId || extractAppId();
|
|
440
485
|
return new DataServiceClientImpl(httpClient, appId);
|
|
441
486
|
}
|
|
442
487
|
|
|
443
488
|
// src/index.ts
|
|
444
|
-
var VERSION = "1.
|
|
489
|
+
var VERSION = "1.8.0";
|
|
445
490
|
export {
|
|
446
491
|
DataServiceError,
|
|
447
492
|
VERSION,
|
|
448
493
|
createClient,
|
|
449
|
-
debugSetToken
|
|
494
|
+
debugSetToken,
|
|
495
|
+
setAppId
|
|
450
496
|
};
|