@telia-ace/widget-services 1.0.4 → 1.0.5

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.
@@ -1,264 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- Object.defineProperty(exports, "__esModule", { value: true });
30
- exports.setApplicationStoreAsync = exports.getApplicationStoreAsync = exports.setCookieStoreAsync = exports.getCookieStoreAsync = exports.createExpirationDate = exports.processStorage = exports.isNotAllowed = exports.isNotAllowedAsync = exports.processStore = exports.createValue = exports.getValue = exports.isExpired = exports.createEmptyStore = void 0;
31
- var widget_utilities_1 = require("@telia-ace/widget-utilities");
32
- var storage_1 = require("./storage");
33
- var types_1 = require("./types");
34
- var hasEntries = function (object) {
35
- return !!Object.entries(object).filter(function (_a) {
36
- var _b = __read(_a, 2), _ = _b[0], value = _b[1];
37
- return typeof value !== 'undefined';
38
- }).length;
39
- };
40
- var cleanUndefined = function (object) {
41
- return Object.entries(object)
42
- .filter(function (_a) {
43
- var _b = __read(_a, 2), _ = _b[0], value = _b[1];
44
- return typeof value !== 'undefined';
45
- })
46
- .forEach(function (_a) {
47
- var _b = __read(_a, 2), key = _b[0], value = _b[1].value;
48
- if (typeof value === 'undefined') {
49
- delete object[key];
50
- }
51
- });
52
- };
53
- var mergeAndClean = function (target, source) {
54
- var merged = {
55
- tenant: __assign(__assign({}, target.tenant), source.tenant),
56
- implementation: __assign(__assign({}, target.implementation), source.implementation),
57
- widget: __assign(__assign({}, target.widget), source.widget)
58
- };
59
- cleanUndefined(merged.tenant);
60
- cleanUndefined(merged.implementation);
61
- cleanUndefined(merged.widget);
62
- return merged;
63
- };
64
- var getLongestExpirationDate = function (store) {
65
- var expirationDates = Object.entries(store).reduce(function (acc, storeEntry) {
66
- var _a = __read(storeEntry, 2), _ = _a[0], container = _a[1];
67
- Object.entries(container).forEach(function (containerEntry) {
68
- var _a = __read(containerEntry, 2), _ = _a[0], expiration = _a[1].expiration;
69
- if (expiration)
70
- acc.push(expiration);
71
- });
72
- return acc;
73
- }, []);
74
- return (expirationDates
75
- .slice()
76
- .sort(function (a, b) { return new Date(b).valueOf() - new Date(a).valueOf(); })
77
- .shift() || (0, exports.createExpirationDate)());
78
- };
79
- var createStorageKeys = function (container) {
80
- var _a = container.get('$widget'), widgetName = _a.name, _b = _a.implementation, implementationName = _b.name, tenantName = _b.tenant;
81
- return [
82
- "humany_".concat(tenantName),
83
- "humany_".concat((0, widget_utilities_1.hash)(tenantName + implementationName)),
84
- "humany_".concat((0, widget_utilities_1.hash)(tenantName + implementationName + widgetName)),
85
- ];
86
- };
87
- var getCookie = function (cookieName) {
88
- var regExp = new RegExp("(?:^|;)\\s?".concat(cookieName, "=(.*?)(?:;|$)"), 'i');
89
- var cookie = document.cookie.match(regExp);
90
- var value = cookie && cookie[1] && JSON.parse(cookie[1]);
91
- return value || undefined;
92
- };
93
- var setCookie = function (cookieName, value, expires) {
94
- var cookie = "".concat(cookieName, "=").concat(JSON.stringify(value).trim(), ";");
95
- var expiration = "expires=".concat(new Date(expires).toUTCString(), ";");
96
- var path = 'path=/';
97
- return (document.cookie = "".concat(cookie, " ").concat(expiration, " ").concat(path));
98
- };
99
- var getCookieStore = function (container) {
100
- var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
101
- var tenant = getCookie(tenantKey) || {};
102
- var implementation = getCookie(implementationKey) || {};
103
- var widget = getCookie(widgetKey) || {};
104
- return {
105
- tenant: tenant,
106
- implementation: implementation,
107
- widget: widget,
108
- };
109
- };
110
- var setCookieStore = function (container, store) {
111
- var current = getCookieStore(container);
112
- var merged = mergeAndClean(current, store);
113
- var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
114
- var tenant = merged.tenant, implementation = merged.implementation, widget = merged.widget;
115
- var expirationDate = getLongestExpirationDate(store);
116
- var deleteDate = (0, exports.createExpirationDate)({ days: -1 });
117
- setCookie(tenantKey, tenant, hasEntries(tenant) ? expirationDate : deleteDate);
118
- setCookie(implementationKey, implementation, hasEntries(implementation) ? expirationDate : deleteDate);
119
- setCookie(widgetKey, widget, hasEntries(widget) ? expirationDate : deleteDate);
120
- };
121
- var getApplicationStore = function (container, storage) {
122
- var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
123
- var tenant = storage.getItem(tenantKey) || '{}';
124
- var implementation = storage.getItem(implementationKey) || '{}';
125
- var widget = storage.getItem(widgetKey) || '{}';
126
- return {
127
- tenant: JSON.parse(tenant),
128
- implementation: JSON.parse(implementation),
129
- widget: JSON.parse(widget),
130
- };
131
- };
132
- var setApplicationStore = function (container, applicationStorage, store) {
133
- var current = getApplicationStore(container, applicationStorage);
134
- var merged = mergeAndClean(current, store);
135
- var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
136
- var tenant = merged.tenant, implementation = merged.implementation, widget = merged.widget;
137
- hasEntries(tenant)
138
- ? applicationStorage.setItem(tenantKey, JSON.stringify(tenant))
139
- : applicationStorage.removeItem(tenantKey);
140
- hasEntries(implementation)
141
- ? applicationStorage.setItem(implementationKey, JSON.stringify(implementation))
142
- : applicationStorage.removeItem(implementationKey);
143
- hasEntries(widget)
144
- ? applicationStorage.setItem(widgetKey, JSON.stringify(widget))
145
- : applicationStorage.removeItem(widgetKey);
146
- };
147
- var createEmptyStore = function () { return ({ tenant: {}, implementation: {}, widget: {} }); };
148
- exports.createEmptyStore = createEmptyStore;
149
- var isExpired = function (isoDateString) {
150
- if (!isoDateString)
151
- return false;
152
- return !!isoDateString && new Date() > new Date(isoDateString);
153
- };
154
- exports.isExpired = isExpired;
155
- var getValue = function (store, key) {
156
- var storedValue = store.widget[key] || store.implementation[key] || store.tenant[key];
157
- return storedValue === null || storedValue === void 0 ? void 0 : storedValue.value;
158
- };
159
- exports.getValue = getValue;
160
- var createValue = function (value, category, duration) {
161
- if (duration) {
162
- return {
163
- value: value,
164
- category: category,
165
- expiration: typeof duration === 'object' ? (0, exports.createExpirationDate)(duration) : duration,
166
- };
167
- }
168
- return {
169
- value: value,
170
- category: category,
171
- };
172
- };
173
- exports.createValue = createValue;
174
- var processStore = function (store, policy) {
175
- var clone = (0, widget_utilities_1.deepClone)(store);
176
- Object.entries(clone).forEach(function (_a) {
177
- var _b = __read(_a, 2), _ = _b[0], storageContainer = _b[1];
178
- Object.entries(storageContainer).forEach(function (_a) {
179
- var _b = __read(_a, 2), key = _b[0], storedValue = _b[1];
180
- var expiration = storedValue.expiration, category = storedValue.category;
181
- if ((0, exports.isExpired)(expiration) || (0, exports.isNotAllowed)(policy, category))
182
- storageContainer[key] = (0, exports.createValue)(undefined, category, expiration);
183
- });
184
- });
185
- return clone;
186
- };
187
- exports.processStore = processStore;
188
- /**
189
- * @param category Storage Category.
190
- * @param policy Current Storage Policy.
191
- * @returns true if not allowed, false if allowed.
192
- */
193
- var isNotAllowedAsync = function (container, category) {
194
- return (0, storage_1.readStorage)(container, 'storagePolicy', types_1.StorageMedium.Cookie).then(function (policy) {
195
- if (policy === void 0) { policy = [types_1.StorageCategory.Necessary]; }
196
- return !!category && policy.indexOf(category) === -1;
197
- });
198
- };
199
- exports.isNotAllowedAsync = isNotAllowedAsync;
200
- /**
201
- * @param category Storage Category.
202
- * @param policy Current Storage Policy.
203
- * @returns true if not allowed, false if allowed.
204
- */
205
- var isNotAllowed = function (policy, category) {
206
- if (policy === void 0) { policy = [types_1.StorageCategory.Necessary]; }
207
- return !!category && policy.indexOf(category) === -1;
208
- };
209
- exports.isNotAllowed = isNotAllowed;
210
- /**
211
- * Processes storage and removes items if they are expired or if
212
- * the storage policy is defined and the category of the stored
213
- * value is not allowed.
214
- * @param container Widget Container
215
- */
216
- var processStorage = function (container) {
217
- var cookieStore = getCookieStore(container);
218
- var localStore = getApplicationStore(container, window.localStorage);
219
- var sessionStore = getApplicationStore(container, sessionStorage);
220
- var policy = (0, exports.getValue)(cookieStore, 'storagePolicy');
221
- var _a = __read([
222
- cookieStore,
223
- localStore,
224
- sessionStore,
225
- ].map(function (store) { return (0, exports.processStore)(store, policy); }), 3), processedCookieStore = _a[0], processedLocalStore = _a[1], processedSessionStore = _a[2];
226
- setCookieStore(container, processedCookieStore);
227
- setApplicationStore(container, window.localStorage, processedLocalStore);
228
- setApplicationStore(container, window.sessionStorage, processedSessionStore);
229
- };
230
- exports.processStorage = processStorage;
231
- /**
232
- * Creates an expiration date string with the passed options.
233
- * @param options Duration options.
234
- * @returns ISO formatted date string.
235
- */
236
- var createExpirationDate = function (options) {
237
- if (options === void 0) { options = { days: 30 }; }
238
- var _a = options.days, days = _a === void 0 ? 0 : _a, _b = options.hours, hours = _b === void 0 ? 0 : _b, _c = options.minutes, minutes = _c === void 0 ? 0 : _c, _d = options.seconds, seconds = _d === void 0 ? 0 : _d;
239
- var date = new Date();
240
- date.setTime(date.getTime() + hours * 60 * 60 * 1000);
241
- date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
242
- date.setTime(date.getTime() + minutes * 60 * 1000);
243
- date.setTime(date.getTime() + seconds * 1000);
244
- return date.toISOString();
245
- };
246
- exports.createExpirationDate = createExpirationDate;
247
- var getCookieStoreAsync = function (container) {
248
- return (0, widget_utilities_1.lock)(document)(function () { return getCookieStore(container); });
249
- };
250
- exports.getCookieStoreAsync = getCookieStoreAsync;
251
- var setCookieStoreAsync = function (container, store) {
252
- return (0, widget_utilities_1.lock)(document)(function () { return setCookieStore(container, store); });
253
- };
254
- exports.setCookieStoreAsync = setCookieStoreAsync;
255
- var getApplicationStoreAsync = function (container, type) {
256
- var storage = type === types_1.StorageMedium.Local ? window.localStorage : window.sessionStorage;
257
- return (0, widget_utilities_1.lock)(storage)(function () { return getApplicationStore(container, storage); });
258
- };
259
- exports.getApplicationStoreAsync = getApplicationStoreAsync;
260
- var setApplicationStoreAsync = function (container, type, store) {
261
- var storage = type === types_1.StorageMedium.Local ? window.localStorage : window.sessionStorage;
262
- return (0, widget_utilities_1.lock)(storage)(function () { return setApplicationStore(container, storage, store); });
263
- };
264
- exports.setApplicationStoreAsync = setApplicationStoreAsync;
package/lib-esm/index.js DELETED
@@ -1 +0,0 @@
1
- export { createStorageWriter, LegacyStorage as StorageService, LegacyStorageProvider as StorageProvider, readStorage, StorageCategory, StorageMedium, storagePolicyConfigurationApi, StorageScope, } from './storage';
@@ -1,4 +0,0 @@
1
- export { default as LegacyStorageProvider, LegacyStorage } from './legacy-storage-provider';
2
- export { createStorageWriter, readStorage } from './storage';
3
- export { default as storagePolicyConfigurationApi } from './storage-policy-configuration-api';
4
- export { StorageCategory, StorageMedium, StorageScope } from './types';
@@ -1,117 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (_) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- import { lock, Logger } from '@telia-ace/widget-utilities';
38
- import { StorageCategory } from '.';
39
- import { StorageScope } from './types';
40
- import { createExpirationDate, createValue, getValue, processStore } from './utils';
41
- var LegacyStorage = /** @class */ (function () {
42
- function LegacyStorage() {
43
- this.memory = {
44
- tenant: {},
45
- implementation: {},
46
- widget: {},
47
- };
48
- }
49
- LegacyStorage.prototype.setItem = function (key, value, options) {
50
- if (options === void 0) { options = {}; }
51
- return __awaiter(this, void 0, void 0, function () {
52
- var _this = this;
53
- return __generator(this, function (_a) {
54
- return [2 /*return*/, lock(this)(function () {
55
- var processedStore = processStore(_this.memory, [StorageCategory.Necessary]);
56
- var _a = options.scope, scope = _a === void 0 ? StorageScope.Tenant : _a, expiration = options.expiration;
57
- processedStore[scope][key] = createValue(value, StorageCategory.Necessary, (expiration === null || expiration === void 0 ? void 0 : expiration.toISOString()) || createExpirationDate());
58
- _this.memory = processedStore;
59
- if (process.env.NODE_ENV !== 'production') {
60
- Logger.warn("StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value stored temporarily in memory.\n Key: ".concat(key, "\n Value: ").concat(JSON.stringify(value), "\n Options: ").concat(JSON.stringify(options), "\n Memory: ").concat(JSON.stringify(processedStore)));
61
- }
62
- })];
63
- });
64
- });
65
- };
66
- LegacyStorage.prototype.getItem = function (key) {
67
- return __awaiter(this, void 0, void 0, function () {
68
- var _this = this;
69
- return __generator(this, function (_a) {
70
- return [2 /*return*/, lock(this)(function () {
71
- var processedStore = processStore(_this.memory, [StorageCategory.Necessary]);
72
- if (process.env.NODE_ENV !== 'production') {
73
- Logger.warn("StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value fetched from legacy memory storage.\n Key: ".concat(key, "\n Memory: ").concat(JSON.stringify(processedStore)));
74
- }
75
- var value = getValue(processedStore, key);
76
- _this.memory = processedStore;
77
- return value;
78
- })];
79
- });
80
- });
81
- };
82
- LegacyStorage.prototype.removeItem = function (key) {
83
- return __awaiter(this, void 0, void 0, function () {
84
- var _this = this;
85
- return __generator(this, function (_a) {
86
- return [2 /*return*/, lock(this)(function () {
87
- var processedStore = processStore(_this.memory, [StorageCategory.Necessary]);
88
- delete processedStore.tenant[key];
89
- delete processedStore.implementation[key];
90
- delete processedStore.widget[key];
91
- _this.memory = processedStore;
92
- if (process.env.NODE_ENV !== 'production') {
93
- Logger.warn("StorageProvider: StorageProvider is deprecated, use createStorageWriter and readStorage instead, value removed from memory.\n Key: ".concat(key, "\n Memory: ").concat(JSON.stringify(processedStore)));
94
- }
95
- })];
96
- });
97
- });
98
- };
99
- LegacyStorage.prototype.clear = function () {
100
- return __awaiter(this, void 0, void 0, function () {
101
- return __generator(this, function (_a) {
102
- return [2 /*return*/, Promise.resolve()];
103
- });
104
- });
105
- };
106
- return LegacyStorage;
107
- }());
108
- export { LegacyStorage };
109
- var LegacyStorageProvider = /** @class */ (function () {
110
- function LegacyStorageProvider() {
111
- this.cookies = new LegacyStorage();
112
- this.session = new LegacyStorage();
113
- this.local = new LegacyStorage();
114
- }
115
- return LegacyStorageProvider;
116
- }());
117
- export default LegacyStorageProvider;
@@ -1,71 +0,0 @@
1
- import { Logger } from '@telia-ace/widget-utilities';
2
- import { createConfigurationApi } from '@webprovisions/platform';
3
- import { createStorageWriter } from './storage';
4
- import { StorageCategory, StorageMedium, StorageScope } from './types';
5
- import { processStorage } from './utils';
6
- export default createConfigurationApi(function (builder) {
7
- builder.createCommand('storage', function (context) { return function (options) {
8
- if (options === void 0) { options = {}; }
9
- var consent = options.consent;
10
- if (typeof consent !== 'undefined') {
11
- if (typeof consent !== 'number' &&
12
- typeof consent !== 'string' &&
13
- !Array.isArray(consent)) {
14
- if (process.env.NODE_ENV !== 'production') {
15
- Logger.warn("storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.\n Passed consent: ".concat(JSON.stringify(consent)));
16
- }
17
- return;
18
- }
19
- if (Array.isArray(consent) &&
20
- !consent.every(function (c) { return typeof c === 'number' || typeof c === 'string'; })) {
21
- if (process.env.NODE_ENV !== 'production') {
22
- Logger.warn("storagePolicyConfigurationApi: passed consent is not valid. Consent either has to be a number, a string, an array of strings or an array of numbers.\n Passed consent: ".concat(JSON.stringify(consent)));
23
- }
24
- return;
25
- }
26
- var policy_1 = [
27
- StorageCategory.Necessary,
28
- StorageCategory.Functional,
29
- StorageCategory.Analytical,
30
- StorageCategory.Marketing,
31
- ].filter(function (category, index, array) {
32
- if (typeof consent === 'number') {
33
- // Passed consent is a category level (number)
34
- // Include the passed category level and all categories below it
35
- return consent > index;
36
- }
37
- else if (typeof consent === 'string') {
38
- // Passed consent is a category (string)
39
- // Include the passed category and all categories below its level
40
- return array.indexOf(consent) + 1 > index;
41
- }
42
- else if (Array.isArray(consent)) {
43
- // Passed consent is an array with category levels (number), categories (string) or both
44
- // Include all categories in passed array
45
- return (consent.findIndex(function (c) {
46
- return c === index + 1 || c === category;
47
- }) > -1);
48
- }
49
- else {
50
- return true;
51
- }
52
- });
53
- if (policy_1.indexOf(StorageCategory.Necessary) === -1) {
54
- if (process.env.NODE_ENV !== 'production') {
55
- Logger.warn("storagePolicyConfigurationApi: passed consent does not include category: '".concat(StorageCategory.Necessary, "'. Consent has to atleast include the '").concat(StorageCategory.Necessary, "' category.\n Passed consent: ").concat(JSON.stringify(consent)));
56
- }
57
- policy_1.unshift(StorageCategory.Necessary);
58
- }
59
- return createStorageWriter(context.container, 'storagePolicy', StorageCategory.Necessary, {
60
- medium: StorageMedium.Cookie,
61
- scope: StorageScope.Tenant,
62
- duration: { days: 365 },
63
- })
64
- .then(function (write) { return write(policy_1); })
65
- .then(function () {
66
- processStorage(context.container);
67
- context.events.dispatch('storage:policy-changed', policy_1);
68
- });
69
- }
70
- }; });
71
- });
@@ -1,60 +0,0 @@
1
- import { Logger } from '@telia-ace/widget-utilities';
2
- import { StorageMedium, StorageScope, } from './types';
3
- import { createEmptyStore, createValue, getApplicationStoreAsync, getCookieStoreAsync, getValue, isNotAllowedAsync, processStorage, setApplicationStoreAsync, setCookieStoreAsync, } from './utils';
4
- /**
5
- * Creates a writer to be used to write to cookie, local or session storage.
6
- * @param container Widget Container.
7
- * @param key Key to use when writing.
8
- * @param category StorageCategory to use when writing.
9
- * @param options StorageWriterOptions to use when writing default {scope: StorageScope.Widget, medium = StorageMedium.Session, duration = infinite }.
10
- * @returns a function to be used to write a value to the storage using given settings.
11
- */
12
- export var createStorageWriter = function (container, key, category, options) {
13
- if (options === void 0) { options = {}; }
14
- return isNotAllowedAsync(container, category).then(function (notAllowed) {
15
- if (process.env.NODE_ENV !== 'production') {
16
- if (notAllowed) {
17
- Logger.warn("createStorageWriter: Attempted to create storage writer using unallowed category.\n Key: ".concat(key, "\n Category: ").concat(JSON.stringify(category), "\n Options: ").concat(JSON.stringify(options)));
18
- }
19
- }
20
- return function (value) {
21
- processStorage(container);
22
- return isNotAllowedAsync(container, category).then(function (notAllowed) {
23
- if (notAllowed) {
24
- if (process.env.NODE_ENV !== 'production') {
25
- Logger.warn("storageWriter: Attempted to write to storage using unallowed category.\n Key: ".concat(key, "\n Category: ").concat(JSON.stringify(category), "\n Value: ").concat(JSON.stringify(value), "\n Options: ").concat(JSON.stringify(options)));
26
- }
27
- return;
28
- }
29
- var _a = options.scope, scope = _a === void 0 ? StorageScope.Widget : _a, _b = options.medium, medium = _b === void 0 ? StorageMedium.Session : _b, duration = options.duration;
30
- var store = createEmptyStore();
31
- store[scope][key] = createValue(value, category, duration);
32
- if (medium === StorageMedium.Cookie)
33
- return setCookieStoreAsync(container, store);
34
- else
35
- return setApplicationStoreAsync(container, medium, store);
36
- });
37
- };
38
- });
39
- };
40
- /**
41
- * Read the widget cookie, local or session storage.
42
- * @param container Widget Container.
43
- * @param key Key to read.
44
- * @param medium Storage medium to read from (default: session).
45
- * @returns the value stored with passed key.
46
- */
47
- export var readStorage = function (container, key, medium) {
48
- if (medium === void 0) { medium = StorageMedium.Session; }
49
- processStorage(container);
50
- if (typeof key === 'undefined') {
51
- if (process.env.NODE_ENV !== 'production') {
52
- Logger.warn('readStorage: Undefined key passed when reading storage. ', key, medium);
53
- }
54
- return Promise.resolve(undefined);
55
- }
56
- if (medium === StorageMedium.Cookie)
57
- return getCookieStoreAsync(container).then(function (store) { return getValue(store, key); });
58
- else
59
- return getApplicationStoreAsync(container, medium).then(function (store) { return getValue(store, key); });
60
- };
@@ -1,19 +0,0 @@
1
- export var StorageMedium;
2
- (function (StorageMedium) {
3
- StorageMedium["Cookie"] = "cookie";
4
- StorageMedium["Local"] = "local";
5
- StorageMedium["Session"] = "session";
6
- })(StorageMedium || (StorageMedium = {}));
7
- export var StorageScope;
8
- (function (StorageScope) {
9
- StorageScope["Tenant"] = "tenant";
10
- StorageScope["Implementation"] = "implementation";
11
- StorageScope["Widget"] = "widget";
12
- })(StorageScope || (StorageScope = {}));
13
- export var StorageCategory;
14
- (function (StorageCategory) {
15
- StorageCategory["Necessary"] = "necessary";
16
- StorageCategory["Functional"] = "functional";
17
- StorageCategory["Analytical"] = "analytical";
18
- StorageCategory["Marketing"] = "marketing";
19
- })(StorageCategory || (StorageCategory = {}));