@lovrabet/sdk 1.1.19 → 1.1.20
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 +23 -74
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -246,7 +246,7 @@ SDK 提供了 `client.api` 命名空间,用于执行自定义 SQL 查询和其
|
|
|
246
246
|
|
|
247
247
|
```typescript
|
|
248
248
|
// 执行 SQL 查询,返回结果数组
|
|
249
|
-
const results = await client.api.executeSql(
|
|
249
|
+
const results = await client.api.executeSql("fc8e7777-06e3847d");
|
|
250
250
|
console.log(results);
|
|
251
251
|
// [
|
|
252
252
|
// { creation_date: '2025-08-13', page_count: 2 },
|
|
@@ -259,9 +259,9 @@ console.log(results);
|
|
|
259
259
|
|
|
260
260
|
```typescript
|
|
261
261
|
// 传递参数到 SQL
|
|
262
|
-
const results = await client.api.executeSql(
|
|
263
|
-
userId:
|
|
264
|
-
startDate:
|
|
262
|
+
const results = await client.api.executeSql("fc8e7777-xxxxx", {
|
|
263
|
+
userId: "123",
|
|
264
|
+
startDate: "2025-01-01",
|
|
265
265
|
});
|
|
266
266
|
```
|
|
267
267
|
|
|
@@ -275,9 +275,9 @@ interface PageStat {
|
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
// 使用泛型获得类型安全
|
|
278
|
-
const stats = await client.api.executeSql<PageStat>(
|
|
279
|
-
stats.forEach(stat => {
|
|
280
|
-
console.log(stat.creation_date);
|
|
278
|
+
const stats = await client.api.executeSql<PageStat>("fc8e7777-06e3847d");
|
|
279
|
+
stats.forEach((stat) => {
|
|
280
|
+
console.log(stat.creation_date); // TypeScript 自动补全
|
|
281
281
|
console.log(stat.page_count);
|
|
282
282
|
});
|
|
283
283
|
```
|
|
@@ -286,31 +286,33 @@ stats.forEach(stat => {
|
|
|
286
286
|
|
|
287
287
|
```typescript
|
|
288
288
|
// 获取第一条结果
|
|
289
|
-
const results = await client.api.executeSql<PageStat>(
|
|
289
|
+
const results = await client.api.executeSql<PageStat>("fc8e7777-xxxxx");
|
|
290
290
|
const firstResult = results[0];
|
|
291
291
|
|
|
292
292
|
if (firstResult) {
|
|
293
|
-
console.log(
|
|
293
|
+
console.log(
|
|
294
|
+
`日期: ${firstResult.creation_date}, 数量: ${firstResult.page_count}`
|
|
295
|
+
);
|
|
294
296
|
} else {
|
|
295
|
-
console.log(
|
|
297
|
+
console.log("无结果");
|
|
296
298
|
}
|
|
297
299
|
|
|
298
300
|
// 过滤结果
|
|
299
|
-
const activeItems = results.filter(item => item.status ===
|
|
301
|
+
const activeItems = results.filter((item) => item.status === "active");
|
|
300
302
|
|
|
301
303
|
// 查找特定结果
|
|
302
|
-
const targetItem = results.find(item => item.id ===
|
|
304
|
+
const targetItem = results.find((item) => item.id === "123");
|
|
303
305
|
```
|
|
304
306
|
|
|
305
307
|
#### 错误处理
|
|
306
308
|
|
|
307
309
|
```typescript
|
|
308
310
|
try {
|
|
309
|
-
const results = await client.api.executeSql(
|
|
311
|
+
const results = await client.api.executeSql("fc8e7777-xxxxx");
|
|
310
312
|
} catch (error) {
|
|
311
313
|
if (error instanceof LovrabetError) {
|
|
312
|
-
console.error(
|
|
313
|
-
console.error(
|
|
314
|
+
console.error("SQL执行失败:", error.message);
|
|
315
|
+
console.error("错误代码:", error.code);
|
|
314
316
|
}
|
|
315
317
|
}
|
|
316
318
|
```
|
|
@@ -507,23 +509,25 @@ const client = createClient({ token });
|
|
|
507
509
|
|
|
508
510
|
```typescript
|
|
509
511
|
// 执行 SQL 查询
|
|
510
|
-
const data = await client.api.executeSql(
|
|
512
|
+
const data = await client.api.executeSql("fc8e7777-06e3847d");
|
|
511
513
|
|
|
512
514
|
// 应用层检查执行结果
|
|
513
515
|
if (data.execSuccess && data.execResult) {
|
|
514
|
-
data.execResult.forEach(row => {
|
|
516
|
+
data.execResult.forEach((row) => {
|
|
515
517
|
console.log(row);
|
|
516
518
|
});
|
|
517
519
|
}
|
|
518
520
|
```
|
|
519
521
|
|
|
520
522
|
**核心特性:**
|
|
523
|
+
|
|
521
524
|
- 支持参数化查询,防止 SQL 注入
|
|
522
525
|
- 完整的 TypeScript 类型支持(泛型)
|
|
523
526
|
- 返回 `{ execSuccess: boolean, execResult?: T[] }` 结构
|
|
524
527
|
- 应用层负责检查 `execSuccess` 状态
|
|
525
528
|
|
|
526
529
|
**适用场景:**
|
|
530
|
+
|
|
527
531
|
- 复杂数据统计和聚合查询
|
|
528
532
|
- 跨表关联查询
|
|
529
533
|
- 自定义报表数据获取
|
|
@@ -531,66 +535,11 @@ const client = createClient({ token });
|
|
|
531
535
|
|
|
532
536
|
- 🏗️ **API 命名空间架构** - 新增 `client.api` 命名空间,用于管理通用 API 调用
|
|
533
537
|
|
|
534
|
-
|
|
535
|
-
const client = createClient({ /* ... */ });
|
|
536
|
-
|
|
537
|
-
// 通过 api 命名空间访问
|
|
538
|
-
const data = await client.api.executeSql('sqlCode');
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
- 统一的 API 调用入口
|
|
542
|
-
- 为未来扩展预留空间(executeFunction、callWebhook 等)
|
|
543
|
-
- 与 Model API 保持一致的设计风格
|
|
544
|
-
|
|
545
|
-
**类型定义 (Type Definitions):**
|
|
546
|
-
|
|
547
|
-
- 新增 `SqlExecuteRequest` 接口:
|
|
548
|
-
|
|
549
|
-
```typescript
|
|
550
|
-
interface SqlExecuteRequest {
|
|
551
|
-
sqlCode: string | number; // SQL 代码
|
|
552
|
-
params?: Record<string, string | number>; // SQL 参数
|
|
553
|
-
}
|
|
554
|
-
```
|
|
555
|
-
|
|
556
|
-
- 新增 `SqlExecuteResult<T>` 接口:
|
|
557
|
-
|
|
558
|
-
```typescript
|
|
559
|
-
interface SqlExecuteResult<T = Record<string, any>> {
|
|
560
|
-
execSuccess: boolean; // SQL 执行是否成功
|
|
561
|
-
execResult?: T[]; // 查询结果数组
|
|
562
|
-
}
|
|
563
|
-
```
|
|
564
|
-
|
|
565
|
-
- 导出 `ApiNamespace` 类供高级用户使用
|
|
566
|
-
|
|
567
|
-
**重要说明 (Important Notes):**
|
|
568
|
-
|
|
569
|
-
:::warning SDK 返回值设计原则
|
|
570
|
-
|
|
571
|
-
SDK 的 `executeSql()` 方法返回包含 `execSuccess` 和 `execResult` 的对象,**不直接返回结果数组**。
|
|
572
|
-
|
|
573
|
-
这是因为:
|
|
574
|
-
1. **分离职责** - SDK 负责 HTTP 通信,应用层负责业务逻辑判断
|
|
575
|
-
2. **错误处理** - `execSuccess: false` 表示 SQL 执行失败(业务错误),HTTP 失败才抛异常
|
|
576
|
-
3. **灵活性** - 应用层可以根据 `execSuccess` 自行决定如何处理
|
|
577
|
-
|
|
578
|
-
```typescript
|
|
579
|
-
// ✅ 正确使用方式
|
|
580
|
-
const data = await client.api.executeSql('sqlCode');
|
|
581
|
-
if (data.execSuccess && data.execResult) {
|
|
582
|
-
// 使用 execResult
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// ❌ 错误使用方式
|
|
586
|
-
const data = await client.api.executeSql('sqlCode');
|
|
587
|
-
data.execResult.forEach(...); // 没有检查 execSuccess!
|
|
588
|
-
```
|
|
589
|
-
:::
|
|
538
|
+
详细说明参考:[说明文档](https://open.lovrabet.com/docs/lovrabet-sdk/sql-api)
|
|
590
539
|
|
|
591
540
|
---
|
|
592
541
|
|
|
593
|
-
### v1.1.
|
|
542
|
+
### v1.1.17 (2025-10-18)
|
|
594
543
|
|
|
595
544
|
**新增功能 (New Features):**
|
|
596
545
|
|