@optique/config 1.0.0-dev.429 → 1.0.0-dev.432

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.
@@ -1,359 +0,0 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
-
23
- //#endregion
24
- const __optique_core_annotations = __toESM(require("@optique/core/annotations"));
25
- const __optique_core_message = __toESM(require("@optique/core/message"));
26
-
27
- //#region src/index.ts
28
- /**
29
- * Unique symbol for config data in annotations.
30
- * @since 0.10.0
31
- */
32
- const configKey = Symbol.for("@optique/config");
33
- /**
34
- * Unique symbol for config metadata in annotations.
35
- * @since 1.0.0
36
- */
37
- const configMetaKey = Symbol.for("@optique/config/meta");
38
- /**
39
- * Internal registry for active config data during runWithConfig execution.
40
- * This is a workaround for the limitation that object() doesn't propagate
41
- * annotations to child field parsers.
42
- * @internal
43
- */
44
- const activeConfigRegistry = /* @__PURE__ */ new Map();
45
- /**
46
- * Internal registry for active config metadata during runWithConfig execution.
47
- * @internal
48
- */
49
- const activeConfigMetaRegistry = /* @__PURE__ */ new Map();
50
- /**
51
- * Sets active config data for a context.
52
- * @internal
53
- */
54
- function setActiveConfig(contextId, data) {
55
- activeConfigRegistry.set(contextId, data);
56
- }
57
- /**
58
- * Gets active config data for a context.
59
- * @internal
60
- */
61
- function getActiveConfig(contextId) {
62
- return activeConfigRegistry.get(contextId);
63
- }
64
- /**
65
- * Clears active config data for a context.
66
- * @internal
67
- */
68
- function clearActiveConfig(contextId) {
69
- activeConfigRegistry.delete(contextId);
70
- }
71
- /**
72
- * Sets active config metadata for a context.
73
- * @internal
74
- */
75
- function setActiveConfigMeta(contextId, meta) {
76
- activeConfigMetaRegistry.set(contextId, meta);
77
- }
78
- /**
79
- * Gets active config metadata for a context.
80
- * @internal
81
- */
82
- function getActiveConfigMeta(contextId) {
83
- return activeConfigMetaRegistry.get(contextId);
84
- }
85
- /**
86
- * Clears active config metadata for a context.
87
- * @internal
88
- */
89
- function clearActiveConfigMeta(contextId) {
90
- activeConfigMetaRegistry.delete(contextId);
91
- }
92
- /**
93
- * Creates a config context for use with Optique parsers.
94
- *
95
- * The config context implements the SourceContext interface and can be used
96
- * with runWith() or runWithConfig() to provide configuration file support.
97
- *
98
- * @template T The output type of the config schema.
99
- * @template TConfigMeta The metadata type for config sources.
100
- * @param options Configuration options including schema and optional parser.
101
- * @returns A config context that can be used with bindConfig() and runWithConfig().
102
- * @since 0.10.0
103
- *
104
- * @example
105
- * ```typescript
106
- * import { z } from "zod";
107
- * import { createConfigContext } from "@optique/config";
108
- *
109
- * const schema = z.object({
110
- * host: z.string(),
111
- * port: z.number(),
112
- * });
113
- *
114
- * const configContext = createConfigContext({ schema });
115
- * ```
116
- */
117
- function createConfigContext(options) {
118
- const contextId = Symbol.for(`@optique/config:${Math.random()}`);
119
- return {
120
- id: contextId,
121
- schema: options.schema,
122
- getAnnotations(parsed) {
123
- if (!parsed) return {};
124
- return {};
125
- }
126
- };
127
- }
128
- /**
129
- * Binds a parser to configuration values with fallback priority.
130
- *
131
- * The binding implements the following priority order:
132
- * 1. CLI argument (if provided)
133
- * 2. Config file value (if available)
134
- * 3. Default value (if specified)
135
- * 4. Error (if none of the above)
136
- *
137
- * @template M The parser mode (sync or async).
138
- * @template TValue The parser value type.
139
- * @template TState The parser state type.
140
- * @template T The config data type.
141
- * @param parser The parser to bind to config values.
142
- * @param options Binding options including context, key, and default.
143
- * @returns A new parser with config fallback behavior.
144
- * @since 0.10.0
145
- *
146
- * @example
147
- * ```typescript
148
- * import { bindConfig } from "@optique/config";
149
- * import { option } from "@optique/core/primitives";
150
- * import { string } from "@optique/core/valueparser";
151
- *
152
- * const hostParser = bindConfig(option("--host", string()), {
153
- * context: configContext,
154
- * key: "host",
155
- * default: "localhost",
156
- * });
157
- * ```
158
- */
159
- function bindConfig(parser, options) {
160
- return {
161
- $mode: parser.$mode,
162
- $valueType: parser.$valueType,
163
- $stateType: parser.$stateType,
164
- priority: parser.priority,
165
- usage: options.default !== void 0 ? [{
166
- type: "optional",
167
- terms: parser.usage
168
- }] : parser.usage,
169
- initialState: parser.initialState,
170
- parse: (context) => {
171
- const annotations = (0, __optique_core_annotations.getAnnotations)(context.state);
172
- const result = parser.parse(context);
173
- if (!(result instanceof Promise)) {
174
- if (result.success) {
175
- const newState$1 = {
176
- hasCliValue: true,
177
- cliState: result.next.state,
178
- ...annotations && { [__optique_core_annotations.annotationKey]: annotations }
179
- };
180
- return {
181
- success: true,
182
- next: {
183
- ...result.next,
184
- state: newState$1
185
- },
186
- consumed: result.consumed
187
- };
188
- }
189
- const newState = {
190
- hasCliValue: false,
191
- ...annotations && { [__optique_core_annotations.annotationKey]: annotations }
192
- };
193
- return {
194
- success: true,
195
- next: {
196
- ...context,
197
- state: newState
198
- },
199
- consumed: []
200
- };
201
- }
202
- return result.then((res) => {
203
- if (res.success) {
204
- const newState$1 = {
205
- hasCliValue: true,
206
- cliState: res.next.state,
207
- ...annotations && { [__optique_core_annotations.annotationKey]: annotations }
208
- };
209
- return {
210
- success: true,
211
- next: {
212
- ...res.next,
213
- state: newState$1
214
- },
215
- consumed: res.consumed
216
- };
217
- }
218
- const newState = {
219
- hasCliValue: false,
220
- ...annotations && { [__optique_core_annotations.annotationKey]: annotations }
221
- };
222
- return {
223
- success: true,
224
- next: {
225
- ...context,
226
- state: newState
227
- },
228
- consumed: []
229
- };
230
- });
231
- },
232
- complete: (state) => {
233
- const bindState = state;
234
- if (bindState?.hasCliValue && bindState.cliState !== void 0) {
235
- const innerResult = parser.complete(bindState.cliState);
236
- if (innerResult instanceof Promise) return innerResult.then((res) => {
237
- if (res.success) return {
238
- success: true,
239
- value: res.value
240
- };
241
- return res;
242
- });
243
- if (innerResult.success) return {
244
- success: true,
245
- value: innerResult.value
246
- };
247
- return innerResult;
248
- }
249
- return getConfigOrDefault(state, options);
250
- },
251
- suggest: parser.suggest,
252
- getDocFragments(state, upperDefaultValue) {
253
- const defaultValue = upperDefaultValue ?? options.default;
254
- return parser.getDocFragments(state, defaultValue);
255
- }
256
- };
257
- }
258
- /**
259
- * Helper function to get value from config or default.
260
- * Checks both annotations (for top-level parsers) and the active config
261
- * registry (for parsers nested inside object() when used with runWithConfig).
262
- */
263
- function getConfigOrDefault(state, options) {
264
- const annotations = (0, __optique_core_annotations.getAnnotations)(state);
265
- let configData = annotations?.[configKey];
266
- let configMeta = annotations?.[configMetaKey];
267
- if (configData === void 0 || configData === null) {
268
- const contextId = options.context.id;
269
- configData = getActiveConfig(contextId);
270
- configMeta = getActiveConfigMeta(contextId);
271
- }
272
- let configValue;
273
- if (configData !== void 0 && configData !== null) if (typeof options.key === "function") try {
274
- configValue = options.key(configData, configMeta);
275
- } catch {
276
- configValue = void 0;
277
- }
278
- else configValue = configData[options.key];
279
- if (configValue !== void 0) return {
280
- success: true,
281
- value: configValue
282
- };
283
- if (options.default !== void 0) return {
284
- success: true,
285
- value: options.default
286
- };
287
- return {
288
- success: false,
289
- error: __optique_core_message.message`Missing required configuration value.`
290
- };
291
- }
292
-
293
- //#endregion
294
- Object.defineProperty(exports, '__toESM', {
295
- enumerable: true,
296
- get: function () {
297
- return __toESM;
298
- }
299
- });
300
- Object.defineProperty(exports, 'bindConfig', {
301
- enumerable: true,
302
- get: function () {
303
- return bindConfig;
304
- }
305
- });
306
- Object.defineProperty(exports, 'clearActiveConfig', {
307
- enumerable: true,
308
- get: function () {
309
- return clearActiveConfig;
310
- }
311
- });
312
- Object.defineProperty(exports, 'clearActiveConfigMeta', {
313
- enumerable: true,
314
- get: function () {
315
- return clearActiveConfigMeta;
316
- }
317
- });
318
- Object.defineProperty(exports, 'configKey', {
319
- enumerable: true,
320
- get: function () {
321
- return configKey;
322
- }
323
- });
324
- Object.defineProperty(exports, 'configMetaKey', {
325
- enumerable: true,
326
- get: function () {
327
- return configMetaKey;
328
- }
329
- });
330
- Object.defineProperty(exports, 'createConfigContext', {
331
- enumerable: true,
332
- get: function () {
333
- return createConfigContext;
334
- }
335
- });
336
- Object.defineProperty(exports, 'getActiveConfig', {
337
- enumerable: true,
338
- get: function () {
339
- return getActiveConfig;
340
- }
341
- });
342
- Object.defineProperty(exports, 'getActiveConfigMeta', {
343
- enumerable: true,
344
- get: function () {
345
- return getActiveConfigMeta;
346
- }
347
- });
348
- Object.defineProperty(exports, 'setActiveConfig', {
349
- enumerable: true,
350
- get: function () {
351
- return setActiveConfig;
352
- }
353
- });
354
- Object.defineProperty(exports, 'setActiveConfigMeta', {
355
- enumerable: true,
356
- get: function () {
357
- return setActiveConfigMeta;
358
- }
359
- });