@backstage/config-loader 1.2.0 → 1.3.0

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/dist/index.d.ts CHANGED
@@ -1,30 +1,6 @@
1
- import { AppConfig } from '@backstage/config';
2
1
  import { JSONSchema7 } from 'json-schema';
3
- import { JsonObject } from '@backstage/types';
4
-
5
- /**
6
- * Read runtime configuration from the environment.
7
- *
8
- * Only environment variables prefixed with APP_CONFIG_ will be considered.
9
- *
10
- * For each variable, the prefix will be removed, and rest of the key will
11
- * be split by '_'. Each part will then be used as keys to build up a nested
12
- * config object structure. The treatment of the entire environment variable
13
- * is case-sensitive.
14
- *
15
- * The value of the variable should be JSON serialized, as it will be parsed
16
- * and the type will be kept intact. For example "true" and true are treated
17
- * differently, as well as "42" and 42.
18
- *
19
- * For example, to set the config app.title to "My Title", use the following:
20
- *
21
- * APP_CONFIG_app_title='"My Title"'
22
- *
23
- * @public
24
- */
25
- declare function readEnvConfig(env: {
26
- [name: string]: string | undefined;
27
- }): AppConfig[];
2
+ import { JsonObject, HumanDuration, Observable } from '@backstage/types';
3
+ import { AppConfig, Config } from '@backstage/config';
28
4
 
29
5
  /**
30
6
  * A type representing the possible configuration value visibilities
@@ -98,11 +74,13 @@ declare function mergeConfigSchemas(schemas: JSONSchema7[]): JSONSchema7;
98
74
  *
99
75
  * @public
100
76
  */
