@telia-ace/widget-services 1.0.4 → 1.0.7
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.
- package/LICENSE.txt +5 -5
- package/README.md +3 -3
- package/{lib-esm → dist}/index.d.ts +2 -2
- package/dist/index.js +431 -0
- package/dist/index.js.map +1 -0
- package/{lib → dist}/storage/index.d.ts +5 -5
- package/{lib-esm → dist}/storage/legacy-storage-provider.d.ts +25 -25
- package/{lib-esm → dist}/storage/storage-policy-configuration-api.d.ts +2 -2
- package/{lib-esm → dist}/storage/storage.d.ts +19 -19
- package/{lib-esm → dist}/storage/types.d.ts +45 -45
- package/{lib → dist}/storage/utils.d.ts +36 -36
- package/package.json +9 -10
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -12
- package/lib/storage/index.js +0 -18
- package/lib/storage/legacy-storage-provider.d.ts +0 -25
- package/lib/storage/legacy-storage-provider.js +0 -120
- package/lib/storage/storage-policy-configuration-api.d.ts +0 -2
- package/lib/storage/storage-policy-configuration-api.js +0 -73
- package/lib/storage/storage.d.ts +0 -19
- package/lib/storage/storage.js +0 -65
- package/lib/storage/types.d.ts +0 -45
- package/lib/storage/types.js +0 -22
- package/lib/storage/utils.js +0 -264
- package/lib-esm/index.js +0 -1
- package/lib-esm/storage/index.d.ts +0 -5
- package/lib-esm/storage/index.js +0 -4
- package/lib-esm/storage/legacy-storage-provider.js +0 -117
- package/lib-esm/storage/storage-policy-configuration-api.js +0 -71
- package/lib-esm/storage/storage.js +0 -60
- package/lib-esm/storage/types.js +0 -19
- package/lib-esm/storage/utils.d.ts +0 -36
- package/lib-esm/storage/utils.js +0 -248
package/lib-esm/storage/utils.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
13
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
14
|
-
if (!m) return o;
|
|
15
|
-
var i = m.call(o), r, ar = [], e;
|
|
16
|
-
try {
|
|
17
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
18
|
-
}
|
|
19
|
-
catch (error) { e = { error: error }; }
|
|
20
|
-
finally {
|
|
21
|
-
try {
|
|
22
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
-
}
|
|
24
|
-
finally { if (e) throw e.error; }
|
|
25
|
-
}
|
|
26
|
-
return ar;
|
|
27
|
-
};
|
|
28
|
-
import { deepClone, hash, lock } from '@telia-ace/widget-utilities';
|
|
29
|
-
import { readStorage } from './storage';
|
|
30
|
-
import { StorageCategory, StorageMedium, } from './types';
|
|
31
|
-
var hasEntries = function (object) {
|
|
32
|
-
return !!Object.entries(object).filter(function (_a) {
|
|
33
|
-
var _b = __read(_a, 2), _ = _b[0], value = _b[1];
|
|
34
|
-
return typeof value !== 'undefined';
|
|
35
|
-
}).length;
|
|
36
|
-
};
|
|
37
|
-
var cleanUndefined = function (object) {
|
|
38
|
-
return Object.entries(object)
|
|
39
|
-
.filter(function (_a) {
|
|
40
|
-
var _b = __read(_a, 2), _ = _b[0], value = _b[1];
|
|
41
|
-
return typeof value !== 'undefined';
|
|
42
|
-
})
|
|
43
|
-
.forEach(function (_a) {
|
|
44
|
-
var _b = __read(_a, 2), key = _b[0], value = _b[1].value;
|
|
45
|
-
if (typeof value === 'undefined') {
|
|
46
|
-
delete object[key];
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
var mergeAndClean = function (target, source) {
|
|
51
|
-
var merged = {
|
|
52
|
-
tenant: __assign(__assign({}, target.tenant), source.tenant),
|
|
53
|
-
implementation: __assign(__assign({}, target.implementation), source.implementation),
|
|
54
|
-
widget: __assign(__assign({}, target.widget), source.widget)
|
|
55
|
-
};
|
|
56
|
-
cleanUndefined(merged.tenant);
|
|
57
|
-
cleanUndefined(merged.implementation);
|
|
58
|
-
cleanUndefined(merged.widget);
|
|
59
|
-
return merged;
|
|
60
|
-
};
|
|
61
|
-
var getLongestExpirationDate = function (store) {
|
|
62
|
-
var expirationDates = Object.entries(store).reduce(function (acc, storeEntry) {
|
|
63
|
-
var _a = __read(storeEntry, 2), _ = _a[0], container = _a[1];
|
|
64
|
-
Object.entries(container).forEach(function (containerEntry) {
|
|
65
|
-
var _a = __read(containerEntry, 2), _ = _a[0], expiration = _a[1].expiration;
|
|
66
|
-
if (expiration)
|
|
67
|
-
acc.push(expiration);
|
|
68
|
-
});
|
|
69
|
-
return acc;
|
|
70
|
-
}, []);
|
|
71
|
-
return (expirationDates
|
|
72
|
-
.slice()
|
|
73
|
-
.sort(function (a, b) { return new Date(b).valueOf() - new Date(a).valueOf(); })
|
|
74
|
-
.shift() || createExpirationDate());
|
|
75
|
-
};
|
|
76
|
-
var createStorageKeys = function (container) {
|
|
77
|
-
var _a = container.get('$widget'), widgetName = _a.name, _b = _a.implementation, implementationName = _b.name, tenantName = _b.tenant;
|
|
78
|
-
return [
|
|
79
|
-
"humany_".concat(tenantName),
|
|
80
|
-
"humany_".concat(hash(tenantName + implementationName)),
|
|
81
|
-
"humany_".concat(hash(tenantName + implementationName + widgetName)),
|
|
82
|
-
];
|
|
83
|
-
};
|
|
84
|
-
var getCookie = function (cookieName) {
|
|
85
|
-
var regExp = new RegExp("(?:^|;)\\s?".concat(cookieName, "=(.*?)(?:;|$)"), 'i');
|
|
86
|
-
var cookie = document.cookie.match(regExp);
|
|
87
|
-
var value = cookie && cookie[1] && JSON.parse(cookie[1]);
|
|
88
|
-
return value || undefined;
|
|
89
|
-
};
|
|
90
|
-
var setCookie = function (cookieName, value, expires) {
|
|
91
|
-
var cookie = "".concat(cookieName, "=").concat(JSON.stringify(value).trim(), ";");
|
|
92
|
-
var expiration = "expires=".concat(new Date(expires).toUTCString(), ";");
|
|
93
|
-
var path = 'path=/';
|
|
94
|
-
return (document.cookie = "".concat(cookie, " ").concat(expiration, " ").concat(path));
|
|
95
|
-
};
|
|
96
|
-
var getCookieStore = function (container) {
|
|
97
|
-
var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
|
|
98
|
-
var tenant = getCookie(tenantKey) || {};
|
|
99
|
-
var implementation = getCookie(implementationKey) || {};
|
|
100
|
-
var widget = getCookie(widgetKey) || {};
|
|
101
|
-
return {
|
|
102
|
-
tenant: tenant,
|
|
103
|
-
implementation: implementation,
|
|
104
|
-
widget: widget,
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
var setCookieStore = function (container, store) {
|
|
108
|
-
var current = getCookieStore(container);
|
|
109
|
-
var merged = mergeAndClean(current, store);
|
|
110
|
-
var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
|
|
111
|
-
var tenant = merged.tenant, implementation = merged.implementation, widget = merged.widget;
|
|
112
|
-
var expirationDate = getLongestExpirationDate(store);
|
|
113
|
-
var deleteDate = createExpirationDate({ days: -1 });
|
|
114
|
-
setCookie(tenantKey, tenant, hasEntries(tenant) ? expirationDate : deleteDate);
|
|
115
|
-
setCookie(implementationKey, implementation, hasEntries(implementation) ? expirationDate : deleteDate);
|
|
116
|
-
setCookie(widgetKey, widget, hasEntries(widget) ? expirationDate : deleteDate);
|
|
117
|
-
};
|
|
118
|
-
var getApplicationStore = function (container, storage) {
|
|
119
|
-
var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
|
|
120
|
-
var tenant = storage.getItem(tenantKey) || '{}';
|
|
121
|
-
var implementation = storage.getItem(implementationKey) || '{}';
|
|
122
|
-
var widget = storage.getItem(widgetKey) || '{}';
|
|
123
|
-
return {
|
|
124
|
-
tenant: JSON.parse(tenant),
|
|
125
|
-
implementation: JSON.parse(implementation),
|
|
126
|
-
widget: JSON.parse(widget),
|
|
127
|
-
};
|
|
128
|
-
};
|
|
129
|
-
var setApplicationStore = function (container, applicationStorage, store) {
|
|
130
|
-
var current = getApplicationStore(container, applicationStorage);
|
|
131
|
-
var merged = mergeAndClean(current, store);
|
|
132
|
-
var _a = __read(createStorageKeys(container), 3), tenantKey = _a[0], implementationKey = _a[1], widgetKey = _a[2];
|
|
133
|
-
var tenant = merged.tenant, implementation = merged.implementation, widget = merged.widget;
|
|
134
|
-
hasEntries(tenant)
|
|
135
|
-
? applicationStorage.setItem(tenantKey, JSON.stringify(tenant))
|
|
136
|
-
: applicationStorage.removeItem(tenantKey);
|
|
137
|
-
hasEntries(implementation)
|
|
138
|
-
? applicationStorage.setItem(implementationKey, JSON.stringify(implementation))
|
|
139
|
-
: applicationStorage.removeItem(implementationKey);
|
|
140
|
-
hasEntries(widget)
|
|
141
|
-
? applicationStorage.setItem(widgetKey, JSON.stringify(widget))
|
|
142
|
-
: applicationStorage.removeItem(widgetKey);
|
|
143
|
-
};
|
|
144
|
-
export var createEmptyStore = function () { return ({ tenant: {}, implementation: {}, widget: {} }); };
|
|
145
|
-
export var isExpired = function (isoDateString) {
|
|
146
|
-
if (!isoDateString)
|
|
147
|
-
return false;
|
|
148
|
-
return !!isoDateString && new Date() > new Date(isoDateString);
|
|
149
|
-
};
|
|
150
|
-
export var getValue = function (store, key) {
|
|
151
|
-
var storedValue = store.widget[key] || store.implementation[key] || store.tenant[key];
|
|
152
|
-
return storedValue === null || storedValue === void 0 ? void 0 : storedValue.value;
|
|
153
|
-
};
|
|
154
|
-
export var createValue = function (value, category, duration) {
|
|
155
|
-
if (duration) {
|
|
156
|
-
return {
|
|
157
|
-
value: value,
|
|
158
|
-
category: category,
|
|
159
|
-
expiration: typeof duration === 'object' ? createExpirationDate(duration) : duration,
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
return {
|
|
163
|
-
value: value,
|
|
164
|
-
category: category,
|
|
165
|
-
};
|
|
166
|
-
};
|
|
167
|
-
export var processStore = function (store, policy) {
|
|
168
|
-
var clone = deepClone(store);
|
|
169
|
-
Object.entries(clone).forEach(function (_a) {
|
|
170
|
-
var _b = __read(_a, 2), _ = _b[0], storageContainer = _b[1];
|
|
171
|
-
Object.entries(storageContainer).forEach(function (_a) {
|
|
172
|
-
var _b = __read(_a, 2), key = _b[0], storedValue = _b[1];
|
|
173
|
-
var expiration = storedValue.expiration, category = storedValue.category;
|
|
174
|
-
if (isExpired(expiration) || isNotAllowed(policy, category))
|
|
175
|
-
storageContainer[key] = createValue(undefined, category, expiration);
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
return clone;
|
|
179
|
-
};
|
|
180
|
-
/**
|
|
181
|
-
* @param category Storage Category.
|
|
182
|
-
* @param policy Current Storage Policy.
|
|
183
|
-
* @returns true if not allowed, false if allowed.
|
|
184
|
-
*/
|
|
185
|
-
export var isNotAllowedAsync = function (container, category) {
|
|
186
|
-
return readStorage(container, 'storagePolicy', StorageMedium.Cookie).then(function (policy) {
|
|
187
|
-
if (policy === void 0) { policy = [StorageCategory.Necessary]; }
|
|
188
|
-
return !!category && policy.indexOf(category) === -1;
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
/**
|
|
192
|
-
* @param category Storage Category.
|
|
193
|
-
* @param policy Current Storage Policy.
|
|
194
|
-
* @returns true if not allowed, false if allowed.
|
|
195
|
-
*/
|
|
196
|
-
export var isNotAllowed = function (policy, category) {
|
|
197
|
-
if (policy === void 0) { policy = [StorageCategory.Necessary]; }
|
|
198
|
-
return !!category && policy.indexOf(category) === -1;
|
|
199
|
-
};
|
|
200
|
-
/**
|
|
201
|
-
* Processes storage and removes items if they are expired or if
|
|
202
|
-
* the storage policy is defined and the category of the stored
|
|
203
|
-
* value is not allowed.
|
|
204
|
-
* @param container Widget Container
|
|
205
|
-
*/
|
|
206
|
-
export var processStorage = function (container) {
|
|
207
|
-
var cookieStore = getCookieStore(container);
|
|
208
|
-
var localStore = getApplicationStore(container, window.localStorage);
|
|
209
|
-
var sessionStore = getApplicationStore(container, sessionStorage);
|
|
210
|
-
var policy = getValue(cookieStore, 'storagePolicy');
|
|
211
|
-
var _a = __read([
|
|
212
|
-
cookieStore,
|
|
213
|
-
localStore,
|
|
214
|
-
sessionStore,
|
|
215
|
-
].map(function (store) { return processStore(store, policy); }), 3), processedCookieStore = _a[0], processedLocalStore = _a[1], processedSessionStore = _a[2];
|
|
216
|
-
setCookieStore(container, processedCookieStore);
|
|
217
|
-
setApplicationStore(container, window.localStorage, processedLocalStore);
|
|
218
|
-
setApplicationStore(container, window.sessionStorage, processedSessionStore);
|
|
219
|
-
};
|
|
220
|
-
/**
|
|
221
|
-
* Creates an expiration date string with the passed options.
|
|
222
|
-
* @param options Duration options.
|
|
223
|
-
* @returns ISO formatted date string.
|
|
224
|
-
*/
|
|
225
|
-
export var createExpirationDate = function (options) {
|
|
226
|
-
if (options === void 0) { options = { days: 30 }; }
|
|
227
|
-
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;
|
|
228
|
-
var date = new Date();
|
|
229
|
-
date.setTime(date.getTime() + hours * 60 * 60 * 1000);
|
|
230
|
-
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
231
|
-
date.setTime(date.getTime() + minutes * 60 * 1000);
|
|
232
|
-
date.setTime(date.getTime() + seconds * 1000);
|
|
233
|
-
return date.toISOString();
|
|
234
|
-
};
|
|
235
|
-
export var getCookieStoreAsync = function (container) {
|
|
236
|
-
return lock(document)(function () { return getCookieStore(container); });
|
|
237
|
-
};
|
|
238
|
-
export var setCookieStoreAsync = function (container, store) {
|
|
239
|
-
return lock(document)(function () { return setCookieStore(container, store); });
|
|
240
|
-
};
|
|
241
|
-
export var getApplicationStoreAsync = function (container, type) {
|
|
242
|
-
var storage = type === StorageMedium.Local ? window.localStorage : window.sessionStorage;
|
|
243
|
-
return lock(storage)(function () { return getApplicationStore(container, storage); });
|
|
244
|
-
};
|
|
245
|
-
export var setApplicationStoreAsync = function (container, type, store) {
|
|
246
|
-
var storage = type === StorageMedium.Local ? window.localStorage : window.sessionStorage;
|
|
247
|
-
return lock(storage)(function () { return setApplicationStore(container, storage, store); });
|
|
248
|
-
};
|