@kwiz/common 1.0.78 → 1.0.80
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/.github/workflows/npm-publish.yml +24 -0
- package/.madgerc +2 -2
- package/LICENSE +21 -21
- package/fix-folder-imports.js +26 -26
- package/lib/cjs/helpers/sharepoint.js +5 -1
- package/lib/cjs/helpers/sharepoint.js.map +1 -1
- package/lib/cjs/helpers/typecheckers.js +5 -1
- package/lib/cjs/helpers/typecheckers.js.map +1 -1
- package/lib/cjs/types/libs/msal.types.js +26 -26
- package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
- package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
- package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
- package/lib/esm/helpers/sharepoint.js +3 -0
- package/lib/esm/helpers/sharepoint.js.map +1 -1
- package/lib/esm/helpers/typecheckers.js +3 -0
- package/lib/esm/helpers/typecheckers.js.map +1 -1
- package/lib/esm/types/libs/msal.types.js +26 -26
- package/lib/esm/utils/sharepoint.rest/list.js +2 -2
- package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
- package/lib/esm/utils/sharepoint.rest/user.js +11 -11
- package/lib/types/helpers/sharepoint.d.ts +1 -0
- package/lib/types/helpers/typecheckers.d.ts +1 -0
- package/package.json +77 -77
- package/readme.md +17 -17
- package/src/_dependencies.ts +12 -12
- package/src/config.ts +17 -17
- package/src/helpers/Guid.ts +181 -181
- package/src/helpers/base64.ts +173 -173
- package/src/helpers/browser.test.js +13 -13
- package/src/helpers/browser.ts +1348 -1348
- package/src/helpers/browserinfo.ts +292 -292
- package/src/helpers/collections.base.test.js +25 -25
- package/src/helpers/collections.base.ts +437 -437
- package/src/helpers/collections.ts +107 -107
- package/src/helpers/color.ts +54 -54
- package/src/helpers/cookies.ts +59 -59
- package/src/helpers/date.test.js +119 -119
- package/src/helpers/date.ts +188 -188
- package/src/helpers/debug.ts +186 -186
- package/src/helpers/emails.ts +6 -6
- package/src/helpers/eval.ts +5 -5
- package/src/helpers/file.test.js +50 -50
- package/src/helpers/file.ts +58 -58
- package/src/helpers/flatted.ts +149 -149
- package/src/helpers/functions.ts +16 -16
- package/src/helpers/graph/calendar.types.ts +10 -10
- package/src/helpers/http.ts +69 -69
- package/src/helpers/images.ts +22 -22
- package/src/helpers/json.ts +38 -38
- package/src/helpers/md5.ts +189 -189
- package/src/helpers/objects.test.js +33 -33
- package/src/helpers/objects.ts +270 -270
- package/src/helpers/promises.test.js +37 -37
- package/src/helpers/promises.ts +165 -165
- package/src/helpers/random.ts +27 -27
- package/src/helpers/scheduler/scheduler.test.js +103 -103
- package/src/helpers/scheduler/scheduler.ts +131 -131
- package/src/helpers/sharepoint.ts +776 -772
- package/src/helpers/strings.test.js +101 -101
- package/src/helpers/strings.ts +317 -317
- package/src/helpers/typecheckers.test.js +34 -34
- package/src/helpers/typecheckers.ts +266 -262
- package/src/helpers/url.test.js +43 -43
- package/src/helpers/url.ts +207 -207
- package/src/helpers/urlhelper.ts +111 -111
- package/src/index.ts +6 -6
- package/src/types/auth.ts +54 -54
- package/src/types/common.types.ts +15 -15
- package/src/types/flatted.types.ts +59 -59
- package/src/types/globals.types.ts +6 -6
- package/src/types/graph/calendar.types.ts +80 -80
- package/src/types/knownscript.types.ts +18 -18
- package/src/types/libs/datajs.types.ts +28 -28
- package/src/types/libs/ics.types.ts +30 -30
- package/src/types/libs/msal.types.ts +49 -49
- package/src/types/locales.ts +124 -124
- package/src/types/localstoragecache.types.ts +8 -8
- package/src/types/location.types.ts +27 -27
- package/src/types/moment.ts +11 -11
- package/src/types/regex.types.ts +16 -16
- package/src/types/rest.types.ts +95 -95
- package/src/types/sharepoint.types.ts +1465 -1465
- package/src/types/sharepoint.utils.types.ts +287 -287
- package/src/utils/auth/common.ts +74 -74
- package/src/utils/auth/discovery.test.js +12 -12
- package/src/utils/auth/discovery.ts +132 -132
- package/src/utils/base64.ts +27 -27
- package/src/utils/consolelogger.ts +320 -320
- package/src/utils/date.ts +35 -35
- package/src/utils/emails.ts +24 -24
- package/src/utils/knownscript.ts +286 -286
- package/src/utils/localstoragecache.ts +441 -441
- package/src/utils/rest.ts +501 -501
- package/src/utils/script.ts +170 -170
- package/src/utils/sharepoint.rest/common.ts +154 -154
- package/src/utils/sharepoint.rest/date.ts +62 -62
- package/src/utils/sharepoint.rest/file.folder.ts +598 -598
- package/src/utils/sharepoint.rest/item.ts +547 -547
- package/src/utils/sharepoint.rest/list.ts +1388 -1388
- package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
- package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
- package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
- package/src/utils/sharepoint.rest/location.ts +141 -141
- package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
- package/src/utils/sharepoint.rest/user-search.ts +252 -252
- package/src/utils/sharepoint.rest/user.ts +491 -491
- package/src/utils/sharepoint.rest/web.ts +1384 -1384
- package/src/utils/sod.ts +194 -194
- package/lib/cjs/helpers/_dependencies.js +0 -21
- package/lib/cjs/helpers/_dependencies.js.map +0 -1
- package/lib/cjs/utils/_dependencies.js +0 -24
- package/lib/cjs/utils/_dependencies.js.map +0 -1
- package/lib/esm/helpers/_dependencies.js +0 -3
- package/lib/esm/helpers/_dependencies.js.map +0 -1
- package/lib/esm/utils/_dependencies.js +0 -4
- package/lib/esm/utils/_dependencies.js.map +0 -1
- package/lib/types/helpers/_dependencies.d.ts +0 -2
- package/lib/types/utils/_dependencies.d.ts +0 -3
|
@@ -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
|
+
}
|