{"version":3,"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"],"names":["Settings","this","_settings","Dictionary","prototype","add","key","value","addJSON","JSON","stringify","apply","hash","_this","Promise","resolve","reject","merge","e","load","provider","getConfiguration","then","catch","reason","get","getJSON","o","parse","NoCacheAvailableException","_super","msg","call","name","Logger","log","data","level","message","tslib_1.__extends","Error","CachingConfigurationProvider","wrappedProvider","cacheKey","cacheStore","store","selectPnPCache","getWrappedProvider","enabled","cachedConfig","providerPromise","providedConfig","put","pnpCache","PnPClientStorage","local","session","SPListConfigurationProvider","sourceWeb","sourceListTitle","keyFieldName","valueFieldName","Object","defineProperty","web","lists","getByTitle","listTitle","items","select","reduce","c","item","configurable","enumerable","writable","asCaching","toUrl"],"mappings":"wWAkBA,IAAAA,EAAA,WAYI,SAAAA,IACIC,KAAKC,UAAY,IAAIC,EAAAA,WA+E7B,OAtEWH,EAAAI,UAAAC,IAAP,SAAWC,EAAaC,GACpBN,KAAKC,UAAUG,IAAIC,EAAKC,IASrBP,EAAAI,UAAAI,QAAP,SAAeF,EAAaC,GACxBN,KAAKC,UAAUG,IAAIC,EAAKG,KAAKC,UAAUH,KAQpCP,EAAAI,UAAAO,MAAP,SAAaC,GAAb,IAAAC,EAAAZ,KACI,OAAO,IAAIa,QAAc,SAACC,EAASC,GAC/B,IACIH,EAAKX,UAAUe,MAAML,GACrBG,IACF,MAAOG,GACLF,EAAOE,OAUZlB,EAAAI,UAAAe,KAAP,SAAYC,GAAZ,IAAAP,EAAAZ,KACI,OAAO,IAAIa,QAAc,SAACC,EAASC,GAC/BI,EAASC,mBAAmBC,KAAK,SAACf,GAC9BM,EAAKX,UAAUe,MAAMV,GACrBQ,MACDQ,MAAM,SAACC,GACNR,EAAOQ,QAWZxB,EAAAI,UAAAqB,IAAP,SAAWnB,GACP,OAAOL,KAAKC,UAAUu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,QAAe,QAC/C,OAAOH,EAASG,QAEpB,MAAM,IAAI3B,GAElBY,EAhEA,GCCAgB,EAAA,WASI,SAAAA,EAAoBC,EAAwBC,EAAoCC,EAAgCC,QAApE,IAAAF,IAAAA,EAAA,eAAoC,IAAAC,IAAAA,EAAA,cAAgC,IAAAC,IAAAA,EAAA,SAA5F5D,KAAAyD,UAAAA,EAAwBzD,KAAA0D,gBAAAA,EAAoC1D,KAAA2D,aAAAA,EAAgC3D,KAAA4D,eAAAA,EAiDpH,OAzCIC,OAAAC,eAAWN,EAAArD,UAAA,WAAX,WACI,OAAOH,KAAKyD,2CAQhBI,OAAAC,eAAWN,EAAArD,UAAA,iBAAX,WACI,OAAOH,KAAK0D,iDAQTF,EAAArD,UAAAiB,iBAAP,WAAA,IAAAR,EAAAZ,KAEI,OAAOA,KAAK+D,IAAIC,MAAMC,WAAWjE,KAAKkE,WAAWC,MAAMC,OAAOpE,KAAK2D,aAAc3D,KAAK4D,gBACjFpC,MAAaH,KAAK,SAACc,GAAS,OAAAA,EAAKkC,OAAO,SAACC,EAAQC,GAE9C,OAAOV,OAAOC,eAAeQ,EAAGC,EAAK3D,EAAK+C,eACtCa,cAAc,EACdC,YAAY,EACZnE,MAAOiE,EAAK3D,EAAKgD,gBACjBc,UAAU,YAUnBlB,EAAArD,UAAAwE,UAAP,WACI,IAAMjC,EAAW,UAAU1C,KAAK+D,IAAIa,QAAO,IAAI5E,KAAKkE,UACpD,OAAO,IAAI1B,EAA6BxC,KAAM0C,IAEtDc,EA1DA","sourcesContent":["import { TypedHash, Dictionary } from \"@pnp/common\";\n\n/**\n * Interface for configuration providers\n *\n */\nexport interface IConfigurationProvider {\n\n /**\n * Gets the configuration from the provider\n */\n getConfiguration(): Promise>;\n}\n\n/**\n * Class used to manage the current application settings\n *\n */\nexport class Settings {\n\n /**\n * The settings currently stored in this instance\n */\n private _settings: Dictionary;\n\n /**\n * Creates a new instance of the settings class\n *\n * @constructor\n */\n constructor() {\n this._settings = new Dictionary();\n }\n\n /**\n * Adds a new single setting, or overwrites a previous setting with the same key\n *\n * @param {string} key The key used to store this setting\n * @param {string} value The setting value to store\n */\n public add(key: string, value: string) {\n this._settings.add(key, value);\n }\n\n /**\n * Adds a JSON value to the collection as a string, you must use getJSON to rehydrate the object when read\n *\n * @param {string} key The key used to store this setting\n * @param {any} value The setting value to store\n */\n public addJSON(key: string, value: any) {\n this._settings.add(key, JSON.stringify(value));\n }\n\n /**\n * Applies the supplied hash to the setting collection overwriting any existing value, or created new values\n *\n * @param {TypedHash} hash The set of values to add\n */\n public apply(hash: TypedHash): Promise {\n return new Promise((resolve, reject) => {\n try {\n this._settings.merge(hash);\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n }\n\n /**\n * Loads configuration settings into the collection from the supplied provider and returns a Promise\n *\n * @param {IConfigurationProvider} provider The provider from which we will load the settings\n */\n public load(provider: IConfigurationProvider): Promise {\n return new Promise((resolve, reject) => {\n provider.getConfiguration().then((value) => {\n this._settings.merge(value);\n resolve();\n }).catch((reason) => {\n reject(reason);\n });\n });\n }\n\n /**\n * Gets a value from the configuration\n *\n * @param {string} key The key whose value we want to return. Returns null if the key does not exist\n * @return {string} string value from the configuration\n */\n public get(key: string): string | null {\n return this._settings.get(key);\n }\n\n /**\n * Gets a JSON value, rehydrating the stored string to the original object\n *\n * @param {string} key The key whose value we want to return. Returns null if the key does not exist\n * @return {any} object from the configuration\n */\n public getJSON(key: string): any {\n const o = this.get(key);\n if (typeof o === \"undefined\" || o === null) {\n return o;\n }\n\n return JSON.parse(o);\n }\n}\n","import { Logger, LogLevel } from \"@pnp/logging\";\n\nexport class NoCacheAvailableException extends Error {\n\n constructor(msg = \"Cannot create a caching configuration provider since cache is not available.\") {\n super(msg);\n this.name = \"NoCacheAvailableException\";\n Logger.log({ data: {}, level: LogLevel.Error, message: `[${this.name}]::${this.message}` });\n }\n}\n","import { IConfigurationProvider } from \"../configuration\";\nimport { TypedHash, PnPClientStore, PnPClientStorage } from \"@pnp/common\";\nimport { NoCacheAvailableException } from \"../exceptions\";\n\n/**\n * A caching provider which can wrap other non-caching providers\n *\n */\nexport default class CachingConfigurationProvider implements IConfigurationProvider {\n private wrappedProvider: IConfigurationProvider;\n private store: PnPClientStore;\n private cacheKey: string;\n\n /**\n * Creates a new caching configuration provider\n * @constructor\n * @param {IConfigurationProvider} wrappedProvider Provider which will be used to fetch the configuration\n * @param {string} cacheKey Key that will be used to store cached items to the cache\n * @param {IPnPClientStore} cacheStore OPTIONAL storage, which will be used to store cached settings.\n */\n constructor(wrappedProvider: IConfigurationProvider, cacheKey: string, cacheStore?: PnPClientStore) {\n this.wrappedProvider = wrappedProvider;\n this.store = (cacheStore) ? cacheStore : this.selectPnPCache();\n this.cacheKey = `_configcache_${cacheKey}`;\n }\n\n /**\n * Gets the wrapped configuration providers\n *\n * @return {IConfigurationProvider} Wrapped configuration provider\n */\n public getWrappedProvider(): IConfigurationProvider {\n return this.wrappedProvider;\n }\n\n /**\n * Loads the configuration values either from the cache or from the wrapped provider\n *\n * @return {Promise>} Promise of loaded configuration values\n */\n public getConfiguration(): Promise> {\n // Cache not available, pass control to the wrapped provider\n if ((!this.store) || (!this.store.enabled)) {\n return this.wrappedProvider.getConfiguration();\n }\n\n // Value is found in cache, return it directly\n const cachedConfig = this.store.get(this.cacheKey);\n if (cachedConfig) {\n return new Promise>((resolve) => {\n resolve(cachedConfig);\n });\n }\n\n // Get and cache value from the wrapped provider\n const providerPromise = this.wrappedProvider.getConfiguration();\n providerPromise.then((providedConfig) => {\n this.store.put(this.cacheKey, providedConfig);\n });\n return providerPromise;\n }\n\n private selectPnPCache(): PnPClientStore {\n const pnpCache = new PnPClientStorage();\n if ((pnpCache.local) && (pnpCache.local.enabled)) {\n return pnpCache.local;\n }\n if ((pnpCache.session) && (pnpCache.session.enabled)) {\n return pnpCache.session;\n }\n throw new NoCacheAvailableException();\n }\n}\n","import { IConfigurationProvider } from \"../configuration\";\nimport { TypedHash } from \"@pnp/common\";\nimport { default as CachingConfigurationProvider } from \"./cachingConfigurationProvider\";\nimport { Web } from \"@pnp/sp\";\n\n/**\n * A configuration provider which loads configuration values from a SharePoint list\n *\n */\nexport default class SPListConfigurationProvider implements IConfigurationProvider {\n /**\n * Creates a new SharePoint list based configuration provider\n * @constructor\n * @param {string} webUrl Url of the SharePoint site, where the configuration list is located\n * @param {string} listTitle Title of the SharePoint list, which contains the configuration settings (optional, default: \"config\")\n * @param {string} keyFieldName The name of the field in the list to use as the setting key (optional, default: \"Title\")\n * @param {string} valueFieldName The name of the field in the list to use as the setting value (optional, default: \"Value\")\n */\n constructor(private sourceWeb: Web, private sourceListTitle = \"config\", private keyFieldName = \"Title\", private valueFieldName = \"Value\") {\n }\n\n /**\n * Gets the url of the SharePoint site, where the configuration list is located\n *\n * @return {string} Url address of the site\n */\n public get web(): Web {\n return this.sourceWeb;\n }\n\n /**\n * Gets the title of the SharePoint list, which contains the configuration settings\n *\n * @return {string} List title\n */\n public get listTitle(): string {\n return this.sourceListTitle;\n }\n\n /**\n * Loads the configuration values from the SharePoint list\n *\n * @return {Promise>} Promise of loaded configuration values\n */\n public getConfiguration(): Promise> {\n\n return this.web.lists.getByTitle(this.listTitle).items.select(this.keyFieldName, this.valueFieldName)\n .get().then((data) => data.reduce((c: any, item: any) => {\n\n return Object.defineProperty(c, item[this.keyFieldName], {\n configurable: false,\n enumerable: false,\n value: item[this.valueFieldName],\n writable: false,\n });\n }, {}));\n }\n\n /**\n * Wraps the current provider in a cache enabled provider\n *\n * @return {CachingConfigurationProvider} Caching providers which wraps the current provider\n */\n public asCaching(): CachingConfigurationProvider {\n const cacheKey = `splist_${this.web.toUrl()}+${this.listTitle}`;\n return new CachingConfigurationProvider(this, cacheKey);\n }\n}\n"]}