{"version":3,"file":"config-store.js","sources":["../../../../packages/config-store/src/configuration.ts","../../../../packages/config-store/src/exceptions.ts","../../../../packages/config-store/src/providers/cachingConfigurationProvider.ts","../../../../packages/config-store/src/providers/spListConfigurationProvider.ts"],"sourcesContent":["import { TypedHash, Dictionary } from \"@pnp/common\";\r\n\r\n/**\r\n * Interface for configuration providers\r\n *\r\n */\r\nexport interface IConfigurationProvider {\r\n\r\n /**\r\n * Gets the configuration from the provider\r\n */\r\n getConfiguration(): Promise>;\r\n}\r\n\r\n/**\r\n * Class used to manage the current application settings\r\n *\r\n */\r\nexport class Settings {\r\n\r\n /**\r\n * The settings currently stored in this instance\r\n */\r\n private _settings: Dictionary;\r\n\r\n /**\r\n * Creates a new instance of the settings class\r\n *\r\n * @constructor\r\n */\r\n constructor() {\r\n this._settings = new Dictionary();\r\n }\r\n\r\n /**\r\n * Adds a new single setting, or overwrites a previous setting with the same key\r\n *\r\n * @param {string} key The key used to store this setting\r\n * @param {string} value The setting value to store\r\n */\r\n public add(key: string, value: string) {\r\n this._settings.add(key, value);\r\n }\r\n\r\n /**\r\n * Adds a JSON value to the collection as a string, you must use getJSON to rehydrate the object when read\r\n *\r\n * @param {string} key The key used to store this setting\r\n * @param {any} value The setting value to store\r\n */\r\n public addJSON(key: string, value: any) {\r\n this._settings.add(key, JSON.stringify(value));\r\n }\r\n\r\n /**\r\n * Applies the supplied hash to the setting collection overwriting any existing value, or created new values\r\n *\r\n * @param {TypedHash} hash The set of values to add\r\n */\r\n public apply(hash: TypedHash): Promise {\r\n return new Promise((resolve, reject) => {\r\n try {\r\n this._settings.merge(hash);\r\n resolve();\r\n } catch (e) {\r\n reject(e);\r\n }\r\n });\r\n }\r\n\r\n /**\r\n * Loads configuration settings into the collection from the supplied provider and returns a Promise\r\n *\r\n * @param {IConfigurationProvider} provider The provider from which we will load the settings\r\n */\r\n public load(provider: IConfigurationProvider): Promise {\r\n return new Promise((resolve, reject) => {\r\n provider.getConfiguration().then((value) => {\r\n this._settings.merge(value);\r\n resolve();\r\n }).catch((reason) => {\r\n reject(reason);\r\n });\r\n });\r\n }\r\n\r\n /**\r\n * Gets a value from the configuration\r\n *\r\n * @param {string} key The key whose value we want to return. Returns null if the key does not exist\r\n * @return {string} string value from the configuration\r\n */\r\n public get(key: string): string | null {\r\n return this._settings.get(key);\r\n }\r\n\r\n /**\r\n * Gets a JSON value, rehydrating the stored string to the original object\r\n *\r\n * @param {string} key The key whose value we want to return. Returns null if the key does not exist\r\n * @return {any} object from the configuration\r\n */\r\n public getJSON(key: string): any {\r\n const o = this.get(key);\r\n if (typeof o === \"undefined\" || o === null) {\r\n return o;\r\n }\r\n\r\n return JSON.parse(o);\r\n }\r\n}\r\n","import { Logger, LogLevel } from \"@pnp/logging\";\r\n\r\nexport class NoCacheAvailableException extends Error {\r\n\r\n constructor(msg = \"Cannot create a caching configuration provider since cache is not available.\") {\r\n super(msg);\r\n this.name = \"NoCacheAvailableException\";\r\n Logger.log({ data: {}, level: LogLevel.Error, message: `[${this.name}]::${this.message}` });\r\n }\r\n}\r\n","import { IConfigurationProvider } from \"../configuration\";\r\nimport { TypedHash, PnPClientStore, PnPClientStorage } from \"@pnp/common\";\r\nimport { NoCacheAvailableException } from \"../exceptions\";\r\n\r\n/**\r\n * A caching provider which can wrap other non-caching providers\r\n *\r\n */\r\nexport default class CachingConfigurationProvider implements IConfigurationProvider {\r\n private wrappedProvider: IConfigurationProvider;\r\n private store: PnPClientStore;\r\n private cacheKey: string;\r\n\r\n /**\r\n * Creates a new caching configuration provider\r\n * @constructor\r\n * @param {IConfigurationProvider} wrappedProvider Provider which will be used to fetch the configuration\r\n * @param {string} cacheKey Key that will be used to store cached items to the cache\r\n * @param {IPnPClientStore} cacheStore OPTIONAL storage, which will be used to store cached settings.\r\n */\r\n constructor(wrappedProvider: IConfigurationProvider, cacheKey: string, cacheStore?: PnPClientStore) {\r\n this.wrappedProvider = wrappedProvider;\r\n this.store = (cacheStore) ? cacheStore : this.selectPnPCache();\r\n this.cacheKey = `_configcache_${cacheKey}`;\r\n }\r\n\r\n /**\r\n * Gets the wrapped configuration providers\r\n *\r\n * @return {IConfigurationProvider} Wrapped configuration provider\r\n */\r\n public getWrappedProvider(): IConfigurationProvider {\r\n return this.wrappedProvider;\r\n }\r\n\r\n /**\r\n * Loads the configuration values either from the cache or from the wrapped provider\r\n *\r\n * @return {Promise>} Promise of loaded configuration values\r\n */\r\n public getConfiguration(): Promise> {\r\n // Cache not available, pass control to the wrapped provider\r\n if ((!this.store) || (!this.store.enabled)) {\r\n return this.wrappedProvider.getConfiguration();\r\n }\r\n\r\n // Value is found in cache, return it directly\r\n const cachedConfig = this.store.get(this.cacheKey);\r\n if (cachedConfig) {\r\n return new Promise>((resolve) => {\r\n resolve(cachedConfig);\r\n });\r\n }\r\n\r\n // Get and cache value from the wrapped provider\r\n const providerPromise = this.wrappedProvider.getConfiguration();\r\n providerPromise.then((providedConfig) => {\r\n this.store.put(this.cacheKey, providedConfig);\r\n });\r\n return providerPromise;\r\n }\r\n\r\n private selectPnPCache(): PnPClientStore {\r\n const pnpCache = new PnPClientStorage();\r\n if ((pnpCache.local) && (pnpCache.local.enabled)) {\r\n return pnpCache.local;\r\n }\r\n if ((pnpCache.session) && (pnpCache.session.enabled)) {\r\n return pnpCache.session;\r\n }\r\n throw new NoCacheAvailableException();\r\n }\r\n}\r\n","import { IConfigurationProvider } from \"../configuration\";\r\nimport { TypedHash } from \"@pnp/common\";\r\nimport { default as CachingConfigurationProvider } from \"./cachingConfigurationProvider\";\r\nimport { Web } from \"@pnp/sp\";\r\n\r\n/**\r\n * A configuration provider which loads configuration values from a SharePoint list\r\n *\r\n */\r\nexport default class SPListConfigurationProvider implements IConfigurationProvider {\r\n /**\r\n * Creates a new SharePoint list based configuration provider\r\n * @constructor\r\n * @param {string} webUrl Url of the SharePoint site, where the configuration list is located\r\n * @param {string} listTitle Title of the SharePoint list, which contains the configuration settings (optional, default = \"config\")\r\n */\r\n constructor(private sourceWeb: Web, private sourceListTitle = \"config\") {\r\n }\r\n\r\n /**\r\n * Gets the url of the SharePoint site, where the configuration list is located\r\n *\r\n * @return {string} Url address of the site\r\n */\r\n public get web(): Web {\r\n return this.sourceWeb;\r\n }\r\n\r\n /**\r\n * Gets the title of the SharePoint list, which contains the configuration settings\r\n *\r\n * @return {string} List title\r\n */\r\n public get listTitle(): string {\r\n return this.sourceListTitle;\r\n }\r\n\r\n /**\r\n * Loads the configuration values from the SharePoint list\r\n *\r\n * @return {Promise>} Promise of loaded configuration values\r\n */\r\n public getConfiguration(): Promise> {\r\n\r\n return this.web.lists.getByTitle(this.listTitle).items.select(\"Title\", \"Value\")\r\n .getAs<{ Title: string, Value: string }[]>().then((data) => {\r\n return data.reduce((configuration, item) => {\r\n\r\n return Object.defineProperty(configuration, item.Title, {\r\n configurable: false,\r\n enumerable: false,\r\n value: item.Value,\r\n writable: false,\r\n });\r\n\r\n }, {});\r\n });\r\n }\r\n\r\n /**\r\n * Wraps the current provider in a cache enabled provider\r\n *\r\n * @return {CachingConfigurationProvider} Caching providers which wraps the current provider\r\n */\r\n public asCaching(): CachingConfigurationProvider {\r\n const cacheKey = `splist_${this.web.toUrl()}+${this.listTitle}`;\r\n return new CachingConfigurationProvider(this, cacheKey);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;AAcA;;;;AAIA;;;;;;IAYI;QACI,IAAI,CAAC,SAAS,GAAG,IAAI,UAAU,EAAU,CAAC;KAC7C;;;;;;;IAQM,GAAG,CAAC,GAAW,EAAE,KAAa;QACjC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;KAClC;;;;;;;IAQM,OAAO,CAAC,GAAW,EAAE,KAAU;QAClC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;KAClD;;;;;;IAOM,KAAK,CAAC,IAAoB;QAC7B,OAAO,IAAI,OAAO,CAAO,CAAC,O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