@kwiz/common 1.0.107 → 1.0.108

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 (102) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/browser.js +9 -5
  6. package/lib/cjs/helpers/browser.js.map +1 -1
  7. package/lib/cjs/types/libs/msal.types.js +26 -26
  8. package/lib/cjs/types/libs/msal.types.js.map +1 -1
  9. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  10. package/lib/esm/helpers/browser.js +9 -5
  11. package/lib/esm/helpers/browser.js.map +1 -1
  12. package/lib/esm/types/libs/msal.types.js +26 -26
  13. package/lib/esm/types/libs/msal.types.js.map +1 -1
  14. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  15. package/lib/types/types/libs/msal.types.d.ts +8 -3
  16. package/package.json +81 -81
  17. package/readme.md +17 -17
  18. package/src/_dependencies.ts +12 -12
  19. package/src/config.ts +17 -17
  20. package/src/helpers/Guid.ts +181 -181
  21. package/src/helpers/base64.ts +173 -173
  22. package/src/helpers/browser.test.js +13 -13
  23. package/src/helpers/browser.ts +1448 -1448
  24. package/src/helpers/browserinfo.ts +292 -292
  25. package/src/helpers/collections.base.test.js +25 -25
  26. package/src/helpers/collections.base.ts +437 -437
  27. package/src/helpers/collections.ts +107 -107
  28. package/src/helpers/color.ts +54 -54
  29. package/src/helpers/cookies.ts +59 -59
  30. package/src/helpers/date.test.js +119 -119
  31. package/src/helpers/date.ts +188 -188
  32. package/src/helpers/debug.ts +186 -186
  33. package/src/helpers/diagrams.ts +43 -43
  34. package/src/helpers/emails.ts +6 -6
  35. package/src/helpers/eval.ts +5 -5
  36. package/src/helpers/file.test.js +50 -50
  37. package/src/helpers/file.ts +63 -63
  38. package/src/helpers/flatted.ts +149 -149
  39. package/src/helpers/functions.ts +16 -16
  40. package/src/helpers/graph/calendar.types.ts +10 -10
  41. package/src/helpers/http.ts +69 -69
  42. package/src/helpers/images.ts +22 -22
  43. package/src/helpers/json.ts +44 -44
  44. package/src/helpers/md5.ts +189 -189
  45. package/src/helpers/objects.test.js +33 -33
  46. package/src/helpers/objects.ts +274 -274
  47. package/src/helpers/promises.test.js +37 -37
  48. package/src/helpers/promises.ts +165 -165
  49. package/src/helpers/random.ts +27 -27
  50. package/src/helpers/scheduler/scheduler.test.js +103 -103
  51. package/src/helpers/scheduler/scheduler.ts +131 -131
  52. package/src/helpers/sharepoint.ts +785 -785
  53. package/src/helpers/strings.test.js +122 -122
  54. package/src/helpers/strings.ts +337 -337
  55. package/src/helpers/typecheckers.test.js +34 -34
  56. package/src/helpers/typecheckers.ts +266 -266
  57. package/src/helpers/url.test.js +43 -43
  58. package/src/helpers/url.ts +207 -207
  59. package/src/helpers/urlhelper.ts +111 -111
  60. package/src/index.ts +6 -6
  61. package/src/types/auth.ts +54 -54
  62. package/src/types/common.types.ts +15 -15
  63. package/src/types/flatted.types.ts +59 -59
  64. package/src/types/globals.types.ts +6 -6
  65. package/src/types/graph/calendar.types.ts +80 -80
  66. package/src/types/knownscript.types.ts +18 -18
  67. package/src/types/libs/datajs.types.ts +28 -28
  68. package/src/types/libs/ics.types.ts +30 -30
  69. package/src/types/libs/msal.types.ts +57 -49
  70. package/src/types/locales.ts +125 -125
  71. package/src/types/localstoragecache.types.ts +8 -8
  72. package/src/types/location.types.ts +27 -27
  73. package/src/types/moment.ts +11 -11
  74. package/src/types/regex.types.ts +16 -16
  75. package/src/types/rest.types.ts +95 -95
  76. package/src/types/sharepoint.types.ts +1466 -1466
  77. package/src/types/sharepoint.utils.types.ts +287 -287
  78. package/src/utils/auth/common.ts +74 -74
  79. package/src/utils/auth/discovery.test.js +12 -12
  80. package/src/utils/auth/discovery.ts +132 -132
  81. package/src/utils/base64.ts +27 -27
  82. package/src/utils/consolelogger.ts +320 -320
  83. package/src/utils/date.ts +172 -172
  84. package/src/utils/emails.ts +24 -24
  85. package/src/utils/knownscript.ts +286 -286
  86. package/src/utils/localstoragecache.ts +446 -446
  87. package/src/utils/rest.ts +501 -501
  88. package/src/utils/script.ts +170 -170
  89. package/src/utils/sharepoint.rest/common.ts +154 -154
  90. package/src/utils/sharepoint.rest/date.ts +62 -62
  91. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  92. package/src/utils/sharepoint.rest/item.ts +547 -547
  93. package/src/utils/sharepoint.rest/list.ts +1481 -1481
  94. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  95. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  96. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  97. package/src/utils/sharepoint.rest/location.ts +141 -141
  98. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  99. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  100. package/src/utils/sharepoint.rest/user.ts +491 -491
  101. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  102. package/src/utils/sod.ts +194 -194
