@kwiz/common 1.0.129 → 1.0.133

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.
Files changed (111) hide show
  1. package/LICENSE +21 -21
  2. package/lib/cjs/config.js +2 -0
  3. package/lib/cjs/config.js.map +1 -1
  4. package/lib/cjs/types/libs/msal.types.js +26 -26
  5. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  6. package/lib/esm/config.js +2 -0
  7. package/lib/esm/config.js.map +1 -1
  8. package/lib/esm/types/libs/msal.types.js +26 -26
  9. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  10. package/lib/types/config.d.ts +2 -0
  11. package/package.json +81 -81
  12. package/readme.md +17 -17
  13. package/.github/workflows/npm-publish.yml +0 -24
  14. package/.madgerc +0 -3
  15. package/fix-folder-imports.js +0 -27
  16. package/src/_dependencies.ts +0 -13
  17. package/src/config.ts +0 -17
  18. package/src/exports-index.ts +0 -4
  19. package/src/helpers/Guid.ts +0 -182
  20. package/src/helpers/base64.ts +0 -174
  21. package/src/helpers/browser.test.js +0 -13
  22. package/src/helpers/browser.ts +0 -1505
  23. package/src/helpers/browserinfo.ts +0 -293
  24. package/src/helpers/collections.base.test.js +0 -26
  25. package/src/helpers/collections.base.ts +0 -438
  26. package/src/helpers/collections.ts +0 -108
  27. package/src/helpers/color.ts +0 -55
  28. package/src/helpers/cookies.ts +0 -60
  29. package/src/helpers/date.test.js +0 -120
  30. package/src/helpers/date.ts +0 -189
  31. package/src/helpers/debug.ts +0 -187
  32. package/src/helpers/diagrams.ts +0 -44
  33. package/src/helpers/emails.ts +0 -7
  34. package/src/helpers/eval.ts +0 -5
  35. package/src/helpers/exports-index.ts +0 -30
  36. package/src/helpers/file.test.js +0 -51
  37. package/src/helpers/file.ts +0 -64
  38. package/src/helpers/flatted.ts +0 -150
  39. package/src/helpers/functions.ts +0 -17
  40. package/src/helpers/graph/calendar.types.ts +0 -11
  41. package/src/helpers/graph/exports-index.ts +0 -1
  42. package/src/helpers/http.ts +0 -70
  43. package/src/helpers/images.ts +0 -23
  44. package/src/helpers/json.ts +0 -45
  45. package/src/helpers/md5.ts +0 -190
  46. package/src/helpers/objects.test.js +0 -34
  47. package/src/helpers/objects.ts +0 -275
  48. package/src/helpers/promises.test.js +0 -38
  49. package/src/helpers/promises.ts +0 -166
  50. package/src/helpers/random.ts +0 -27
  51. package/src/helpers/scheduler/exports-index.ts +0 -1
  52. package/src/helpers/scheduler/scheduler.test.js +0 -104
  53. package/src/helpers/scheduler/scheduler.ts +0 -132
  54. package/src/helpers/sharepoint.ts +0 -797
  55. package/src/helpers/strings.test.js +0 -123
  56. package/src/helpers/strings.ts +0 -338
  57. package/src/helpers/typecheckers.test.js +0 -35
  58. package/src/helpers/typecheckers.ts +0 -267
  59. package/src/helpers/url.test.js +0 -44
  60. package/src/helpers/url.ts +0 -208
  61. package/src/helpers/urlhelper.ts +0 -112
  62. package/src/index.ts +0 -6
  63. package/src/types/auth.ts +0 -63
  64. package/src/types/common.types.ts +0 -16
  65. package/src/types/exports-index.ts +0 -15
  66. package/src/types/flatted.types.ts +0 -60
  67. package/src/types/globals.types.ts +0 -7
  68. package/src/types/graph/calendar.types.ts +0 -81
  69. package/src/types/graph/exports-index.ts +0 -1
  70. package/src/types/knownscript.types.ts +0 -19
  71. package/src/types/libs/datajs.types.ts +0 -29
  72. package/src/types/libs/exports-index.ts +0 -3
  73. package/src/types/libs/ics.types.ts +0 -31
  74. package/src/types/libs/msal.types.ts +0 -58
  75. package/src/types/locales.ts +0 -126
  76. package/src/types/localstoragecache.types.ts +0 -9
  77. package/src/types/location.types.ts +0 -28
  78. package/src/types/moment.ts +0 -12
  79. package/src/types/regex.types.ts +0 -17
  80. package/src/types/rest.types.ts +0 -96
  81. package/src/types/sharepoint.types.ts +0 -1467
  82. package/src/types/sharepoint.utils.types.ts +0 -307
  83. package/src/utils/auth/common.ts +0 -119
  84. package/src/utils/auth/discovery.test.js +0 -13
  85. package/src/utils/auth/discovery.ts +0 -132
  86. package/src/utils/auth/exports-index.ts +0 -2
  87. package/src/utils/base64.ts +0 -28
  88. package/src/utils/consolelogger.ts +0 -334
  89. package/src/utils/date.ts +0 -173
  90. package/src/utils/emails.ts +0 -25
  91. package/src/utils/exports-index.ts +0 -11
  92. package/src/utils/knownscript.ts +0 -287
  93. package/src/utils/localstoragecache.ts +0 -447
  94. package/src/utils/rest.ts +0 -502
  95. package/src/utils/script.ts +0 -171
  96. package/src/utils/sharepoint.rest/common.ts +0 -160
  97. package/src/utils/sharepoint.rest/date.ts +0 -63
  98. package/src/utils/sharepoint.rest/exports-index.ts +0 -11
  99. package/src/utils/sharepoint.rest/file.folder.ts +0 -685
  100. package/src/utils/sharepoint.rest/item.ts +0 -547
  101. package/src/utils/sharepoint.rest/list.ts +0 -1572
  102. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -775
  103. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -275
  104. package/src/utils/sharepoint.rest/listutils/common.ts +0 -207
  105. package/src/utils/sharepoint.rest/listutils/exports-index.ts +0 -3
  106. package/src/utils/sharepoint.rest/location.ts +0 -142
  107. package/src/utils/sharepoint.rest/navigation-links.ts +0 -87
  108. package/src/utils/sharepoint.rest/user-search.ts +0 -253
  109. package/src/utils/sharepoint.rest/user.ts +0 -559
  110. package/src/utils/sharepoint.rest/web.ts +0 -1385
  111. package/src/utils/sod.ts +0 -195
