@h-ai/core 1.0.0-beta.4 → 1.0.0-beta.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.
@@ -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 - 文件所在目录及环境模式
@@ -2194,6 +2332,8 @@ interface CoreOptions {
2194
2332
  * ```
2195
2333
  */
2196
2334
  configDir?: string;
2335
+ /** 宿主显式提供模块 Schema,供扫描时识别可选字段和环境变量类型;Core 不注册业务模块。 */
2336
+ configSchemas?: Readonly<Record<string, z.ZodType>>;
2197
2337
  /** 是否启用配置文件监听(默认 false) */
2198
2338
  watchConfig?: boolean;
2199
2339
  }
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-Irg7caTy.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-Irg7caTy.js';
3
3
  import 'zod';
4
4
 
5
5
  /**