@@ -1,275 +1,275 @@
1
- import { chunkArray } from "../../../helpers/collections.base";
2
- import { getGlobal, jsonClone } from "../../../helpers/objects";
3
- import { isBoolean, isNotEmptyArray, isNullOrEmptyArray, isNullOrUndefined, isNumber } from "../../../helpers/typecheckers";
4
- import { jsonTypes } from "../../../types/rest.types";
5
- import { IRestItem } from "../../../types/sharepoint.utils.types";
6
- import { GetJson, GetJsonSync } from "../../rest";
7
- import { GetListRestUrl } from "../list";
8
-
9
- function _getGlobalCache() {
10
- let _cache = getGlobal<{ getItemsByIdCache: { [cachekey: string]: IRestItem[]; }; }>("SharePoint_Rest_List_Cache", {
11
- getItemsByIdCache: {}
12
- });
13
- return _cache;
14
- }
15
-
16
- /** return array will use the item ID as indexer, not a real array */
17
- export async function GetItemsById<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
18
- expand?: string[];
19
- select?: string[];
20
- refreshCache?: boolean;
21
- jsonMetadata?: jsonTypes;
22
- }) {
23
- try {
24
- let baseParams = _parseItemsByIdParams<T>(siteUrl, listIdOrTitle, itemIds, { ...options, batchRequests: itemIds.length > 1 });
25
-
26
- const { results, allowCache, queue } = baseParams;
27
- let cacheKey = baseParams.cacheKey;
28
-
29
- if (options && !isNullOrUndefined(options.jsonMetadata)) {
30
- cacheKey += "|jsonMetadata=" + options.jsonMetadata
31
- }
32
-
33
- if (queue.length > 0) {
34
- let promises = queue.map(batchRequestUrl => {
35
- return GetJson<tGetItemsByIdResult<T>>(batchRequestUrl, null, {
36
- //do not allow cache. modern forms have apply option which will need this to reload the item
37
- //it is cached in _restListItems anyways so this is not needed.
38
- allowCache: allowCache === true,
39
- includeDigestInGet: true,
40
- jsonMetadata: options && options.jsonMetadata
41
- }).then(obj => {
42
- if (!isNullOrUndefined(obj)) {
43
- //no-metadata will return a value, as a single result or array
44
- //otherwise, it'll go into "d"
45
- let items: T[] = [];
46
-
47
- if (isNoMetaDataResult(obj)) {
48
- items = isNotEmptyArray(obj.value)
49
- ? obj.value
50
- : [obj.value];
51
- }
52
- else if (isVerboseResult(obj)) {
53
- items = Array.isArray(obj.d.results)
54
- ? obj.d.results
55
- : [obj.d];
56
- }
57
- else if (isSingleResult(obj)) {
58
- // Issue 1471: If only single item returns it not an array and just object
59
- items.push(obj);
60
- }
61
-
62
- items.forEach((restItem) => {
63
- results[Number(restItem.Id)] = restItem;
64
- _addCacheItem(cacheKey, restItem);
65
- });
66
- }
67
- });
68
- });
69
-
70
- await Promise.all(promises);
71
- }
72
-
73
- return results;
74
- } catch (e) {
75
- throw new Error("Could not retrieve rest item from list");
76
- }
77
- }
78
-
79
- export function GetItemsByIdSync<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
80
- expand?: string[];
81
- select?: string[];
82
- refreshCache?: boolean;
83
- jsonMetadata?: jsonTypes;
84
- }) {
85
- try {
86
- let baseParams = _parseItemsByIdParams<T>(siteUrl, listIdOrTitle, itemIds, { ...options, batchRequests: itemIds.length > 1 });
87
-
88
- const { results, allowCache, queue } = baseParams;
89
- let cacheKey = baseParams.cacheKey;
90
-
91
- if (options && !isNullOrUndefined(options.jsonMetadata)) {
92
- cacheKey += "|jsonMetadata=" + options.jsonMetadata
93
- }
94
-
95
- if (queue.length > 0) {
96
- queue.forEach((batchRequestUrl) => {
97
- let response = GetJsonSync<tGetItemsByIdResult<T>>(batchRequestUrl, null, {
98
- //do not allow cache. modern forms have apply option which will need this to reload the item
99
- //it is cached in _restListItems anyways so this is not needed.
100
- allowCache: allowCache === true,
101
- includeDigestInGet: true,
102
- jsonMetadata: options && options.jsonMetadata
103
- });
104
-
105
- if (response && response.success && response.result) {
106
- //no-metadata will return a value, as a single result or array
107
- //otherwise, it'll go into "d"
108
- let items: T[] = [];
109
- if (isNoMetaDataResult(response.result)) {
110
- items = isNotEmptyArray(response.result.value)
111
- ? response.result.value
112
- : [response.result.value];
113
- }
114
- else if (isVerboseResult(response.result)) {
115
- items = Array.isArray(response.result.d.results)
116
- ? response.result.d.results
117
- : [response.result.d];
118
- }
119
- else if (isSingleResult(response.result)) {
120
- // Issue 1471: If only single item returns it not an array and just object
121
- items.push(response.result);
122
- }
123
-
124
- items.forEach((restItem) => {
125
- results[Number(restItem.Id)] = restItem;
126
- _addCacheItem(cacheKey, restItem);
127
- });
128
- }
129
- });
130
- }
131
-
132
- return results;
133
- } catch (e) {
134
- throw new Error("Could not retrieve rest item from list");
135
- }
136
- }
137
-
138
- function _addCacheItem<T extends IRestItem>(cacheKey: string, item: T) {
139
- let g_cache = _getGlobalCache();
140
- if (isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey])) {
141
- g_cache.getItemsByIdCache[cacheKey] = [];
142
- }
143
- g_cache.getItemsByIdCache[cacheKey][item.Id] = jsonClone(item);
144
- }
145
-
146
- function _getCacheItem<T extends IRestItem>(cacheKey: string, itemId: number) {
147
- let g_cache = _getGlobalCache();
148
- if (isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey])) {
149
- g_cache.getItemsByIdCache[cacheKey] = [];
150
- }
151
-
152
- if (!isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey][Number(itemId)])) {
153
- return jsonClone(g_cache.getItemsByIdCache[cacheKey][Number(itemId)]) as T;
154
- }
155
-
156
- return null;
157
- }
158
-
159
- function _refreshCache(cacheKey: string) {
160
- let g_cache = _getGlobalCache();
161
- g_cache.getItemsByIdCache[cacheKey] = [];
162
- }
163
-
164
- function _getItemsByIdBaseUrl(siteUrl: string, listIdOrTitle: string) {
165
- return `${GetListRestUrl(siteUrl, listIdOrTitle)}/items`;
166
- }
167
-
168
- function _parseItemsByIdParams<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
169
- expand?: string[];
170
- select?: string[];
171
- refreshCache?: boolean;
172
- batchRequests?: boolean;
173
- }) {
174
- let baseUrl = _getItemsByIdBaseUrl(siteUrl, listIdOrTitle);
175
-
176
- let expand: string[] = [];
177
- let select: string[] = [];
178
- let allowCache = true;
179
- let results: T[] = [];
180
- let queue: string[] = [];
181
-
182
- if (!isNullOrUndefined(options)) {
183
- if (!isNullOrEmptyArray(options.select)) {
184
- select = options.select.sort();
185
- }
186
-
187
- if (!isNullOrEmptyArray(options.expand)) {
188
- expand = options.expand.sort();
189
- }
190
-
191
- if (isBoolean(options.refreshCache)) {
192
- allowCache = options.refreshCache !== true;
193
- }
194
- }
195
-
196
- let cacheKey = [baseUrl, select.join(",").toLowerCase(), expand.join(",").toLowerCase()].join("|");
197
-
198
- if (allowCache === true) {
199
- itemIds.forEach((itemId) => {
200
- let cachedItem = _getCacheItem<T>(cacheKey, itemId);
201
- if (!isNullOrUndefined(cachedItem)) {
202
- results[itemId] = cachedItem;
203
- }
204
- });
205
- } else {
206
- _refreshCache(cacheKey);
207
- }
208
-
209
- //remove item ids that were retrieved from cache
210
- itemIds = itemIds.filter((itemId) => {
211
- return !results.some((result) => {
212
- return Number(itemId) === Number(result.Id);
213
- });
214
- });
215
-
216
- if (itemIds.length !== 0) {
217
- let selectExpand: string[] = [];
218
-
219
- if (select.length) {
220
- selectExpand.push(`$select=${select.join(",")}`);
221
- }
222
- if (expand.length) {
223
- selectExpand.push(`$expand=${expand.join(",")}`);
224
- }
225
-
226
- let selectExpandQS = selectExpand.join("&");
227
-
228
- if (options.batchRequests === false) {
229
- itemIds.forEach((itemId) => {
230
- let getItemsRequestUrl = `${baseUrl}(${itemId})?${selectExpandQS}`;
231
- queue.push(getItemsRequestUrl);
232
- });
233
- } else {
234
- let chunks = chunkArray(itemIds, 60);
235
- for (var chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
236
- let chunk = chunks[chunkIndex];
237
-
238
- let filter = chunk.map((id) => {
239
- return `(ID eq ${id})`;
240
- }).join("or");
241
-
242
- let getItemsRequestUrl = `${baseUrl}?${[`$filter=${filter}`, selectExpandQS].join("&")}`;
243
- queue.push(getItemsRequestUrl);
244
- }
245
- }
246
- }
247
-
248
- return {
249
- results: results,
250
- allowCache: allowCache,
251
- queue: queue,
252
- cacheKey: cacheKey
253
- };
254
- }
255
-
256
- type tGetItemsByIdResult_Single<T> = T;
257
- type tGetItemsByIdResult_Verbose<T> = {
258
- d: T | {
259
- results: T[];
260
- }
261
- }
262
- type tGetItemsByIdResult_NoMetadata<T> = {
263
- value: T | T[]
264
- }
265
- type tGetItemsByIdResult<T> = tGetItemsByIdResult_Single<T> | tGetItemsByIdResult_Verbose<T> | tGetItemsByIdResult_NoMetadata<T>;
266
-
267
- function isVerboseResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_Verbose<T> {
268
- return !isNullOrUndefined((result as tGetItemsByIdResult_Verbose<T>).d);
269
- }
270
- function isNoMetaDataResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_NoMetadata<T> {
271
- return !isNullOrUndefined((result as tGetItemsByIdResult_NoMetadata<T>).value);
272
- }
273
- function isSingleResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_Single<T> {
274
- return isNumber((result as tGetItemsByIdResult_Single<T>).Id);
275
- }
1
+ import { chunkArray } from "../../../helpers/collections.base";
2
+ import { getGlobal, jsonClone } from "../../../helpers/objects";
3
+ import { isBoolean, isNotEmptyArray, isNullOrEmptyArray, isNullOrUndefined, isNumber } from "../../../helpers/typecheckers";
4
+ import { jsonTypes } from "../../../types/rest.types";
5
+ import { IRestItem } from "../../../types/sharepoint.utils.types";
6
+ import { GetJson, GetJsonSync } from "../../rest";
7
+ import { GetListRestUrl } from "../list";
8
+
9
+ function _getGlobalCache() {
10
+ let _cache = getGlobal<{ getItemsByIdCache: { [cachekey: string]: IRestItem[]; }; }>("SharePoint_Rest_List_Cache", {
11
+ getItemsByIdCache: {}
12
+ });
13
+ return _cache;
14
+ }
15
+
16
+ /** return array will use the item ID as indexer, not a real array */
17
+ export async function GetItemsById<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
18
+ expand?: string[];
19
+ select?: string[];
20
+ refreshCache?: boolean;
21
+ jsonMetadata?: jsonTypes;
22
+ }) {
23
+ try {
24
+ let baseParams = _parseItemsByIdParams<T>(siteUrl, listIdOrTitle, itemIds, { ...options, batchRequests: itemIds.length > 1 });
25
+
26
+ const { results, allowCache, queue } = baseParams;
27
+ let cacheKey = baseParams.cacheKey;
28
+
29
+ if (options && !isNullOrUndefined(options.jsonMetadata)) {
30
+ cacheKey += "|jsonMetadata=" + options.jsonMetadata
31
+ }
32
+
33
+ if (queue.length > 0) {
34
+ let promises = queue.map(batchRequestUrl => {
35
+ return GetJson<tGetItemsByIdResult<T>>(batchRequestUrl, null, {
36
+ //do not allow cache. modern forms have apply option which will need this to reload the item
37
+ //it is cached in _restListItems anyways so this is not needed.
38
+ allowCache: allowCache === true,
39
+ includeDigestInGet: true,
40
+ jsonMetadata: options && options.jsonMetadata
41
+ }).then(obj => {
42
+ if (!isNullOrUndefined(obj)) {
43
+ //no-metadata will return a value, as a single result or array
44
+ //otherwise, it'll go into "d"
45
+ let items: T[] = [];
46
+
47
+ if (isNoMetaDataResult(obj)) {
48
+ items = isNotEmptyArray(obj.value)
49
+ ? obj.value
50
+ : [obj.value];
51
+ }
52
+ else if (isVerboseResult(obj)) {
53
+ items = Array.isArray(obj.d.results)
54
+ ? obj.d.results
55
+ : [obj.d];
56
+ }
57
+ else if (isSingleResult(obj)) {
58
+ // Issue 1471: If only single item returns it not an array and just object
59
+ items.push(obj);
60
+ }
61
+
62
+ items.forEach((restItem) => {
63
+ results[Number(restItem.Id)] = restItem;
64
+ _addCacheItem(cacheKey, restItem);
65
+ });
66
+ }
67
+ });
68
+ });
69
+
70
+ await Promise.all(promises);
71
+ }
72
+
73
+ return results;
74
+ } catch (e) {
75
+ throw new Error("Could not retrieve rest item from list");
76
+ }
77
+ }
78
+
79
+ export function GetItemsByIdSync<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
80
+ expand?: string[];
81
+ select?: string[];
82
+ refreshCache?: boolean;
83
+ jsonMetadata?: jsonTypes;
84
+ }) {
85
+ try {
86
+ let baseParams = _parseItemsByIdParams<T>(siteUrl, listIdOrTitle, itemIds, { ...options, batchRequests: itemIds.length > 1 });
87
+
88
+ const { results, allowCache, queue } = baseParams;
89
+ let cacheKey = baseParams.cacheKey;
90
+
91
+ if (options && !isNullOrUndefined(options.jsonMetadata)) {
92
+ cacheKey += "|jsonMetadata=" + options.jsonMetadata
93
+ }
94
+
95
+ if (queue.length > 0) {
96
+ queue.forEach((batchRequestUrl) => {
97
+ let response = GetJsonSync<tGetItemsByIdResult<T>>(batchRequestUrl, null, {
98
+ //do not allow cache. modern forms have apply option which will need this to reload the item
99
+ //it is cached in _restListItems anyways so this is not needed.
100
+ allowCache: allowCache === true,
101
+ includeDigestInGet: true,
102
+ jsonMetadata: options && options.jsonMetadata
103
+ });
104
+
105
+ if (response && response.success && response.result) {
106
+ //no-metadata will return a value, as a single result or array
107
+ //otherwise, it'll go into "d"
108
+ let items: T[] = [];
109
+ if (isNoMetaDataResult(response.result)) {
110
+ items = isNotEmptyArray(response.result.value)
111
+ ? response.result.value
112
+ : [response.result.value];
113
+ }
114
+ else if (isVerboseResult(response.result)) {
115
+ items = Array.isArray(response.result.d.results)
116
+ ? response.result.d.results
117
+ : [response.result.d];
118
+ }
119
+ else if (isSingleResult(response.result)) {
120
+ // Issue 1471: If only single item returns it not an array and just object
121
+ items.push(response.result);
122
+ }
123
+
124
+ items.forEach((restItem) => {
125
+ results[Number(restItem.Id)] = restItem;
126
+ _addCacheItem(cacheKey, restItem);
127
+ });
128
+ }
129
+ });
130
+ }
131
+
132
+ return results;
133
+ } catch (e) {
134
+ throw new Error("Could not retrieve rest item from list");
135
+ }
136
+ }
137
+
138
+ function _addCacheItem<T extends IRestItem>(cacheKey: string, item: T) {
139
+ let g_cache = _getGlobalCache();
140
+ if (isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey])) {
141
+ g_cache.getItemsByIdCache[cacheKey] = [];
142
+ }
143
+ g_cache.getItemsByIdCache[cacheKey][item.Id] = jsonClone(item);
144
+ }
145
+
146
+ function _getCacheItem<T extends IRestItem>(cacheKey: string, itemId: number) {
147
+ let g_cache = _getGlobalCache();
148
+ if (isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey])) {
149
+ g_cache.getItemsByIdCache[cacheKey] = [];
150
+ }
151
+
152
+ if (!isNullOrUndefined(g_cache.getItemsByIdCache[cacheKey][Number(itemId)])) {
153
+ return jsonClone(g_cache.getItemsByIdCache[cacheKey][Number(itemId)]) as T;
154
+ }
155
+
156
+ return null;
157
+ }
158
+
159
+ function _refreshCache(cacheKey: string) {
160
+ let g_cache = _getGlobalCache();
161
+ g_cache.getItemsByIdCache[cacheKey] = [];
162
+ }
163
+
164
+ function _getItemsByIdBaseUrl(siteUrl: string, listIdOrTitle: string) {
165
+ return `${GetListRestUrl(siteUrl, listIdOrTitle)}/items`;
166
+ }
167
+
168
+ function _parseItemsByIdParams<T extends IRestItem>(siteUrl: string, listIdOrTitle: string, itemIds: number[], options?: {
169
+ expand?: string[];
170
+ select?: string[];
171
+ refreshCache?: boolean;
172
+ batchRequests?: boolean;
173
+ }) {
174
+ let baseUrl = _getItemsByIdBaseUrl(siteUrl, listIdOrTitle);
175
+
176
+ let expand: string[] = [];
177
+ let select: string[] = [];
178
+ let allowCache = true;
179
+ let results: T[] = [];
180
+ let queue: string[] = [];
181
+
182
+ if (!isNullOrUndefined(options)) {
183
+ if (!isNullOrEmptyArray(options.select)) {
184
+ select = options.select.sort();
185
+ }
186
+
187
+ if (!isNullOrEmptyArray(options.expand)) {
188
+ expand = options.expand.sort();
189
+ }
190
+
191
+ if (isBoolean(options.refreshCache)) {
192
+ allowCache = options.refreshCache !== true;
193
+ }
194
+ }
195
+
196
+ let cacheKey = [baseUrl, select.join(",").toLowerCase(), expand.join(",").toLowerCase()].join("|");
197
+
198
+ if (allowCache === true) {
199
+ itemIds.forEach((itemId) => {
200
+ let cachedItem = _getCacheItem<T>(cacheKey, itemId);
201
+ if (!isNullOrUndefined(cachedItem)) {
202
+ results[itemId] = cachedItem;
203
+ }
204
+ });
205
+ } else {
206
+ _refreshCache(cacheKey);
207
+ }
208
+
209
+ //remove item ids that were retrieved from cache
210
+ itemIds = itemIds.filter((itemId) => {
211
+ return !results.some((result) => {
212
+ return Number(itemId) === Number(result.Id);
213
+ });
214
+ });
215
+
216
+ if (itemIds.length !== 0) {
217
+ let selectExpand: string[] = [];
218
+
219
+ if (select.length) {
220
+ selectExpand.push(`$select=${select.join(",")}`);
221
+ }
222
+ if (expand.length) {
223
+ selectExpand.push(`$expand=${expand.join(",")}`);
224
+ }
225
+
226
+ let selectExpandQS = selectExpand.join("&");
227
+
228
+ if (options.batchRequests === false) {
229
+ itemIds.forEach((itemId) => {
230
+ let getItemsRequestUrl = `${baseUrl}(${itemId})?${selectExpandQS}`;
231
+ queue.push(getItemsRequestUrl);
232
+ });
233
+ } else {
234
+ let chunks = chunkArray(itemIds, 60);
235
+ for (var chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
236
+ let chunk = chunks[chunkIndex];
237
+
238
+ let filter = chunk.map((id) => {
239
+ return `(ID eq ${id})`;
240
+ }).join("or");
241
+
242
+ let getItemsRequestUrl = `${baseUrl}?${[`$filter=${filter}`, selectExpandQS].join("&")}`;
243
+ queue.push(getItemsRequestUrl);
244
+ }
245
+ }
246
+ }
247
+
248
+ return {
249
+ results: results,
250
+ allowCache: allowCache,
251
+ queue: queue,
252
+ cacheKey: cacheKey
253
+ };
254
+ }
255
+
256
+ type tGetItemsByIdResult_Single<T> = T;
257
+ type tGetItemsByIdResult_Verbose<T> = {
258
+ d: T | {
259
+ results: T[];
260
+ }
261
+ }
262
+ type tGetItemsByIdResult_NoMetadata<T> = {
263
+ value: T | T[]
264
+ }
265
+ type tGetItemsByIdResult<T> = tGetItemsByIdResult_Single<T> | tGetItemsByIdResult_Verbose<T> | tGetItemsByIdResult_NoMetadata<T>;
266
+
267
+ function isVerboseResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_Verbose<T> {
268
+ return !isNullOrUndefined((result as tGetItemsByIdResult_Verbose<T>).d);
269
+ }
270
+ function isNoMetaDataResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_NoMetadata<T> {
271
+ return !isNullOrUndefined((result as tGetItemsByIdResult_NoMetadata<T>).value);
272
+ }
273
+ function isSingleResult<T extends IRestItem>(result: tGetItemsByIdResult<T>): result is tGetItemsByIdResult_Single<T> {
274
+ return isNumber((result as tGetItemsByIdResult_Single<T>).Id);
275
+ }