@optique/config 0.10.7-dev.485 → 1.0.0-dev.1109
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 +4 -3
- package/dist/index.cjs +616 -7
- package/dist/index.d.cts +237 -2
- package/dist/index.d.ts +237 -2
- package/dist/index.js +586 -2
- package/package.json +4 -11
- package/dist/index-CYn_0yAG.d.ts +0 -155
- package/dist/index-D25zYfjf.d.cts +0 -155
- package/dist/run.cjs +0 -189
- package/dist/run.d.cts +0 -297
- package/dist/run.d.ts +0 -297
- package/dist/run.js +0 -189
- package/dist/src-9MkUoh9z.cjs +0 -301
- package/dist/src-Dm_17c1d.js +0 -237
package/dist/index.d.cts
CHANGED
|
@@ -1,2 +1,237 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { ParserValuePlaceholder, SourceContext } from "@optique/core/context";
|
|
3
|
+
import { Parser } from "@optique/core/parser";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Metadata about the loaded config source.
|
|
9
|
+
*
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
interface ConfigMeta {
|
|
13
|
+
/**
|
|
14
|
+
* Directory containing the config file.
|
|
15
|
+
*/
|
|
16
|
+
readonly configDir: string;
|
|
17
|
+
/**
|
|
18
|
+
* Absolute path to the config file.
|
|
19
|
+
*/
|
|
20
|
+
readonly configPath: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sets active config data for a context.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
declare function setActiveConfig<T>(contextId: symbol, data: T): void;
|
|
27
|
+
/**
|
|
28
|
+
* Gets active config data for a context.
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
declare function getActiveConfig<T>(contextId: symbol): T | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Clears active config data for a context.
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
declare function clearActiveConfig(contextId: symbol): void;
|
|
37
|
+
/**
|
|
38
|
+
* Sets active config metadata for a context.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
declare function setActiveConfigMeta<T>(contextId: symbol, meta: T): void;
|
|
42
|
+
/**
|
|
43
|
+
* Gets active config metadata for a context.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
declare function getActiveConfigMeta<T>(contextId: symbol): T | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Clears active config metadata for a context.
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
declare function clearActiveConfigMeta(contextId: symbol): void;
|
|
52
|
+
/**
|
|
53
|
+
* Options for creating a config context.
|
|
54
|
+
*
|
|
55
|
+
* @template T The output type of the config schema.
|
|
56
|
+
* @since 0.10.0
|
|
57
|
+
*/
|
|
58
|
+
interface ConfigContextOptions<T> {
|
|
59
|
+
/**
|
|
60
|
+
* Standard Schema validator for the config file.
|
|
61
|
+
* Accepts any Standard Schema-compatible library (Zod, Valibot, ArkType, etc.).
|
|
62
|
+
*/
|
|
63
|
+
readonly schema: StandardSchemaV1<unknown, T>;
|
|
64
|
+
/**
|
|
65
|
+
* Custom parser function for reading config file contents.
|
|
66
|
+
* If not provided, defaults to JSON.parse.
|
|
67
|
+
*
|
|
68
|
+
* This option is only used in single-file mode (with `getConfigPath`).
|
|
69
|
+
* When using the `load` callback, file parsing is handled by the loader.
|
|
70
|
+
*
|
|
71
|
+
* @param contents The raw file contents as Uint8Array.
|
|
72
|
+
* @returns The parsed config data (will be validated by schema).
|
|
73
|
+
* @since 1.0.0
|
|
74
|
+
*/
|
|
75
|
+
readonly fileParser?: (contents: Uint8Array) => unknown;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Result type for custom config loading.
|
|
79
|
+
*
|
|
80
|
+
* @template TConfigMeta Metadata type associated with loaded config data.
|
|
81
|
+
* @since 1.0.0
|
|
82
|
+
*/
|
|
83
|
+
interface ConfigLoadResult<TConfigMeta = ConfigMeta> {
|
|
84
|
+
/**
|
|
85
|
+
* Raw config data to validate against the schema.
|
|
86
|
+
*/
|
|
87
|
+
readonly config: unknown;
|
|
88
|
+
/**
|
|
89
|
+
* Metadata about where the config came from, if available.
|
|
90
|
+
*/
|
|
91
|
+
readonly meta: TConfigMeta | undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Required options for ConfigContext when used with `runWith()` or `run()`.
|
|
95
|
+
* The `ParserValuePlaceholder` will be substituted with the actual parser
|
|
96
|
+
* result type by `runWith()`.
|
|
97
|
+
*
|
|
98
|
+
* Provide *either* `getConfigPath` (single-file mode) *or* `load` (custom
|
|
99
|
+
* multi-file mode). At least one must be provided; a runtime error is
|
|
100
|
+
* thrown otherwise.
|
|
101
|
+
*
|
|
102
|
+
* @template TConfigMeta Metadata type for config sources.
|
|
103
|
+
* @since 0.10.0
|
|
104
|
+
*/
|
|
105
|
+
interface ConfigContextRequiredOptions<TConfigMeta = ConfigMeta> {
|
|
106
|
+
/**
|
|
107
|
+
* Function to extract config file path from parsed CLI arguments.
|
|
108
|
+
* Used in single-file mode. The `parsed` parameter is typed as the
|
|
109
|
+
* parser's result type.
|
|
110
|
+
*
|
|
111
|
+
* @param parsed The parsed CLI arguments (typed from parser).
|
|
112
|
+
* @returns The config file path, or undefined if not specified.
|
|
113
|
+
*/
|
|
114
|
+
readonly getConfigPath?: (parsed: ParserValuePlaceholder) => string | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Custom loader function that receives the first-pass parse result and
|
|
117
|
+
* returns the config data (or a Promise of it). This allows full control
|
|
118
|
+
* over file discovery, loading, merging, and error handling.
|
|
119
|
+
*
|
|
120
|
+
* The returned data will be validated against the schema.
|
|
121
|
+
*
|
|
122
|
+
* When `load` is provided, `getConfigPath` is ignored.
|
|
123
|
+
*
|
|
124
|
+
* @param parsed The result from the first parse pass.
|
|
125
|
+
* @returns Config data and metadata (config is validated by schema).
|
|
126
|
+
* @since 1.0.0
|
|
127
|
+
*/
|
|
128
|
+
readonly load?: (parsed: ParserValuePlaceholder) => Promise<ConfigLoadResult<TConfigMeta>> | ConfigLoadResult<TConfigMeta>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A config context that provides configuration data via annotations.
|
|
132
|
+
*
|
|
133
|
+
* When used with `runWith()` or `run()`, the options must include either
|
|
134
|
+
* `getConfigPath` or `load` with the correct parser result type. The
|
|
135
|
+
* `ParserValuePlaceholder` in `ConfigContextRequiredOptions` is substituted
|
|
136
|
+
* with the actual parser type.
|
|
137
|
+
*
|
|
138
|
+
* @template T The validated config data type.
|
|
139
|
+
* @template TConfigMeta Metadata type for config sources.
|
|
140
|
+
* @since 0.10.0
|
|
141
|
+
*/
|
|
142
|
+
interface ConfigContext<T, TConfigMeta = ConfigMeta> extends SourceContext<ConfigContextRequiredOptions<TConfigMeta>> {
|
|
143
|
+
/**
|
|
144
|
+
* The Standard Schema validator for the config file.
|
|
145
|
+
*/
|
|
146
|
+
readonly schema: StandardSchemaV1<unknown, T>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Creates a config context for use with Optique parsers.
|
|
150
|
+
*
|
|
151
|
+
* The config context implements the `SourceContext` interface and can be used
|
|
152
|
+
* with `runWith()` from *@optique/core* or `run()`/`runAsync()` from
|
|
153
|
+
* *@optique/run* to provide configuration file support.
|
|
154
|
+
*
|
|
155
|
+
* @template T The output type of the config schema.
|
|
156
|
+
* @template TConfigMeta The metadata type for config sources.
|
|
157
|
+
* @param options Configuration options including schema and optional file
|
|
158
|
+
* parser.
|
|
159
|
+
* @returns A config context that can be used with `bindConfig()` and runners.
|
|
160
|
+
* @throws {TypeError} If `schema` is not a valid Standard Schema object.
|
|
161
|
+
* @throws {TypeError} If `fileParser` is provided but is not a function.
|
|
162
|
+
* @since 0.10.0
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* import { z } from "zod";
|
|
167
|
+
* import { createConfigContext } from "@optique/config";
|
|
168
|
+
*
|
|
169
|
+
* const schema = z.object({
|
|
170
|
+
* host: z.string(),
|
|
171
|
+
* port: z.number(),
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* const configContext = createConfigContext({ schema });
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
declare function createConfigContext<T, TConfigMeta = ConfigMeta>(options: ConfigContextOptions<T>): ConfigContext<T, TConfigMeta>;
|
|
178
|
+
/**
|
|
179
|
+
* Options for binding a parser to config values.
|
|
180
|
+
*
|
|
181
|
+
* @template T The config data type.
|
|
182
|
+
* @template TValue The value type extracted from config.
|
|
183
|
+
* @since 0.10.0
|
|
184
|
+
*/
|
|
185
|
+
interface BindConfigOptions<T, TValue, TConfigMeta = ConfigMeta> {
|
|
186
|
+
/**
|
|
187
|
+
* The config context to use for fallback values.
|
|
188
|
+
*/
|
|
189
|
+
readonly context: ConfigContext<T, TConfigMeta>;
|
|
190
|
+
/**
|
|
191
|
+
* Key or accessor function to extract the value from config.
|
|
192
|
+
* Can be a property key (for top-level config values) or a function
|
|
193
|
+
* that extracts nested values. Accessor callbacks receive config metadata,
|
|
194
|
+
* if available, as the second argument.
|
|
195
|
+
*/
|
|
196
|
+
readonly key: keyof T | ((config: T, meta: TConfigMeta | undefined) => TValue);
|
|
197
|
+
/**
|
|
198
|
+
* Default value to use when neither CLI nor config provides a value.
|
|
199
|
+
* If not specified, the parser will fail when no value is available.
|
|
200
|
+
*/
|
|
201
|
+
readonly default?: TValue;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Binds a parser to configuration values with fallback priority.
|
|
205
|
+
*
|
|
206
|
+
* The binding implements the following priority order:
|
|
207
|
+
* 1. CLI argument (if provided)
|
|
208
|
+
* 2. Config file value (if available)
|
|
209
|
+
* 3. Default value (if specified)
|
|
210
|
+
* 4. Error (if none of the above)
|
|
211
|
+
*
|
|
212
|
+
* @template M The parser mode (sync or async).
|
|
213
|
+
* @template TValue The parser value type.
|
|
214
|
+
* @template TState The parser state type.
|
|
215
|
+
* @template T The config data type.
|
|
216
|
+
* @param parser The parser to bind to config values.
|
|
217
|
+
* @param options Binding options including context, key, and default.
|
|
218
|
+
* @returns A new parser with config fallback behavior.
|
|
219
|
+
* @throws {TypeError} If `key` is not a property key or function.
|
|
220
|
+
* @since 0.10.0
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* import { bindConfig } from "@optique/config";
|
|
225
|
+
* import { option } from "@optique/core/primitives";
|
|
226
|
+
* import { string } from "@optique/core/valueparser";
|
|
227
|
+
*
|
|
228
|
+
* const hostParser = bindConfig(option("--host", string()), {
|
|
229
|
+
* context: configContext,
|
|
230
|
+
* key: "host",
|
|
231
|
+
* default: "localhost",
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function bindConfig<M extends "sync" | "async", TValue, TState, T, TConfigMeta = ConfigMeta>(parser: Parser<M, TValue, TState>, options: BindConfigOptions<T, TValue, TConfigMeta>): Parser<M, TValue, TState>;
|
|
236
|
+
//#endregion
|
|
237
|
+
export { BindConfigOptions, ConfigContext, ConfigContextOptions, ConfigContextRequiredOptions, ConfigLoadResult, ConfigMeta, bindConfig, clearActiveConfig, clearActiveConfigMeta, createConfigContext, getActiveConfig, getActiveConfigMeta, setActiveConfig, setActiveConfigMeta };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,237 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { ParserValuePlaceholder, SourceContext } from "@optique/core/context";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
import { Parser } from "@optique/core/parser";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Metadata about the loaded config source.
|
|
9
|
+
*
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
*/
|
|
12
|
+
interface ConfigMeta {
|
|
13
|
+
/**
|
|
14
|
+
* Directory containing the config file.
|
|
15
|
+
*/
|
|
16
|
+
readonly configDir: string;
|
|
17
|
+
/**
|
|
18
|
+
* Absolute path to the config file.
|
|
19
|
+
*/
|
|
20
|
+
readonly configPath: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sets active config data for a context.
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
declare function setActiveConfig<T>(contextId: symbol, data: T): void;
|
|
27
|
+
/**
|
|
28
|
+
* Gets active config data for a context.
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
declare function getActiveConfig<T>(contextId: symbol): T | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Clears active config data for a context.
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
declare function clearActiveConfig(contextId: symbol): void;
|
|
37
|
+
/**
|
|
38
|
+
* Sets active config metadata for a context.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
declare function setActiveConfigMeta<T>(contextId: symbol, meta: T): void;
|
|
42
|
+
/**
|
|
43
|
+
* Gets active config metadata for a context.
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
declare function getActiveConfigMeta<T>(contextId: symbol): T | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Clears active config metadata for a context.
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
declare function clearActiveConfigMeta(contextId: symbol): void;
|
|
52
|
+
/**
|
|
53
|
+
* Options for creating a config context.
|
|
54
|
+
*
|
|
55
|
+
* @template T The output type of the config schema.
|
|
56
|
+
* @since 0.10.0
|
|
57
|
+
*/
|
|
58
|
+
interface ConfigContextOptions<T> {
|
|
59
|
+
/**
|
|
60
|
+
* Standard Schema validator for the config file.
|
|
61
|
+
* Accepts any Standard Schema-compatible library (Zod, Valibot, ArkType, etc.).
|
|
62
|
+
*/
|
|
63
|
+
readonly schema: StandardSchemaV1<unknown, T>;
|
|
64
|
+
/**
|
|
65
|
+
* Custom parser function for reading config file contents.
|
|
66
|
+
* If not provided, defaults to JSON.parse.
|
|
67
|
+
*
|
|
68
|
+
* This option is only used in single-file mode (with `getConfigPath`).
|
|
69
|
+
* When using the `load` callback, file parsing is handled by the loader.
|
|
70
|
+
*
|
|
71
|
+
* @param contents The raw file contents as Uint8Array.
|
|
72
|
+
* @returns The parsed config data (will be validated by schema).
|
|
73
|
+
* @since 1.0.0
|
|
74
|
+
*/
|
|
75
|
+
readonly fileParser?: (contents: Uint8Array) => unknown;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Result type for custom config loading.
|
|
79
|
+
*
|
|
80
|
+
* @template TConfigMeta Metadata type associated with loaded config data.
|
|
81
|
+
* @since 1.0.0
|
|
82
|
+
*/
|
|
83
|
+
interface ConfigLoadResult<TConfigMeta = ConfigMeta> {
|
|
84
|
+
/**
|
|
85
|
+
* Raw config data to validate against the schema.
|
|
86
|
+
*/
|
|
87
|
+
readonly config: unknown;
|
|
88
|
+
/**
|
|
89
|
+
* Metadata about where the config came from, if available.
|
|
90
|
+
*/
|
|
91
|
+
readonly meta: TConfigMeta | undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Required options for ConfigContext when used with `runWith()` or `run()`.
|
|
95
|
+
* The `ParserValuePlaceholder` will be substituted with the actual parser
|
|
96
|
+
* result type by `runWith()`.
|
|
97
|
+
*
|
|
98
|
+
* Provide *either* `getConfigPath` (single-file mode) *or* `load` (custom
|
|
99
|
+
* multi-file mode). At least one must be provided; a runtime error is
|
|
100
|
+
* thrown otherwise.
|
|
101
|
+
*
|
|
102
|
+
* @template TConfigMeta Metadata type for config sources.
|
|
103
|
+
* @since 0.10.0
|
|
104
|
+
*/
|
|
105
|
+
interface ConfigContextRequiredOptions<TConfigMeta = ConfigMeta> {
|
|
106
|
+
/**
|
|
107
|
+
* Function to extract config file path from parsed CLI arguments.
|
|
108
|
+
* Used in single-file mode. The `parsed` parameter is typed as the
|
|
109
|
+
* parser's result type.
|
|
110
|
+
*
|
|
111
|
+
* @param parsed The parsed CLI arguments (typed from parser).
|
|
112
|
+
* @returns The config file path, or undefined if not specified.
|
|
113
|
+
*/
|
|
114
|
+
readonly getConfigPath?: (parsed: ParserValuePlaceholder) => string | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* Custom loader function that receives the first-pass parse result and
|
|
117
|
+
* returns the config data (or a Promise of it). This allows full control
|
|
118
|
+
* over file discovery, loading, merging, and error handling.
|
|
119
|
+
*
|
|
120
|
+
* The returned data will be validated against the schema.
|
|
121
|
+
*
|
|
122
|
+
* When `load` is provided, `getConfigPath` is ignored.
|
|
123
|
+
*
|
|
124
|
+
* @param parsed The result from the first parse pass.
|
|
125
|
+
* @returns Config data and metadata (config is validated by schema).
|
|
126
|
+
* @since 1.0.0
|
|
127
|
+
*/
|
|
128
|
+
readonly load?: (parsed: ParserValuePlaceholder) => Promise<ConfigLoadResult<TConfigMeta>> | ConfigLoadResult<TConfigMeta>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A config context that provides configuration data via annotations.
|
|
132
|
+
*
|
|
133
|
+
* When used with `runWith()` or `run()`, the options must include either
|
|
134
|
+
* `getConfigPath` or `load` with the correct parser result type. The
|
|
135
|
+
* `ParserValuePlaceholder` in `ConfigContextRequiredOptions` is substituted
|
|
136
|
+
* with the actual parser type.
|
|
137
|
+
*
|
|
138
|
+
* @template T The validated config data type.
|
|
139
|
+
* @template TConfigMeta Metadata type for config sources.
|
|
140
|
+
* @since 0.10.0
|
|
141
|
+
*/
|
|
142
|
+
interface ConfigContext<T, TConfigMeta = ConfigMeta> extends SourceContext<ConfigContextRequiredOptions<TConfigMeta>> {
|
|
143
|
+
/**
|
|
144
|
+
* The Standard Schema validator for the config file.
|
|
145
|
+
*/
|
|
146
|
+
readonly schema: StandardSchemaV1<unknown, T>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Creates a config context for use with Optique parsers.
|
|
150
|
+
*
|
|
151
|
+
* The config context implements the `SourceContext` interface and can be used
|
|
152
|
+
* with `runWith()` from *@optique/core* or `run()`/`runAsync()` from
|
|
153
|
+
* *@optique/run* to provide configuration file support.
|
|
154
|
+
*
|
|
155
|
+
* @template T The output type of the config schema.
|
|
156
|
+
* @template TConfigMeta The metadata type for config sources.
|
|
157
|
+
* @param options Configuration options including schema and optional file
|
|
158
|
+
* parser.
|
|
159
|
+
* @returns A config context that can be used with `bindConfig()` and runners.
|
|
160
|
+
* @throws {TypeError} If `schema` is not a valid Standard Schema object.
|
|
161
|
+
* @throws {TypeError} If `fileParser` is provided but is not a function.
|
|
162
|
+
* @since 0.10.0
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* import { z } from "zod";
|
|
167
|
+
* import { createConfigContext } from "@optique/config";
|
|
168
|
+
*
|
|
169
|
+
* const schema = z.object({
|
|
170
|
+
* host: z.string(),
|
|
171
|
+
* port: z.number(),
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* const configContext = createConfigContext({ schema });
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
declare function createConfigContext<T, TConfigMeta = ConfigMeta>(options: ConfigContextOptions<T>): ConfigContext<T, TConfigMeta>;
|
|
178
|
+
/**
|
|
179
|
+
* Options for binding a parser to config values.
|
|
180
|
+
*
|
|
181
|
+
* @template T The config data type.
|
|
182
|
+
* @template TValue The value type extracted from config.
|
|
183
|
+
* @since 0.10.0
|
|
184
|
+
*/
|
|
185
|
+
interface BindConfigOptions<T, TValue, TConfigMeta = ConfigMeta> {
|
|
186
|
+
/**
|
|
187
|
+
* The config context to use for fallback values.
|
|
188
|
+
*/
|
|
189
|
+
readonly context: ConfigContext<T, TConfigMeta>;
|
|
190
|
+
/**
|
|
191
|
+
* Key or accessor function to extract the value from config.
|
|
192
|
+
* Can be a property key (for top-level config values) or a function
|
|
193
|
+
* that extracts nested values. Accessor callbacks receive config metadata,
|
|
194
|
+
* if available, as the second argument.
|
|
195
|
+
*/
|
|
196
|
+
readonly key: keyof T | ((config: T, meta: TConfigMeta | undefined) => TValue);
|
|
197
|
+
/**
|
|
198
|
+
* Default value to use when neither CLI nor config provides a value.
|
|
199
|
+
* If not specified, the parser will fail when no value is available.
|
|
200
|
+
*/
|
|
201
|
+
readonly default?: TValue;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Binds a parser to configuration values with fallback priority.
|
|
205
|
+
*
|
|
206
|
+
* The binding implements the following priority order:
|
|
207
|
+
* 1. CLI argument (if provided)
|
|
208
|
+
* 2. Config file value (if available)
|
|
209
|
+
* 3. Default value (if specified)
|
|
210
|
+
* 4. Error (if none of the above)
|
|
211
|
+
*
|
|
212
|
+
* @template M The parser mode (sync or async).
|
|
213
|
+
* @template TValue The parser value type.
|
|
214
|
+
* @template TState The parser state type.
|
|
215
|
+
* @template T The config data type.
|
|
216
|
+
* @param parser The parser to bind to config values.
|
|
217
|
+
* @param options Binding options including context, key, and default.
|
|
218
|
+
* @returns A new parser with config fallback behavior.
|
|
219
|
+
* @throws {TypeError} If `key` is not a property key or function.
|
|
220
|
+
* @since 0.10.0
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* import { bindConfig } from "@optique/config";
|
|
225
|
+
* import { option } from "@optique/core/primitives";
|
|
226
|
+
* import { string } from "@optique/core/valueparser";
|
|
227
|
+
*
|
|
228
|
+
* const hostParser = bindConfig(option("--host", string()), {
|
|
229
|
+
* context: configContext,
|
|
230
|
+
* key: "host",
|
|
231
|
+
* default: "localhost",
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function bindConfig<M extends "sync" | "async", TValue, TState, T, TConfigMeta = ConfigMeta>(parser: Parser<M, TValue, TState>, options: BindConfigOptions<T, TValue, TConfigMeta>): Parser<M, TValue, TState>;
|
|
236
|
+
//#endregion
|
|
237
|
+
export { BindConfigOptions, ConfigContext, ConfigContextOptions, ConfigContextRequiredOptions, ConfigLoadResult, ConfigMeta, bindConfig, clearActiveConfig, clearActiveConfigMeta, createConfigContext, getActiveConfig, getActiveConfigMeta, setActiveConfig, setActiveConfigMeta };
|