@h-ai/core 0.1.0-alpha.13 → 0.1.0-alpha.16
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 +62 -0
- package/dist/browser.d.ts +3 -2
- package/dist/browser.js +2 -2
- package/dist/{chunk-STH6Q4GV.js → chunk-JM2QORKH.js} +118 -2
- package/dist/chunk-JM2QORKH.js.map +1 -0
- package/dist/{core-util-module-B-wH5_ZT.d.ts → core-types-aT8pmvMq.d.ts} +126 -45
- package/dist/node.d.ts +195 -33
- package/dist/node.js +4 -2
- package/dist/node.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-STH6Q4GV.js.map +0 -1
|
@@ -169,6 +169,82 @@ declare const CoreConfigSchema: z.ZodObject<{
|
|
|
169
169
|
}, z.core.$strip>;
|
|
170
170
|
type CoreConfig = z.infer<typeof CoreConfigSchema>;
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* 校验消息 key 集合(locale 无关的标识符)。
|
|
174
|
+
*
|
|
175
|
+
* 调用方负责把每个 key 映射到自己模块的 i18n 字典(例如 kit 的
|
|
176
|
+
* `kit_validationStringMin`、serv 的 `serv_validationStringMin`)。
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const key: ZodValidationMessageKey = 'validationRequired'
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
/**
|
|
184
|
+
* 创建按固定前缀派生消息 key 的 `ZodMessageGetter`。
|
|
185
|
+
*
|
|
186
|
+
* 适用于模块消息键遵循 `{prefix}_{ZodValidationMessageKey}` 命名约定的场景,
|
|
187
|
+
* 例如 `serv_validationRequired`、`kit_validationEmail`。
|
|
188
|
+
*
|
|
189
|
+
* @param prefix - 模块消息前缀,如 `serv`、`kit`
|
|
190
|
+
* @param getMessage - 实际消息获取函数,接收带前缀的消息 key
|
|
191
|
+
* @returns 可注入给 `core.zodValidation.*` 的 `ZodMessageGetter`
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const getMessage = createPrefixedZodMessageGetter<string>(
|
|
196
|
+
* 'serv',
|
|
197
|
+
* (messageKey, params) => `${messageKey}:${params?.min ?? ''}`,
|
|
198
|
+
* )
|
|
199
|
+
*
|
|
200
|
+
* getMessage('validationStringMin', { min: 3 })
|
|
201
|
+
* // 'serv_validationStringMin:3'
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function createPrefixedZodMessageGetter<TMessageKey extends string>(prefix: string, getMessage: (messageKey: TMessageKey, params?: Record<string, string | number>) => string): ZodMessageGetter;
|
|
205
|
+
/**
|
|
206
|
+
* 一步把 Zod SafeParseError / ZodError 转为扁平 `ValidationFormError[]`。
|
|
207
|
+
*
|
|
208
|
+
* @param error - Zod `SafeParseError`、`ZodError` 或兼容的错误对象
|
|
209
|
+
* @param getMessage - 调用方注入的消息获取器
|
|
210
|
+
* @returns 扁平化后的表单错误列表
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* const errors = mapZodErrorToFormErrors(
|
|
215
|
+
* {
|
|
216
|
+
* issues: [
|
|
217
|
+
* { path: ['email'], code: 'invalid_format', format: 'email', message: 'Invalid email address' },
|
|
218
|
+
* ],
|
|
219
|
+
* },
|
|
220
|
+
* key => key === 'validationEmail' ? '请输入合法邮箱地址' : '输入不合法',
|
|
221
|
+
* )
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
declare function mapZodErrorToFormErrors(error: unknown, getMessage: ZodMessageGetter): ValidationFormError[];
|
|
225
|
+
/**
|
|
226
|
+
* Zod 校验 i18n 工具集合(通过 `core.zodValidation` 暴露)。
|
|
227
|
+
*
|
|
228
|
+
* 按最小知识原则,仅暴露调用方真正需要的两个入口:
|
|
229
|
+
* 1. `createPrefixedZodMessageGetter()`:把模块前缀与消息获取器适配起来
|
|
230
|
+
* 2. `mapZodErrorToFormErrors()`:一步完成 ZodError → 本地化扁平错误列表
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* const getMessage = zodValidation.createPrefixedZodMessageGetter(
|
|
235
|
+
* 'kit',
|
|
236
|
+
* (messageKey, params) => messageKey === 'kit_validationEmail'
|
|
237
|
+
* ? '请输入合法邮箱地址'
|
|
238
|
+
* : `至少输入 ${params?.min} 个字符`,
|
|
239
|
+
* )
|
|
240
|
+
* const errors = zodValidation.mapZodErrorToFormErrors(zodError, getMessage)
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
declare const zodValidation: {
|
|
244
|
+
createPrefixedZodMessageGetter: typeof createPrefixedZodMessageGetter;
|
|
245
|
+
mapZodErrorToFormErrors: typeof mapZodErrorToFormErrors;
|
|
246
|
+
};
|
|
247
|
+
|
|
172
248
|
/**
|
|
173
249
|
* @h-ai/core — 错误注册与处理
|
|
174
250
|
*
|
|
@@ -670,6 +746,43 @@ declare const async: {
|
|
|
670
746
|
/** async 子工具类型 */
|
|
671
747
|
type AsyncFn = typeof async;
|
|
672
748
|
|
|
749
|
+
/**
|
|
750
|
+
* @h-ai/core — 模块初始化工具
|
|
751
|
+
*
|
|
752
|
+
* 封装各模块共同的「未初始化」错误处理模式,消除跨模块冗余。
|
|
753
|
+
* @module core-util-module
|
|
754
|
+
*/
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* 未初始化工具集返回类型。
|
|
758
|
+
*
|
|
759
|
+
* 提供错误创建、HaiResult 包装和 Proxy 代理等能力,
|
|
760
|
+
* 用于模块未初始化时的安全回退。
|
|
761
|
+
*
|
|
762
|
+
* @template E - 模块错误类型(必须继承 HaiError)
|
|
763
|
+
*/
|
|
764
|
+
interface NotInitializedKit<E extends HaiError> {
|
|
765
|
+
/** 创建未初始化错误对象 */
|
|
766
|
+
error: () => E;
|
|
767
|
+
/** 创建包含未初始化错误的失败 HaiResult */
|
|
768
|
+
result: <T>() => HaiResult<T>;
|
|
769
|
+
/**
|
|
770
|
+
* 创建 Proxy 代理,拦截所有方法调用并返回未初始化错误。
|
|
771
|
+
*
|
|
772
|
+
* @param mode - 'async'(默认)所有方法返回 `Promise<HaiResult>`;'sync' 所有方法返回 `HaiResult`
|
|
773
|
+
*/
|
|
774
|
+
proxy: <T>(mode?: 'async' | 'sync') => T;
|
|
775
|
+
}
|
|
776
|
+
/** module 子工具类型 */
|
|
777
|
+
/** overloaded function type for createNotInitializedKit */
|
|
778
|
+
interface CreateNotInitializedKitFn {
|
|
779
|
+
(codeOrDef: HaiErrorDef, messageFn: () => string): NotInitializedKit<HaiError>;
|
|
780
|
+
<E extends HaiError>(codeOrDef: E['code'], messageFn: () => string): NotInitializedKit<E>;
|
|
781
|
+
}
|
|
782
|
+
interface ModuleFn {
|
|
783
|
+
createNotInitializedKit: CreateNotInitializedKitFn;
|
|
784
|
+
}
|
|
785
|
+
|
|
673
786
|
/**
|
|
674
787
|
* @h-ai/core — 对象操作工具
|
|
675
788
|
* @module core-util-object
|
|
@@ -1222,13 +1335,16 @@ declare const typeUtils: {
|
|
|
1222
1335
|
/** typeUtils 子工具类型 */
|
|
1223
1336
|
type TypeUtilFn = typeof typeUtils;
|
|
1224
1337
|
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1338
|
+
type ZodValidationMessageKey = 'validationFailed' | 'validationRequired' | 'validationInvalid' | 'validationInvalidType' | 'validationStringMin' | 'validationStringMax' | 'validationNumberMin' | 'validationNumberMax' | 'validationArrayMin' | 'validationArrayMax' | 'validationTooSmall' | 'validationTooBig' | 'validationEmail' | 'validationUrl' | 'validationUuid' | 'validationEnum';
|
|
1339
|
+
type ZodMessageGetter = (key: ZodValidationMessageKey, params?: Record<string, string | number>) => string;
|
|
1340
|
+
interface ValidationFormError {
|
|
1341
|
+
field: string;
|
|
1342
|
+
message: string;
|
|
1343
|
+
}
|
|
1344
|
+
interface ZodValidationFn {
|
|
1345
|
+
readonly createPrefixedZodMessageGetter: <TMessageKey extends string>(prefix: string, getMessage: (messageKey: TMessageKey, params?: Record<string, string | number>) => string) => ZodMessageGetter;
|
|
1346
|
+
readonly mapZodErrorToFormErrors: (error: unknown, getMessage: ZodMessageGetter) => ValidationFormError[];
|
|
1347
|
+
}
|
|
1232
1348
|
type HaiResult<T> = {
|
|
1233
1349
|
success: true;
|
|
1234
1350
|
data: T;
|
|
@@ -1566,6 +1682,8 @@ interface CoreFunctions {
|
|
|
1566
1682
|
readonly logger: CoreLogger;
|
|
1567
1683
|
/** 国际化工具 */
|
|
1568
1684
|
readonly i18n: I18nFn;
|
|
1685
|
+
/** Zod 校验错误 → i18n 消息映射工具(kit / serv / api-client 共享) */
|
|
1686
|
+
readonly zodValidation: typeof zodValidation;
|
|
1569
1687
|
/** ID 生成工具 */
|
|
1570
1688
|
readonly id: IdFn;
|
|
1571
1689
|
/** 类型检查工具 */
|
|
@@ -1586,41 +1704,4 @@ interface CoreFunctions {
|
|
|
1586
1704
|
readonly module: ModuleFn;
|
|
1587
1705
|
}
|
|
1588
1706
|
|
|
1589
|
-
|
|
1590
|
-
* @h-ai/core — 模块初始化工具
|
|
1591
|
-
*
|
|
1592
|
-
* 封装各模块共同的「未初始化」错误处理模式,消除跨模块冗余。
|
|
1593
|
-
* @module core-util-module
|
|
1594
|
-
*/
|
|
1595
|
-
|
|
1596
|
-
/**
|
|
1597
|
-
* 未初始化工具集返回类型。
|
|
1598
|
-
*
|
|
1599
|
-
* 提供错误创建、HaiResult 包装和 Proxy 代理等能力,
|
|
1600
|
-
* 用于模块未初始化时的安全回退。
|
|
1601
|
-
*
|
|
1602
|
-
* @template E - 模块错误类型(必须继承 HaiError)
|
|
1603
|
-
*/
|
|
1604
|
-
interface NotInitializedKit<E extends HaiError> {
|
|
1605
|
-
/** 创建未初始化错误对象 */
|
|
1606
|
-
error: () => E;
|
|
1607
|
-
/** 创建包含未初始化错误的失败 HaiResult */
|
|
1608
|
-
result: <T>() => HaiResult<T>;
|
|
1609
|
-
/**
|
|
1610
|
-
* 创建 Proxy 代理,拦截所有方法调用并返回未初始化错误。
|
|
1611
|
-
*
|
|
1612
|
-
* @param mode - 'async'(默认)所有方法返回 `Promise<HaiResult>`;'sync' 所有方法返回 `HaiResult`
|
|
1613
|
-
*/
|
|
1614
|
-
proxy: <T>(mode?: 'async' | 'sync') => T;
|
|
1615
|
-
}
|
|
1616
|
-
/** module 子工具类型 */
|
|
1617
|
-
/** overloaded function type for createNotInitializedKit */
|
|
1618
|
-
interface CreateNotInitializedKitFn {
|
|
1619
|
-
(codeOrDef: HaiErrorDef, messageFn: () => string): NotInitializedKit<HaiError>;
|
|
1620
|
-
<E extends HaiError>(codeOrDef: E['code'], messageFn: () => string): NotInitializedKit<E>;
|
|
1621
|
-
}
|
|
1622
|
-
interface ModuleFn {
|
|
1623
|
-
createNotInitializedKit: CreateNotInitializedKitFn;
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
|
-
export { type ArrayFn as A, type LoggerFunctions as B, type CoreOptions as C, type LoggerOptions as D, type ErrorFn as E, type LoggingConfig as F, LoggingConfigSchema as G, type HaiError as H, type I18nFn as I, type MatchHandlers as J, type MessageDictionary as K, type Locale as L, type ModuleFn as M, type MessageOptions as N, type ObjectFn as O, type PaginatedResult as P, type PaginationOptions as Q, type PaginationOptionsInput as R, type StringFn as S, type TypeUtilFn as T, err as U, ok as V, type HaiResult as a, type CoreLogger as b, type IdFn as c, type AsyncFn as d, type TimeFn as e, type CoreConfig as f, CoreConfigSchema as g, type CoreFunctions as h, type Env as i, EnvSchema as j, type ErrorInfo as k, type ErrorInfoValue as l, HaiCommonError as m, HaiConfigError as n, type HaiErrorDef as o, type IdConfig as p, IdConfigSchema as q, type InterpolationParams as r, type LocaleInfo as s, type LocaleMessages as t, type LogContext as u, type LogFormat as v, LogFormatSchema as w, type LogLevel as x, LogLevelSchema as y, type Logger as z };
|
|
1707
|
+
export { type MessageOptions as A, type PaginationOptions as B, type CoreFunctions as C, type PaginationOptionsInput as D, type Env as E, type ZodValidationFn as F, type ZodValidationMessageKey as G, type HaiResult as H, type IdConfig as I, err as J, ok as K, type Locale as L, type MatchHandlers as M, type I18nFn as N, zodValidation as O, type PaginatedResult as P, type IdFn as Q, type ObjectFn as R, type StringFn as S, type TypeUtilFn as T, type ArrayFn as U, type ValidationFormError as V, type AsyncFn as W, type TimeFn as X, type ErrorFn as Y, type ZodMessageGetter as Z, type ModuleFn as _, type HaiError as a, type CoreOptions as b, type CoreConfig as c, CoreConfigSchema as d, type CoreLogger as e, EnvSchema as f, type ErrorInfo as g, type ErrorInfoValue as h, HaiCommonError as i, HaiConfigError as j, type HaiErrorDef as k, IdConfigSchema as l, type InterpolationParams as m, type LocaleInfo as n, type LocaleMessages as o, type LogContext as p, type LogFormat as q, LogFormatSchema as r, type LogLevel as s, LogLevelSchema as t, type Logger as u, type LoggerFunctions as v, type LoggerOptions as w, type LoggingConfig as x, LoggingConfigSchema as y, type MessageDictionary as z };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { H as
|
|
2
|
-
export {
|
|
3
|
-
import
|
|
1
|
+
import { H as HaiResult, a as HaiError, C as CoreFunctions, b as CoreOptions } from './core-types-aT8pmvMq.js';
|
|
2
|
+
export { c as CoreConfig, d as CoreConfigSchema, e as CoreLogger, E as Env, f as EnvSchema, g as ErrorInfo, h as ErrorInfoValue, i as HaiCommonError, j as HaiConfigError, k as HaiErrorDef, I as IdConfig, l as IdConfigSchema, m as InterpolationParams, L as Locale, n as LocaleInfo, o as LocaleMessages, p as LogContext, q as LogFormat, r as LogFormatSchema, s as LogLevel, t as LogLevelSchema, u as Logger, v as LoggerFunctions, w as LoggerOptions, x as LoggingConfig, y as LoggingConfigSchema, M as MatchHandlers, z as MessageDictionary, A as MessageOptions, P as PaginatedResult, B as PaginationOptions, D as PaginationOptionsInput, V as ValidationFormError, Z as ZodMessageGetter, F as ZodValidationFn, G as ZodValidationMessageKey, J as err, K as ok } from './core-types-aT8pmvMq.js';
|
|
3
|
+
import { ZodType } from 'zod';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @h-ai/core — 配置管理(Node.js 专用)
|
|
@@ -23,7 +23,197 @@ import * as zod from 'zod';
|
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
25
|
type WatchCallback<T = unknown> = (config: T | null, error?: HaiError) => void;
|
|
26
|
+
/**
|
|
27
|
+
* 配置管理对象。
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const result = config.load('app', './config/app.yml')
|
|
32
|
+
* if (result.success) {
|
|
33
|
+
* const cfg = config.get('app')
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
declare const config: {
|
|
38
|
+
/**
|
|
39
|
+
* 加载配置到缓存。
|
|
40
|
+
*
|
|
41
|
+
* 加载 YAML 文件并可选地用 Zod Schema 校验,成功后写入缓存。
|
|
42
|
+
*
|
|
43
|
+
* @param name - 配置名称(缓存 key)
|
|
44
|
+
* @param filePath - YAML 文件路径
|
|
45
|
+
* @param schema - 可选 Zod Schema(不传则跳过校验)
|
|
46
|
+
* @returns 成功时返回解析后的配置数据;失败时返回 HaiError
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const result = config.load('core', './config/_core.yml', CoreConfigSchema)
|
|
51
|
+
* if (result.success) {
|
|
52
|
+
* // result.data 为校验后的配置
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
load<T>(name: string, filePath: string, schema?: ZodType<T>): HaiResult<T>;
|
|
57
|
+
/**
|
|
58
|
+
* 验证已加载的配置数据。
|
|
59
|
+
*
|
|
60
|
+
* 对缓存中的配置数据重新用 Schema 校验,校验通过后更新缓存。
|
|
61
|
+
*
|
|
62
|
+
* @param name - 配置名称
|
|
63
|
+
* @param schema - Zod 验证模式
|
|
64
|
+
* @returns 校验结果;未加载时返回 NOT_LOADED,格式错误返回 VALIDATION_ERROR
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const result = config.validate('app', AppSchema)
|
|
69
|
+
* if (!result.success) {
|
|
70
|
+
* // result.error.code 可能为 NOT_LOADED 或 VALIDATION_ERROR
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
validate<T>(name: string, schema: ZodType<T>): HaiResult<T>;
|
|
75
|
+
/**
|
|
76
|
+
* 获取已加载的配置。
|
|
77
|
+
*
|
|
78
|
+
* @param name - 配置名称
|
|
79
|
+
* @returns 配置数据;未加载时返回 undefined
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const cfg = config.get<CoreConfig>('core')
|
|
84
|
+
* if (cfg) {
|
|
85
|
+
* // 使用 cfg
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
get<T>(name: string): T | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* 获取配置,不存在时抛出错误。
|
|
92
|
+
*
|
|
93
|
+
* @param name - 配置名称
|
|
94
|
+
* @returns 配置数据
|
|
95
|
+
* @throws 配置未加载时抛出 Error
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* try {
|
|
100
|
+
* const cfg = config.getOrThrow<CoreConfig>('core')
|
|
101
|
+
* } catch (e) {
|
|
102
|
+
* // 配置未加载
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
getOrThrow<T>(name: string): T;
|
|
107
|
+
/**
|
|
108
|
+
* 重新加载配置。
|
|
109
|
+
*
|
|
110
|
+
* 从磁盘重新读取配置文件并更新缓存,同时通知所有 watch 回调。
|
|
111
|
+
*
|
|
112
|
+
* @param name - 配置名称
|
|
113
|
+
* @returns 重载结果;未加载时返回 NOT_LOADED
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const result = config.reload('app')
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
reload(name: string): HaiResult<unknown>;
|
|
121
|
+
/**
|
|
122
|
+
* 检查配置是否已加载。
|
|
123
|
+
*
|
|
124
|
+
* @param name - 配置名称
|
|
125
|
+
* @returns 是否已加载到缓存
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* if (config.has('db')) {
|
|
130
|
+
* const dbCfg = config.get('db')
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
has(name: string): boolean;
|
|
135
|
+
/**
|
|
136
|
+
* 清除配置缓存(同时停止对应监听)。
|
|
137
|
+
*
|
|
138
|
+
* @param name - 配置名称;不传则清除全部
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* config.clear('app') // 清除单个
|
|
143
|
+
* config.clear() // 清除全部
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
clear(name?: string): void;
|
|
147
|
+
/**
|
|
148
|
+
* 获取所有已加载的配置名称。
|
|
149
|
+
*
|
|
150
|
+
* @returns 配置名称数组
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const names = config.keys() // ['core', 'db', 'app']
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
keys(): string[];
|
|
158
|
+
/**
|
|
159
|
+
* 监听配置文件变更并自动重载。
|
|
160
|
+
*
|
|
161
|
+
* 文件变更时自动重新加载并调用回调。配置未加载时立即回调 NOT_LOADED 错误。
|
|
162
|
+
*
|
|
163
|
+
* @param name - 配置名称
|
|
164
|
+
* @param callback - 配置变更回调,接收新配置或错误
|
|
165
|
+
* @returns 取消监听函数
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* const unwatch = config.watch('app', (cfg, error) => {
|
|
170
|
+
* if (error) { core.logger.error('reload failed', { error }); return }
|
|
171
|
+
* core.logger.info('config updated', { cfg })
|
|
172
|
+
* })
|
|
173
|
+
* // 取消监听
|
|
174
|
+
* unwatch()
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
watch<T = unknown>(name: string, callback: WatchCallback<T>): () => void;
|
|
178
|
+
/**
|
|
179
|
+
* 停止配置文件监听。
|
|
180
|
+
*
|
|
181
|
+
* @param name - 配置名称;不传则停止所有监听
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* config.unwatch('app') // 停止单个
|
|
186
|
+
* config.unwatch() // 停止全部
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
unwatch(name?: string): void;
|
|
190
|
+
/**
|
|
191
|
+
* 检查是否正在监听某个配置。
|
|
192
|
+
*
|
|
193
|
+
* @param name - 配置名称
|
|
194
|
+
* @returns 是否有活跃的 watcher
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* if (config.isWatching('app')) {
|
|
199
|
+
* config.unwatch('app')
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
isWatching(name: string): boolean;
|
|
204
|
+
};
|
|
26
205
|
|
|
206
|
+
/**
|
|
207
|
+
* @h-ai/core — Core 服务聚合(Node.js)
|
|
208
|
+
*
|
|
209
|
+
* 提供 Node.js 环境的 core 对象,聚合常用功能。 所有功能统一通过 core 对象访问,并提供配置加载能力。
|
|
210
|
+
* @module core-main.node
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
type NodeCoreFunctions = CoreFunctions & {
|
|
214
|
+
readonly config: typeof config;
|
|
215
|
+
readonly init: typeof initCore;
|
|
216
|
+
};
|
|
27
217
|
/**
|
|
28
218
|
* Core 服务对象 - 聚合常用功能(Node.js)。
|
|
29
219
|
*
|
|
@@ -33,35 +223,7 @@ type WatchCallback<T = unknown> = (config: T | null, error?: HaiError) => void;
|
|
|
33
223
|
* core.init({ configDir: './config' })
|
|
34
224
|
* ```
|
|
35
225
|
*/
|
|
36
|
-
declare const core:
|
|
37
|
-
/** 配置管理 */
|
|
38
|
-
config: {
|
|
39
|
-
load<T>(name: string, filePath: string, schema?: zod.ZodType<T>): HaiResult<T>;
|
|
40
|
-
validate<T>(name: string, schema: zod.ZodType<T>): HaiResult<T>;
|
|
41
|
-
get<T>(name: string): T | undefined;
|
|
42
|
-
getOrThrow<T>(name: string): T;
|
|
43
|
-
reload(name: string): HaiResult<unknown>;
|
|
44
|
-
has(name: string): boolean;
|
|
45
|
-
clear(name?: string): void;
|
|
46
|
-
keys(): string[];
|
|
47
|
-
watch<T = unknown>(name: string, callback: WatchCallback<T>): () => void;
|
|
48
|
-
unwatch(name?: string): void;
|
|
49
|
-
isWatching(name: string): boolean;
|
|
50
|
-
};
|
|
51
|
-
/** 初始化 Core */
|
|
52
|
-
init: typeof initCore;
|
|
53
|
-
logger: CoreLogger;
|
|
54
|
-
i18n: I18nFn;
|
|
55
|
-
id: IdFn;
|
|
56
|
-
typeUtils: TypeUtilFn;
|
|
57
|
-
object: ObjectFn;
|
|
58
|
-
string: StringFn;
|
|
59
|
-
array: ArrayFn;
|
|
60
|
-
async: AsyncFn;
|
|
61
|
-
time: TimeFn;
|
|
62
|
-
error: ErrorFn;
|
|
63
|
-
module: ModuleFn;
|
|
64
|
-
};
|
|
226
|
+
declare const core: NodeCoreFunctions;
|
|
65
227
|
/**
|
|
66
228
|
* 初始化 Core(内部实现,通过 `core.init()` 调用)。
|
|
67
229
|
*
|
|
@@ -79,4 +241,4 @@ declare const core: {
|
|
|
79
241
|
*/
|
|
80
242
|
declare function initCore(options?: CoreOptions): void;
|
|
81
243
|
|
|
82
|
-
export {
|
|
244
|
+
export { CoreFunctions, CoreOptions, HaiError, HaiResult, core };
|
package/dist/node.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createCore, CoreConfigSchema, i18n, err, HaiConfigError, ok, typeUtils } from './chunk-
|
|
2
|
-
export { CoreConfigSchema, EnvSchema, HaiCommonError, HaiConfigError, IdConfigSchema, LogFormatSchema, LogLevelSchema, LoggingConfigSchema, err, ok } from './chunk-
|
|
1
|
+
import { createCore, CoreConfigSchema, i18n, err, HaiConfigError, ok, typeUtils } from './chunk-JM2QORKH.js';
|
|
2
|
+
export { CoreConfigSchema, EnvSchema, HaiCommonError, HaiConfigError, IdConfigSchema, LogFormatSchema, LogLevelSchema, LoggingConfigSchema, err, ok } from './chunk-JM2QORKH.js';
|
|
3
3
|
import { existsSync, readdirSync, watch, readFileSync } from 'fs';
|
|
4
4
|
import { join } from 'path';
|
|
5
5
|
import process2 from 'process';
|
|
@@ -572,6 +572,8 @@ function createNodeCore() {
|
|
|
572
572
|
});
|
|
573
573
|
return {
|
|
574
574
|
...baseCore,
|
|
575
|
+
/** Zod 校验错误 → i18n 消息映射工具(kit / serv / api-client 共享)。 */
|
|
576
|
+
zodValidation: baseCore.zodValidation,
|
|
575
577
|
/** 配置管理 */
|
|
576
578
|
config,
|
|
577
579
|
/** 初始化 Core */
|