@seaverse/auth-sdk 0.2.4 → 0.2.6
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 +78 -2
- package/dist/index.cjs +23 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +23 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ SeaVerse Backend API 客户端 SDK - 提供完整的认证、容器管理、技
|
|
|
16
16
|
- 🌍 **多环境支持** - Production、Staging、Development、Local
|
|
17
17
|
- ⚡ **TypeScript** - 完整的类型定义
|
|
18
18
|
- 🔧 **认证集成** - 内置Auth、Hooks系统支持
|
|
19
|
+
- 🔄 **响应格式兼容** - 自动兼容多种API响应格式
|
|
19
20
|
|
|
20
21
|
## 安装
|
|
21
22
|
|
|
@@ -532,7 +533,7 @@ SDK支持以下环境:
|
|
|
532
533
|
|
|
533
534
|
| 环境 | 描述 | BaseURL |
|
|
534
535
|
|------|------|---------|
|
|
535
|
-
| `production` | 生产环境 | `https://
|
|
536
|
+
| `production` | 生产环境 | `https://account-hub.seaverse.ai` |
|
|
536
537
|
| `staging` | 测试环境 | `https://api.staging.seaverse.dev` |
|
|
537
538
|
| `development` | 开发环境 | `https://api.dev.seaverse.dev` |
|
|
538
539
|
| `local` | 本地环境 | `http://localhost:3000` |
|
|
@@ -598,6 +599,74 @@ SDK支持以下环境:
|
|
|
598
599
|
| `getConversationStatus()` | `{ conversationId }` | `ConversationStatusResponse` | 获取对话状态 |
|
|
599
600
|
| `getSpeechToken()` | - | `SpeechTokenResponse` | 获取语音Token |
|
|
600
601
|
|
|
602
|
+
## 响应格式兼容性
|
|
603
|
+
|
|
604
|
+
### 自动响应解包
|
|
605
|
+
|
|
606
|
+
从 v0.2.5 开始,SDK 自动兼容两种API响应格式,无需手动处理:
|
|
607
|
+
|
|
608
|
+
**格式 1: 包装格式**(推荐)
|
|
609
|
+
```json
|
|
610
|
+
{
|
|
611
|
+
"data": {
|
|
612
|
+
"token": "eyJhbGc...",
|
|
613
|
+
"user": { ... }
|
|
614
|
+
},
|
|
615
|
+
"success": true
|
|
616
|
+
}
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
**格式 2: 扁平格式**(向后兼容)
|
|
620
|
+
```json
|
|
621
|
+
{
|
|
622
|
+
"token": "eyJhbGc...",
|
|
623
|
+
"user": { ... }
|
|
624
|
+
}
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
SDK 会自动检测响应格式并提取正确的数据:
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
// 无论后端返回哪种格式,以下代码都能正常工作
|
|
631
|
+
const loginResult = await client.login({
|
|
632
|
+
email: 'user@example.com',
|
|
633
|
+
password: 'password123',
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
console.log(loginResult.token); // ✅ 始终能正确获取 token
|
|
637
|
+
console.log(loginResult.user); // ✅ 始终能正确获取 user
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### 受影响的方法
|
|
641
|
+
|
|
642
|
+
以下方法已实现自动响应解包:
|
|
643
|
+
|
|
644
|
+
- ✅ `login()` - 登录接口
|
|
645
|
+
- ✅ `register()` - 注册接口
|
|
646
|
+
- ✅ `getCurrentUser()` - 获取当前用户
|
|
647
|
+
|
|
648
|
+
### 实现原理
|
|
649
|
+
|
|
650
|
+
SDK 内部通过以下逻辑自动处理响应格式:
|
|
651
|
+
|
|
652
|
+
```typescript
|
|
653
|
+
const response = await httpClient.request(config);
|
|
654
|
+
const responseData = response.data;
|
|
655
|
+
|
|
656
|
+
// 检测并解包
|
|
657
|
+
if (responseData.data && typeof responseData.data === 'object') {
|
|
658
|
+
// 包装格式: 提取 data 字段
|
|
659
|
+
return responseData.data;
|
|
660
|
+
}
|
|
661
|
+
// 扁平格式: 直接返回
|
|
662
|
+
return responseData;
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
这意味着:
|
|
666
|
+
- 🔄 **后端格式变更无需前端修改** - 后端可以自由调整响应格式
|
|
667
|
+
- 🔧 **渐进式迁移** - 可以逐步将不同接口迁移到新格式
|
|
668
|
+
- ✅ **向后兼容** - 旧代码无需修改即可继续工作
|
|
669
|
+
|
|
601
670
|
## 错误处理
|
|
602
671
|
|
|
603
672
|
### 错误响应格式
|
|
@@ -1033,7 +1102,14 @@ MIT © [SeaVerse Team](mailto:support@seaverse.com)
|
|
|
1033
1102
|
|
|
1034
1103
|
## 更新日志
|
|
1035
1104
|
|
|
1036
|
-
### v0.2.
|
|
1105
|
+
### v0.2.5 (当前版本)
|
|
1106
|
+
- 🔄 **响应格式兼容**: 自动兼容包装格式和扁平格式的API响应
|
|
1107
|
+
- 修复登录时提示 "Invalid response from server" 的问题
|
|
1108
|
+
- `login()`, `register()`, `getCurrentUser()` 方法自动解包 `data` 字段
|
|
1109
|
+
- 支持两种格式: `{ data: {...}, success: true }` 和 `{ ... }`
|
|
1110
|
+
- 📝 **文档更新**: 新增响应格式兼容性章节
|
|
1111
|
+
|
|
1112
|
+
### v0.2.0
|
|
1037
1113
|
- 🔄 **API路径更新**: 所有认证API从 `/api/auth/*` 迁移到 `/sdk/v1/auth/*`
|
|
1038
1114
|
- 🏢 **多租户支持**: User模型新增 `app_id` 字段,支持多应用隔离
|
|
1039
1115
|
- 🔑 **重要变更**: `appId` 现在是**必需参数**,SDK 自动在所有请求中添加 `X-App-ID` 请求头
|
package/dist/index.cjs
CHANGED
|
@@ -1043,7 +1043,7 @@ const ENVIRONMENT_CONFIGS = {
|
|
|
1043
1043
|
production: {
|
|
1044
1044
|
name: 'production',
|
|
1045
1045
|
baseURL: 'https://account-hub.seaverse.ai',
|
|
1046
|
-
wsURL: 'wss://
|
|
1046
|
+
wsURL: 'wss://account-hub.seaverse.ai',
|
|
1047
1047
|
isProduction: true,
|
|
1048
1048
|
},
|
|
1049
1049
|
staging: {
|
|
@@ -1317,7 +1317,12 @@ class SeaVerseBackendAPIClient {
|
|
|
1317
1317
|
...options,
|
|
1318
1318
|
};
|
|
1319
1319
|
const response = await this.httpClient.request(config);
|
|
1320
|
-
|
|
1320
|
+
// ✅ 兼容两种响应格式
|
|
1321
|
+
const responseData = response.data;
|
|
1322
|
+
if (responseData.data && typeof responseData.data === 'object') {
|
|
1323
|
+
return responseData.data;
|
|
1324
|
+
}
|
|
1325
|
+
return responseData;
|
|
1321
1326
|
}
|
|
1322
1327
|
/**
|
|
1323
1328
|
* User login
|
|
@@ -1335,7 +1340,16 @@ class SeaVerseBackendAPIClient {
|
|
|
1335
1340
|
...options,
|
|
1336
1341
|
};
|
|
1337
1342
|
const response = await this.httpClient.request(config);
|
|
1338
|
-
|
|
1343
|
+
// ✅ 兼容两种响应格式:
|
|
1344
|
+
// 1. { data: { token, user }, success: true } (新格式)
|
|
1345
|
+
// 2. { token, user } (旧格式)
|
|
1346
|
+
const responseData = response.data;
|
|
1347
|
+
if (responseData.data && typeof responseData.data === 'object') {
|
|
1348
|
+
// 新格式: 解包 data 字段
|
|
1349
|
+
return responseData.data;
|
|
1350
|
+
}
|
|
1351
|
+
// 旧格式: 直接返回
|
|
1352
|
+
return responseData;
|
|
1339
1353
|
}
|
|
1340
1354
|
/**
|
|
1341
1355
|
* Get current user
|
|
@@ -1352,7 +1366,12 @@ class SeaVerseBackendAPIClient {
|
|
|
1352
1366
|
...options,
|
|
1353
1367
|
};
|
|
1354
1368
|
const response = await this.httpClient.request(config);
|
|
1355
|
-
|
|
1369
|
+
// ✅ 兼容两种响应格式
|
|
1370
|
+
const responseData = response.data;
|
|
1371
|
+
if (responseData.data && typeof responseData.data === 'object') {
|
|
1372
|
+
return responseData.data;
|
|
1373
|
+
}
|
|
1374
|
+
return responseData;
|
|
1356
1375
|
}
|
|
1357
1376
|
/**
|
|
1358
1377
|
* User logout
|