import { JSONSchema7 } from 'json-schema'; import { JsonObject, HumanDuration, Observable } from '@backstage/types'; import { AppConfig, Config } from '@backstage/config'; /** * A type representing the possible configuration value visibilities * * @public */ type ConfigVisibility = 'frontend' | 'backend' | 'secret'; /** * A function used to transform primitive configuration values. * * The "path" in the context is a JQ-style path to the current value from * within the original object passed to filterByVisibility(). * For example, "field.list[2]" would refer to: * \{ * field: [ * "foo", * "bar", * "baz" -- this one * ] * \} * * @public */ type TransformFunc = (value: T, context: { visibility: ConfigVisibility; path: string; }) => T | undefined; /** * Options used to process configuration data with a schema. * * @public */ type ConfigSchemaProcessingOptions = { /** * The visibilities that should be included in the output data. * If omitted, the data will not be filtered by visibility. */ visibility?: ConfigVisibility[]; /** * When set to `true`, any schema errors in the provided configuration will be ignored. */ ignoreSchemaErrors?: boolean; /** * A transform function that can be used to transform primitive configuration values * during validation. The value returned from the transform function will be used * instead of the original value. If the transform returns `undefined`, the value * will be omitted. */ valueTransform?: TransformFunc; /** * Whether or not to include the `filteredKeys` property in the output `AppConfig`s. * * Default: `false`. */ withFilteredKeys?: boolean; /** * Whether or not to include the `deprecatedKeys` property in the output `AppConfig`s. * * Default: `true`. */ withDeprecatedKeys?: boolean; }; /** * A loaded configuration schema that is ready to process configuration data. * * @public */ type ConfigSchema = { process(appConfigs: AppConfig[], options?: ConfigSchemaProcessingOptions): AppConfig[]; serialize(): JsonObject; }; /** * Given a list of configuration schemas from packages, merge them * into a single json schema. * * @public */ declare function mergeConfigSchemas(schemas: JSONSchema7[]): JSONSchema7; /** * An error encountered while loading a TypeScript configuration schema. * * @public */ declare class ConfigSchemaError extends Error { name: "ConfigSchemaError"; /** The package that provided the configuration schema. */ readonly source: string; /** The underlying error that caused schema loading to fail. */ readonly cause: Error; constructor(options: { source: string; cause: Error; }); } /** * Options that control the loading of configuration schema files in the backend. * * @public */ type LoadConfigSchemaOptions = (({ dependencies: string[]; packagePaths?: string[]; /** * Whether to exclude schemas from package dependencies. * * Defaults to `false`. */ excludePackageDependencies?: boolean; /** * Receives `ConfigSchemaError` instances and allows TypeScript schema * loading to continue. Unresolved types are treated as unconstrained * values, while schemas that cannot be generated are omitted. * * Without this callback, TypeScript configuration schema errors are * thrown. */ onSchemaError?: (error: ConfigSchemaError) => void; } | { serialized: JsonObject; }) & { noUndeclaredProperties?: boolean; }); /** * Loads config schema for a Backstage instance. * * @public */ declare function loadConfigSchema(options: LoadConfigSchemaOptions): Promise; /** * @public * @deprecated Use {@link ConfigSources.default} instead. */ type ConfigTarget = { path: string; } | { url: string; }; /** * @public * @deprecated Use {@link ConfigSources.default} instead. */ type LoadConfigOptionsWatch = { /** * A listener that is called when a config file is changed. */ onChange: (configs: AppConfig[]) => void; /** * An optional signal that stops the watcher once the promise resolves. */ stopSignal?: Promise; }; /** * @public * @deprecated Use {@link ConfigSources.default} instead. */ type LoadConfigOptionsRemote = { /** * A remote config reloading period, in seconds */ reloadIntervalSeconds: number; }; /** * Options that control the loading of configuration files in the backend. * * @public * @deprecated Use {@link ConfigSources.default} instead. */ type LoadConfigOptions = { configRoot: string; configTargets: ConfigTarget[]; /** * Custom environment variable loading function * * @experimental This API is not stable and may change at any point */ experimentalEnvFunc?: (name: string) => Promise; /** * An optional remote config */ remote?: LoadConfigOptionsRemote; /** * An optional configuration that enables watching of config files. */ watch?: LoadConfigOptionsWatch; }; /** * Results of loading configuration files. * @public * @deprecated Use {@link ConfigSources.default} instead. */ type LoadConfigResult = { /** * Array of all loaded configs. */ appConfigs: AppConfig[]; }; /** * Load configuration data. * * @public * @deprecated Use {@link ConfigSources.default} instead. */ declare function loadConfig(options: LoadConfigOptions): Promise; /** * The data returned by {@link ConfigSource.readConfigData}. * * @public */ interface ConfigSourceData extends AppConfig { /** * The file path that this configuration was loaded from, if it was loaded from a file. */ path?: string; } /** * Options for {@link ConfigSource.readConfigData}. * * @public */ interface ReadConfigDataOptions { signal?: AbortSignal; } /** * The generator returned by {@link ConfigSource.readConfigData}. * * @public */ type AsyncConfigSourceGenerator = AsyncGenerator<{ configs: ConfigSourceData[]; }, void, void>; /** * A source of configuration data. * * @remarks * * It is recommended to implement the `readConfigData` method as an async generator. * * @example * * ```ts * class MyConfigSource implements ConfigSource { * async *readConfigData() { * yield { * config: [{ * context: 'example', * data: { backend: { baseUrl: 'http://localhost' } } * }] * }; * } * } * ``` * * @public */ interface ConfigSource { readConfigData(options?: ReadConfigDataOptions): AsyncConfigSourceGenerator; } /** * A custom function to be used for substitution within configuration files. * * @remarks * * Substitutions use the following syntax: `baseUrl: https://${HOSTNAME}`, where * `'HOSTNAME'` is the name of the variable to be substituted. * * The default substitution function will read the value of the environment. * * @public */ type SubstitutionFunc = (name: string) => Promise; /** * A custom function to be used for parsing configuration content. * * @remarks * * The default parsing function will parse configuration content as yaml. * * @public */ type Parser = ({ contents, }: { contents: string; }) => Promise<{ result?: JsonObject; }>; /** * Options for {@link RemoteConfigSource.create}. * * @public */ interface RemoteConfigSourceOptions { /** * The URL to load the config from. */ url: string; /** * How often to reload the config from the remote URL, defaults to 1 minute. * * Set to Infinity to disable reloading, for example `{ days: Infinity }`. */ reloadInterval?: HumanDuration; /** * A substitution function to use instead of the default environment substitution. */ substitutionFunc?: SubstitutionFunc; /** * A content parsing function to transform string content to configuration values. */ parser?: Parser; } /** * A config source that loads configuration from a remote URL. * * @public */ declare class RemoteConfigSource implements ConfigSource { #private; /** * Creates a new {@link RemoteConfigSource}. * * @param options - Options for the source. * @returns A new remote config source. */ static create(options: RemoteConfigSourceOptions): ConfigSource; private constructor(); readConfigData(options?: ReadConfigDataOptions | undefined): AsyncConfigSourceGenerator; toString(): string; } /** * A target to read configuration from. * * @public */ type ConfigSourceTarget = { type: 'path'; target: string; } | { type: 'url'; target: string; }; /** * A config implementation that can be closed. * * @remarks * * Closing the configuration instance will stop the reading from the underlying source. * * @public */ interface ClosableConfig extends Config { /** * Closes the configuration instance. * * @remarks * * The configuration instance will still be usable after closing, but it will * no longer be updated with new values from the underlying source. */ close(): void; } /** * Common options for the default Backstage configuration sources. * * @public */ interface BaseConfigSourcesOptions { watch?: boolean; rootDir?: string; remote?: Pick; /** * Allow the default app-config.yaml to be missing, in which case the source * will not be created. */ allowMissingDefaultConfig?: boolean; /** * A custom substitution function that overrides the default one. * * @remarks * The substitution function handles syntax like `${MY_ENV_VAR}` in configuration values. * The default substitution will read the value from the environment and trim whitespace. */ substitutionFunc?: SubstitutionFunc; } /** * Options for {@link ConfigSources.defaultForTargets}. * * @public */ interface ConfigSourcesDefaultForTargetsOptions extends BaseConfigSourcesOptions { targets: ConfigSourceTarget[]; } /** * Options for {@link ConfigSources.default}. * * @public */ interface ConfigSourcesDefaultOptions extends BaseConfigSourcesOptions { argv?: string[]; env?: Record; } /** * A collection of utilities for working with and creating {@link ConfigSource}s. * * @public */ declare class ConfigSources { /** * Parses command line arguments and returns the config targets. * * @param argv - The command line arguments to parse. Defaults to `process.argv` * @returns A list of config targets */ static parseArgs(argv?: string[]): ConfigSourceTarget[]; /** * Creates the default config sources for the provided targets. * * @remarks * * This will create {@link FileConfigSource}s and {@link RemoteConfigSource}s * for the provided targets, and merge them together to a single source. * If no targets are provided it will fall back to `app-config.yaml` and * `app-config.local.yaml`. * * URL targets are only supported if the `remote` option is provided. * * @param options - Options * @returns A config source for the provided targets */ static defaultForTargets(options: ConfigSourcesDefaultForTargetsOptions): ConfigSource; /** * Creates the default config source for Backstage. * * @remarks * * This will read from `app-config.yaml` and `app-config.local.yaml` by * default, as well as environment variables prefixed with `APP_CONFIG_`. * If `--config ` command line arguments are passed, these will * override the default configuration file paths. URLs are only supported * if the `remote` option is provided. * * @param options - Options * @returns The default Backstage config source */ static default(options: ConfigSourcesDefaultOptions): ConfigSource; /** * Merges multiple config sources into a single source that reads from all * sources and concatenates the result. * * @param sources - The config sources to merge * @returns A single config source that concatenates the data from the given sources */ static merge(sources: ConfigSource[]): ConfigSource; /** * Creates an observable {@link @backstage/config#Config} implementation from a {@link ConfigSource}. * * @remarks * * If you only want to read the config once you can close the returned config immediately. * * @example * * ```ts * const sources = ConfigSources.default(...) * const config = await ConfigSources.toConfig(source) * config.close() * const example = config.getString(...) * ``` * * @param source - The config source to read from * @returns A promise that resolves to a closable config */ static toConfig(source: ConfigSource): Promise; } /** * Options for {@link EnvConfigSource.create}. * * @public */ interface EnvConfigSourceOptions { /** * The environment variables to use, defaults to `process.env`. */ env?: Record; } /** * A config source that reads configuration from the environment. * * @remarks * * Only environment variables prefixed with APP_CONFIG_ will be considered. * * For each variable, the prefix will be removed, and rest of the key will * be split by '_'. Each part will then be used as keys to build up a nested * config object structure. The treatment of the entire environment variable * is case-sensitive. * * The value of the variable should be JSON serialized, as it will be parsed * and the type will be kept intact. For example "true" and true are treated * differently, as well as "42" and 42. * * For example, to set the config app.title to "My Title", use the following: * * APP_CONFIG_app_title='"My Title"' * * @public */ declare class EnvConfigSource implements ConfigSource { private readonly env; /** * Creates a new config source that reads from the environment. * * @param options - Options for the config source. * @returns A new config source that reads from the environment. */ static create(options: EnvConfigSourceOptions): ConfigSource; private constructor(); readConfigData(): AsyncConfigSourceGenerator; toString(): string; } /** * Read runtime configuration from the environment. * * @remarks * * Only environment variables prefixed with APP_CONFIG_ will be considered. * * For each variable, the prefix will be removed, and rest of the key will * be split by '_'. Each part will then be used as keys to build up a nested * config object structure. The treatment of the entire environment variable * is case-sensitive. * * The value of the variable should be JSON serialized, as it will be parsed * and the type will be kept intact. For example "true" and true are treated * differently, as well as "42" and 42. * * For example, to set the config app.title to "My Title", use the following: * * APP_CONFIG_app_title='"My Title"' * * @public * @deprecated Use {@link EnvConfigSource} instead */ declare function readEnvConfig(env: { [name: string]: string | undefined; }): AppConfig[]; /** * Options for {@link FileConfigSource.create}. * * @public */ interface FileConfigSourceOptions { /** * The path to the config file that should be loaded. */ path: string; /** * Set to `false` to disable file watching, defaults to `true`. */ watch?: boolean; /** * A substitution function to use instead of the default environment substitution. */ substitutionFunc?: SubstitutionFunc; /** * A content parsing function to transform string content to configuration values. */ parser?: Parser; } /** * A config source that loads configuration from a local file. * * @public */ declare class FileConfigSource implements ConfigSource { #private; /** * Creates a new config source that loads configuration from the given path. * * @remarks * * The source will watch the file for changes, as well as any referenced files. * * @param options - Options for the config source. * @returns A new config source that loads from the given path. */ static create(options: FileConfigSourceOptions): ConfigSource; private constructor(); readConfigData(options?: ReadConfigDataOptions): AsyncConfigSourceGenerator; toString(): string; } /** * Options for {@link MutableConfigSource.create}. * * @public */ interface MutableConfigSourceOptions { data?: JsonObject; context?: string; } /** * A config source that can be updated with new data. * * @public */ declare class MutableConfigSource implements ConfigSource { #private; /** * Creates a new mutable config source. * * @param options - Options for the config source. * @returns A new mutable config source. */ static create(options?: MutableConfigSourceOptions): MutableConfigSource; private constructor(); readConfigData(options?: ReadConfigDataOptions | undefined): AsyncConfigSourceGenerator; /** * Set the data of the config source. * * @param data - The new data to set */ setData(data: JsonObject): void; /** * Close the config source, preventing any further updates. */ close(): void; toString(): string; } /** * Options for {@link StaticConfigSource.create}. * * @public */ interface StaticConfigSourceOptions { data: JsonObject | Observable | PromiseLike | AsyncIterable; context?: string; } /** * A configuration source that reads from a static object, promise, iterable, or observable. * * @public */ declare class StaticConfigSource implements ConfigSource { /** * Creates a new {@link StaticConfigSource}. * * @param options - Options for the config source * @returns A new static config source */ static create(options: StaticConfigSourceOptions): ConfigSource; private readonly promise; private readonly context; private constructor(); readConfigData(): AsyncConfigSourceGenerator; toString(): string; } export { ConfigSchemaError, ConfigSources, EnvConfigSource, FileConfigSource, MutableConfigSource, RemoteConfigSource, StaticConfigSource, loadConfig, loadConfigSchema, mergeConfigSchemas, readEnvConfig }; export type { AsyncConfigSourceGenerator, BaseConfigSourcesOptions, ClosableConfig, ConfigSchema, ConfigSchemaProcessingOptions, ConfigSource, ConfigSourceData, ConfigSourceTarget, ConfigSourcesDefaultForTargetsOptions, ConfigSourcesDefaultOptions, ConfigTarget, ConfigVisibility, EnvConfigSourceOptions, SubstitutionFunc as EnvFunc, FileConfigSourceOptions, LoadConfigOptions, LoadConfigOptionsRemote, LoadConfigOptionsWatch, LoadConfigResult, LoadConfigSchemaOptions, MutableConfigSourceOptions, Parser, ReadConfigDataOptions, RemoteConfigSourceOptions, StaticConfigSourceOptions, TransformFunc };