@seaverse/data-sdk 0.1.1 → 0.1.3
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 +304 -111
- package/dist/index.cjs +429 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +247 -1
- package/dist/index.js +414 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -260,6 +260,38 @@ declare const ENDPOINTS: {
|
|
|
260
260
|
APP_DATA: string;
|
|
261
261
|
APP_ADMINS: string;
|
|
262
262
|
};
|
|
263
|
+
/**
|
|
264
|
+
* 自动获取 API Token
|
|
265
|
+
*
|
|
266
|
+
* 按照以下优先级获取 token:
|
|
267
|
+
* 1. 传入的 token 参数
|
|
268
|
+
* 2. localStorage 中的 token(auth-sdk 标准 key)
|
|
269
|
+
* 3. localStorage 中的 auth_token(向后兼容)
|
|
270
|
+
* 4. PostMessage 从父窗口获取(iframe 场景)
|
|
271
|
+
* 5. 环境变量 DATA_SDK_TOKEN
|
|
272
|
+
*
|
|
273
|
+
* @param providedToken - 可选的直接提供的 token
|
|
274
|
+
* @returns token 字符串
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* // 通常由 auth-sdk 的 login() 方法设置
|
|
279
|
+
* // localStorage.setItem('token', loginResult.token);
|
|
280
|
+
*
|
|
281
|
+
* // data-sdk 自动获取
|
|
282
|
+
* const token = await getApiToken();
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
declare function getApiToken(providedToken?: string): Promise<string>;
|
|
286
|
+
/**
|
|
287
|
+
* 获取 App ID
|
|
288
|
+
*
|
|
289
|
+
* appId 必须由用户明确提供,不会自动从其他地方获取
|
|
290
|
+
*
|
|
291
|
+
* @param providedAppId - 必需的 appId 参数
|
|
292
|
+
* @returns appId 字符串
|
|
293
|
+
*/
|
|
294
|
+
declare function getAppId(providedAppId?: string): Promise<string>;
|
|
263
295
|
|
|
264
296
|
/**
|
|
265
297
|
* Data SDK Client
|
|
@@ -538,5 +570,219 @@ declare class DataClient {
|
|
|
538
570
|
getUserId(): string;
|
|
539
571
|
}
|
|
540
572
|
|
|
541
|
-
|
|
573
|
+
/**
|
|
574
|
+
* Global Configuration for Data SDK
|
|
575
|
+
* 全局配置模块 - 支持无缝接入
|
|
576
|
+
*/
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* 初始化 Data SDK(可选)
|
|
580
|
+
*
|
|
581
|
+
* 可以在应用启动时调用,也可以完全不调用(SDK会自动从localStorage获取配置)
|
|
582
|
+
*
|
|
583
|
+
* @param options - 初始化选项(所有参数都是可选的)
|
|
584
|
+
*
|
|
585
|
+
* @example
|
|
586
|
+
* ```typescript
|
|
587
|
+
* // 方式1: 显式初始化(推荐用于需要自定义配置的场景)
|
|
588
|
+
* await initData({
|
|
589
|
+
* appId: 'my-app-123',
|
|
590
|
+
* token: 'user-jwt-token',
|
|
591
|
+
* debug: true,
|
|
592
|
+
* });
|
|
593
|
+
*
|
|
594
|
+
* // 方式2: 不初始化,直接使用(推荐用于SeaVerse平台应用)
|
|
595
|
+
* // SDK 会自动从 localStorage 读取 auth_token 和 app_id
|
|
596
|
+
* const notes = await query({ table: 'notes' });
|
|
597
|
+
* ```
|
|
598
|
+
*/
|
|
599
|
+
declare function initData(options?: Partial<DataClientOptions>): Promise<void>;
|
|
600
|
+
/**
|
|
601
|
+
* 获取或创建 DataClient 实例
|
|
602
|
+
*
|
|
603
|
+
* 如果已经初始化,返回已有实例;否则自动创建新实例
|
|
604
|
+
*
|
|
605
|
+
* @returns DataClient 实例
|
|
606
|
+
* @throws 如果无法获取必需的配置
|
|
607
|
+
*/
|
|
608
|
+
declare function getClient(): Promise<DataClient>;
|
|
609
|
+
/**
|
|
610
|
+
* 更新全局 token(用于 token 刷新场景)
|
|
611
|
+
*
|
|
612
|
+
* @param token - 新的 JWT token
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```typescript
|
|
616
|
+
* // Token 刷新后更新
|
|
617
|
+
* updateToken('new-jwt-token');
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
declare function updateToken(token: string): void;
|
|
621
|
+
/**
|
|
622
|
+
* 更新全局 appId(用于切换应用场景)
|
|
623
|
+
*
|
|
624
|
+
* @param appId - 新的应用 ID
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```typescript
|
|
628
|
+
* // 切换到其他应用
|
|
629
|
+
* updateAppId('another-app-id');
|
|
630
|
+
* ```
|
|
631
|
+
*/
|
|
632
|
+
declare function updateAppId(appId: string): void;
|
|
633
|
+
/**
|
|
634
|
+
* 获取当前配置的 appId
|
|
635
|
+
*
|
|
636
|
+
* @returns 当前的 appId 或 undefined
|
|
637
|
+
*/
|
|
638
|
+
declare function getCurrentAppId(): string | undefined;
|
|
639
|
+
/**
|
|
640
|
+
* 清除全局配置(用于登出场景)
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* ```typescript
|
|
644
|
+
* // 用户登出时清除配置
|
|
645
|
+
* clearConfig();
|
|
646
|
+
* ```
|
|
647
|
+
*/
|
|
648
|
+
declare function clearConfig(): void;
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Convenient Function-based APIs
|
|
652
|
+
* 便捷的函数式 API 封装 - 无需创建 client 实例
|
|
653
|
+
*/
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* 查询数据列表
|
|
657
|
+
*
|
|
658
|
+
* 便捷函数 - 无需创建 client 实例
|
|
659
|
+
*
|
|
660
|
+
* @param options - 查询选项
|
|
661
|
+
* @returns 数据记录数组
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* import { query } from '@seaverse/data-sdk';
|
|
666
|
+
*
|
|
667
|
+
* const notes = await query({
|
|
668
|
+
* table: 'notes',
|
|
669
|
+
* filters: { category: 'work' },
|
|
670
|
+
* order: { field: 'created_at', direction: 'desc' },
|
|
671
|
+
* });
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
declare function query(options: QueryOptions): Promise<DataRecord[]>;
|
|
675
|
+
/**
|
|
676
|
+
* 获取单条数据
|
|
677
|
+
*
|
|
678
|
+
* @param id - 数据 ID
|
|
679
|
+
* @returns 数据记录或 null
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* ```typescript
|
|
683
|
+
* import { get } from '@seaverse/data-sdk';
|
|
684
|
+
*
|
|
685
|
+
* const note = await get('note-id-123');
|
|
686
|
+
* ```
|
|
687
|
+
*/
|
|
688
|
+
declare function get(id: string): Promise<DataRecord | null>;
|
|
689
|
+
/**
|
|
690
|
+
* 创建数据
|
|
691
|
+
*
|
|
692
|
+
* @param options - 创建选项
|
|
693
|
+
* @returns 创建的数据记录
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```typescript
|
|
697
|
+
* import { create } from '@seaverse/data-sdk';
|
|
698
|
+
*
|
|
699
|
+
* const newNote = await create({
|
|
700
|
+
* table: 'notes',
|
|
701
|
+
* data: { title: 'My Note', content: '...' },
|
|
702
|
+
* visibility: 'private',
|
|
703
|
+
* });
|
|
704
|
+
* ```
|
|
705
|
+
*/
|
|
706
|
+
declare function create(options: CreateDataOptions): Promise<DataRecord>;
|
|
707
|
+
/**
|
|
708
|
+
* 更新数据
|
|
709
|
+
*
|
|
710
|
+
* @param id - 数据 ID
|
|
711
|
+
* @param options - 更新选项
|
|
712
|
+
* @returns 更新后的数据记录
|
|
713
|
+
*
|
|
714
|
+
* @example
|
|
715
|
+
* ```typescript
|
|
716
|
+
* import { update } from '@seaverse/data-sdk';
|
|
717
|
+
*
|
|
718
|
+
* const updated = await update('note-id-123', {
|
|
719
|
+
* data: { title: 'Updated Title' },
|
|
720
|
+
* });
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
declare function update(id: string, options: UpdateDataOptions): Promise<DataRecord>;
|
|
724
|
+
/**
|
|
725
|
+
* 删除数据
|
|
726
|
+
*
|
|
727
|
+
* @param id - 数据 ID
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
* ```typescript
|
|
731
|
+
* import { deleteData } from '@seaverse/data-sdk';
|
|
732
|
+
*
|
|
733
|
+
* await deleteData('note-id-123');
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
declare function deleteData(id: string): Promise<void>;
|
|
737
|
+
/**
|
|
738
|
+
* 批量删除数据
|
|
739
|
+
*
|
|
740
|
+
* @param ids - 数据 ID 数组
|
|
741
|
+
*
|
|
742
|
+
* @example
|
|
743
|
+
* ```typescript
|
|
744
|
+
* import { batchDelete } from '@seaverse/data-sdk';
|
|
745
|
+
*
|
|
746
|
+
* await batchDelete(['id-1', 'id-2', 'id-3']);
|
|
747
|
+
* ```
|
|
748
|
+
*/
|
|
749
|
+
declare function batchDelete(ids: string[]): Promise<void>;
|
|
750
|
+
/**
|
|
751
|
+
* 查询数据并返回分页信息
|
|
752
|
+
*
|
|
753
|
+
* @param options - 查询选项
|
|
754
|
+
* @returns 分页响应(包含 total, hasMore 等元数据)
|
|
755
|
+
*
|
|
756
|
+
* @example
|
|
757
|
+
* ```typescript
|
|
758
|
+
* import { queryWithPagination } from '@seaverse/data-sdk';
|
|
759
|
+
*
|
|
760
|
+
* const result = await queryWithPagination({
|
|
761
|
+
* table: 'posts',
|
|
762
|
+
* pagination: { limit: 10, offset: 0 },
|
|
763
|
+
* });
|
|
764
|
+
*
|
|
765
|
+
* console.log(`总共 ${result.total} 条数据`);
|
|
766
|
+
* console.log(`是否还有更多: ${result.hasMore}`);
|
|
767
|
+
* ```
|
|
768
|
+
*/
|
|
769
|
+
declare function queryWithPagination(options: QueryOptions): Promise<PaginatedResponse<DataRecord>>;
|
|
770
|
+
/**
|
|
771
|
+
* 检查当前用户是否是管理员
|
|
772
|
+
*
|
|
773
|
+
* @returns 是否是管理员
|
|
774
|
+
*
|
|
775
|
+
* @example
|
|
776
|
+
* ```typescript
|
|
777
|
+
* import { isAdmin } from '@seaverse/data-sdk';
|
|
778
|
+
*
|
|
779
|
+
* const admin = await isAdmin();
|
|
780
|
+
* if (admin) {
|
|
781
|
+
* console.log('当前用户是管理员');
|
|
782
|
+
* }
|
|
783
|
+
* ```
|
|
784
|
+
*/
|
|
785
|
+
declare function isAdmin(): Promise<boolean>;
|
|
786
|
+
|
|
787
|
+
export { DEFAULT_TIMEOUT, DataClient, DataSDKError, ENDPOINTS, ENVIRONMENT_CONFIGS, NotFoundError, PermissionError, RateLimitError, SDKError, ValidationError, batchDelete, clearConfig, create, deleteData, detectEnvironment, get, getApiToken, getAppId, getClient, getCurrentAppId, getEnvironmentConfig, initData, isAdmin, query, queryWithPagination, resolveBaseURL, update, updateAppId, updateToken };
|
|
542
788
|
export type { AdminRole, CreateDataOptions, DataClientOptions, DataRecord, Environment, EnvironmentConfig, FilterOperator, JWTClaims, PaginatedResponse, QueryOptions, UpdateDataOptions, Visibility };
|
package/dist/index.js
CHANGED
|
@@ -180,6 +180,100 @@ const ENDPOINTS = {
|
|
|
180
180
|
// Admin roles
|
|
181
181
|
APP_ADMINS: '/app_admins',
|
|
182
182
|
};
|
|
183
|
+
/**
|
|
184
|
+
* 自动获取 API Token
|
|
185
|
+
*
|
|
186
|
+
* 按照以下优先级获取 token:
|
|
187
|
+
* 1. 传入的 token 参数
|
|
188
|
+
* 2. localStorage 中的 token(auth-sdk 标准 key)
|
|
189
|
+
* 3. localStorage 中的 auth_token(向后兼容)
|
|
190
|
+
* 4. PostMessage 从父窗口获取(iframe 场景)
|
|
191
|
+
* 5. 环境变量 DATA_SDK_TOKEN
|
|
192
|
+
*
|
|
193
|
+
* @param providedToken - 可选的直接提供的 token
|
|
194
|
+
* @returns token 字符串
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* // 通常由 auth-sdk 的 login() 方法设置
|
|
199
|
+
* // localStorage.setItem('token', loginResult.token);
|
|
200
|
+
*
|
|
201
|
+
* // data-sdk 自动获取
|
|
202
|
+
* const token = await getApiToken();
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
async function getApiToken(providedToken) {
|
|
206
|
+
// Priority 1: Provided token
|
|
207
|
+
if (providedToken) {
|
|
208
|
+
return providedToken;
|
|
209
|
+
}
|
|
210
|
+
// Priority 2: localStorage 'token' (auth-sdk standard key)
|
|
211
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
212
|
+
const storedToken = localStorage.getItem('token');
|
|
213
|
+
if (storedToken) {
|
|
214
|
+
return storedToken;
|
|
215
|
+
}
|
|
216
|
+
// Priority 3: localStorage 'auth_token' (backward compatibility)
|
|
217
|
+
const authToken = localStorage.getItem('auth_token');
|
|
218
|
+
if (authToken) {
|
|
219
|
+
return authToken;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Priority 4: PostMessage from parent (iframe scenario)
|
|
223
|
+
if (typeof window !== 'undefined' && window.parent && window.parent !== window) {
|
|
224
|
+
try {
|
|
225
|
+
const token = await getTokenFromParent();
|
|
226
|
+
if (token) {
|
|
227
|
+
return token;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
console.warn('[DataSDK] Failed to get token from parent window:', error);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Priority 5: Environment variable
|
|
235
|
+
if (typeof process !== 'undefined' && process.env && process.env.DATA_SDK_TOKEN) {
|
|
236
|
+
return process.env.DATA_SDK_TOKEN;
|
|
237
|
+
}
|
|
238
|
+
return '';
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* 获取 App ID
|
|
242
|
+
*
|
|
243
|
+
* appId 必须由用户明确提供,不会自动从其他地方获取
|
|
244
|
+
*
|
|
245
|
+
* @param providedAppId - 必需的 appId 参数
|
|
246
|
+
* @returns appId 字符串
|
|
247
|
+
*/
|
|
248
|
+
async function getAppId(providedAppId) {
|
|
249
|
+
if (providedAppId) {
|
|
250
|
+
return providedAppId;
|
|
251
|
+
}
|
|
252
|
+
// appId 必须由用户明确提供
|
|
253
|
+
return '';
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* 通过 PostMessage 从父窗口获取 token (iframe 场景)
|
|
257
|
+
*
|
|
258
|
+
* @returns Promise<string> token
|
|
259
|
+
* @private
|
|
260
|
+
*/
|
|
261
|
+
function getTokenFromParent() {
|
|
262
|
+
return new Promise((resolve, reject) => {
|
|
263
|
+
const timeout = setTimeout(() => {
|
|
264
|
+
reject(new Error('Timeout waiting for token from parent'));
|
|
265
|
+
}, 3000);
|
|
266
|
+
const handleMessage = (event) => {
|
|
267
|
+
if (event.data.type === 'DATA_SDK_TOKEN_RESPONSE') {
|
|
268
|
+
clearTimeout(timeout);
|
|
269
|
+
window.removeEventListener('message', handleMessage);
|
|
270
|
+
resolve(event.data.token || '');
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
window.addEventListener('message', handleMessage);
|
|
274
|
+
window.parent.postMessage({ type: 'DATA_SDK_TOKEN_REQUEST' }, '*');
|
|
275
|
+
});
|
|
276
|
+
}
|
|
183
277
|
|
|
184
278
|
/**
|
|
185
279
|
* Data SDK Client
|
|
@@ -247,6 +341,16 @@ class DataClient {
|
|
|
247
341
|
...headers,
|
|
248
342
|
},
|
|
249
343
|
});
|
|
344
|
+
// Add request interceptor to inject app_id query parameter
|
|
345
|
+
this.axiosInstance.interceptors.request.use((config) => {
|
|
346
|
+
// Add app_id query parameter to all requests
|
|
347
|
+
// This is required for PostgREST's data_set_role() pre-request hook
|
|
348
|
+
if (!config.params) {
|
|
349
|
+
config.params = {};
|
|
350
|
+
}
|
|
351
|
+
config.params.app_id = `eq.${this.appId}`;
|
|
352
|
+
return config;
|
|
353
|
+
}, (error) => Promise.reject(error));
|
|
250
354
|
// Add response interceptor for error handling
|
|
251
355
|
this.axiosInstance.interceptors.response.use((response) => response, (error) => {
|
|
252
356
|
throw this._handleError(error);
|
|
@@ -372,7 +476,7 @@ class DataClient {
|
|
|
372
476
|
*/
|
|
373
477
|
async query(options) {
|
|
374
478
|
const query = {
|
|
375
|
-
app_id
|
|
479
|
+
// app_id is automatically added by request interceptor
|
|
376
480
|
table_name: `eq.${options.table}`,
|
|
377
481
|
};
|
|
378
482
|
// Add filters
|
|
@@ -766,5 +870,313 @@ class DataClient {
|
|
|
766
870
|
}
|
|
767
871
|
}
|
|
768
872
|
|
|
769
|
-
|
|
873
|
+
/**
|
|
874
|
+
* Global Configuration for Data SDK
|
|
875
|
+
* 全局配置模块 - 支持无缝接入
|
|
876
|
+
*/
|
|
877
|
+
/**
|
|
878
|
+
* 全局配置实例
|
|
879
|
+
*/
|
|
880
|
+
const globalConfig = {};
|
|
881
|
+
/**
|
|
882
|
+
* 初始化 Data SDK(可选)
|
|
883
|
+
*
|
|
884
|
+
* 可以在应用启动时调用,也可以完全不调用(SDK会自动从localStorage获取配置)
|
|
885
|
+
*
|
|
886
|
+
* @param options - 初始化选项(所有参数都是可选的)
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```typescript
|
|
890
|
+
* // 方式1: 显式初始化(推荐用于需要自定义配置的场景)
|
|
891
|
+
* await initData({
|
|
892
|
+
* appId: 'my-app-123',
|
|
893
|
+
* token: 'user-jwt-token',
|
|
894
|
+
* debug: true,
|
|
895
|
+
* });
|
|
896
|
+
*
|
|
897
|
+
* // 方式2: 不初始化,直接使用(推荐用于SeaVerse平台应用)
|
|
898
|
+
* // SDK 会自动从 localStorage 读取 auth_token 和 app_id
|
|
899
|
+
* const notes = await query({ table: 'notes' });
|
|
900
|
+
* ```
|
|
901
|
+
*/
|
|
902
|
+
async function initData(options = {}) {
|
|
903
|
+
const appId = options.appId || (await getAppId());
|
|
904
|
+
const token = options.token || (await getApiToken());
|
|
905
|
+
if (!appId || !token) {
|
|
906
|
+
throw new Error('[DataSDK] appId and token are required. Please call initData() with credentials or ensure they are available in localStorage/environment.');
|
|
907
|
+
}
|
|
908
|
+
globalConfig.appId = appId;
|
|
909
|
+
globalConfig.token = token;
|
|
910
|
+
globalConfig.debug = options.debug;
|
|
911
|
+
// Create client instance
|
|
912
|
+
globalConfig.client = new DataClient({
|
|
913
|
+
appId,
|
|
914
|
+
token,
|
|
915
|
+
baseURL: options.baseURL,
|
|
916
|
+
environment: options.environment,
|
|
917
|
+
timeout: options.timeout,
|
|
918
|
+
headers: options.headers,
|
|
919
|
+
debug: options.debug,
|
|
920
|
+
});
|
|
921
|
+
if (globalConfig.debug) {
|
|
922
|
+
console.log('[DataSDK] Initialized', { appId, hasToken: !!token });
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* 获取或创建 DataClient 实例
|
|
927
|
+
*
|
|
928
|
+
* 如果已经初始化,返回已有实例;否则自动创建新实例
|
|
929
|
+
*
|
|
930
|
+
* @returns DataClient 实例
|
|
931
|
+
* @throws 如果无法获取必需的配置
|
|
932
|
+
*/
|
|
933
|
+
async function getClient() {
|
|
934
|
+
if (globalConfig.client) {
|
|
935
|
+
return globalConfig.client;
|
|
936
|
+
}
|
|
937
|
+
// Auto-initialize with defaults
|
|
938
|
+
const appId = await getAppId();
|
|
939
|
+
const token = await getApiToken();
|
|
940
|
+
if (!appId || !token) {
|
|
941
|
+
throw new Error('[DataSDK] Unable to initialize: appId or token not found. Please call initData() first or ensure credentials are available in localStorage.');
|
|
942
|
+
}
|
|
943
|
+
globalConfig.client = new DataClient({
|
|
944
|
+
appId,
|
|
945
|
+
token,
|
|
946
|
+
debug: globalConfig.debug,
|
|
947
|
+
});
|
|
948
|
+
return globalConfig.client;
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* 更新全局 token(用于 token 刷新场景)
|
|
952
|
+
*
|
|
953
|
+
* @param token - 新的 JWT token
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* ```typescript
|
|
957
|
+
* // Token 刷新后更新
|
|
958
|
+
* updateToken('new-jwt-token');
|
|
959
|
+
* ```
|
|
960
|
+
*/
|
|
961
|
+
function updateToken(token) {
|
|
962
|
+
globalConfig.token = token;
|
|
963
|
+
if (globalConfig.client) {
|
|
964
|
+
globalConfig.client.updateToken(token);
|
|
965
|
+
}
|
|
966
|
+
// Update localStorage
|
|
967
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
968
|
+
localStorage.setItem('auth_token', token);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* 更新全局 appId(用于切换应用场景)
|
|
973
|
+
*
|
|
974
|
+
* @param appId - 新的应用 ID
|
|
975
|
+
*
|
|
976
|
+
* @example
|
|
977
|
+
* ```typescript
|
|
978
|
+
* // 切换到其他应用
|
|
979
|
+
* updateAppId('another-app-id');
|
|
980
|
+
* ```
|
|
981
|
+
*/
|
|
982
|
+
function updateAppId(appId) {
|
|
983
|
+
globalConfig.appId = appId;
|
|
984
|
+
if (globalConfig.client) {
|
|
985
|
+
globalConfig.client.updateAppId(appId);
|
|
986
|
+
}
|
|
987
|
+
// Update localStorage
|
|
988
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
989
|
+
localStorage.setItem('app_id', appId);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* 获取当前配置的 appId
|
|
994
|
+
*
|
|
995
|
+
* @returns 当前的 appId 或 undefined
|
|
996
|
+
*/
|
|
997
|
+
function getCurrentAppId() {
|
|
998
|
+
return globalConfig.appId || globalConfig.client?.getAppId();
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* 清除全局配置(用于登出场景)
|
|
1002
|
+
*
|
|
1003
|
+
* @example
|
|
1004
|
+
* ```typescript
|
|
1005
|
+
* // 用户登出时清除配置
|
|
1006
|
+
* clearConfig();
|
|
1007
|
+
* ```
|
|
1008
|
+
*/
|
|
1009
|
+
function clearConfig() {
|
|
1010
|
+
globalConfig.client = undefined;
|
|
1011
|
+
globalConfig.appId = undefined;
|
|
1012
|
+
globalConfig.token = undefined;
|
|
1013
|
+
globalConfig.debug = undefined;
|
|
1014
|
+
// Clear localStorage
|
|
1015
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
1016
|
+
localStorage.removeItem('auth_token');
|
|
1017
|
+
localStorage.removeItem('app_id');
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Convenient Function-based APIs
|
|
1023
|
+
* 便捷的函数式 API 封装 - 无需创建 client 实例
|
|
1024
|
+
*/
|
|
1025
|
+
/**
|
|
1026
|
+
* 查询数据列表
|
|
1027
|
+
*
|
|
1028
|
+
* 便捷函数 - 无需创建 client 实例
|
|
1029
|
+
*
|
|
1030
|
+
* @param options - 查询选项
|
|
1031
|
+
* @returns 数据记录数组
|
|
1032
|
+
*
|
|
1033
|
+
* @example
|
|
1034
|
+
* ```typescript
|
|
1035
|
+
* import { query } from '@seaverse/data-sdk';
|
|
1036
|
+
*
|
|
1037
|
+
* const notes = await query({
|
|
1038
|
+
* table: 'notes',
|
|
1039
|
+
* filters: { category: 'work' },
|
|
1040
|
+
* order: { field: 'created_at', direction: 'desc' },
|
|
1041
|
+
* });
|
|
1042
|
+
* ```
|
|
1043
|
+
*/
|
|
1044
|
+
async function query(options) {
|
|
1045
|
+
const client = await getClient();
|
|
1046
|
+
return client.query(options);
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* 获取单条数据
|
|
1050
|
+
*
|
|
1051
|
+
* @param id - 数据 ID
|
|
1052
|
+
* @returns 数据记录或 null
|
|
1053
|
+
*
|
|
1054
|
+
* @example
|
|
1055
|
+
* ```typescript
|
|
1056
|
+
* import { get } from '@seaverse/data-sdk';
|
|
1057
|
+
*
|
|
1058
|
+
* const note = await get('note-id-123');
|
|
1059
|
+
* ```
|
|
1060
|
+
*/
|
|
1061
|
+
async function get(id) {
|
|
1062
|
+
const client = await getClient();
|
|
1063
|
+
return client.get(id);
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* 创建数据
|
|
1067
|
+
*
|
|
1068
|
+
* @param options - 创建选项
|
|
1069
|
+
* @returns 创建的数据记录
|
|
1070
|
+
*
|
|
1071
|
+
* @example
|
|
1072
|
+
* ```typescript
|
|
1073
|
+
* import { create } from '@seaverse/data-sdk';
|
|
1074
|
+
*
|
|
1075
|
+
* const newNote = await create({
|
|
1076
|
+
* table: 'notes',
|
|
1077
|
+
* data: { title: 'My Note', content: '...' },
|
|
1078
|
+
* visibility: 'private',
|
|
1079
|
+
* });
|
|
1080
|
+
* ```
|
|
1081
|
+
*/
|
|
1082
|
+
async function create(options) {
|
|
1083
|
+
const client = await getClient();
|
|
1084
|
+
return client.create(options);
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* 更新数据
|
|
1088
|
+
*
|
|
1089
|
+
* @param id - 数据 ID
|
|
1090
|
+
* @param options - 更新选项
|
|
1091
|
+
* @returns 更新后的数据记录
|
|
1092
|
+
*
|
|
1093
|
+
* @example
|
|
1094
|
+
* ```typescript
|
|
1095
|
+
* import { update } from '@seaverse/data-sdk';
|
|
1096
|
+
*
|
|
1097
|
+
* const updated = await update('note-id-123', {
|
|
1098
|
+
* data: { title: 'Updated Title' },
|
|
1099
|
+
* });
|
|
1100
|
+
* ```
|
|
1101
|
+
*/
|
|
1102
|
+
async function update(id, options) {
|
|
1103
|
+
const client = await getClient();
|
|
1104
|
+
return client.update(id, options);
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* 删除数据
|
|
1108
|
+
*
|
|
1109
|
+
* @param id - 数据 ID
|
|
1110
|
+
*
|
|
1111
|
+
* @example
|
|
1112
|
+
* ```typescript
|
|
1113
|
+
* import { deleteData } from '@seaverse/data-sdk';
|
|
1114
|
+
*
|
|
1115
|
+
* await deleteData('note-id-123');
|
|
1116
|
+
* ```
|
|
1117
|
+
*/
|
|
1118
|
+
async function deleteData(id) {
|
|
1119
|
+
const client = await getClient();
|
|
1120
|
+
return client.delete(id);
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1123
|
+
* 批量删除数据
|
|
1124
|
+
*
|
|
1125
|
+
* @param ids - 数据 ID 数组
|
|
1126
|
+
*
|
|
1127
|
+
* @example
|
|
1128
|
+
* ```typescript
|
|
1129
|
+
* import { batchDelete } from '@seaverse/data-sdk';
|
|
1130
|
+
*
|
|
1131
|
+
* await batchDelete(['id-1', 'id-2', 'id-3']);
|
|
1132
|
+
* ```
|
|
1133
|
+
*/
|
|
1134
|
+
async function batchDelete(ids) {
|
|
1135
|
+
const client = await getClient();
|
|
1136
|
+
return client.batchDelete(ids);
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* 查询数据并返回分页信息
|
|
1140
|
+
*
|
|
1141
|
+
* @param options - 查询选项
|
|
1142
|
+
* @returns 分页响应(包含 total, hasMore 等元数据)
|
|
1143
|
+
*
|
|
1144
|
+
* @example
|
|
1145
|
+
* ```typescript
|
|
1146
|
+
* import { queryWithPagination } from '@seaverse/data-sdk';
|
|
1147
|
+
*
|
|
1148
|
+
* const result = await queryWithPagination({
|
|
1149
|
+
* table: 'posts',
|
|
1150
|
+
* pagination: { limit: 10, offset: 0 },
|
|
1151
|
+
* });
|
|
1152
|
+
*
|
|
1153
|
+
* console.log(`总共 ${result.total} 条数据`);
|
|
1154
|
+
* console.log(`是否还有更多: ${result.hasMore}`);
|
|
1155
|
+
* ```
|
|
1156
|
+
*/
|
|
1157
|
+
async function queryWithPagination(options) {
|
|
1158
|
+
const client = await getClient();
|
|
1159
|
+
return client.queryWithPagination(options);
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* 检查当前用户是否是管理员
|
|
1163
|
+
*
|
|
1164
|
+
* @returns 是否是管理员
|
|
1165
|
+
*
|
|
1166
|
+
* @example
|
|
1167
|
+
* ```typescript
|
|
1168
|
+
* import { isAdmin } from '@seaverse/data-sdk';
|
|
1169
|
+
*
|
|
1170
|
+
* const admin = await isAdmin();
|
|
1171
|
+
* if (admin) {
|
|
1172
|
+
* console.log('当前用户是管理员');
|
|
1173
|
+
* }
|
|
1174
|
+
* ```
|
|
1175
|
+
*/
|
|
1176
|
+
async function isAdmin() {
|
|
1177
|
+
const client = await getClient();
|
|
1178
|
+
return client.isAdmin();
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
export { DEFAULT_TIMEOUT, DataClient, DataSDKError, ENDPOINTS, ENVIRONMENT_CONFIGS, NotFoundError, PermissionError, RateLimitError, SDKError, ValidationError, batchDelete, clearConfig, create, deleteData, detectEnvironment, get, getApiToken, getAppId, getClient, getCurrentAppId, getEnvironmentConfig, initData, isAdmin, query, queryWithPagination, resolveBaseURL, update, updateAppId, updateToken };
|
|
770
1182
|
//# sourceMappingURL=index.js.map
|