@h-ai/core 1.0.0-beta.1 → 1.0.0-beta.11

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.
@@ -330,7 +330,88 @@ declare const zodValidation: {
330
330
  * if (!result.success) result.error.message
331
331
  * ```
332
332
  */
333
- declare const config$1: ConfigFn;
333
+ declare const config$1: {
334
+ loadEnvFiles(_options: {
335
+ mode: string;
336
+ envDir?: string;
337
+ }): HaiResult<Record<string, string>>;
338
+ load<T>(_name: string, _filePath: string, _schema?: ZodType<T>): HaiResult<T>;
339
+ validate<T>(_name: string, _schema: ZodType<T>): HaiResult<T>;
340
+ get<T>(_name: string): T | undefined;
341
+ getOrThrow<T>(_name: string): T;
342
+ reload(_name: string): HaiResult<unknown>;
343
+ has(_name: string): boolean;
344
+ clear(_name?: string): void;
345
+ keys(): string[];
346
+ watch<T = unknown>(_name: string, callback: WatchCallback<T>): () => void;
347
+ unwatch(_name?: string): void;
348
+ isWatching(_name: string): boolean;
349
+ };
350
+
351
+ /** 通用环境配置:来源合并、结构覆盖与无损 dotenv 编解码,不依赖业务模块。 */
352
+
353
+ /**
354
+ * 单个环境来源中的字面变量,undefined 表示未设置。
355
+ * @example 模块内部上下文
356
+ * ```ts
357
+ * const values: Environment = { HAI_APP_NAME: 'Example' }
358
+ * ```
359
+ */
360
+ type Environment = Record<string, string | undefined>;
361
+ /**
362
+ * 从低到高合并来源;高来源父项删除旧细项,同来源始终先父后子。
363
+ * @example 模块内部上下文
364
+ * ```ts
365
+ * const values = mergeEnv([{ HAI_APP_NAME: 'base' }, { HAI_APP_NAME: 'test' }])
366
+ * ```
367
+ */
368
+ declare function mergeEnv(layers: readonly Environment[]): Record<string, string>;
369
+ /**
370
+ * dotenv 使用 Node 字面规则;补齐生成器双引号的 JSON 转义,拒绝同文件重复键。
371
+ * @example 模块内部上下文
372
+ * ```ts
373
+ * const values = parseEnvironment('HAI_APP_NAME=Example\n')
374
+ * ```
375
+ */
376
+ declare function parseEnvironment(text: string): Record<string, string>;
377
+ /**
378
+ * 所有值均按字面量编码;排序让模块 JSON 位于其细项之前。
379
+ * @example 模块内部上下文
380
+ * ```ts
381
+ * const text = stringifyEnv({ HAI_APP_NAME: 'Example' })
382
+ * ```
383
+ */
384
+ declare function stringifyEnv(values: Record<string, string>, options?: {
385
+ target?: 'node' | 'compose';
386
+ }): string;
387
+ /**
388
+ * 在给定结构上解析环境变量;父对象提供基础值,具体路径继续覆盖。
389
+ * @example 模块内部上下文
390
+ * ```ts
391
+ * const result = resolveEnvironment({ name: 'app', value: { name: 'Example' }, environment: { HAI_APP_NAME: 'Test' } })
392
+ * ```
393
+ */
394
+ declare function resolveEnvironment(options: {
395
+ name: string;
396
+ value: unknown;
397
+ environment?: Environment;
398
+ schema?: ZodType;
399
+ shape?: Readonly<Record<string, unknown>>;
400
+ missing?: 'error' | 'empty';
401
+ }, literal?: boolean): HaiResult<unknown>;
402
+ /**
403
+ * 默认展开对象,数组与动态键字典保留 JSON;不做模块名称判断。
404
+ * @example 模块内部上下文
405
+ * ```ts
406
+ * const values = toEnv({ prefix: 'HAI_APP', value: { name: 'Example', tags: ['demo'] } })
407
+ * ```
408
+ */
409
+ declare function toEnv(options: {
410
+ prefix: string;
411
+ value: unknown;
412
+ schema?: ZodType;
413
+ shape?: Readonly<Record<string, unknown>>;
414
+ }): Record<string, string>;
334
415
 
335
416
  /**
336
417
  * 配置管理对象。
@@ -341,10 +422,67 @@ declare const config$1: ConfigFn;
341
422
  * ```
342
423
  */
343
424
  declare const config: {
425
+ /**
426
+ * 合并从低到高的环境来源;高来源父项替换旧节点,同来源细项覆盖父 JSON。
427
+ * @param layers - 按优先级排列的字面变量。
428
+ * @returns 合并后的变量,可能含秘密。
429
+ * @example
430
+ * ```ts
431
+ * import { core } from '@h-ai/core'
432
+ * const value = core.config.mergeEnv([{ HAI_APP_NAME: 'base' }, { HAI_APP_NAME: 'test' }])
433
+ * ```
434
+ */
435
+ mergeEnv: typeof mergeEnv;
436
+ /**
437
+ * 解析 dotenv 字面文本,拒绝重复键,不执行 shell 或变量展开。
438
+ * @param text - dotenv 文本,可能包含秘密。
439
+ * @returns 字面变量;重复键抛出不含变量值的错误。
440
+ * @example
441
+ * ```ts
442
+ * import { core } from '@h-ai/core'
443
+ * const value = core.config.parseEnv('HAI_APP_NAME=Example\n')
444
+ * ```
445
+ */
446
+ parseEnv: typeof parseEnvironment;
447
+ /**
448
+ * 无损编码 dotenv,父变量排在细项前;含秘密时只能写入受保护文件。
449
+ * @param values - 字面变量,不进行脱敏。
450
+ * @param options - 输出语法目标,默认 Node dotenv。
451
+ * @param options.target - Compose 目标同时处理其美元符号插值规则。
452
+ * @returns dotenv 文本;非法变量名抛错。
453
+ * @example
454
+ * ```ts
455
+ * import { core } from '@h-ai/core'
456
+ * const value = core.config.stringifyEnv({ HAI_APP_NAME: 'Example' })
457
+ * ```
458
+ */
459
+ stringifyEnv: typeof stringifyEnv;
460
+ /**
461
+ * 按已有结构或 Schema 解析父 JSON 和细项,不修改缓存或进程环境。
462
+ * @param options - 配置名、原始结构、变量、可选 Schema 和缺失插值规则。
463
+ * @returns 解析结果;调用方仍须执行最终 Schema 校验。
464
+ * @example
465
+ * ```ts
466
+ * import { core } from '@h-ai/core'
467
+ * const value = core.config.resolve({ name: 'app', value: { name: 'Example' }, environment: { HAI_APP_NAME: 'Test' } })
468
+ * ```
469
+ */
470
+ resolve: (options: Parameters<typeof resolveEnvironment>[0]) => HaiResult<unknown>;
471
+ /**
472
+ * 默认将对象展开为细项;数组、动态字典和空对象保留 JSON。
473
+ * @param options - 完整变量前缀、配置值和可选 Schema。
474
+ * @returns 字面环境变量,不脱敏;非法名称抛错。
475
+ * @example
476
+ * ```ts
477
+ * import { core } from '@h-ai/core'
478
+ * const value = core.config.toEnv({ prefix: 'HAI_APP', value: { name: 'Example', tags: ['demo'] } })
479
+ * ```
480
+ */
481
+ toEnv: typeof toEnv;
344
482
  /**
345
483
  * 读取 dotenv 文件并按统一顺序合并,不修改进程环境。
346
484
  *
347
- * 优先级:已有进程环境 > .env.local > .env.<mode> > .env。
485
+ * 优先级:已有进程环境 > .env.<mode>.local > .env.local > .env.<mode> > .env。
348
486
  * development/production 分别映射到 dev/prod;不存在的文件跳过。
349
487
  * 返回值可能包含私有配置,仅在 Node 启动/构建边界注入,不下发浏览器。
350
488
  * @param options - 文件所在目录及环境模式
@@ -486,7 +624,8 @@ declare const config: {
486
624
  /**
487
625
  * 监听配置文件变更并自动重载。
488
626
  *
489
- * 文件变更时自动重新加载并调用回调。配置未加载时立即回调 NOT_LOADED 错误。
627
+ * 普通写入、原子替换及删除重建均触发重载;失败保留最近成功的缓存并通知错误。
628
+ * 配置未加载时立即回调 NOT_LOADED 错误。
490
629
  *
491
630
  * @param name - 配置名称
492
631
  * @param callback - 配置变更回调,接收新配置或错误
@@ -695,7 +834,7 @@ declare const i18n: {
695
834
  getGlobalLocale: typeof getGlobalLocale;
696
835
  runWithLocale: typeof runWithLocale;
697
836
  createMessageGetter: typeof createMessageGetter;
698
- coreM: (key: "$schema" | "core_errorUnknown" | "core_errorNetwork" | "core_errorTimeout" | "core_errorNotFound" | "core_errorUnauthorized" | "core_errorForbidden" | "core_errorValidation" | "core_errorInternal" | "core_actionConfirm" | "core_actionCancel" | "core_actionSave" | "core_actionDelete" | "core_actionEdit" | "core_actionCreate" | "core_actionUpdate" | "core_actionSearch" | "core_actionReset" | "core_actionSubmit" | "core_actionLoading" | "core_actionRetry" | "core_statusSuccess" | "core_statusFailed" | "core_statusPending" | "core_statusProcessing" | "core_statusCompleted" | "core_statusCancelled" | "core_validationRequired" | "core_validationMinLength" | "core_validationMaxLength" | "core_validationEmail" | "core_validationUrl" | "core_validationNumber" | "core_validationInteger" | "core_validationPositive" | "core_validationPattern" | "core_timeJustNow" | "core_timeSecondsAgo" | "core_timeMinutesAgo" | "core_timeHoursAgo" | "core_timeDaysAgo" | "core_timeWeeksAgo" | "core_timeMonthsAgo" | "core_timeYearsAgo" | "core_browserFeatureUnsupported" | "core_configEnvVarMissing" | "core_configFileNotExist" | "core_configParseFailed" | "core_configValidationFailed" | "core_configNotLoaded" | "core_configUnsupportedInBrowser" | "core_cryptoRandomUnavailable" | "core_runtimeStartFailed" | "core_runtimeCloseFailed" | "core_runtimeEnvMissing" | "core_runtimeConfigInvalid" | "core_runtimeConfigProblem" | "core_runtimeErrorCode" | "core_requestAccessDenied", options?: MessageOptions) => string;
837
+ coreM: (key: "$schema" | "core_errorUnknown" | "core_errorNetwork" | "core_errorTimeout" | "core_errorNotFound" | "core_errorUnauthorized" | "core_errorForbidden" | "core_errorValidation" | "core_errorInternal" | "core_actionConfirm" | "core_actionCancel" | "core_actionSave" | "core_actionDelete" | "core_actionEdit" | "core_actionCreate" | "core_actionUpdate" | "core_actionSearch" | "core_actionReset" | "core_actionSubmit" | "core_actionLoading" | "core_actionRetry" | "core_statusSuccess" | "core_statusFailed" | "core_statusPending" | "core_statusProcessing" | "core_statusCompleted" | "core_statusCancelled" | "core_validationRequired" | "core_validationMinLength" | "core_validationMaxLength" | "core_validationEmail" | "core_validationUrl" | "core_validationNumber" | "core_validationInteger" | "core_validationPositive" | "core_validationPattern" | "core_timeJustNow" | "core_timeSecondsAgo" | "core_timeMinutesAgo" | "core_timeHoursAgo" | "core_timeDaysAgo" | "core_timeWeeksAgo" | "core_timeMonthsAgo" | "core_timeYearsAgo" | "core_browserFeatureUnsupported" | "core_configEnvVarMissing" | "core_configFileNotExist" | "core_configParseFailed" | "core_configValidationFailed" | "core_configNotLoaded" | "core_configUnsupportedInBrowser" | "core_cryptoRandomUnavailable" | "core_parallelConcurrencyInvalid" | "core_runtimeStartFailed" | "core_runtimeCloseFailed" | "core_runtimeEnvMissing" | "core_runtimeConfigInvalid" | "core_runtimeConfigProblem" | "core_runtimeErrorCode" | "core_requestAccessDenied", options?: MessageOptions) => string;
699
838
  };
700
839
 
701
840
  /**
@@ -719,7 +858,7 @@ declare function unique<T>(arr: T[]): T[];
719
858
  * @param arr - 输入数组
720
859
  * @param fn - 分组键生成函数
721
860
  * @returns 分组结果对象
722
- * @remarks 当数组为空时返回空对象。
861
+ * @remarks 当数组为空时返回空对象;原型同名键也作为独立的自有分组。
723
862
  *
724
863
  * @example
725
864
  * ```ts
@@ -902,9 +1041,10 @@ declare function retry<T>(fn: () => Promise<T>, options?: {
902
1041
  *
903
1042
  * @param items - 输入列表
904
1043
  * @param fn - 处理函数
905
- * @param concurrency - 最大并发数
1044
+ * @param concurrency - 最大并发数,必须为大于 0 的安全整数
906
1045
  * @returns 处理结果列表
907
1046
  * @remarks 结果顺序与输入顺序一致。
1047
+ * @throws 并发数非法时拒绝为 RangeError,不执行任何任务;任务异常原样传播。
908
1048
  *
909
1049
  * @example
910
1050
  * ```ts
@@ -938,7 +1078,7 @@ declare function serial<T, R>(items: T[], fn: (item: T, index: number) => Promis
938
1078
  * const onInput = async.debounce(() => {}, 300)
939
1079
  * ```
940
1080
  */
941
- declare function debounce<T extends (...args: unknown[]) => unknown>(fn: T, ms: number): (...args: Parameters<T>) => void;
1081
+ declare function debounce<Args extends unknown[]>(fn: (...args: Args) => unknown, ms: number): (...args: Args) => void;
942
1082
  /**
943
1083
  * 节流。
944
1084
  *
@@ -952,7 +1092,7 @@ declare function debounce<T extends (...args: unknown[]) => unknown>(fn: T, ms:
952
1092
  * const onScroll = async.throttle(() => {}, 200)
953
1093
  * ```
954
1094
  */
955
- declare function throttle<T extends (...args: unknown[]) => unknown>(fn: T, ms: number): (...args: Parameters<T>) => void;
1095
+ declare function throttle<Args extends unknown[]>(fn: (...args: Args) => unknown, ms: number): (...args: Args) => void;
956
1096
  /**
957
1097
  * 异步操作工具对象。
958
1098
  * @example
@@ -1016,7 +1156,7 @@ declare function deepClone<T>(obj: T): T;
1016
1156
  * 深度合并多个对象。
1017
1157
  * @param objects - 需要合并的对象列表
1018
1158
  * @returns 合并后的新对象
1019
- * @remarks 仅合并纯对象字段,数组会被直接覆盖。
1159
+ * @remarks 仅递归合并普通对象字段;数组、Date、Map、Set 和类实例由后值直接覆盖,不修改输入。
1020
1160
  *
1021
1161
  * @example
1022
1162
  * ```ts
@@ -1174,7 +1314,7 @@ declare function sanitizeUrl(url: string, options?: SanitizeSensitiveFieldsOptio
1174
1314
  * @param key - 当前字段名;根节点传 null
1175
1315
  * @param options - 脱敏选项
1176
1316
  * @param cache - 循环引用 / 重复引用缓存
1177
- * @returns 递归脱敏后的值;普通对象循环保留,Error 规范化为有界诊断树,原值不被修改。
1317
+ * @returns 脱敏数据副本;类实例不保留原型和方法,普通对象循环保留,Error 规范化为有界诊断树,原值不被修改。
1178
1318
  * @example
1179
1319
  * ```ts
1180
1320
  * import { core } from '@h-ai/core'
@@ -1189,7 +1329,8 @@ declare function sanitizeSensitiveValue(value: unknown, key: string | null, opti
1189
1329
  *
1190
1330
  * @param value - 原始对象/数组/值
1191
1331
  * @param options - 脱敏选项
1192
- * @returns 脱敏后的新值
1332
+ * 类实例只复制自有可枚举数据属性;不保留原型、访问器和 toJSON。Date 转 ISO 字符串,URL 转脱敏字符串。
1333
+ * @returns 用于诊断的脱敏副本,不作为原类实例使用。
1193
1334
  * @example
1194
1335
  * ```ts
1195
1336
  * import { core } from '@h-ai/core'
@@ -2194,6 +2335,8 @@ interface CoreOptions {
2194
2335
  * ```
2195
2336
  */
2196
2337
  configDir?: string;
2338
+ /** 宿主显式提供模块 Schema,供扫描时识别可选字段和环境变量类型;Core 不注册业务模块。 */
2339
+ configSchemas?: Readonly<Record<string, z.ZodType>>;
2197
2340
  /** 是否启用配置文件监听(默认 false) */
2198
2341
  watchConfig?: boolean;
2199
2342
  }
package/dist/node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { W as NodeCoreFunctions } from './core-types-_JqVAGGT.js';
2
- export { A as AccessCondition, a as AccessConditionSchema, b as AccessDecision, c as AccessFunctions, d as ArrayFn, e as AsyncFn, f as BrowserConfigFn, B as BrowserCoreFunctions, C as ConfigFn, g as CoreConfig, h as CoreConfigSchema, i as CoreFunctions, j as CoreLogger, k as CoreOptions, l as CreateNotInitializedKitFn, E as Env, m as EnvSchema, n as ErrorFn, o as ErrorInfo, p as ErrorInfoValue, H as HaiCommonError, q as HaiConfigError, r as HaiError, s as HaiErrorDef, t as HaiResult, I as I18nFn, u as IdFn, v as InterpolationParams, L as Locale, w as LocaleInfo, x as LocaleMessages, y as LogContext, z as LogFormat, D as LogFormatSchema, F as LogLevel, G as LogLevelSchema, J as LogObserver, K as LogRecord, M as Logger, N as LoggerFunctions, O as LoggerOptions, P as LoggingConfig, Q as LoggingConfigSchema, R as MatchHandlers, S as MessageDictionary, T as MessageOptions, U as ModuleFn, V as ModuleRuntime, X as NotInitializedKit, Y as ObjectFn, Z as PaginatedResult, _ as PaginationOptions, $ as PaginationOptionsInput, a0 as RequestAccessCheck, a1 as RequestAccessConfigInput, a2 as RequestAccessConfigSchema, a3 as RequestAccessPolicy, a4 as RequestAccessPolicySchema, a5 as RequestContext, a6 as RequestEncryptionMode, a7 as RuntimeStep, a8 as SanitizeFn, a9 as SanitizeSensitiveFieldsOptions, aa as SensitiveKeyMatcher, ab as StringFn, ac as TimeFn, ad as TrustedProxies, ae as TrustedProxiesSchema, af as TypeUtilFn, ag as ValidationFormError, ah as WatchCallback, ai as ZodMessageGetter, aj as ZodValidationFn, ak as ZodValidationMessageKey, al as err, am as ok } from './core-types-_JqVAGGT.js';
1
+ import { W as NodeCoreFunctions } from './core-types-DM_AiIaS.js';
2
+ export { A as AccessCondition, a as AccessConditionSchema, b as AccessDecision, c as AccessFunctions, d as ArrayFn, e as AsyncFn, f as BrowserConfigFn, B as BrowserCoreFunctions, C as ConfigFn, g as CoreConfig, h as CoreConfigSchema, i as CoreFunctions, j as CoreLogger, k as CoreOptions, l as CreateNotInitializedKitFn, E as Env, m as EnvSchema, n as ErrorFn, o as ErrorInfo, p as ErrorInfoValue, H as HaiCommonError, q as HaiConfigError, r as HaiError, s as HaiErrorDef, t as HaiResult, I as I18nFn, u as IdFn, v as InterpolationParams, L as Locale, w as LocaleInfo, x as LocaleMessages, y as LogContext, z as LogFormat, D as LogFormatSchema, F as LogLevel, G as LogLevelSchema, J as LogObserver, K as LogRecord, M as Logger, N as LoggerFunctions, O as LoggerOptions, P as LoggingConfig, Q as LoggingConfigSchema, R as MatchHandlers, S as MessageDictionary, T as MessageOptions, U as ModuleFn, V as ModuleRuntime, X as NotInitializedKit, Y as ObjectFn, Z as PaginatedResult, _ as PaginationOptions, $ as PaginationOptionsInput, a0 as RequestAccessCheck, a1 as RequestAccessConfigInput, a2 as RequestAccessConfigSchema, a3 as RequestAccessPolicy, a4 as RequestAccessPolicySchema, a5 as RequestContext, a6 as RequestEncryptionMode, a7 as RuntimeStep, a8 as SanitizeFn, a9 as SanitizeSensitiveFieldsOptions, aa as SensitiveKeyMatcher, ab as StringFn, ac as TimeFn, ad as TrustedProxies, ae as TrustedProxiesSchema, af as TypeUtilFn, ag as ValidationFormError, ah as WatchCallback, ai as ZodMessageGetter, aj as ZodValidationFn, ak as ZodValidationMessageKey, al as err, am as ok } from './core-types-DM_AiIaS.js';
3
3
  import 'zod';
4
4
 
5
5
  /**