@clevercloud/reglage

中文文档
0.0.8发布于 2026/11/07Apache-2.0TypeScriptES Module零依赖近 30 天下载 1.2万

Schema-driven configuration library: define, validate and merge configuration from multiple named sources with Zod schemas, then read it back type-safely, with secret masking and Markdown doc generation.

原始使用文档
文档目录6 节
  1. Usage
  2. API
  3. createConfigBuilder(schema)
  4. ConfigBuilder
  5. Config
  6. InvalidConfigError

@clevercloud/reglage

Schema-driven configuration library built on Zod. Define your options once and get validated, type-safe config from any number of sources.

Usage

import { createConfigBuilder } from '@clevercloud/reglage';
import { z } from 'zod';

const builder = createConfigBuilder({
  API_URL: {
    schema: z.url().default('https://api.example.com'),
    documentation: 'The API base URL',
  },
  PORT: {
    schema: z.number().default(8080),
    tags: ['server'],
    documentation: 'Port to listen on',
  },
  SECRET: {
    schema: z.string().min(1),
    secret: true,
    documentation: 'A required secret',
  },
});

builder.addSource('defaults', { PORT: '3000' });
builder.addSource('env', process.env);

const config = builder.buildConfig(); // throws InvalidConfigError on validation failure

config.get('API_URL'); // string
config.get('PORT'); // number
config.getAll('server'); // { PORT: number }

API

createConfigBuilder(schema)

Creates a ConfigBuilder from a schema record. Each key maps to an OptionDef:

FieldTypeDescription
schemaz.ZodTypeZod schema used to validate and transform the raw value.
secretboolean (optional)Marks the value as sensitive.
tagsstring[] (optional)Arbitrary tags for categorising options.
documentationstringHuman-readable description of the option.

ConfigBuilder

addSource(name, values)

Registers a named key/value source (e.g. "env", "file"). Sources are merged in order — later sources override earlier ones for the same key. Keys not present in the schema are silently ignored.

buildConfig()

Merges all sources and validates every value against the schema. Returns a Config instance or throws an InvalidConfigError.

refine(key, fn)

Adds a cross-field validation rule for the given key. The callback receives the original Zod schema and all resolved config values, and must return a Zod schema to validate the field against. Multiple refinements can be registered (even on the same key). They run after all fields have been individually validated.

// Make SECRET required when FEATURE_X is enabled
builder.refine('SECRET', (schema, resolved) => {
  if (resolved.FEATURE_X) return z.string().min(1);
  return schema;
});

// Restrict PORT in production
builder.refine('PORT', (schema, resolved) => {
  if (resolved.MODE === 'production') {
    return schema.refine((v) => v >= 1024, { message: 'Must be >= 1024 in production' });
  }
  return schema;
});

Config

get(key)

Returns the resolved value for a single key.

getAll(tag?)

Returns all resolved values. When tag is provided, only options tagged with that value are included.

getSource(key)

Returns the name of the source that provided the winning value for a key (e.g. "env", "file"), or "default" if the value came from a Zod schema default.

getSourceHistory(key)

Returns the full override history for a single key, ordered from earliest (schema default) to latest (winning source). Each entry is a SourceHistoryEntry:

interface SourceHistoryEntry {
  source: string;
  rawValue: unknown;
  resolvedValue?: unknown; // only set on the last (winning) entry
}
builder.addSource('file', { PORT: 3000 });
builder.addSource('env', { PORT: '8080' });
const config = builder.buildConfig();

config.getSourceHistory('PORT');
// [
//   { source: 'default', rawValue: 80 },
//   { source: 'file', rawValue: 3000 },
//   { source: 'env', rawValue: '8080', resolvedValue: 8080 }
// ]

getAllSourceHistory()

Returns the full history for all keys as an object mapping each key to its SourceHistoryEntry[].

toString(format?)

Returns a human-readable string of all configuration values. Secret values are masked with ******. Keys are sorted alphabetically.

  • "simple" (default) — one line per key with the winning source:

    API_URL="https://api.example.com" (env)
    PORT=8080 (env)
    SECRET=****** (env)
    
  • "chain" — one line per key with the full source chain:

    API_URL="https://api.example.com" (default -> env)
    PORT=8080 (default -> file -> env)
    SECRET=****** (env)
    
  • "verbose" — multi-line output showing each source's raw value with an <-- active marker on the winning entry:

    API_URL="https://api.example.com" (env)
      default -> "https://default.example.com"
      env     -> "https://api.example.com"  <-- active
    PORT=8080 (env)
      default -> 80
      file    -> 3000
      env     -> "8080"  <-- active
    SECRET=****** (env)
      default -> ******
      env     -> ******  <-- active
    

InvalidConfigError

Thrown when validation fails. Exposes an issues array of ConfigIssue objects:

interface ConfigIssue {
  key: string;
  source: string | undefined;
  message: string;
}

版本与发布记录

@clevercloud/reglage 共发布 6 个版本,首次发布于 2026/04/08;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.0.8latest2026/11/073 天前
0.0.72026/11/073 天前
0.0.62026/04/216 个月前
0.0.52026/04/106 个月前
0.0.42026/04/087 个月前
0.0.22026/04/087 个月前

关于 @clevercloud/reglage 的常见问题

@clevercloud/reglage 是什么?

Schema-driven configuration library: define, validate and merge configuration from multiple named sources with Zod schemas, then read it back type-safely, with secret masking and Markdown doc generation.

如何安装 @clevercloud/reglage?

在项目目录运行 npm install @clevercloud/reglage,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@clevercloud/reglage 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@clevercloud/reglage 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@clevercloud/reglage 可以商用吗?

@clevercloud/reglage 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@clevercloud/reglage 的下载量有多少?

根据 npm 官方下载统计,@clevercloud/reglage 在 2026/10/10 – 2026/11/08 这 30 天内被下载 12,165 次,日均约 406 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@clevercloud/reglage 还在维护吗?

最新版本 0.0.8 发布于 2026/11/07(3 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/04/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。