imean-service-engine 2.3.0 → 2.4.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/dist/index.d.mts +529 -8
- package/dist/index.d.ts +529 -8
- package/dist/index.js +793 -1757
- package/dist/index.mjs +790 -1758
- package/package.json +53 -49
package/dist/index.d.mts
CHANGED
|
@@ -1090,7 +1090,7 @@ interface SchedulePluginOptions {
|
|
|
1090
1090
|
* Etcd3 客户端实例
|
|
1091
1091
|
* 如果未提供且 useMockEtcd 为 false,插件将不会启动调度任务
|
|
1092
1092
|
*/
|
|
1093
|
-
etcdClient?: Etcd3;
|
|
1093
|
+
etcdClient?: Etcd3$1;
|
|
1094
1094
|
/**
|
|
1095
1095
|
* 是否使用 Mock Etcd(用于测试和本地开发)
|
|
1096
1096
|
* 当设置为 true 时,将使用内置的 MockEtcd3,始终选举自己作为 leader
|
|
@@ -1102,20 +1102,26 @@ interface SchedulePluginOptions {
|
|
|
1102
1102
|
}
|
|
1103
1103
|
/**
|
|
1104
1104
|
* Etcd3 类型定义(避免直接依赖 etcd3 包)
|
|
1105
|
+
* 这些类型定义与 etcd3@1.1.2 的实际类型兼容
|
|
1105
1106
|
*/
|
|
1106
|
-
interface Etcd3 {
|
|
1107
|
+
interface Etcd3$1 {
|
|
1107
1108
|
election(key: string, ttl: number): Election;
|
|
1109
|
+
close(): void;
|
|
1110
|
+
delete(): any;
|
|
1111
|
+
get(key: string): any;
|
|
1108
1112
|
}
|
|
1109
1113
|
interface Election {
|
|
1110
1114
|
observe(): Promise<Observer>;
|
|
1111
1115
|
campaign(candidate: string): Campaign;
|
|
1112
1116
|
}
|
|
1113
1117
|
interface Observer {
|
|
1114
|
-
on(event: "change", callback: (leader: string) => void):
|
|
1118
|
+
on(event: "change", callback: (leader: string | undefined) => void): Observer;
|
|
1119
|
+
on(event: "disconnected", callback: (error: Error) => void): Observer;
|
|
1120
|
+
on(event: "error", callback: (error: Error) => void): Observer;
|
|
1115
1121
|
}
|
|
1116
1122
|
interface Campaign {
|
|
1117
|
-
on(event: "error", callback: (error: any) => void):
|
|
1118
|
-
on(event: "elected", callback: () => void):
|
|
1123
|
+
on(event: "error", callback: (error: any) => void): Campaign;
|
|
1124
|
+
on(event: "elected", callback: () => void): Campaign;
|
|
1119
1125
|
resign(): Promise<void>;
|
|
1120
1126
|
}
|
|
1121
1127
|
|
|
@@ -1173,7 +1179,7 @@ declare class Scheduler {
|
|
|
1173
1179
|
private campaigns;
|
|
1174
1180
|
private timers;
|
|
1175
1181
|
private isLeader;
|
|
1176
|
-
constructor(etcdClient: Etcd3);
|
|
1182
|
+
constructor(etcdClient: Etcd3$1);
|
|
1177
1183
|
/**
|
|
1178
1184
|
* 启动调度任务
|
|
1179
1185
|
*/
|
|
@@ -1197,9 +1203,12 @@ declare class Scheduler {
|
|
|
1197
1203
|
* 用于测试和本地开发,模拟 etcd 的选举机制
|
|
1198
1204
|
* 始终选举第一个参与选举的候选者作为 leader
|
|
1199
1205
|
*/
|
|
1200
|
-
declare class MockEtcd3 implements Etcd3 {
|
|
1206
|
+
declare class MockEtcd3 implements Etcd3$1 {
|
|
1201
1207
|
private elections;
|
|
1202
1208
|
election(key: string, ttl: number): Election;
|
|
1209
|
+
close(): void;
|
|
1210
|
+
delete(): any;
|
|
1211
|
+
get(key: string): any;
|
|
1203
1212
|
getElection(key: string): MockElection | undefined;
|
|
1204
1213
|
clearElections(): void;
|
|
1205
1214
|
}
|
|
@@ -1339,6 +1348,518 @@ declare function convertHandlersToModuleInfoWithMetadata(handlers: HandlerMetada
|
|
|
1339
1348
|
name: string;
|
|
1340
1349
|
} | undefined): Record<string, ModuleInfo>;
|
|
1341
1350
|
|
|
1351
|
+
/**
|
|
1352
|
+
* 配置项元数据
|
|
1353
|
+
*
|
|
1354
|
+
* 定义配置的基本信息,包括键、描述、Schema、默认值等
|
|
1355
|
+
*/
|
|
1356
|
+
interface ConfigMetadata {
|
|
1357
|
+
/**
|
|
1358
|
+
* 配置键(用于 etcd 存储)
|
|
1359
|
+
*/
|
|
1360
|
+
key: string;
|
|
1361
|
+
/**
|
|
1362
|
+
* 配置描述
|
|
1363
|
+
*/
|
|
1364
|
+
description?: string;
|
|
1365
|
+
/**
|
|
1366
|
+
* 配置的 Zod Schema(用于运行时验证)
|
|
1367
|
+
*/
|
|
1368
|
+
schema?: z.ZodTypeAny;
|
|
1369
|
+
/**
|
|
1370
|
+
* 默认值
|
|
1371
|
+
*/
|
|
1372
|
+
defaultValue?: any;
|
|
1373
|
+
/**
|
|
1374
|
+
* 是否为敏感信息(日志中脱敏)
|
|
1375
|
+
*/
|
|
1376
|
+
sensitive?: boolean;
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* 动态配置装饰器选项
|
|
1380
|
+
*
|
|
1381
|
+
* 扩展 ConfigMetadata,添加 onChange 回调支持
|
|
1382
|
+
*
|
|
1383
|
+
* 配置优先级:ETCD > 环境变量 > defaultValue
|
|
1384
|
+
* 环境变量名由 key 自动转换(kebab-case -> UPPER_SNAKE_CASE)
|
|
1385
|
+
*
|
|
1386
|
+
* 💡 **推荐**:直接使用 UPPER_SNAKE_CASE 格式作为 key,与环境变量名保持一致
|
|
1387
|
+
*
|
|
1388
|
+
* @example
|
|
1389
|
+
* ```typescript
|
|
1390
|
+
* // 推荐:使用 UPPER_SNAKE_CASE 格式 + 属性装饰器
|
|
1391
|
+
* @Config({
|
|
1392
|
+
* key: "MAX_CONNECTIONS", // 直接对应环境变量 MAX_CONNECTIONS
|
|
1393
|
+
* defaultValue: 100,
|
|
1394
|
+
* })
|
|
1395
|
+
* maxConnections!: number; // TypeScript 完美推断类型
|
|
1396
|
+
*
|
|
1397
|
+
* // 也支持:使用 kebab-case 格式(自动转换)
|
|
1398
|
+
* @Config({
|
|
1399
|
+
* key: "max-connections", // 自动转换为环境变量 MAX_CONNECTIONS
|
|
1400
|
+
* defaultValue: 100,
|
|
1401
|
+
* })
|
|
1402
|
+
* maxConnections!: number;
|
|
1403
|
+
* ```
|
|
1404
|
+
*/
|
|
1405
|
+
interface DynamicConfigOptions extends ConfigMetadata {
|
|
1406
|
+
/**
|
|
1407
|
+
* 配置变更回调(可选)
|
|
1408
|
+
*
|
|
1409
|
+
* 当配置值发生变化时,会触发此回调函数
|
|
1410
|
+
*
|
|
1411
|
+
* @param newValue 新的配置值
|
|
1412
|
+
* @param oldValue 旧的配置值
|
|
1413
|
+
*/
|
|
1414
|
+
onChange?: (newValue: any, oldValue: any) => void | Promise<void>;
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* 插件配置选项
|
|
1418
|
+
*
|
|
1419
|
+
* 定义 DynamicConfigPlugin 的初始化选项
|
|
1420
|
+
*/
|
|
1421
|
+
interface DynamicConfigPluginOptions {
|
|
1422
|
+
/**
|
|
1423
|
+
* Etcd3 客户端实例
|
|
1424
|
+
* 如果未提供且 useMockEtcd 为 false,插件将使用默认配置
|
|
1425
|
+
*/
|
|
1426
|
+
etcdClient?: Etcd3;
|
|
1427
|
+
/**
|
|
1428
|
+
* 是否使用 Mock Etcd(用于测试和本地开发)
|
|
1429
|
+
* @default false
|
|
1430
|
+
*/
|
|
1431
|
+
useMockEtcd?: boolean;
|
|
1432
|
+
/**
|
|
1433
|
+
* etcd 配置键前缀
|
|
1434
|
+
* @default "/config"
|
|
1435
|
+
*/
|
|
1436
|
+
etcdPrefix?: string;
|
|
1437
|
+
/**
|
|
1438
|
+
* 是否启用配置缓存(缓存到 MySQL)
|
|
1439
|
+
* @default false
|
|
1440
|
+
*/
|
|
1441
|
+
enablePersistence?: boolean;
|
|
1442
|
+
/**
|
|
1443
|
+
* MySQL 数据库配置(当 enablePersistence 为 true 时需要)
|
|
1444
|
+
*/
|
|
1445
|
+
mysql?: {
|
|
1446
|
+
host: string;
|
|
1447
|
+
port: number;
|
|
1448
|
+
user: string;
|
|
1449
|
+
password: string;
|
|
1450
|
+
database: string;
|
|
1451
|
+
};
|
|
1452
|
+
/**
|
|
1453
|
+
* 配置同步间隔(毫秒)
|
|
1454
|
+
* 从 MySQL 同步配置到 etcd 的间隔
|
|
1455
|
+
* @default 30000 (30秒)
|
|
1456
|
+
*/
|
|
1457
|
+
syncInterval?: number;
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* 配置存储接口
|
|
1461
|
+
*
|
|
1462
|
+
* 定义配置存储的标准操作,支持 etcd 和内存两种实现
|
|
1463
|
+
*/
|
|
1464
|
+
interface ConfigStorage {
|
|
1465
|
+
/**
|
|
1466
|
+
* 获取配置
|
|
1467
|
+
*/
|
|
1468
|
+
get(key: string): Promise<any>;
|
|
1469
|
+
/**
|
|
1470
|
+
* 设置配置
|
|
1471
|
+
*/
|
|
1472
|
+
set(key: string, value: any, metadata?: ConfigMetadata): Promise<void>;
|
|
1473
|
+
/**
|
|
1474
|
+
* 删除配置
|
|
1475
|
+
*/
|
|
1476
|
+
delete(key: string): Promise<void>;
|
|
1477
|
+
/**
|
|
1478
|
+
* 获取所有配置
|
|
1479
|
+
*/
|
|
1480
|
+
getAll(prefix?: string): Promise<Map<string, any>>;
|
|
1481
|
+
/**
|
|
1482
|
+
* 监听配置变化
|
|
1483
|
+
*/
|
|
1484
|
+
watch(key: string, callback: (newValue: any, oldValue: any) => void): () => void;
|
|
1485
|
+
/**
|
|
1486
|
+
* 同步获取缓存的配置(不访问 etcd)
|
|
1487
|
+
* 用于 configProxy 的同步访问
|
|
1488
|
+
*/
|
|
1489
|
+
getCached?(key: string): any;
|
|
1490
|
+
}
|
|
1491
|
+
/**
|
|
1492
|
+
* 配置项(用于前端管理界面)
|
|
1493
|
+
*
|
|
1494
|
+
* 包含配置的详细信息,用于前端展示和编辑
|
|
1495
|
+
*/
|
|
1496
|
+
interface ConfigItem {
|
|
1497
|
+
/**
|
|
1498
|
+
* 配置键
|
|
1499
|
+
*/
|
|
1500
|
+
key: string;
|
|
1501
|
+
/**
|
|
1502
|
+
* 配置值
|
|
1503
|
+
*/
|
|
1504
|
+
value: any;
|
|
1505
|
+
/**
|
|
1506
|
+
* 配置描述
|
|
1507
|
+
*/
|
|
1508
|
+
description?: string;
|
|
1509
|
+
/**
|
|
1510
|
+
* 配置类型
|
|
1511
|
+
*/
|
|
1512
|
+
type: string;
|
|
1513
|
+
/**
|
|
1514
|
+
* 是否为敏感信息
|
|
1515
|
+
*/
|
|
1516
|
+
sensitive: boolean;
|
|
1517
|
+
/**
|
|
1518
|
+
* 创建时间
|
|
1519
|
+
*/
|
|
1520
|
+
createdAt: Date;
|
|
1521
|
+
/**
|
|
1522
|
+
* 更新时间
|
|
1523
|
+
*/
|
|
1524
|
+
updatedAt: Date;
|
|
1525
|
+
/**
|
|
1526
|
+
* 创建人
|
|
1527
|
+
*/
|
|
1528
|
+
createdBy?: string;
|
|
1529
|
+
/**
|
|
1530
|
+
* 更新人
|
|
1531
|
+
*/
|
|
1532
|
+
updatedBy?: string;
|
|
1533
|
+
}
|
|
1534
|
+
/**
|
|
1535
|
+
* 配置变更历史
|
|
1536
|
+
*
|
|
1537
|
+
* 记录配置的每次变更,用于审计和回滚
|
|
1538
|
+
*/
|
|
1539
|
+
interface ConfigHistory {
|
|
1540
|
+
/**
|
|
1541
|
+
* 历史 ID
|
|
1542
|
+
*/
|
|
1543
|
+
id: string;
|
|
1544
|
+
/**
|
|
1545
|
+
* 配置键
|
|
1546
|
+
*/
|
|
1547
|
+
key: string;
|
|
1548
|
+
/**
|
|
1549
|
+
* 旧值
|
|
1550
|
+
*/
|
|
1551
|
+
oldValue: any;
|
|
1552
|
+
/**
|
|
1553
|
+
* 新值
|
|
1554
|
+
*/
|
|
1555
|
+
newValue: any;
|
|
1556
|
+
/**
|
|
1557
|
+
* 变更时间
|
|
1558
|
+
*/
|
|
1559
|
+
changedAt: Date;
|
|
1560
|
+
/**
|
|
1561
|
+
* 变更人
|
|
1562
|
+
*/
|
|
1563
|
+
changedBy: string;
|
|
1564
|
+
/**
|
|
1565
|
+
* 变更原因
|
|
1566
|
+
*/
|
|
1567
|
+
reason?: string;
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* Module 配置选项
|
|
1571
|
+
*
|
|
1572
|
+
* 用于 @Module 装饰器的 options 参数
|
|
1573
|
+
*/
|
|
1574
|
+
interface DynamicConfigModuleOptions {
|
|
1575
|
+
/**
|
|
1576
|
+
* 配置命名空间(用于区分不同模块的配置)
|
|
1577
|
+
*
|
|
1578
|
+
* 默认使用模块名
|
|
1579
|
+
*/
|
|
1580
|
+
configNamespace?: string;
|
|
1581
|
+
}
|
|
1582
|
+
/**
|
|
1583
|
+
* Etcd3 类型定义
|
|
1584
|
+
*
|
|
1585
|
+
* 避免直接依赖 etcd3 包,提供最小化的类型定义
|
|
1586
|
+
*/
|
|
1587
|
+
interface Etcd3 {
|
|
1588
|
+
get(key: string): {
|
|
1589
|
+
string(): Promise<string | null>;
|
|
1590
|
+
};
|
|
1591
|
+
put(key: string): {
|
|
1592
|
+
value(value: string): Promise<void>;
|
|
1593
|
+
};
|
|
1594
|
+
delete(): {
|
|
1595
|
+
key(key: string): Promise<void>;
|
|
1596
|
+
};
|
|
1597
|
+
getAll(): {
|
|
1598
|
+
prefix(prefix: string): {
|
|
1599
|
+
strings(): Promise<Record<string, string>>;
|
|
1600
|
+
};
|
|
1601
|
+
};
|
|
1602
|
+
watch(): Watch;
|
|
1603
|
+
}
|
|
1604
|
+
interface Watch {
|
|
1605
|
+
key(key: string): WatchBuilder;
|
|
1606
|
+
}
|
|
1607
|
+
interface WatchBuilder {
|
|
1608
|
+
create(): Promise<Watcher>;
|
|
1609
|
+
}
|
|
1610
|
+
interface Watcher {
|
|
1611
|
+
on(event: "put", callback: (kv: {
|
|
1612
|
+
key: Buffer;
|
|
1613
|
+
value: Buffer;
|
|
1614
|
+
}) => void): void;
|
|
1615
|
+
on(event: "delete", callback: (kv: {
|
|
1616
|
+
key: Buffer;
|
|
1617
|
+
}) => void): void;
|
|
1618
|
+
cancel(): void;
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
/**
|
|
1622
|
+
* DynamicConfigPlugin - 动态配置插件
|
|
1623
|
+
*
|
|
1624
|
+
* 提供基于 etcd 的动态配置功能,支持:
|
|
1625
|
+
* - 装饰器驱动的配置定义 (@Config)
|
|
1626
|
+
* - 实时配置热更新(通过 etcd watch 机制)
|
|
1627
|
+
* - 类型安全(Zod Schema 运行时验证)
|
|
1628
|
+
* - 完美的 TypeScript 类型推断
|
|
1629
|
+
* - 自动预加载配置到缓存
|
|
1630
|
+
* - 配置变更回调
|
|
1631
|
+
* - 同步访问配置
|
|
1632
|
+
*
|
|
1633
|
+
* @example
|
|
1634
|
+
* ```typescript
|
|
1635
|
+
* // 基础用法
|
|
1636
|
+
* const plugin = new DynamicConfigPlugin({
|
|
1637
|
+
* etcdClient: etcdInstance,
|
|
1638
|
+
* etcdPrefix: "/config",
|
|
1639
|
+
* });
|
|
1640
|
+
*
|
|
1641
|
+
* @Module("user-service")
|
|
1642
|
+
* class UserService {
|
|
1643
|
+
* @Config({
|
|
1644
|
+
* key: "MAX_LOGIN_ATTEMPTS",
|
|
1645
|
+
* defaultValue: 5,
|
|
1646
|
+
* schema: z.number().min(1).max(10),
|
|
1647
|
+
* })
|
|
1648
|
+
* maxLoginAttempts!: number;
|
|
1649
|
+
* }
|
|
1650
|
+
* ```
|
|
1651
|
+
*/
|
|
1652
|
+
declare class DynamicConfigPlugin implements Plugin<DynamicConfigModuleOptions> {
|
|
1653
|
+
readonly name = "dynamic-config-plugin";
|
|
1654
|
+
readonly priority = PluginPriority.BUSINESS;
|
|
1655
|
+
private engine;
|
|
1656
|
+
private storage;
|
|
1657
|
+
private configHandlers;
|
|
1658
|
+
private unwatchFunctions;
|
|
1659
|
+
constructor(options?: DynamicConfigPluginOptions);
|
|
1660
|
+
/**
|
|
1661
|
+
* 声明 Module 配置 Schema
|
|
1662
|
+
*/
|
|
1663
|
+
getModuleOptionsSchema(): PluginModuleOptionsSchema<DynamicConfigModuleOptions>;
|
|
1664
|
+
/**
|
|
1665
|
+
* 引擎初始化钩子
|
|
1666
|
+
*/
|
|
1667
|
+
onInit(engine: Microservice): void;
|
|
1668
|
+
/**
|
|
1669
|
+
* Handler 加载钩子:收集所有动态配置
|
|
1670
|
+
*/
|
|
1671
|
+
onHandlerLoad(handlers: HandlerMetadata[]): void;
|
|
1672
|
+
/**
|
|
1673
|
+
* 包装配置方法,实现动态配置注入
|
|
1674
|
+
*/
|
|
1675
|
+
private wrapConfigHandler;
|
|
1676
|
+
/**
|
|
1677
|
+
* 引擎启动后钩子:启动配置监听和预加载配置
|
|
1678
|
+
*/
|
|
1679
|
+
onAfterStart(engine: Microservice): Promise<void>;
|
|
1680
|
+
/**
|
|
1681
|
+
* 预加载所有配置到缓存,并将配置方法/属性转换为同步 getter
|
|
1682
|
+
*/
|
|
1683
|
+
private preloadConfigs;
|
|
1684
|
+
/**
|
|
1685
|
+
* 引擎销毁钩子:清理资源
|
|
1686
|
+
*/
|
|
1687
|
+
onDestroy(): Promise<void>;
|
|
1688
|
+
/**
|
|
1689
|
+
* 获取模块名称
|
|
1690
|
+
*/
|
|
1691
|
+
private getModuleName;
|
|
1692
|
+
/**
|
|
1693
|
+
* 构建完整的配置键
|
|
1694
|
+
*/
|
|
1695
|
+
private buildConfigKey;
|
|
1696
|
+
/**
|
|
1697
|
+
* 公共 API:获取配置
|
|
1698
|
+
*/
|
|
1699
|
+
getConfig(key: string): Promise<any>;
|
|
1700
|
+
/**
|
|
1701
|
+
* 公共 API:同步获取配置(从缓存)
|
|
1702
|
+
* 用于 configProxy 的同步访问
|
|
1703
|
+
*/
|
|
1704
|
+
getConfigCached(key: string): any;
|
|
1705
|
+
/**
|
|
1706
|
+
* 公共 API:设置配置
|
|
1707
|
+
*/
|
|
1708
|
+
setConfig(key: string, value: any, metadata?: ConfigMetadata): Promise<void>;
|
|
1709
|
+
/**
|
|
1710
|
+
* 公共 API:删除配置
|
|
1711
|
+
*/
|
|
1712
|
+
deleteConfig(key: string): Promise<void>;
|
|
1713
|
+
/**
|
|
1714
|
+
* 公共 API:获取所有配置
|
|
1715
|
+
*/
|
|
1716
|
+
getAllConfigs(prefix?: string): Promise<Map<string, any>>;
|
|
1717
|
+
/**
|
|
1718
|
+
* 公共 API:监听配置变化
|
|
1719
|
+
*/
|
|
1720
|
+
watchConfig(key: string, callback: (newValue: any, oldValue: any) => void): () => void;
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
/**
|
|
1724
|
+
* Config 属性装饰器
|
|
1725
|
+
*
|
|
1726
|
+
* 标记需要动态配置的属性,使其能够从 etcd 动态读取配置值
|
|
1727
|
+
* ✅ 完美的 TypeScript 类型推断,无需类型断言
|
|
1728
|
+
*
|
|
1729
|
+
* @param options 动态配置选项
|
|
1730
|
+
* @returns 属性装饰器
|
|
1731
|
+
*
|
|
1732
|
+
* @example
|
|
1733
|
+
* ```typescript
|
|
1734
|
+
* @Module("user-service")
|
|
1735
|
+
* class UserService {
|
|
1736
|
+
* // ✅ 使用属性装饰器,完美的类型推断
|
|
1737
|
+
* @Config({
|
|
1738
|
+
* key: "MAX_LOGIN_ATTEMPTS",
|
|
1739
|
+
* description: "最大登录尝试次数",
|
|
1740
|
+
* schema: z.number().min(1).max(10),
|
|
1741
|
+
* defaultValue: 5,
|
|
1742
|
+
* })
|
|
1743
|
+
* maxLoginAttempts!: number; // TypeScript 正确识别为 number
|
|
1744
|
+
*
|
|
1745
|
+
* async login(username: string, password: string) {
|
|
1746
|
+
* // ✅ 完美:直接访问,类型正确
|
|
1747
|
+
* const maxAttempts = this.maxLoginAttempts; // number 类型
|
|
1748
|
+
* if (attempts > maxAttempts) {
|
|
1749
|
+
* throw new Error("Too many attempts");
|
|
1750
|
+
* }
|
|
1751
|
+
* }
|
|
1752
|
+
* }
|
|
1753
|
+
* ```
|
|
1754
|
+
*
|
|
1755
|
+
* @example
|
|
1756
|
+
* ```typescript
|
|
1757
|
+
* // 带变更回调
|
|
1758
|
+
* type FeatureFlags = {
|
|
1759
|
+
* enableNewUI: boolean;
|
|
1760
|
+
* enableBetaFeatures: boolean;
|
|
1761
|
+
* };
|
|
1762
|
+
*
|
|
1763
|
+
* @Config({
|
|
1764
|
+
* key: "FEATURE_FLAGS",
|
|
1765
|
+
* schema: z.object({
|
|
1766
|
+
* enableNewUI: z.boolean(),
|
|
1767
|
+
* enableBetaFeatures: z.boolean(),
|
|
1768
|
+
* }),
|
|
1769
|
+
* defaultValue: {
|
|
1770
|
+
* enableNewUI: false,
|
|
1771
|
+
* enableBetaFeatures: false,
|
|
1772
|
+
* },
|
|
1773
|
+
* onChange: async (newValue, oldValue) => {
|
|
1774
|
+
* console.log("Feature flags changed:", { newValue, oldValue });
|
|
1775
|
+
* },
|
|
1776
|
+
* })
|
|
1777
|
+
* featureFlags!: FeatureFlags; // TypeScript 正确识别类型
|
|
1778
|
+
* ```
|
|
1779
|
+
*
|
|
1780
|
+
* @example
|
|
1781
|
+
* ```typescript
|
|
1782
|
+
* // 支持环境变量(优先级:ETCD > ENV > DEFAULT)
|
|
1783
|
+
* @Config({
|
|
1784
|
+
* key: "MAX_CONNECTIONS",
|
|
1785
|
+
* schema: z.number().min(1).max(1000),
|
|
1786
|
+
* defaultValue: 100,
|
|
1787
|
+
* })
|
|
1788
|
+
* maxConnections!: number;
|
|
1789
|
+
*
|
|
1790
|
+
* // 使用时完美
|
|
1791
|
+
* useConnection() {
|
|
1792
|
+
* const num = this.maxConnections + 10; // ✅ 类型正确,可以直接运算
|
|
1793
|
+
* const sum = this.maxConnections * 2; // ✅ 类型正确,可以直接运算
|
|
1794
|
+
* }
|
|
1795
|
+
* ```
|
|
1796
|
+
*/
|
|
1797
|
+
declare function Config(options: DynamicConfigOptions): (target: undefined, context: ClassFieldDecoratorContext) => void;
|
|
1798
|
+
|
|
1799
|
+
/**
|
|
1800
|
+
* Etcd 配置存储实现
|
|
1801
|
+
*
|
|
1802
|
+
* 负责与 etcd 交互,提供配置的读取、写入、删除、监听功能
|
|
1803
|
+
* 内置缓存机制,减少 etcd 访问次数
|
|
1804
|
+
*/
|
|
1805
|
+
declare class EtcdConfigStorage implements ConfigStorage {
|
|
1806
|
+
private etcdClient;
|
|
1807
|
+
private prefix;
|
|
1808
|
+
private watchers;
|
|
1809
|
+
private cache;
|
|
1810
|
+
constructor(etcdClient: Etcd3, prefix?: string);
|
|
1811
|
+
/**
|
|
1812
|
+
* 构建完整的 etcd 键
|
|
1813
|
+
*/
|
|
1814
|
+
private buildKey;
|
|
1815
|
+
/**
|
|
1816
|
+
* 获取配置
|
|
1817
|
+
*/
|
|
1818
|
+
get(key: string): Promise<any>;
|
|
1819
|
+
/**
|
|
1820
|
+
* 同步获取缓存的配置(不访问 etcd)
|
|
1821
|
+
* 用于 configProxy 的同步访问
|
|
1822
|
+
*/
|
|
1823
|
+
getCached(key: string): any;
|
|
1824
|
+
/**
|
|
1825
|
+
* 设置配置
|
|
1826
|
+
*/
|
|
1827
|
+
set(key: string, value: any, metadata?: ConfigMetadata): Promise<void>;
|
|
1828
|
+
/**
|
|
1829
|
+
* 删除配置
|
|
1830
|
+
*/
|
|
1831
|
+
delete(key: string): Promise<void>;
|
|
1832
|
+
/**
|
|
1833
|
+
* 获取所有配置
|
|
1834
|
+
*/
|
|
1835
|
+
getAll(prefix?: string): Promise<Map<string, any>>;
|
|
1836
|
+
/**
|
|
1837
|
+
* 监听配置变化
|
|
1838
|
+
*/
|
|
1839
|
+
watch(key: string, callback: (newValue: any, oldValue: any) => void): () => void;
|
|
1840
|
+
/**
|
|
1841
|
+
* 清理资源
|
|
1842
|
+
*/
|
|
1843
|
+
destroy(): Promise<void>;
|
|
1844
|
+
}
|
|
1845
|
+
/**
|
|
1846
|
+
* 内存配置存储实现
|
|
1847
|
+
*
|
|
1848
|
+
* 用于测试和本地开发环境,不依赖 etcd
|
|
1849
|
+
* 配置存储在内存中,服务重启后会丢失
|
|
1850
|
+
*/
|
|
1851
|
+
declare class MemoryConfigStorage implements ConfigStorage {
|
|
1852
|
+
private storage;
|
|
1853
|
+
private watchers;
|
|
1854
|
+
get(key: string): Promise<any>;
|
|
1855
|
+
getCached(key: string): any;
|
|
1856
|
+
set(key: string, value: any, metadata?: ConfigMetadata): Promise<void>;
|
|
1857
|
+
delete(key: string): Promise<void>;
|
|
1858
|
+
getAll(prefix?: string): Promise<Map<string, any>>;
|
|
1859
|
+
watch(key: string, callback: (newValue: any, oldValue: any) => void): () => void;
|
|
1860
|
+
destroy(): Promise<void>;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1342
1863
|
/**
|
|
1343
1864
|
* 从插件数组中提取聚合的配置类型
|
|
1344
1865
|
*/
|
|
@@ -1418,4 +1939,4 @@ declare const Testing: {
|
|
|
1418
1939
|
|
|
1419
1940
|
declare const logger: winston.Logger;
|
|
1420
1941
|
|
|
1421
|
-
export { Action, type ActionInfo, type ActionModuleOptions, type ActionOptions, ActionPlugin, BaseLayout, Cache, type CacheAdapter, type CacheItem, type CacheModuleOptions, type CacheOptions, CachePlugin, type Campaign, type Class, type ClassDecoratorContext, type ClassMethodDecoratorContext$1 as ClassMethodDecoratorContext, ClientCodePlugin, type ClientCodePluginOptions, DEFAULT_TEST_OPTIONS, DuplicateModuleError, type Election, type ErrorTransformer, type Etcd3, Factory, GracefulShutdownPlugin, type GracefulShutdownPluginOptions, type HTTPMethod, Handler, type HandlerMetadata, type HandlerWrapper, HtmxLayout, MemoryCacheAdapter, Microservice, type MicroserviceOptions, MockEtcd3, ModuleConfigValidationError, type ModuleDecorator, type ModuleInfo, type ModuleMetadata, type Observer, Page, type Plugin, type PluginModuleOptionsSchema, PluginNameRequiredError, PluginPriority, type PreStartChecker, RedisCacheAdapter, type RedisCacheAdapterOptions, Route, type RouteModuleOptions, type RouteOptions, RoutePlugin, type RoutePluginOptions, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, SchedulePlugin, type SchedulePluginOptions, Scheduler, ServiceInfoCards, type ServiceStatusInfo, ServiceStatusPage, Testing, convertHandlersToModuleInfo, convertHandlersToModuleInfoWithMetadata, generateClientCode, getZodTypeString, logger, startCheck };
|
|
1942
|
+
export { Action, type ActionInfo, type ActionModuleOptions, type ActionOptions, ActionPlugin, BaseLayout, Cache, type CacheAdapter, type CacheItem, type CacheModuleOptions, type CacheOptions, CachePlugin, type Campaign, type Class, type ClassDecoratorContext, type ClassMethodDecoratorContext$1 as ClassMethodDecoratorContext, ClientCodePlugin, type ClientCodePluginOptions, Config, type ConfigHistory, type ConfigItem, type ConfigMetadata, type ConfigStorage, DEFAULT_TEST_OPTIONS, DuplicateModuleError, type DynamicConfigModuleOptions, type DynamicConfigOptions, DynamicConfigPlugin, type DynamicConfigPluginOptions, type Election, type ErrorTransformer, type Etcd3$1 as Etcd3, EtcdConfigStorage, Factory, GracefulShutdownPlugin, type GracefulShutdownPluginOptions, type HTTPMethod, Handler, type HandlerMetadata, type HandlerWrapper, HtmxLayout, MemoryCacheAdapter, MemoryConfigStorage, Microservice, type MicroserviceOptions, MockEtcd3, ModuleConfigValidationError, type ModuleDecorator, type ModuleInfo, type ModuleMetadata, type Observer, Page, type Plugin, type PluginModuleOptionsSchema, PluginNameRequiredError, PluginPriority, type PreStartChecker, RedisCacheAdapter, type RedisCacheAdapterOptions, Route, type RouteModuleOptions, type RouteOptions, RoutePlugin, type RoutePluginOptions, Schedule, type ScheduleMetadata, ScheduleMode, type ScheduleOptions, SchedulePlugin, type SchedulePluginOptions, Scheduler, ServiceInfoCards, type ServiceStatusInfo, ServiceStatusPage, Testing, convertHandlersToModuleInfo, convertHandlersToModuleInfoWithMetadata, generateClientCode, getZodTypeString, logger, startCheck };
|