export type EnvironmentUtils = import('./environmentUtils').EnvironmentUtils; export type RootRequireFn = import('./rootRequire').RootRequireFn; export type ProviderCreator = import('../shared/jimpleFns').ProviderCreator; export type AppConfigurationOptions = { /** * The name of the default configuration. */ defaultConfigurationName?: string; /** * The name of the variable it will read in order to determine which configuration to * load. */ environmentVariable?: string; /** * The path to the configurations directory, relative to the project root path. */ path?: string; /** * The name format of the configuration files. You need to use the `[name]` * placeholder so the service can replace it with the name of the configuration. */ filenameFormat?: string; }; export type AppConfigurationServiceMap = { /** * The name of the service for {@link EnvironmentUtils } or an instance of it. * `environmentUtils` by default. */ environmentUtils?: string | EnvironmentUtils; /** * The name of the service for {@link RootRequireFn } or an instance of it. `rootRequire` * by default. */ rootRequire?: string | RootRequireFn; }; export type AppConfigurationProviderOptions = { /** * The name that will be used to register an instance of {@link AppConfiguration }. Its * default value is `appConfiguration`. */ serviceName: string; /** * The name of the application. */ appName: string; /** * The service default configuration. */ defaultConfiguration: any; /** * Overwrites for the service customization options. */ options: Partial; /** * A dictionary with the services that need to be injected on the class. */ services: AppConfigurationServiceMap; }; /** * @module node/appConfiguration */ /** * @typedef {import('./environmentUtils').EnvironmentUtils} EnvironmentUtils * @typedef {import('./rootRequire').RootRequireFn} RootRequireFn */ /** * @typedef {import('../shared/jimpleFns').ProviderCreator} ProviderCreator * @template O */ /** * @typedef {Object} AppConfigurationOptions * @property {string} [defaultConfigurationName='default'] * The name of the default configuration. * @property {string} [environmentVariable='APP_CONFIG'] * The name of the variable it will read in order to determine which configuration to * load. * @property {string} [path='./config/[app-name]'] * The path to the configurations directory, relative to the project root path. * @property {string} [filenameFormat='[app-name].[name].config.js'] * The name format of the configuration files. You need to use the `[name]` * placeholder so the service can replace it with the name of the configuration. * @parent module:node/appConfiguration */ /** * @typedef {Object} AppConfigurationServiceMap * @property {string | EnvironmentUtils} [environmentUtils] * The name of the service for {@link EnvironmentUtils} or an instance of it. * `environmentUtils` by default. * @property {string | RootRequireFn} [rootRequire] * The name of the service for {@link RootRequireFn} or an instance of it. `rootRequire` * by default. * @parent module:node/appConfiguration */ /** * @typedef {Object} AppConfigurationProviderOptions * @property {string} serviceName * The name that will be used to register an instance of {@link AppConfiguration}. Its * default value is `appConfiguration`. * @property {string} appName * The name of the application. * @property {Object} defaultConfiguration * The service default configuration. * @property {Partial} options * Overwrites for the service customization options. * @property {AppConfigurationServiceMap} services * A dictionary with the services that need to be injected on the class. * @parent module:node/appConfiguration */ /** * This is a service to manage applications configurations. It takes care of loading, * activating, * switching and merging configuration files. * * @parent module:node/appConfiguration * @tutorial appConfiguration */ export class AppConfiguration { /** * @param {EnvironmentUtils} environmentUtils * Required to read the environment variables and determine which configuration to use. * @param {RootRequireFn} rootRequire * Necessary to be able to require the configuration files with paths relative to the * app root directory. * @param {string} [appName='app'] * The name of the app using this service. It's also used as part of the name of the * configuration files. * @param {Object} [defaultConfiguration={}] * The default configuration the others will extend. * @param {Partial} [options={}] * Options to customize the service. */ constructor(environmentUtils: EnvironmentUtils, rootRequire: RootRequireFn, appName?: string, defaultConfiguration?: any, options?: Partial); /** * A local reference for the `environmentUtils` service. * * @type {EnvironmentUtils} * @access protected * @ignore */ _environmentUtils: EnvironmentUtils; /** * The function that allows the service to `require` a configuration file with a path * relative to the app root directory. * * @type {RootRequireFn} * @access protected * @ignore */ _rootRequire: RootRequireFn; /** * The service customizable options. * * @type {AppConfigurationOptions} * @access protected * @ignore */ _options: AppConfigurationOptions; /** * A dictionary with all the loaded configurations. It uses the names of the * configurations as keys. * * @type {Object.} * @access protected * @ignore */ _configurations: { [x: string]: any; }; /** * The name of the active configuration. * * @type {string} * @access protected * @ignore */ _activeConfiguration: string; /** * Whether or not the configuration can be switched. * * @type {boolean} * @access protected * @ignore */ _allowConfigurationSwitch: boolean; /** * Gets a setting or settings from the active configuration. * * @param {string | string[]} setting A setting path or a list of them. * @param {boolean} [asArray=false] When `setting` is an Array, if this is * `true`, * instead of returning an object, it will * return an array of settings. * @returns {*} * @example * * // To get a single setting * const value = appConfiguration.get('some-setting'); * * // To get multiple values * const { settingOne, settingTwo } = appConfiguration.get([ * 'settingOne', * 'settingTwo', * ]); * * // Use paths * const subValue = appConfiguration.get('settingOne.subSetting'); * */ get(setting: string | string[], asArray?: boolean): any; /** * Gets a configuration settings. If no name is specified, it will return the settings * of the active configuration. * * @param {string} [name=''] The name of the configuration. * @returns {?Object} */ getConfig(name?: string): any | null; /** * Load a new configuration. * * @param {string} name The configuration name. * @param {Object} settings The configuration settings. * @param {boolean} [switchTo=true] If the service should switch to the new * configuration after adding it. * @returns {Object} The settings of the new configuration. * @throws {Error} If the configuration tries to extend a configuration that doesn't * exist. */ load(name: string, settings: any, switchTo?: boolean): any; /** * Checks if there's a configuration name on the environment variable and if there is, * try to load the configuration file for it. * * @returns {Object} The loaded configuration or an empty object if the variable was * empty. */ loadFromEnvironment(): any; /** * Loads a configuration from a file. * * @param {string} name The name of the configuration. * @param {boolean} [switchTo=true] If the service should switch to the new * configuration after adding it. * @param {boolean} [checkSwitchFlag=true] If `true`, the service will update the value * of `allowConfigurationSwitch` based on the * loaded configuration setting. * @returns {Object} The settings of the loaded configuration. * @throws {Error} If the configuration file can't be loaded. */ loadFromFile(name: string, switchTo?: boolean, checkSwitchFlag?: boolean): any; /** * Sets the value of a setting or settings from the active configuration. * If both the current and the new value of a setting are objects, then instead of * overwriting it, the method will merge them. * * @param {string | Object.} setting The name of the setting to update or * a dictionary of settings and their * values. * @param {*} [value] The value of the setting. This is * only used when `setting` is a string. * @throws {Error} If `setting` is not a dictionary and `value` is undefined. * @example * * // To set a single setting value * appConfiguration.set('some-setting', 'some-setting-value'); * // To set the value of multiple settings * appConfiguration.set({ * settingOne: 'valueOne', * settingTwo: 'valueTwo', * }); * */ set(setting: string | { [x: string]: any; }, value?: any): void; /** * Overwrites all the settings for a configuration. If the name is not specified, it * will overwrite the active configuration. * * @param {Object} config The new configuration settings. * @param {string} [name=''] The name of the configuration. * @param {boolean} [merge=true] Whether or not to merge the new settings with the * existing ones. * @returns {Object} The updated configuration. */ setConfig(config: any, name?: string, merge?: boolean): any; /** * Switchs to a different configuration. If the configuration is not registered, it will * try to load from a file. * * @param {string} name The new of the configuration to switch to. * @param {boolean} [force=false] A way to force the service to switch even if the * `allowConfigurationSwitch` property if `false`. * @returns {Object} The new active configuration. * @throws {Error} If `force` is `false` and the `allowConfigurationSwitch` property * is `false`. */ switch(name: string, force?: boolean): any; /** * The name of the active configuration. * * @type {string} */ get activeConfiguration(): string; /** * Whether or not the active configuration can be switched. * * @type {boolean} */ get canSwitch(): boolean; /** * A dictionary with all the loaded configurations. It uses the names of the * configurations as keys. * * @type {Object.} */ get configurations(): { [x: string]: any; }; /** * The service customizable options. * * @type {AppConfigurationOptions} */ get options(): AppConfigurationOptions; /** * Add a new configuration to the service. * * @param {string} name The name of the new configuration. * @param {Object} settings The configuration settings. * @param {boolean} checkSwitchFlag Whether or not the `allowConfigurationSwitch` * should be updated with the value of this new * configuration setting. * @param {boolean} switchTo Whether or not to switch it to the active * configuration after adding it. * @access protected * @ignore */ _addConfiguration(name: string, settings: any, checkSwitchFlag: boolean, switchTo: boolean): void; } /** * The service provider to register an instance of {@link AppConfiguration} on the * container. * * @type {ProviderCreator} * @tutorial appConfiguration */ export const appConfiguration: ProviderCreator;