@@ -1,447 +0,0 @@
1
- import { sizeOf } from "../helpers/collections.base";
2
- import { isDebug } from "../helpers/debug";
3
- import { flatted } from "../helpers/flatted";
4
- import { jsonParse } from "../helpers/json";
5
- import { getGlobal } from "../helpers/objects";
6
- import { isDate, isNullOrEmptyString, isNullOrUndefined, isNumber } from "../helpers/typecheckers";
7
- import { ILocalStorageCacheLifetime } from "../types/localstoragecache.types";
8
- import { ConsoleLogger } from "./consolelogger";
9
-
10
- /**key with prefix, value is a date string */
11
- interface IExpirationsDictionary {
12
- [keyWithPRefix: string]: string;
13
- build: string;
14
- }
15
-
16
- let logger = ConsoleLogger.get("localstoragecache");
17
-
18
- export const keyPrefix = "kw$_";
19
- export const LOCAL_STORAGE_PREFIX = "kwizcom-localstorage-cache";
20
- export const LOCAL_STORGAGE_EXPIRATIONS_KEY = LOCAL_STORAGE_PREFIX + "-expirations";
21
- export const DEFAULT_EXPIRATION = 20 * 60 * 1000; // 20 minutes;
22
- /** When caching logic changes (serialization methods, format, schema), the MODULE_REVISION should be incremented
23
- * and all client side apps will need to be rebuilt */
24
- export const MODULE_REVISION = "1";
25
- /** key (no prefix) is kept in lower case. not case sensitive */
26
-
27
- function _getCache() {
28
- let _cache = getGlobal<{
29
- purgeCalled: boolean;
30
- expirations: IExpirationsDictionary;
31
- [keyNoPrefixToLower: string]: any;
32
- }>("common_utils_localstoragecache_module_cache");
33
-
34
- if (!_cache.purgeCalled) {
35
- //issue 7081 - purge all orphans/expired items
36
- _cache.purgeCalled = true;
37
- //clear expired cache items.
38
- (globalThis || window).setTimeout(() => {
39
- purgeCache();
40
- if (isDebug()) {
41
- let size = _getStoredSize();
42
- logger.debug(`Size of items in local storage: ${size}KB`);
43
- }
44
- }, 5000);
45
- }
46
-
47
- return _cache;
48
- }
49
-
50
- var _supportsLocalStorage: boolean = null;
51
-
52
- function _parseExpiration(exp: number | ILocalStorageCacheLifetime | Date): Date {
53
- var expirationDate: Date;
54
-
55
- if (isNumber(exp) && exp > 0) {
56
- expirationDate = new Date();
57
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + exp);
58
- } else if (exp instanceof Date) {
59
- expirationDate = exp;
60
- } else if (exp) {
61
- var tempexp = exp as ILocalStorageCacheLifetime;
62
- var seconds = typeof (tempexp.seconds) === "number" ? tempexp.seconds : undefined;
63
- var minutes = typeof (tempexp.minutes) === "number" ? tempexp.minutes : undefined;
64
- var hours = typeof (tempexp.hours) === "number" ? tempexp.hours : undefined;
65
- var days = typeof (tempexp.days) === "number" ? tempexp.days : undefined;
66
- var months = typeof (tempexp.months) === "number" ? tempexp.months : undefined;
67
- var years = typeof (tempexp.years) === "number" ? tempexp.years : undefined;
68
-
69
- if (seconds || minutes || hours || days || months || years) {
70
- expirationDate = new Date();
71
-
72
- if (seconds) {
73
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (seconds * 1000));
74
- }
75
-
76
- if (minutes) {
77
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (minutes * 60 * 1000));
78
- }
79
-
80
- if (hours) {
81
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (hours * 60 * 60 * 1000));
82
- }
83
-
84
- if (days) {
85
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (days * 24 * 60 * 60 * 1000));
86
- }
87
-
88
- if (months) {
89
- expirationDate.setMonth(expirationDate.getMonth() + months);
90
- }
91
-
92
- if (years) {
93
- expirationDate.setFullYear(expirationDate.getFullYear() + years);
94
- }
95
- }
96
- }
97
-
98
- if (!expirationDate) {
99
- expirationDate = new Date();
100
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + DEFAULT_EXPIRATION);
101
- }
102
-
103
- return expirationDate;
104
- }
105
-
106
- function _getCacheExpirations(): IExpirationsDictionary {
107
- let _cache = _getCache();
108
- if (isNullOrUndefined(_cache.expirations)) {
109
- _cache.expirations = jsonParse<IExpirationsDictionary>(_getItem(LOCAL_STORGAGE_EXPIRATIONS_KEY));
110
-
111
- //ISSUE: 1525 - expire the cache if it was built with a different version number so that the cache
112
- //is compatible with the current build
113
- if (!isNullOrUndefined(_cache.expirations) && _cache.expirations.build !== MODULE_REVISION) {
114
- logger.log(`Purging cache because of build number change`)
115
- purgeCache(true);
116
- _cache.expirations = null;
117
- }
118
-
119
- if (isNullOrUndefined(_cache.expirations)) {
120
- _cache.expirations = {
121
- build: MODULE_REVISION
122
- };
123
- }
124
- }
125
-
126
- return _cache.expirations;
127
- }
128
-
129
- function _saveCacheExpirations() {
130
- let _cache = _getCache();
131
- if (!isNullOrUndefined(_cache.expirations) && sizeOf(_cache.expirations) > 0) {
132
- _setItem(LOCAL_STORGAGE_EXPIRATIONS_KEY, JSON.stringify(_cache.expirations));
133
- }
134
- else {
135
- _removeItem(LOCAL_STORGAGE_EXPIRATIONS_KEY);
136
- }
137
- }
138
-
139
- function _setCacheExpiration(keyWithPrefix: string, expireDate: Date) {
140
- var expirations = _getCacheExpirations();
141
- expirations[keyWithPrefix] = expireDate.toISOString();
142
- _saveCacheExpirations();
143
- }
144
-
145
- function _isKeyExpired(keyWithPrefix: string) {
146
- var expirations = _getCacheExpirations();
147
- if (expirations && expirations[keyWithPrefix]) {
148
- var now = new Date();
149
- var eDate = new Date(expirations[keyWithPrefix]);
150
- if (now > eDate) {
151
- try {
152
- delete expirations[keyWithPrefix];
153
- } catch (ex) {
154
- expirations[keyWithPrefix] = undefined; // undefined variables are removed when passed to JSON.stringify
155
- }
156
-
157
- _saveCacheExpirations();
158
- //has a date, it is expired.
159
- return true;
160
- }
161
- //has a date, it is not expired yet.
162
- return false;
163
- }
164
- //has no date or not in expirations at all - say it is expired...
165
- return true;
166
- }
167
-
168
- function _getItem(key: string) {
169
- try {
170
- return localStorage.getItem(key);
171
- } catch {
172
- }
173
- return null;
174
- }
175
-
176
- function _setItem(key: string, value: string) {
177
- try {
178
- localStorage.setItem(key, value);
179
- return true;
180
- } catch {
181
- }
182
- return false;
183
- }
184
-
185
- function _removeItem(key: string) {
186
- try {
187
- localStorage.removeItem(key);
188
- return true;
189
- } catch {
190
- }
191
- return false;
192
- }
193
-
194
- /**Get the size (KB) of all entries in local storage. Only returns the size for entries with kwizcom key prefix. */
195
- function _getStoredSize() {
196
- let keys = getCacheKeys();
197
- let total = 0;
198
- let length = 0;
199
- let useBlob = 'Blob' in (globalThis || window);
200
-
201
- keys.forEach((key) => {
202
- let v = _getItem(`${keyPrefix}${key}`);
203
- if (!isNullOrEmptyString(v)) {
204
- if (useBlob) {
205
- length = (new Blob([v + key])).size;
206
- } else {
207
- length = ((v.length + key.length) * 2);
208
- }
209
- }
210
- total += length;
211
- });
212
-
213
- return Number((total / 1024).toFixed(2));
214
- }
215
-
216
- export function isLocalStorageSupported() {
217
- if (_supportsLocalStorage !== null) {
218
- return _supportsLocalStorage;
219
- }
220
-
221
- var result;
222
- try {
223
- _setItem(LOCAL_STORAGE_PREFIX, LOCAL_STORAGE_PREFIX);
224
- result = _getItem(LOCAL_STORAGE_PREFIX) === LOCAL_STORAGE_PREFIX;
225
- _removeItem(LOCAL_STORAGE_PREFIX);
226
- _supportsLocalStorage = result;
227
- } catch (ex) {
228
- _supportsLocalStorage = false;
229
- }
230
-
231
- return _supportsLocalStorage;
232
- }
233
-
234
- //#region exported methods
235
- export function getCacheItem<T>(key: string, options?: {
236
- /** Flatted allows serizlising circular dependency objects */
237
- useFlatted?: boolean;
238
- }): T {
239
- key = key.toLowerCase();
240
- let keyWithPrefix = keyPrefix + key;
241
-
242
- let _cache = _getCache();
243
-
244
- if (typeof (_cache[key]) !== "undefined"
245
- && _cache[key] !== null) {
246
- let isExpired = _isKeyExpired(keyWithPrefix);
247
- if (!isExpired) {
248
- return _cache[key] as T;
249
- }
250
- //else remove it from cache
251
- removeCacheItem(key);
252
- }
253
-
254
- if (isLocalStorageSupported()) {
255
- var value: string = _getItem(keyWithPrefix);
256
- if (isNullOrUndefined(value)) {
257
- return null;
258
- }
259
-
260
- let isExpired = _isKeyExpired(keyWithPrefix);
261
-
262
- if (!isExpired) {
263
- let valueAsT: T = options && options.useFlatted ? flatted.parse(value) as T : jsonParse<T>(value);
264
- if (valueAsT !== null) {
265
- _cache[key] = valueAsT;
266
- return valueAsT;
267
- } else {
268
- _cache[key] = value;
269
- return value as any as T;
270
- }
271
- }
272
- //else remove it from cache
273
- removeCacheItem(key);
274
- }
275
-
276
- return null;
277
- }
278
-
279
- export function setCacheItem(key: string, value: any, expiration: number | ILocalStorageCacheLifetime | Date, options?: {
280
- /** Flatted allows serizlising circular dependency objects */
281
- useFlatted?: boolean;
282
- }): void {
283
- if (isLocalStorageSupported()) {
284
- key = key.toLowerCase();
285
- removeCacheItem(key);
286
-
287
- var val = null;
288
- try {
289
- if (options && options.useFlatted)
290
- val = flatted.stringify(value);
291
- else
292
- val = JSON.stringify(value);
293
- } catch (ex) {
294
- logger.debug(`Object cannot be stored in local storage: ${ex && ex.message || ex} ${key}`);
295
- return;//this put [object] in cache for me if object can't be stringified!
296
- }
297
-
298
- let keyWithPrefix = keyPrefix + key;
299
-
300
- var expireDate = _parseExpiration(expiration);
301
-
302
- let saved = _setItem(keyWithPrefix, val);
303
- if (saved) {
304
- _setCacheExpiration(keyWithPrefix, expireDate);
305
- }
306
-
307
- let _cache = _getCache();
308
-
309
- _cache[key] = value;
310
- }
311
- }
312
-
313
- export function removeCacheItem(keyNoPrefix: string) {
314
- keyNoPrefix = keyNoPrefix.toLowerCase();
315
- let _cache = _getCache();
316
- delete _cache[keyNoPrefix];
317
- let keyWithPrefix = keyPrefix + keyNoPrefix;
318
-
319
- if (isLocalStorageSupported()) {
320
- _removeItem(keyNoPrefix);//in case we have an old one
321
- _removeItem(keyWithPrefix);
322
- }
323
- }
324
-
325
- export function removeCacheItems(keys: string[]) {
326
- keys.forEach((key) => {
327
- removeCacheItem(key);
328
- });
329
- }
330
-
331
- export function getCacheKeys() {
332
- let keys: string[] = [];
333
-
334
- if (isLocalStorageSupported()) {
335
- keys = Object.keys(localStorage).filter((key) => {
336
- return key.startsWith(keyPrefix);
337
- }).map((key) => {
338
- return key.substring(keyPrefix.length);
339
- });
340
- }
341
-
342
- return keys;
343
- }
344
-
345
- /** remove expired cache keys created by this utility.
346
- * to remove all keys (non-expired too) send removeAll=true
347
- */
348
- function purgeCache(removeAll?: boolean) {
349
- if (!isLocalStorageSupported()) return;
350
-
351
- var cacheExpirationsKeys = [
352
- LOCAL_STORGAGE_EXPIRATIONS_KEY,
353
- "kwizcom-aplfe-caching-expirations", // old clean up
354
- "localStorageExpirations" // old clean up
355
- ];
356
-
357
- let now = new Date();
358
-
359
- let nonExpiredKeys: string[] = [];
360
-
361
- //get all expiration keys (key/expiration date/time)
362
- for (let j = 0; j < cacheExpirationsKeys.length; j++) {
363
- try {
364
- let expirations = null;
365
- let cacheExpirationsKey = cacheExpirationsKeys[j];
366
-
367
- let removeAllForKey = removeAll || cacheExpirationsKey !== LOCAL_STORGAGE_EXPIRATIONS_KEY;
368
-
369
- if (cacheExpirationsKey === "localStorageExpirations") {
370
- //old format - load expirations from this one as well
371
- expirations = _getItem(cacheExpirationsKey); // "key1^11/18/2011 5pm|key2^3/10/2012 3pm"
372
- if (expirations) {
373
- let arr = expirations.split("|"); // ["key1^11/18/2011 5pm","key2^3/10/2012 3pm"]
374
- for (let i = 0; i < arr.length; i++) {
375
- try {
376
- let key_expiration_format = arr[i]; // "key1^11/18/2011 5pm"
377
- let key = key_expiration_format.split("^")[0];
378
- //old keys - remove all, all the time
379
- _removeItem(key);//remove key from cache
380
- } catch (e) { }
381
- }
382
- }
383
- } else {
384
- //new format
385
- expirations = cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY ? _getCacheExpirations() : jsonParse(_getItem(cacheExpirationsKey));
386
- if (expirations) {
387
- let expirationKeys = Object.keys(expirations);
388
- logger.group(() => {
389
- expirationKeys.forEach(keyWithPrefix => {
390
- try {
391
- let shouldRemoveKey = removeAllForKey || !keyWithPrefix.startsWith(keyPrefix);
392
- if (!shouldRemoveKey) {
393
- //check specific key expiration
394
- let expirationDate = new Date(expirations[keyWithPrefix]);
395
- if (!isDate(expirationDate) || expirationDate < now) {
396
- shouldRemoveKey = true;
397
- delete expirations[keyWithPrefix];
398
- logger.info(`purging key ${keyWithPrefix}`);
399
- }
400
- else {
401
- nonExpiredKeys.push(keyWithPrefix);
402
- }
403
- }
404
- if (shouldRemoveKey) _removeItem(keyWithPrefix);
405
- } catch (e) {
406
- logger.warn(`failed to remove key ${keyWithPrefix}`);
407
- }
408
- });
409
- }, "Checking expired items", true);
410
- }
411
- }
412
-
413
- if (cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY)
414
- _saveCacheExpirations();
415
- else//older keys - just remove them.
416
- _removeItem(cacheExpirationsKey);
417
- } catch (e) {
418
- logger.warn(`something went terribly wrong ${e}`);
419
- }
420
- }
421
-
422
- logger.group(() => {
423
- logger.table(nonExpiredKeys);
424
- //cleanup orphans
425
- //loop on all keys
426
- //if stats with: jsr_, kwfs| or keyPrefix - and not in nonExpiredKeys, it is an orphan. Remove it.
427
- let localStorageKeys = Object.keys(localStorage);
428
- for (let keyIdx = 0; keyIdx < localStorageKeys.length; keyIdx++) {
429
- let key = localStorageKeys[keyIdx];
430
- if (key.startsWith("jsr_") || key.startsWith("kwfs|")) {
431
- logger.log(`removing old key ${key}`);
432
- _removeItem(key);//old key
433
- }
434
- else if (key.startsWith(keyPrefix) && !nonExpiredKeys.includes(key))//orphan!
435
- {
436
- logger.log(`removing orphan key ${key}`);
437
- _removeItem(key);
438
- }
439
- }
440
- }, "Expired keys", true);
441
- }
442
-
443
- /** cleanup - remove all local storage keys created by this utility */
444
- export function clearCache(): void {
445
- return purgeCache(true);
446
- }
447
- //#endregion