@meltstudio/config-loader 3.7.0 → 4.0.3
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 +38 -3
- package/dist/index.d.mts +409 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1579 -0
- package/package.json +16 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A type-safe configuration loader for Node.js. Define your schema once, load from YAML, JSON, or TOML files, `.env` files, environment variables, and CLI arguments — and get a fully typed result with zero manual type annotations.
|
|
4
4
|
|
|
5
|
-
> **Upgrading from v1?** v1.x is deprecated and no longer maintained. Install the latest version with `npm install @meltstudio/config-loader@latest
|
|
5
|
+
> **Upgrading from v1?** v1.x is deprecated and no longer maintained. Install the latest version with `npm install @meltstudio/config-loader@latest`.
|
|
6
6
|
|
|
7
7
|
**[Full documentation](https://meltstudio.github.io/config-loader/)**
|
|
8
8
|
|
|
@@ -62,12 +62,47 @@ const config = c
|
|
|
62
62
|
npm install @meltstudio/config-loader
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
+
```bash
|
|
66
|
+
pnpm add @meltstudio/config-loader
|
|
67
|
+
```
|
|
68
|
+
|
|
65
69
|
```bash
|
|
66
70
|
yarn add @meltstudio/config-loader
|
|
67
71
|
```
|
|
68
72
|
|
|
69
73
|
Requires Node.js >= 20.
|
|
70
74
|
|
|
75
|
+
## Watch Mode
|
|
76
|
+
|
|
77
|
+
Automatically reload config when files change. Watchers use `.unref()` so they don't prevent the process from exiting.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const watcher = c
|
|
81
|
+
.schema({
|
|
82
|
+
port: c.number({ env: "PORT", defaultValue: 3000 }),
|
|
83
|
+
host: c.string({ defaultValue: "localhost" }),
|
|
84
|
+
})
|
|
85
|
+
.watch(
|
|
86
|
+
{ env: true, args: false, files: "./config.yaml" },
|
|
87
|
+
{
|
|
88
|
+
onChange: (newConfig, oldConfig, changes) => {
|
|
89
|
+
for (const change of changes) {
|
|
90
|
+
console.log(
|
|
91
|
+
`${change.path}: ${String(change.oldValue)} → ${String(change.newValue)}`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
onError: (err) => console.error("Reload failed:", err.message),
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// Access current config anytime
|
|
100
|
+
console.log(watcher.config.port);
|
|
101
|
+
|
|
102
|
+
// Stop watching
|
|
103
|
+
watcher.close();
|
|
104
|
+
```
|
|
105
|
+
|
|
71
106
|
## Documentation
|
|
72
107
|
|
|
73
108
|
See the **[full documentation](https://meltstudio.github.io/config-loader/)** for:
|
|
@@ -84,8 +119,8 @@ The [`example/`](./example) directory contains runnable examples:
|
|
|
84
119
|
- **[Advanced](./example/advanced)** — TOML config, `.env` files, `oneOf` constraints, `sensitive` fields, validation, `printConfig()`, `maskSecrets()`, error handling
|
|
85
120
|
|
|
86
121
|
```bash
|
|
87
|
-
|
|
88
|
-
|
|
122
|
+
pnpm example:basic
|
|
123
|
+
pnpm example:advanced
|
|
89
124
|
```
|
|
90
125
|
|
|
91
126
|
## Documentation for AI Agents
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
declare class ArrayValueContainer {
|
|
2
|
+
readonly val: ArrayValue;
|
|
3
|
+
readonly item: OptionTypes;
|
|
4
|
+
constructor(item: OptionTypes, val: ArrayValue);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface EnvFileEntry {
|
|
8
|
+
value: string;
|
|
9
|
+
line: number;
|
|
10
|
+
column: number;
|
|
11
|
+
}
|
|
12
|
+
interface EnvFileResult {
|
|
13
|
+
entries: Map<string, EnvFileEntry>;
|
|
14
|
+
filePath: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A single configuration validation error with optional location metadata. */
|
|
18
|
+
interface ConfigErrorEntry {
|
|
19
|
+
/** Human-readable error description. */
|
|
20
|
+
message: string;
|
|
21
|
+
/** Dot-separated path to the offending config key (e.g. `"db.port"`). */
|
|
22
|
+
path?: string;
|
|
23
|
+
/** The source where the error originated (e.g. file path, `"env"`, `"cli"`). */
|
|
24
|
+
source?: string;
|
|
25
|
+
/** Classification of the error. */
|
|
26
|
+
kind?: "required" | "type_conversion" | "invalid_path" | "invalid_state" | "file_validation" | "null_value" | "strict" | "validation";
|
|
27
|
+
/** Line number in the config file where the error occurred, if applicable. */
|
|
28
|
+
line?: number;
|
|
29
|
+
/** Column number in the config file where the error occurred, if applicable. */
|
|
30
|
+
column?: number;
|
|
31
|
+
}
|
|
32
|
+
/** Thrown when configuration loading fails validation. Contains structured error entries and warnings. */
|
|
33
|
+
declare class ConfigLoadError extends Error {
|
|
34
|
+
/** All validation errors that caused the load to fail. */
|
|
35
|
+
readonly errors: ConfigErrorEntry[];
|
|
36
|
+
/** Non-fatal warnings collected during loading. */
|
|
37
|
+
readonly warnings: string[];
|
|
38
|
+
constructor(errors: ConfigErrorEntry[], warnings: string[]);
|
|
39
|
+
}
|
|
40
|
+
/** Thrown when a configuration file cannot be read or parsed. */
|
|
41
|
+
declare class ConfigFileError extends ConfigLoadError {
|
|
42
|
+
constructor(message: string);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare class OptionErrors {
|
|
46
|
+
errors: ConfigErrorEntry[];
|
|
47
|
+
warnings: string[];
|
|
48
|
+
clearAll(): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type Value = boolean | string | number | object | InvalidValue;
|
|
52
|
+
type DefaultValue = Value | (() => string) | (() => number) | (() => boolean);
|
|
53
|
+
type TypedDefaultValue<T extends OptionKind> = T extends PrimitiveKind ? TypeOfPrimitiveKind<T> | (() => TypeOfPrimitiveKind<T>) : DefaultValue;
|
|
54
|
+
interface Node {
|
|
55
|
+
[key: string]: OptionBase;
|
|
56
|
+
}
|
|
57
|
+
interface OptionClassParams<T extends OptionKind> {
|
|
58
|
+
kind: T;
|
|
59
|
+
required: boolean;
|
|
60
|
+
env: string | null;
|
|
61
|
+
cli: boolean;
|
|
62
|
+
help: string;
|
|
63
|
+
sensitive?: boolean;
|
|
64
|
+
defaultValue?: TypedDefaultValue<T>;
|
|
65
|
+
oneOf?: ReadonlyArray<string | number | boolean>;
|
|
66
|
+
validate?: StandardSchemaV1;
|
|
67
|
+
}
|
|
68
|
+
declare class OptionBase<T extends OptionKind = OptionKind> {
|
|
69
|
+
readonly params: OptionClassParams<T>;
|
|
70
|
+
constructor(params: OptionClassParams<T>);
|
|
71
|
+
getValue<U>(sourceFile: string | string[], env: {
|
|
72
|
+
[key: string]: string | undefined;
|
|
73
|
+
}, args: {
|
|
74
|
+
[key: string]: string | boolean;
|
|
75
|
+
}, path: Path, defaultValues?: Partial<U>, objectFromArray?: {
|
|
76
|
+
value: ConfigFileData;
|
|
77
|
+
file: string;
|
|
78
|
+
sourceMap?: {
|
|
79
|
+
lookup(path: string | string[]): {
|
|
80
|
+
line: number;
|
|
81
|
+
column: number;
|
|
82
|
+
} | undefined;
|
|
83
|
+
} | null;
|
|
84
|
+
}, envFileResults?: EnvFileResult[], errors?: OptionErrors): ConfigNode | null;
|
|
85
|
+
private resolveValue;
|
|
86
|
+
private runOneOfCheck;
|
|
87
|
+
private runValidation;
|
|
88
|
+
private resolveFromFileData;
|
|
89
|
+
checkType(val: Value, path: Path, sourceOfVal: string, errors?: OptionErrors): Value;
|
|
90
|
+
protected findInObject(obj: ConfigFileData, path: Path, errors?: OptionErrors): Value | ArrayValue;
|
|
91
|
+
buildArrayOption(_val: string[] | ConfigFileData[], _errors?: OptionErrors): ArrayValueContainer | InvalidValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type SourceTypes = "file" | "env" | "envFile" | "args" | "default";
|
|
95
|
+
declare class ConfigNode {
|
|
96
|
+
value: Value | ArrayValueContainer;
|
|
97
|
+
path: string;
|
|
98
|
+
sourceType: SourceTypes;
|
|
99
|
+
file: string | null;
|
|
100
|
+
variableName: string | null;
|
|
101
|
+
argName: string | null;
|
|
102
|
+
line: number | null;
|
|
103
|
+
column: number | null;
|
|
104
|
+
sensitive: boolean;
|
|
105
|
+
constructor(value: Value | ArrayValue, path: string, sourceType: SourceTypes, file: string | null, variableName: string | null, argName: string | null, line?: number | null, column?: number | null, sensitive?: boolean);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
declare class PrimitiveOption<T extends PrimitiveKind = PrimitiveKind, Narrowed = TypeOfPrimitiveKind<T>> extends OptionBase<T> {
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type NodeTree = {
|
|
112
|
+
[key: string]: NodeTree | ConfigNode;
|
|
113
|
+
};
|
|
114
|
+
/** Result returned by `SettingsBuilder.loadExtended()`, including raw node data and warnings. */
|
|
115
|
+
type ExtendedResult = {
|
|
116
|
+
/** Tree of `ConfigNode` objects preserving source metadata for each resolved value. */
|
|
117
|
+
data: NodeTree;
|
|
118
|
+
/** Non-fatal warnings collected during loading. */
|
|
119
|
+
warnings: string[];
|
|
120
|
+
};
|
|
121
|
+
type RecursivePartial<T> = {
|
|
122
|
+
[K in keyof T]?: RecursivePartial<T[K]>;
|
|
123
|
+
};
|
|
124
|
+
/** Configuration sources passed to `SettingsBuilder.load()` / `loadExtended()`. Priority: CLI > Env > Files > Defaults. */
|
|
125
|
+
type SettingsSources<T> = {
|
|
126
|
+
/** Whether to read values from `process.env`. */
|
|
127
|
+
env: boolean;
|
|
128
|
+
/** Whether to parse CLI arguments via Commander. */
|
|
129
|
+
args: boolean;
|
|
130
|
+
/** YAML/JSON file path(s) to load, or `false` to skip. */
|
|
131
|
+
files?: string | string[] | false;
|
|
132
|
+
/** Directory to scan for config files, or `false` to skip. */
|
|
133
|
+
dir?: string | false;
|
|
134
|
+
/** `.env` file path(s) to load, or `false` to skip. */
|
|
135
|
+
envFile?: string | string[] | false;
|
|
136
|
+
/** Partial default values applied at the lowest priority. */
|
|
137
|
+
defaults?: RecursivePartial<T>;
|
|
138
|
+
/** When `true`, unknown keys in config files cause errors. */
|
|
139
|
+
strict?: boolean;
|
|
140
|
+
};
|
|
141
|
+
type OptionKind = "boolean" | "string" | "number" | "array" | "object";
|
|
142
|
+
type PrimitiveKind = Extract<OptionKind, "boolean" | "string" | "number">;
|
|
143
|
+
type TypeOfPrimitiveKind<T extends PrimitiveKind> = T extends "boolean" ? boolean : T extends "string" ? string : T extends "number" ? number : never;
|
|
144
|
+
/** Recursively infers the plain TypeScript type from a schema definition. Maps option nodes to their resolved value types. */
|
|
145
|
+
type SchemaValue<T extends OptionBase | Node> = T extends OptionBase ? T extends ArrayOption<OptionTypes> ? SchemaValue<T["item"]>[] : T extends ObjectOption<infer R> ? {
|
|
146
|
+
[K in keyof R]: SchemaValue<R[K]>;
|
|
147
|
+
} : T extends PrimitiveOption<infer _R, infer Narrowed> ? Narrowed : never : T extends Node ? {
|
|
148
|
+
[K in keyof T]: SchemaValue<T[K]>;
|
|
149
|
+
} : never;
|
|
150
|
+
type Path = Array<string | number>;
|
|
151
|
+
type ConfigFileStructure<T> = {
|
|
152
|
+
[key: string]: string | T | number | boolean | Array<T> | string[];
|
|
153
|
+
};
|
|
154
|
+
interface ConfigFileData extends ConfigFileStructure<ConfigFileData> {
|
|
155
|
+
}
|
|
156
|
+
type ArrayValue = Array<string | number | boolean | ConfigFileData>;
|
|
157
|
+
declare class InvalidValue {
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A single issue returned by a Standard Schema validator.
|
|
161
|
+
* Mirrors the Standard Schema v1 spec (https://github.com/standard-schema/standard-schema).
|
|
162
|
+
*/
|
|
163
|
+
interface StandardSchemaIssue {
|
|
164
|
+
message: string;
|
|
165
|
+
path?: ReadonlyArray<PropertyKey | object>;
|
|
166
|
+
}
|
|
167
|
+
type StandardSchemaResult<Output> = {
|
|
168
|
+
value: Output;
|
|
169
|
+
} | {
|
|
170
|
+
issues: ReadonlyArray<StandardSchemaIssue>;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Minimal Standard Schema v1 interface for value validation.
|
|
174
|
+
* Any object with a `~standard.validate()` method is accepted — this covers
|
|
175
|
+
* Zod 3.24+/4+, Valibot 1.0+, ArkType 2.1+, and custom validators.
|
|
176
|
+
*/
|
|
177
|
+
interface StandardSchemaV1<Output = unknown> {
|
|
178
|
+
"~standard": {
|
|
179
|
+
version: 1;
|
|
180
|
+
vendor: string;
|
|
181
|
+
validate(value: unknown): StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
interface ArrayOptionClassParams<T extends OptionTypes> {
|
|
186
|
+
required: boolean;
|
|
187
|
+
defaultValue?: SchemaValue<T>[] | (() => SchemaValue<T>[]);
|
|
188
|
+
item: T;
|
|
189
|
+
validate?: StandardSchemaV1;
|
|
190
|
+
}
|
|
191
|
+
declare class ArrayOption<T extends OptionTypes> extends OptionBase<"array"> {
|
|
192
|
+
item: T;
|
|
193
|
+
constructor(params: ArrayOptionClassParams<T>);
|
|
194
|
+
buildArrayOption(val: string[] | ConfigFileData[], errors?: OptionErrors): ArrayValueContainer | InvalidValue;
|
|
195
|
+
checkType(val: Value, path: Path, sourceOfVal: string, errors?: OptionErrors): Value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface ObjectOptionClassParams<T extends Node> {
|
|
199
|
+
required: boolean;
|
|
200
|
+
item: T;
|
|
201
|
+
validate?: StandardSchemaV1;
|
|
202
|
+
}
|
|
203
|
+
declare class ObjectOption<T extends Node = Node> extends OptionBase<"object"> {
|
|
204
|
+
item: T;
|
|
205
|
+
constructor(params: ObjectOptionClassParams<T>);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
type OptionTypes = PrimitiveOption | ArrayOption<OptionTypes> | ObjectOption<Node>;
|
|
209
|
+
|
|
210
|
+
/** A single change detected between two config loads. */
|
|
211
|
+
interface ConfigChange {
|
|
212
|
+
/** Dot-separated path to the changed key (e.g. "db.url"). */
|
|
213
|
+
path: string;
|
|
214
|
+
/** The previous value (undefined if key was added). */
|
|
215
|
+
oldValue: unknown;
|
|
216
|
+
/** The new value (undefined if key was removed). */
|
|
217
|
+
newValue: unknown;
|
|
218
|
+
/** Whether the change was an addition, removal, or modification. */
|
|
219
|
+
type: "added" | "removed" | "changed";
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Compares two plain config objects and returns a list of changes.
|
|
223
|
+
* Sensitive fields (from the schema) are masked in the output.
|
|
224
|
+
*/
|
|
225
|
+
declare function diffConfig(oldConfig: Record<string, unknown>, newConfig: Record<string, unknown>, schema?: Node): ConfigChange[];
|
|
226
|
+
|
|
227
|
+
/** Options for the `watch()` method. */
|
|
228
|
+
interface WatchOptions<T> {
|
|
229
|
+
/** Called after a successful reload with the new config, old config, and list of changes. */
|
|
230
|
+
onChange: (newConfig: T, oldConfig: T, changes: ConfigChange[]) => void;
|
|
231
|
+
/** Called when a reload fails (parse error, validation error). The previous config is retained. */
|
|
232
|
+
onError?: (error: Error) => void;
|
|
233
|
+
/** Debounce interval in milliseconds. Default: 100. */
|
|
234
|
+
debounce?: number;
|
|
235
|
+
}
|
|
236
|
+
/** Handle returned by `watch()`. Provides access to the current config and a `close()` method. */
|
|
237
|
+
interface ConfigWatcher<T> {
|
|
238
|
+
/** The current resolved configuration. Updated on each successful reload. */
|
|
239
|
+
readonly config: T;
|
|
240
|
+
/** Stop watching all files. Idempotent. */
|
|
241
|
+
close(): void;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Fluent builder that takes a schema and resolves configuration from multiple sources. */
|
|
245
|
+
declare class SettingsBuilder<T extends Node> {
|
|
246
|
+
private readonly schema;
|
|
247
|
+
constructor(schema: T);
|
|
248
|
+
/**
|
|
249
|
+
* Loads and validates configuration, returning a fully-typed plain object.
|
|
250
|
+
* @param sources - Which sources to read (env, args, files, etc.).
|
|
251
|
+
* @returns The resolved configuration object matching the schema type.
|
|
252
|
+
* @throws {ConfigLoadError} If validation fails (missing required fields, type errors, etc.).
|
|
253
|
+
*/
|
|
254
|
+
load(sources: SettingsSources<SchemaValue<T>>): SchemaValue<T>;
|
|
255
|
+
/**
|
|
256
|
+
* Loads configuration and returns raw node data with source metadata alongside warnings.
|
|
257
|
+
* @param sources - Which sources to read (env, args, files, etc.).
|
|
258
|
+
* @returns An `ExtendedResult` containing the node tree and any warnings.
|
|
259
|
+
* @throws {ConfigLoadError} If validation fails.
|
|
260
|
+
*/
|
|
261
|
+
loadExtended(sources: SettingsSources<SchemaValue<T>>): ExtendedResult;
|
|
262
|
+
/**
|
|
263
|
+
* Watches config files for changes and reloads automatically.
|
|
264
|
+
* File watchers are `.unref()`'d so they don't prevent the process from exiting.
|
|
265
|
+
* @param sources - Which sources to read (env, args, files, etc.).
|
|
266
|
+
* @param options - Callbacks and debounce configuration.
|
|
267
|
+
* @returns A `ConfigWatcher` with the current config and a `close()` method.
|
|
268
|
+
* @throws {ConfigLoadError} If the initial load fails.
|
|
269
|
+
*/
|
|
270
|
+
watch(sources: SettingsSources<SchemaValue<T>>, options: WatchOptions<SchemaValue<T>>): ConfigWatcher<SchemaValue<T>>;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Masks sensitive values in an `ExtendedResult` from `loadExtended()`.
|
|
275
|
+
* Fields marked `sensitive: true` have their values replaced with `"***"`.
|
|
276
|
+
*
|
|
277
|
+
* @param result - The `ExtendedResult` returned by `loadExtended()`.
|
|
278
|
+
* @returns A new `ExtendedResult` with sensitive values masked.
|
|
279
|
+
*/
|
|
280
|
+
declare function maskSecrets(result: ExtendedResult): ExtendedResult;
|
|
281
|
+
/**
|
|
282
|
+
* Masks sensitive values in a plain config object from `load()`.
|
|
283
|
+
* Fields marked `sensitive: true` in the schema have their values replaced with `"***"`.
|
|
284
|
+
*
|
|
285
|
+
* @param config - The plain config object returned by `load()`.
|
|
286
|
+
* @param schema - The schema definition used to identify sensitive fields.
|
|
287
|
+
* @returns A new object with sensitive values masked.
|
|
288
|
+
*/
|
|
289
|
+
declare function maskSecrets<T extends Record<string, unknown>>(config: T, schema: Node): T;
|
|
290
|
+
|
|
291
|
+
declare class ConfigNodeArray {
|
|
292
|
+
arrayValues: ConfigNode[];
|
|
293
|
+
constructor(arrayValues: ConfigNode[]);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Formats the result of `loadExtended()` as a readable table showing each
|
|
298
|
+
* resolved value and where it came from.
|
|
299
|
+
*
|
|
300
|
+
* @param result - The `ExtendedResult` returned by `loadExtended()`.
|
|
301
|
+
* @param options - Optional settings.
|
|
302
|
+
* @param options.maxValueLength - Maximum length for value column (default: 50). Values longer than this are truncated.
|
|
303
|
+
* @returns The formatted table string. Also printed to `console.log` unless `options.silent` is `true`.
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```ts
|
|
307
|
+
* const result = c.schema({ port: c.number({ env: "PORT" }) }).loadExtended({ env: true, args: false });
|
|
308
|
+
* printConfig(result);
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
declare function printConfig(result: ExtendedResult, options?: {
|
|
312
|
+
silent?: boolean;
|
|
313
|
+
maxValueLength?: number;
|
|
314
|
+
}): string;
|
|
315
|
+
|
|
316
|
+
/** Options for configuring a primitive (`string`, `number`, `bool`) schema field. */
|
|
317
|
+
interface OptionPropsArgs<T> {
|
|
318
|
+
/** Whether the field must be present in at least one source. */
|
|
319
|
+
required?: boolean;
|
|
320
|
+
/** Environment variable name to read from, or `null` to disable. */
|
|
321
|
+
env?: string | null;
|
|
322
|
+
/** Whether to expose this field as a CLI argument via Commander. */
|
|
323
|
+
cli?: boolean;
|
|
324
|
+
/** Static default value or factory function returning one. */
|
|
325
|
+
defaultValue?: T | (() => T);
|
|
326
|
+
/** Help text shown in CLI `--help` output. */
|
|
327
|
+
help?: string;
|
|
328
|
+
/** Mark this field as sensitive. Sensitive values are masked by `printConfig()` and `maskSecrets()`. */
|
|
329
|
+
sensitive?: boolean;
|
|
330
|
+
/** Restrict the value to a fixed set of allowed values. Checked after type coercion, before `validate`. */
|
|
331
|
+
oneOf?: readonly T[];
|
|
332
|
+
/** Standard Schema validator run after type coercion. Accepts Zod, Valibot, ArkType, or any Standard Schema v1 implementation. */
|
|
333
|
+
validate?: StandardSchemaV1;
|
|
334
|
+
}
|
|
335
|
+
/** Options for configuring an `array` schema field. */
|
|
336
|
+
interface ArrayOptionPropsArgs<T extends OptionTypes> {
|
|
337
|
+
/** Whether the field must be present in at least one source. */
|
|
338
|
+
required?: boolean;
|
|
339
|
+
/** Schema definition for each item in the array. */
|
|
340
|
+
item: T;
|
|
341
|
+
/** Static default value or factory function returning one. */
|
|
342
|
+
defaultValue?: SchemaValue<T>[] | (() => SchemaValue<T>[]);
|
|
343
|
+
/** Standard Schema validator run on the resolved array. Accepts Zod, Valibot, ArkType, or any Standard Schema v1 implementation. */
|
|
344
|
+
validate?: StandardSchemaV1;
|
|
345
|
+
}
|
|
346
|
+
/** Options for configuring a nested `object` schema field. */
|
|
347
|
+
interface ObjectOptionPropsArgs<T extends Node> {
|
|
348
|
+
/** Whether the field must be present in at least one source. */
|
|
349
|
+
required?: boolean;
|
|
350
|
+
/** Schema definition for the nested object's shape. */
|
|
351
|
+
item: T;
|
|
352
|
+
/** Standard Schema validator run on the resolved object. Accepts Zod, Valibot, ArkType, or any Standard Schema v1 implementation. */
|
|
353
|
+
validate?: StandardSchemaV1;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Creates a string configuration option.
|
|
357
|
+
* @param opts - Option configuration (env, cli, required, defaultValue, help, oneOf).
|
|
358
|
+
* @returns A `PrimitiveOption<"string">` for use in a schema.
|
|
359
|
+
* @example
|
|
360
|
+
* c.string({ env: "HOST", defaultValue: "localhost" })
|
|
361
|
+
* c.string({ env: "NODE_ENV", oneOf: ["development", "staging", "production"] })
|
|
362
|
+
*/
|
|
363
|
+
declare function string<const V extends readonly string[]>(opts: OptionPropsArgs<string> & {
|
|
364
|
+
oneOf: V;
|
|
365
|
+
}): PrimitiveOption<"string", V[number]>;
|
|
366
|
+
declare function string(opts?: OptionPropsArgs<string>): PrimitiveOption<"string">;
|
|
367
|
+
/**
|
|
368
|
+
* Creates a number configuration option. String values from env/CLI are coerced to numbers.
|
|
369
|
+
* @param opts - Option configuration (env, cli, required, defaultValue, help, oneOf).
|
|
370
|
+
* @returns A `PrimitiveOption<"number">` for use in a schema.
|
|
371
|
+
* @example
|
|
372
|
+
* c.number({ env: "PORT", defaultValue: 3000 })
|
|
373
|
+
* c.number({ env: "LOG_LEVEL", oneOf: [0, 1, 2, 3] })
|
|
374
|
+
*/
|
|
375
|
+
declare function number<const V extends readonly number[]>(opts: OptionPropsArgs<number> & {
|
|
376
|
+
oneOf: V;
|
|
377
|
+
}): PrimitiveOption<"number", V[number]>;
|
|
378
|
+
declare function number(opts?: OptionPropsArgs<number>): PrimitiveOption<"number">;
|
|
379
|
+
/**
|
|
380
|
+
* Creates a boolean configuration option. String values `"true"`/`"false"` are coerced.
|
|
381
|
+
* @param opts - Option configuration (env, cli, required, defaultValue, help).
|
|
382
|
+
* @returns A `PrimitiveOption<"boolean">` for use in a schema.
|
|
383
|
+
* @example
|
|
384
|
+
* c.bool({ env: "DEBUG", defaultValue: false })
|
|
385
|
+
*/
|
|
386
|
+
declare function bool(opts?: OptionPropsArgs<boolean>): PrimitiveOption<"boolean">;
|
|
387
|
+
/**
|
|
388
|
+
* Config-loader entry point. Provides factory functions to define a typed configuration schema.
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* import c from "@meltstudio/config-loader";
|
|
393
|
+
*
|
|
394
|
+
* const config = c.schema({
|
|
395
|
+
* port: c.number({ env: "PORT", defaultValue: 3000 }),
|
|
396
|
+
* host: c.string({ env: "HOST" }),
|
|
397
|
+
* }).load({ env: true, args: false });
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
declare const option: {
|
|
401
|
+
string: typeof string;
|
|
402
|
+
number: typeof number;
|
|
403
|
+
bool: typeof bool;
|
|
404
|
+
array: <T extends OptionTypes>(opts: ArrayOptionPropsArgs<T>) => ArrayOption<T>;
|
|
405
|
+
object: <T extends Node>(opts: ObjectOptionPropsArgs<T>) => ObjectOption<T>;
|
|
406
|
+
schema: <T extends Node>(theSchema: T) => SettingsBuilder<T>;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
export { type ConfigChange, type ConfigErrorEntry, ConfigFileError, ConfigLoadError, ConfigNode, ConfigNodeArray, type ConfigWatcher, type ExtendedResult, type NodeTree, type RecursivePartial, type SchemaValue, type SettingsSources, type StandardSchemaV1, type WatchOptions, option as default, diffConfig, maskSecrets, printConfig };
|
package/dist/index.js
CHANGED
|
@@ -1258,6 +1258,7 @@ function createWatcher(schema2, sources, options) {
|
|
|
1258
1258
|
if (closed) return;
|
|
1259
1259
|
try {
|
|
1260
1260
|
clearFileCache();
|
|
1261
|
+
clearEnvFileCache();
|
|
1261
1262
|
const settings = new settings_default(schema2, sources);
|
|
1262
1263
|
const newConfig = settings.get();
|
|
1263
1264
|
const changes = diffConfig(
|