101
- type LoadConfigSchemaOptions = {
77
+ type LoadConfigSchemaOptions = ({
102
78
  dependencies: string[];
103
79
  packagePaths?: string[];
104
80
  } | {
105
81
  serialized: JsonObject;
82
+ }) & {
83
+ noUndeclaredProperties?: boolean;
106
84
  };
107
85
  /**
108
86
  * Loads config schema for a Backstage instance.
@@ -111,13 +89,19 @@ type LoadConfigSchemaOptions = {
111
89
  */
112
90
  declare function loadConfigSchema(options: LoadConfigSchemaOptions): Promise<ConfigSchema>;
113
91
 
114
- /** @public */
92
+ /**
93
+ * @public
94
+ * @deprecated Use {@link ConfigSources.default} instead.
95
+ */
115
96
  type ConfigTarget = {
116
97
  path: string;
117
98
  } | {
118
99
  url: string;
119
100
  };
120
- /** @public */
101
+ /**
102
+ * @public
103
+ * @deprecated Use {@link ConfigSources.default} instead.
104
+ */
121
105
  type LoadConfigOptionsWatch = {
122
106
  /**
123
107
  * A listener that is called when a config file is changed.
@@ -128,7 +112,10 @@ type LoadConfigOptionsWatch = {
128
112
  */
129
113
  stopSignal?: Promise<void>;
130
114
  };
131
- /** @public */
115
+ /**
116
+ * @public
117
+ * @deprecated Use {@link ConfigSources.default} instead.
118
+ */
132
119
  type LoadConfigOptionsRemote = {
133
120
  /**
134
121
  * A remote config reloading period, in seconds
@@ -139,6 +126,7 @@ type LoadConfigOptionsRemote = {
139
126
  * Options that control the loading of configuration files in the backend.
140
127
  *
141
128
  * @public
129
+ * @deprecated Use {@link ConfigSources.default} instead.
142
130
  */
143
131
  type LoadConfigOptions = {
144
132
  configRoot: string;
@@ -161,6 +149,7 @@ type LoadConfigOptions = {
161
149
  /**
162
150
  * Results of loading configuration files.
163
151
  * @public
152
+ * @deprecated Use {@link ConfigSources.default} instead.
164
153
  */
165
154
  type LoadConfigResult = {
166
155
  /**
@@ -172,7 +161,427 @@ type LoadConfigResult = {
172
161
  * Load configuration data.
173
162
  *
174
163
  * @public
164
+ * @deprecated Use {@link ConfigSources.default} instead.
175
165
  */
176
166
  declare function loadConfig(options: LoadConfigOptions): Promise<LoadConfigResult>;
177
167
 
178
- export { ConfigSchema, ConfigSchemaProcessingOptions, ConfigTarget, ConfigVisibility, LoadConfigOptions, LoadConfigOptionsRemote, LoadConfigOptionsWatch, LoadConfigResult, LoadConfigSchemaOptions, TransformFunc, loadConfig, loadConfigSchema, mergeConfigSchemas, readEnvConfig };
168
+ /**
169
+ * The data returned by {@link ConfigSource.readConfigData}.
170
+ *
171
+ * @public
172
+ */
173
+ interface ConfigSourceData extends AppConfig {
174
+ /**
175
+ * The file path that this configuration was loaded from, if it was loaded from a file.
176
+ */
177
+ path?: string;
178
+ }
179
+ /**
180
+ * Options for {@link ConfigSource.readConfigData}.
181
+ *
182
+ * @public
183
+ */
184
+ interface ReadConfigDataOptions {
185
+ signal?: AbortSignal;
186
+ }
187
+ /**
188
+ * The the generator returned by {@link ConfigSource.readConfigData}.
189
+ *
190
+ * @public
191
+ */
192
+ type AsyncConfigSourceGenerator = AsyncGenerator<{
193
+ configs: ConfigSourceData[];
194
+ }, void, void>;
195
+ /**
196
+ * A source of configuration data.
197
+ *
198
+ * @remarks
199
+ *
200
+ * It is recommended to implement the `readConfigData` method as an async generator.
201
+ *
202
+ * @example
203
+ *
204
+ * ```ts
205
+ * class MyConfigSource implements ConfigSource {
206
+ * async *readConfigData() {
207
+ * yield {
208
+ * config: [{
209
+ * context: 'example',
210
+ * data: { backend: { baseUrl: 'http://localhost' } }
211
+ * }]
212
+ * };
213
+ * }
214
+ * }
215
+ * ```
216
+ *
217
+ * @public
218
+ */
219
+ interface ConfigSource {
220
+ readConfigData(options?: ReadConfigDataOptions): AsyncConfigSourceGenerator;
221
+ }
222
+ /**
223
+ * A custom function to be used for substitution withing configuration files.
224
+ *
225
+ * @remarks
226
+ *
227
+ * Substitutions use the following syntax: `baseUrl: https://${HOSTNAME}`, where
228
+ * `'HOSTNAME'` is the name of the variable to be substituted.
229
+ *
230
+ * The default substitution function will read the value of the environment.
231
+ *
232
+ * @public
233
+ */
234
+ type SubstitutionFunc = (name: string) => Promise<string | undefined>;
235
+
236
+ /**
237
+ * Options for {@link RemoteConfigSource.create}.
238
+ *
239
+ * @public
240
+ */
241
+ interface RemoteConfigSourceOptions {
242
+ /**
243
+ * The URL to load the config from.
244
+ */
245
+ url: string;
246
+ /**
247
+ * How often to reload the config from the remote URL, defaults to 1 minute.
248
+ *
249
+ * Set to Infinity to disable reloading, for example `{ days: Infinity }`.
250
+ */
251
+ reloadInterval?: HumanDuration;
252
+ /**
253
+ * A substitution function to use instead of the default environment substitution.
254
+ */
255
+ substitutionFunc?: SubstitutionFunc;
256
+ }
257
+ /**
258
+ * A config source that loads configuration from a remote URL.
259
+ *
260
+ * @public
261
+ */
262
+ declare class RemoteConfigSource implements ConfigSource {
263
+ #private;
264
+ /**
265
+ * Creates a new {@link RemoteConfigSource}.
266
+ *
267
+ * @param options - Options for the source.
268
+ * @returns A new remote config source.
269
+ */
270
+ static create(options: RemoteConfigSourceOptions): ConfigSource;
271
+ private constructor();
272
+ readConfigData(options?: ReadConfigDataOptions | undefined): AsyncConfigSourceGenerator;
273
+ toString(): string;
274
+ }
275
+
276
+ /**
277
+ * A target to read configuration from.
278
+ *
279
+ * @public
280
+ */
281
+ type ConfigSourceTarget = {
282
+ type: 'path';
283
+ target: string;
284
+ } | {
285
+ type: 'url';
286
+ target: string;
287
+ };
288
+ /**
289
+ * A config implementation that can be closed.
290
+ *
291
+ * @remarks
292
+ *
293
+ * Closing the configuration instance will stop the reading from the underlying source.
294
+ *
295
+ * @public
296
+ */
297
+ interface ClosableConfig extends Config {
298
+ /**
299
+ * Closes the configuration instance.
300
+ *
301
+ * @remarks
302
+ *
303
+ * The configuration instance will still be usable after closing, but it will
304
+ * no longer be updated with new values from the underlying source.
305
+ */
306
+ close(): void;
307
+ }
308
+ /**
309
+ * Common options for the default Backstage configuration sources.
310
+ *
311
+ * @public
312
+ */
313
+ interface BaseConfigSourcesOptions {
314
+ rootDir?: string;
315
+ remote?: Pick<RemoteConfigSourceOptions, 'reloadInterval'>;
316
+ substitutionFunc?: SubstitutionFunc;
317
+ }
318
+ /**
319
+ * Options for {@link ConfigSources.defaultForTargets}.
320
+ *
321
+ * @public
322
+ */
323
+ interface ConfigSourcesDefaultForTargetsOptions extends BaseConfigSourcesOptions {
324
+ targets: ConfigSourceTarget[];
325
+ }
326
+ /**
327
+ * Options for {@link ConfigSources.default}.
328
+ *
329
+ * @public
330
+ */
331
+ interface ConfigSourcesDefaultOptions extends BaseConfigSourcesOptions {
332
+ argv?: string[];
333
+ env?: Record<string, string>;
334
+ }
335
+ /**
336
+ * A collection of utilities for working with and creating {@link ConfigSource}s.
337
+ *
338
+ * @public
339
+ */
340
+ declare class ConfigSources {
341
+ /**
342
+ * Parses command line arguments and returns the config targets.
343
+ *
344
+ * @param argv - The command line arguments to parse. Defaults to `process.argv`
345
+ * @returns A list of config targets
346
+ */
347
+ static parseArgs(argv?: string[]): ConfigSourceTarget[];
348
+ /**
349
+ * Creates the default config sources for the provided targets.
350
+ *
351
+ * @remarks
352
+ *
353
+ * This will create {@link FileConfigSource}s and {@link RemoteConfigSource}s
354
+ * for the provided targets, and merge them together to a single source.
355
+ * If no targets are provided it will fall back to `app-config.yaml` and
356
+ * `app-config.local.yaml`.
357
+ *
358
+ * URL targets are only supported if the `remote` option is provided.
359
+ *
360
+ * @param options - Options
361
+ * @returns A config source for the provided targets
362
+ */
363
+ static defaultForTargets(options: ConfigSourcesDefaultForTargetsOptions): ConfigSource;
364
+ /**
365
+ * Creates the default config source for Backstage.
366
+ *
367
+ * @remarks
368
+ *
369
+ * This will read from `app-config.yaml` and `app-config.local.yaml` by
370
+ * default, as well as environment variables prefixed with `APP_CONFIG_`.
371
+ * If `--config <path|url>` command line arguments are passed, these will
372
+ * override the default configuration file paths. URLs are only supported
373
+ * if the `remote` option is provided.
374
+ *
375
+ * @param options - Options
376
+ * @returns The default Backstage config source
377
+ */
378
+ static default(options: ConfigSourcesDefaultOptions): ConfigSource;
379
+ /**
380
+ * Merges multiple config sources into a single source that reads from all
381
+ * sources and concatenates the result.
382
+ *
383
+ * @param sources - The config sources to merge
384
+ * @returns A single config source that concatenates the data from the given sources
385
+ */
386
+ static merge(sources: ConfigSource[]): ConfigSource;
387
+ /**
388
+ * Creates an observable {@link @backstage/config#Config} implementation from a {@link ConfigSource}.
389
+ *
390
+ * @remarks
391
+ *
392
+ * If you only want to read the config once you can close the returned config immediately.
393
+ *
394
+ * @example
395
+ *
396
+ * ```ts
397
+ * const sources = ConfigSources.default(...)
398
+ * const config = await ConfigSources.toConfig(source)
399
+ * config.close()
400
+ * const example = config.getString(...)
401
+ * ```
402
+ *
403
+ * @param source - The config source to read from
404
+ * @returns A promise that resolves to a closable config
405
+ */
406
+ static toConfig(source: ConfigSource): Promise<ClosableConfig>;
407
+ }
408
+
409
+ /**
410
+ * Options for {@link EnvConfigSource.create}.
411
+ *
412
+ * @public
413
+ */
414
+ interface EnvConfigSourceOptions {
415
+ /**
416
+ * The environment variables to use, defaults to `process.env`.
417
+ */
418
+ env?: Record<string, string | undefined>;
419
+ }
420
+ /**
421
+ * A config source that reads configuration from the environment.
422
+ *
423
+ * @remarks
424
+ *
425
+ * Only environment variables prefixed with APP_CONFIG_ will be considered.
426
+ *
427
+ * For each variable, the prefix will be removed, and rest of the key will
428
+ * be split by '_'. Each part will then be used as keys to build up a nested
429
+ * config object structure. The treatment of the entire environment variable
430
+ * is case-sensitive.
431
+ *
432
+ * The value of the variable should be JSON serialized, as it will be parsed
433
+ * and the type will be kept intact. For example "true" and true are treated
434
+ * differently, as well as "42" and 42.
435
+ *
436
+ * For example, to set the config app.title to "My Title", use the following:
437
+ *
438
+ * APP_CONFIG_app_title='"My Title"'
439
+ *
440
+ * @public
441
+ */
442
+ declare class EnvConfigSource implements ConfigSource {
443
+ private readonly env;
444
+ /**
445
+ * Creates a new config source that reads from the environment.
446
+ *
447
+ * @param options - Options for the config source.
448
+ * @returns A new config source that reads from the environment.
449
+ */
450
+ static create(options: EnvConfigSourceOptions): ConfigSource;
451
+ private constructor();
452
+ readConfigData(): AsyncConfigSourceGenerator;
453
+ toString(): string;
454
+ }
455
+ /**
456
+ * Read runtime configuration from the environment.
457
+ *
458
+ * @remarks
459
+ *
460
+ * Only environment variables prefixed with APP_CONFIG_ will be considered.
461
+ *
462
+ * For each variable, the prefix will be removed, and rest of the key will
463
+ * be split by '_'. Each part will then be used as keys to build up a nested
464
+ * config object structure. The treatment of the entire environment variable
465
+ * is case-sensitive.
466
+ *
467
+ * The value of the variable should be JSON serialized, as it will be parsed
468
+ * and the type will be kept intact. For example "true" and true are treated
469
+ * differently, as well as "42" and 42.
470
+ *
471
+ * For example, to set the config app.title to "My Title", use the following:
472
+ *
473
+ * APP_CONFIG_app_title='"My Title"'
474
+ *
475
+ * @public
476
+ * @deprecated Use {@link EnvConfigSource} instead
477
+ */
478
+ declare function readEnvConfig(env: {
479
+ [name: string]: string | undefined;
480
+ }): AppConfig[];
481
+
482
+ /**
483
+ * Options for {@link FileConfigSource.create}.
484
+ *
485
+ * @public
486
+ */
487
+ interface FileConfigSourceOptions {
488
+ /**
489
+ * The path to the config file that should be loaded.
490
+ */
491
+ path: string;
492
+ /**
493
+ * A substitution function to use instead of the default environment substitution.
494
+ */
495
+ substitutionFunc?: SubstitutionFunc;
496
+ }
497
+ /**
498
+ * A config source that loads configuration from a local file.
499
+ *
500
+ * @public
501
+ */
502
+ declare class FileConfigSource implements ConfigSource {
503
+ #private;
504
+ /**
505
+ * Creates a new config source that loads configuration from the given path.
506
+ *
507
+ * @remarks
508
+ *
509
+ * The source will watch the file for changes, as well as any referenced files.
510
+ *
511
+ * @param options - Options for the config source.
512
+ * @returns A new config source that loads from the given path.
513
+ */
514
+ static create(options: FileConfigSourceOptions): ConfigSource;
515
+ private constructor();
516
+ readConfigData(options?: ReadConfigDataOptions): AsyncConfigSourceGenerator;
517
+ toString(): string;
518
+ }
519
+
520
+ /**
521
+ * Options for {@link MutableConfigSource.create}.
522
+ *
523
+ * @public
524
+ */
525
+ interface MutableConfigSourceOptions {
526
+ data?: JsonObject;
527
+ context?: string;
528
+ }
529
+ /**
530
+ * A config source that can be updated with new data.
531
+ *
532
+ * @public
533
+ */
534
+ declare class MutableConfigSource implements ConfigSource {
535
+ #private;
536
+ /**
537
+ * Creates a new mutable config source.
538
+ *
539
+ * @param options - Options for the config source.
540
+ * @returns A new mutable config source.
541
+ */
542
+ static create(options?: MutableConfigSourceOptions): MutableConfigSource;
543
+ private constructor();
544
+ readConfigData(options?: ReadConfigDataOptions | undefined): AsyncConfigSourceGenerator;
545
+ /**
546
+ * Set the data of the config source.
547
+ *
548
+ * @param data - The new data to set
549
+ */
550
+ setData(data: JsonObject): void;
551
+ /**
552
+ * Close the config source, preventing any further updates.
553
+ */
554
+ close(): void;
555
+ toString(): string;
556
+ }
557
+
558
+ /**
559
+ * Options for {@link StaticConfigSource.create}.
560
+ *
561
+ * @public
562
+ */
563
+ interface StaticConfigSourceOptions {
564
+ data: JsonObject | Observable<JsonObject> | PromiseLike<JsonObject> | AsyncIterable<JsonObject>;
565
+ context?: string;
566
+ }
567
+ /**
568
+ * A configuration source that reads from a static object, promise, iterable, or observable.
569
+ *
570
+ * @public
571
+ */
572
+ declare class StaticConfigSource implements ConfigSource {
573
+ private readonly promise;
574
+ private readonly context;
575
+ /**
576
+ * Creates a new {@link StaticConfigSource}.
577
+ *
578
+ * @param options - Options for the config source
579
+ * @returns A new static config source
580
+ */
581
+ static create(options: StaticConfigSourceOptions): ConfigSource;
582
+ private constructor();
583
+ readConfigData(): AsyncConfigSourceGenerator;
584
+ toString(): string;
585
+ }
586
+
587
+ export { AsyncConfigSourceGenerator, BaseConfigSourcesOptions, ClosableConfig, ConfigSchema, ConfigSchemaProcessingOptions, ConfigSource, ConfigSourceData, ConfigSourceTarget, ConfigSources, ConfigSourcesDefaultForTargetsOptions, ConfigSourcesDefaultOptions, ConfigTarget, ConfigVisibility, EnvConfigSource, EnvConfigSourceOptions, SubstitutionFunc as EnvFunc, FileConfigSource, FileConfigSourceOptions, LoadConfigOptions, LoadConfigOptionsRemote, LoadConfigOptionsWatch, LoadConfigResult, LoadConfigSchemaOptions, MutableConfigSource, MutableConfigSourceOptions, ReadConfigDataOptions, RemoteConfigSource, RemoteConfigSourceOptions, StaticConfigSource, StaticConfigSourceOptions, TransformFunc, loadConfig, loadConfigSchema, mergeConfigSchemas, readEnvConfig };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@backstage/config-loader",
3
3
  "description": "Config loading functionality used by Backstage backend, and CLI",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "publishConfig": {
6
6
  "access": "public",
7
7
  "main": "dist/index.cjs.js",
@@ -44,19 +44,23 @@
44
44
  "json-schema": "^0.4.0",
45
45
  "json-schema-merge-allof": "^0.8.1",
46
46
  "json-schema-traverse": "^1.0.0",
47
+ "lodash": "^4.17.21",
48
+ "minimist": "^1.2.5",
47
49
  "node-fetch": "^2.6.7",
48
50
  "typescript-json-schema": "^0.55.0",
49
51
  "yaml": "^2.0.0",
50
52
  "yup": "^0.32.9"
51
53
  },
52
54
  "devDependencies": {
53
- "@backstage/cli": "^0.22.6",
55
+ "@backstage/backend-test-utils": "^0.1.37",
56
+ "@backstage/cli": "^0.22.7",
54
57
  "@types/json-schema-merge-allof": "^0.6.0",
55
58
  "@types/mock-fs": "^4.10.0",
56
59
  "@types/node": "^16.11.26",
57
60
  "@types/yup": "^0.29.13",
58
61
  "mock-fs": "^5.1.0",
59
- "msw": "^1.0.0"
62
+ "msw": "^1.0.0",
63
+ "zen-observable": "^0.10.0"
60
64
  },
61
65
  "files": [
62
66
  "dist"