@lindle/sharepoint_requests 0.1.19 → 0.1.20

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 (49) hide show
  1. package/dist/index.d.mts +469 -0
  2. package/dist/index.d.ts +460 -8
  3. package/dist/index.js +892 -5
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +863 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +5 -5
  8. package/src/root/index.ts +29 -9
  9. package/src/root/instance.ts +7 -0
  10. package/src/root/internal/context.ts +0 -1
  11. package/src/root/internal/digest.ts +16 -10
  12. package/src/root/internal/emailRequests.ts +2 -5
  13. package/src/root/internal/listRequests/createAttachment.ts +2 -2
  14. package/src/root/internal/listRequests/createListItem.ts +3 -18
  15. package/src/root/internal/listRequests/deleteFile.ts +1 -2
  16. package/src/root/internal/listRequests/deleteItem.ts +1 -1
  17. package/src/root/internal/listRequests/getListItems.ts +34 -7
  18. package/src/root/internal/listRequests/resolveMetadataType.ts +14 -0
  19. package/src/root/internal/listRequests/updateListItem.ts +3 -19
  20. package/src/root/internal/listRequests/uploadFile.ts +8 -13
  21. package/src/root/internal/userRequests.ts +52 -28
  22. package/src/types.ts +3 -2
  23. package/dist/root/index.d.ts +0 -163
  24. package/dist/root/instance.d.ts +0 -3
  25. package/dist/root/internal/context.d.ts +0 -11
  26. package/dist/root/internal/digest.d.ts +0 -2
  27. package/dist/root/internal/emailRequests.d.ts +0 -10
  28. package/dist/root/internal/listRequests/createAttachment.d.ts +0 -3
  29. package/dist/root/internal/listRequests/createListItem.d.ts +0 -3
  30. package/dist/root/internal/listRequests/deleteFile.d.ts +0 -3
  31. package/dist/root/internal/listRequests/deleteItem.d.ts +0 -3
  32. package/dist/root/internal/listRequests/getFiles.d.ts +0 -8
  33. package/dist/root/internal/listRequests/getListItems.d.ts +0 -8
  34. package/dist/root/internal/listRequests/getOnly.d.ts +0 -3
  35. package/dist/root/internal/listRequests/index.d.ts +0 -9
  36. package/dist/root/internal/listRequests/normalizePayload.d.ts +0 -4
  37. package/dist/root/internal/listRequests/updateListItem.d.ts +0 -3
  38. package/dist/root/internal/listRequests/uploadFile.d.ts +0 -6
  39. package/dist/root/internal/listRequests/utils.d.ts +0 -2
  40. package/dist/root/internal/odata.d.ts +0 -5
  41. package/dist/root/internal/sharepointUrl.d.ts +0 -2
  42. package/dist/root/internal/userRequests.d.ts +0 -15
  43. package/dist/sharepoint_requests.cjs.development.js +0 -1497
  44. package/dist/sharepoint_requests.cjs.development.js.map +0 -1
  45. package/dist/sharepoint_requests.cjs.production.min.js +0 -2
  46. package/dist/sharepoint_requests.cjs.production.min.js.map +0 -1
  47. package/dist/sharepoint_requests.esm.js +0 -1490
  48. package/dist/sharepoint_requests.esm.js.map +0 -1
  49. package/dist/types.d.ts +0 -260
package/dist/index.mjs ADDED
@@ -0,0 +1,863 @@
1
+ // src/root/instance.ts
2
+ import axios, { AxiosHeaders } from "axios";
3
+ var defaultConfig = {
4
+ headers: {
5
+ Accept: "application/json; odata=verbose",
6
+ "Content-Type": "application/json; odata=verbose"
7
+ },
8
+ timeout: 15e3,
9
+ withCredentials: true,
10
+ paramsSerializer: {
11
+ serialize: (params) => Object.entries(params).filter(([, v]) => v !== void 0 && v !== null).map(([k, v]) => `${k}=${String(v)}`).join("&")
12
+ }
13
+ };
14
+ var instance = (baseURL, config) => {
15
+ const userHeaders = (config == null ? void 0 : config.headers) instanceof AxiosHeaders ? config.headers.toJSON() : (config == null ? void 0 : config.headers) ?? {};
16
+ return axios.create({
17
+ ...defaultConfig,
18
+ ...config,
19
+ baseURL,
20
+ headers: {
21
+ ...defaultConfig.headers,
22
+ ...userHeaders
23
+ }
24
+ });
25
+ };
26
+ var instance_default = instance;
27
+
28
+ // src/root/internal/digest.ts
29
+ var cache = /* @__PURE__ */ new WeakMap();
30
+ async function fetchDigest(instance2) {
31
+ var _a, _b;
32
+ const cached = cache.get(instance2);
33
+ if (cached && Date.now() < cached.expiresAt) {
34
+ return cached.value;
35
+ }
36
+ try {
37
+ const response = await instance2.post("_api/contextinfo");
38
+ const info = (_b = (_a = response.data) == null ? void 0 : _a.d) == null ? void 0 : _b.GetContextWebInformation;
39
+ const formDigestValue = info == null ? void 0 : info.FormDigestValue;
40
+ if (!formDigestValue) {
41
+ throw new Error("Invalid response structure: Missing FormDigestValue");
42
+ }
43
+ const ttl = ((info == null ? void 0 : info.FormDigestTimeoutSeconds) ?? 1800) - 60;
44
+ cache.set(instance2, { value: formDigestValue, expiresAt: Date.now() + ttl * 1e3 });
45
+ return formDigestValue;
46
+ } catch (error) {
47
+ const cause = error instanceof Error ? error.message : "Unknown contextinfo error";
48
+ throw new Error(`Error retrieving Form Digest Value: ${cause}`);
49
+ }
50
+ }
51
+
52
+ // src/root/internal/emailRequests.ts
53
+ async function sendEmail(context, { From, To, Subject, Body }) {
54
+ const response = await context.instance({
55
+ url: "_api/SP.Utilities.Utility.SendEmail",
56
+ method: "POST",
57
+ headers: {
58
+ "X-RequestDigest": await fetchDigest(context.instance),
59
+ "Content-Type": "application/json;odata=verbose"
60
+ },
61
+ data: JSON.stringify({
62
+ properties: {
63
+ __metadata: {
64
+ type: "SP.Utilities.EmailProperties"
65
+ },
66
+ From,
67
+ To: {
68
+ results: Array.isArray(To) ? To : [To]
69
+ },
70
+ Body,
71
+ Subject
72
+ }
73
+ })
74
+ });
75
+ return response;
76
+ }
77
+
78
+ // src/root/internal/listRequests/normalizePayload.ts
79
+ var LOOKUP_ID_KEYS = [
80
+ "lookupId",
81
+ "LookupId",
82
+ "lookupID",
83
+ "LookupID",
84
+ "Id",
85
+ "ID"
86
+ ];
87
+ function isPlainObject(value) {
88
+ return !!value && typeof value === "object" && !Array.isArray(value);
89
+ }
90
+ function extractLookupPrimitive(value, allowGenericId) {
91
+ for (const key of LOOKUP_ID_KEYS) {
92
+ if (!allowGenericId && (key === "Id" || key === "ID")) {
93
+ continue;
94
+ }
95
+ if (value[key] !== void 0 && value[key] !== null) {
96
+ return value[key];
97
+ }
98
+ }
99
+ return void 0;
100
+ }
101
+ function normalizeLookupValue(value) {
102
+ if (Array.isArray(value)) {
103
+ return { results: value };
104
+ }
105
+ if (isPlainObject(value)) {
106
+ if (Array.isArray(value.results)) {
107
+ return { results: value.results };
108
+ }
109
+ const primitive = extractLookupPrimitive(value, true);
110
+ if (primitive !== void 0) {
111
+ return primitive;
112
+ }
113
+ }
114
+ if (typeof value === "string") {
115
+ const trimmed = value.trim();
116
+ if (!trimmed) {
117
+ return value;
118
+ }
119
+ const numeric = Number(trimmed);
120
+ return Number.isNaN(numeric) ? trimmed : numeric;
121
+ }
122
+ return value;
123
+ }
124
+ function resolveLookupField(key, value) {
125
+ const lower = key.toLowerCase();
126
+ const endsWithId = lower.endsWith("id");
127
+ const endsWithRef = lower.endsWith("ref");
128
+ const endsWithLookup = lower.endsWith("lookup");
129
+ const hasLookupHint = endsWithId || endsWithRef || endsWithLookup;
130
+ if (value === void 0) {
131
+ return null;
132
+ }
133
+ if (Array.isArray(value)) {
134
+ const targetKey2 = endsWithId ? key : `${key}Id`;
135
+ return {
136
+ key: targetKey2,
137
+ value: { results: value }
138
+ };
139
+ }
140
+ if (isPlainObject(value)) {
141
+ const primitive = extractLookupPrimitive(value, hasLookupHint);
142
+ if (primitive !== void 0) {
143
+ const targetKey2 = endsWithId ? key : `${key}Id`;
144
+ return {
145
+ key: targetKey2,
146
+ value: normalizeLookupValue(primitive)
147
+ };
148
+ }
149
+ if (Array.isArray(value.results)) {
150
+ const targetKey2 = endsWithId ? key : `${key}Id`;
151
+ return {
152
+ key: targetKey2,
153
+ value: { results: value.results }
154
+ };
155
+ }
156
+ if (!hasLookupHint) {
157
+ return null;
158
+ }
159
+ } else if (!hasLookupHint) {
160
+ return null;
161
+ }
162
+ const targetKey = endsWithId ? key : `${key}Id`;
163
+ if (value === null) {
164
+ return {
165
+ key: targetKey,
166
+ value: null
167
+ };
168
+ }
169
+ if (typeof value === "number") {
170
+ return {
171
+ key: targetKey,
172
+ value
173
+ };
174
+ }
175
+ if (typeof value === "string") {
176
+ const trimmed = value.trim();
177
+ if (!trimmed) {
178
+ return {
179
+ key: targetKey,
180
+ value: ""
181
+ };
182
+ }
183
+ const numeric = Number(trimmed);
184
+ return {
185
+ key: targetKey,
186
+ value: Number.isNaN(numeric) ? trimmed : numeric
187
+ };
188
+ }
189
+ return null;
190
+ }
191
+ function normalizeListPayload(data) {
192
+ const result = {};
193
+ if (!data) {
194
+ return result;
195
+ }
196
+ for (const [key, value] of Object.entries(data)) {
197
+ if (key === "__metadata") {
198
+ continue;
199
+ }
200
+ const lookupField = resolveLookupField(key, value);
201
+ if (lookupField) {
202
+ result[lookupField.key] = lookupField.value;
203
+ continue;
204
+ }
205
+ if (value !== void 0) {
206
+ result[key] = value;
207
+ }
208
+ }
209
+ return result;
210
+ }
211
+
212
+ // src/root/internal/listRequests/resolveMetadataType.ts
213
+ async function resolveMetadataType(context) {
214
+ if (context.resolveListItemEntityType) {
215
+ return context.resolveListItemEntityType();
216
+ }
217
+ if (context.listItemEntityTypeFullName) {
218
+ return context.listItemEntityTypeFullName;
219
+ }
220
+ return `SP.Data.${context.listName}ListItem`;
221
+ }
222
+
223
+ // src/root/internal/listRequests/createListItem.ts
224
+ async function createListItem(context, listData) {
225
+ const normalizedData = normalizeListPayload(listData);
226
+ let payload = normalizedData;
227
+ if (context.sharepointVersion === "2013") {
228
+ const metadataType = await resolveMetadataType(context);
229
+ const userMetadata = listData.__metadata;
230
+ payload = {
231
+ ...normalizedData,
232
+ __metadata: {
233
+ ...userMetadata && typeof userMetadata === "object" ? userMetadata : {},
234
+ type: metadataType
235
+ }
236
+ };
237
+ }
238
+ const response = await context.instance({
239
+ url: context.endpoint,
240
+ method: "POST",
241
+ data: JSON.stringify(payload),
242
+ headers: {
243
+ "X-RequestDigest": await fetchDigest(context.instance)
244
+ }
245
+ });
246
+ return response;
247
+ }
248
+
249
+ // src/root/internal/sharepointUrl.ts
250
+ function escapeODataStringLiteral(value) {
251
+ return value.replace(/'/g, "''");
252
+ }
253
+ function buildListByTitleEndpoint(listName) {
254
+ return `_api/web/lists/GetByTitle('${escapeODataStringLiteral(listName)}')`;
255
+ }
256
+
257
+ // src/root/internal/listRequests/utils.ts
258
+ function buildLibraryPath(folderPath) {
259
+ if (!folderPath) {
260
+ return "";
261
+ }
262
+ const segments = Array.isArray(folderPath) ? folderPath : folderPath.split("/").filter(Boolean);
263
+ if (!segments.length) {
264
+ return "";
265
+ }
266
+ const escapedSegments = segments.map((segment) => {
267
+ if (!segment.trim()) {
268
+ throw new Error("Folder path segments cannot be empty.");
269
+ }
270
+ const sanitized = segment.replace(/'/g, "''");
271
+ return `/Folders('${encodeURIComponent(sanitized)}')`;
272
+ });
273
+ return escapedSegments.join("");
274
+ }
275
+ function encodeFileName(fileName) {
276
+ if (!fileName.trim()) {
277
+ throw new Error("File name cannot be empty.");
278
+ }
279
+ return encodeURIComponent(fileName.replace(/'/g, "''"));
280
+ }
281
+
282
+ // src/root/internal/listRequests/createAttachment.ts
283
+ async function createAttachment(context, { file, itemId }) {
284
+ if (context.sharepointVersion !== "2013") {
285
+ throw new Error(
286
+ "createAttachment is only supported for SharePoint 2013 REST endpoints."
287
+ );
288
+ }
289
+ const encodedFileName = encodeFileName(file.name);
290
+ const requestUrl = `${buildListByTitleEndpoint(
291
+ context.listName
292
+ )}/items(${itemId})/AttachmentFiles/add(FileName='${encodedFileName}')`;
293
+ const response = await context.instance.post(requestUrl, file, {
294
+ headers: {
295
+ "X-RequestDigest": await fetchDigest(context.instance)
296
+ }
297
+ });
298
+ return response;
299
+ }
300
+
301
+ // src/root/internal/listRequests/deleteItem.ts
302
+ async function deleteItem(context, id) {
303
+ const requestDigest = await fetchDigest(context.instance);
304
+ const url = context.sharepointVersion === "2013" ? `${buildListByTitleEndpoint(context.listName)}/items(${id})` : `${context.endpoint}(${id})`;
305
+ const response = await context.instance.post(url, void 0, {
306
+ headers: {
307
+ "X-RequestDigest": requestDigest,
308
+ "IF-MATCH": "*",
309
+ "X-HTTP-Method": "DELETE"
310
+ }
311
+ });
312
+ return response;
313
+ }
314
+
315
+ // src/root/internal/listRequests/deleteFile.ts
316
+ var SUPPORTED_VERSION = "2013";
317
+ async function deleteFile(context, { fileName, folderPath }) {
318
+ if (context.sharepointVersion !== SUPPORTED_VERSION) {
319
+ throw new Error(
320
+ "deleteFile is only supported for SharePoint 2013 REST endpoints."
321
+ );
322
+ }
323
+ const encodedFileName = encodeFileName(fileName);
324
+ let requestUrl = `${buildListByTitleEndpoint(context.listName)}/RootFolder`;
325
+ requestUrl += buildLibraryPath(folderPath);
326
+ requestUrl += `/Files('${encodedFileName}')`;
327
+ const digest = await fetchDigest(context.instance);
328
+ const response = await context.instance.post(requestUrl, void 0, {
329
+ headers: {
330
+ "X-HTTP-Method": "DELETE",
331
+ "IF-MATCH": "*",
332
+ "X-RequestDigest": digest
333
+ }
334
+ });
335
+ return response;
336
+ }
337
+
338
+ // src/root/internal/odata.ts
339
+ function buildODataParams(options, orderBySeparator = ",") {
340
+ const { expand, orderBy, limit, filter, cols, skip } = options || {};
341
+ const params = {
342
+ $expand: Array.isArray(expand) ? expand.join(",") : expand,
343
+ $top: limit,
344
+ $skip: skip,
345
+ $select: Array.isArray(cols) ? cols.join(",") : cols,
346
+ $filter: filter,
347
+ $orderby: Array.isArray(orderBy) ? orderBy.join(orderBySeparator) : orderBy
348
+ };
349
+ return params;
350
+ }
351
+ function appendParamsToUrl(baseUrl, params) {
352
+ Object.entries(params).forEach(([key, value]) => {
353
+ if (value !== void 0 && value !== null) {
354
+ baseUrl.searchParams.append(key, String(value));
355
+ }
356
+ });
357
+ }
358
+
359
+ // src/root/internal/listRequests/getFiles.ts
360
+ async function getFiles(context, options) {
361
+ var _a, _b, _c;
362
+ const params = buildODataParams(options, ",");
363
+ const url = new URL(context.endpoint, context.instance.defaults.baseURL);
364
+ appendParamsToUrl(url, params);
365
+ const response = await context.instance.request({
366
+ url: context.endpoint,
367
+ method: "GET",
368
+ params
369
+ });
370
+ const data = ((_b = (_a = response.data) == null ? void 0 : _a.d) == null ? void 0 : _b.results) ?? ((_c = response.data) == null ? void 0 : _c.d) ?? response.data;
371
+ const meta = { url };
372
+ return { data, meta };
373
+ }
374
+
375
+ // src/root/internal/listRequests/getListItems.ts
376
+ async function getListItems(context, options) {
377
+ var _a, _b, _c;
378
+ const params = buildODataParams(options, " ");
379
+ const url = new URL(context.endpoint, context.instance.defaults.baseURL);
380
+ appendParamsToUrl(url, params);
381
+ const response = await context.instance.request({
382
+ url: context.endpoint,
383
+ method: "GET",
384
+ params
385
+ });
386
+ const rawData = ((_b = (_a = response.data) == null ? void 0 : _a.d) == null ? void 0 : _b.results) ?? ((_c = response.data) == null ? void 0 : _c.d) ?? response.data;
387
+ const cols = options == null ? void 0 : options.cols;
388
+ const keys = cols ? (Array.isArray(cols) ? cols : [cols]).filter(
389
+ (key) => typeof key === "string"
390
+ ) : void 0;
391
+ const filterItem = (item) => {
392
+ if (!keys || !item || typeof item !== "object") {
393
+ return item;
394
+ }
395
+ return keys.reduce((acc, key) => {
396
+ if (key in item) {
397
+ acc[key] = item[key];
398
+ }
399
+ return acc;
400
+ }, {});
401
+ };
402
+ const data = Array.isArray(rawData) ? rawData.map(filterItem) : filterItem(rawData);
403
+ const meta = { url };
404
+ return {
405
+ data,
406
+ meta
407
+ };
408
+ }
409
+
410
+ // src/root/internal/listRequests/getOnly.ts
411
+ async function getOnly(instance2, endpoint, options) {
412
+ const params = buildODataParams(options, ",");
413
+ const { data } = await instance2.get(endpoint, { params });
414
+ if (!data) {
415
+ return void 0;
416
+ }
417
+ if (data.d) {
418
+ return data.d.results ? data.d.results : data.d;
419
+ }
420
+ return data;
421
+ }
422
+
423
+ // src/root/internal/listRequests/updateListItem.ts
424
+ async function updateListItem(context, id, data, digest) {
425
+ const formDigestValue = digest ?? await fetchDigest(context.instance);
426
+ const normalizedData = normalizeListPayload(data);
427
+ let payload = normalizedData;
428
+ if (context.sharepointVersion === "2013") {
429
+ const metadataType = await resolveMetadataType(context);
430
+ const userMetadata = data.__metadata;
431
+ payload = {
432
+ ...normalizedData,
433
+ __metadata: {
434
+ ...userMetadata && typeof userMetadata === "object" ? userMetadata : {},
435
+ type: metadataType
436
+ }
437
+ };
438
+ }
439
+ const url = context.sharepointVersion === "2013" ? `${buildListByTitleEndpoint(context.listName)}/items(${id})` : `${context.endpoint}(${id})`;
440
+ const response = await context.instance({
441
+ url,
442
+ method: "POST",
443
+ data: JSON.stringify(payload),
444
+ headers: {
445
+ "X-RequestDigest": formDigestValue,
446
+ "IF-MATCH": "*",
447
+ "X-HTTP-Method": "MERGE"
448
+ }
449
+ });
450
+ return response;
451
+ }
452
+
453
+ // src/root/internal/listRequests/uploadFile.ts
454
+ var SUPPORTED_VERSION2 = "2013";
455
+ async function uploadFile(context, { file, folderPath, overwrite = true }) {
456
+ var _a, _b, _c, _d, _e;
457
+ if (context.sharepointVersion !== SUPPORTED_VERSION2) {
458
+ throw new Error(
459
+ "uploadFile is only supported for SharePoint 2013 REST endpoints."
460
+ );
461
+ }
462
+ if (!(file == null ? void 0 : file.name)) {
463
+ throw new Error("A file with a valid name must be provided.");
464
+ }
465
+ let requestUrl = `${buildListByTitleEndpoint(context.listName)}/RootFolder`;
466
+ requestUrl += buildLibraryPath(folderPath);
467
+ const encodedFileName = encodeFileName(file.name);
468
+ requestUrl += `/Files/add(url='${encodedFileName}',overwrite=${overwrite ? "true" : "false"})?$expand=ListItemAllFields&$select=ListItemAllFields/Id`;
469
+ const digest = await fetchDigest(context.instance);
470
+ const response = await context.instance.post(requestUrl, file, {
471
+ headers: {
472
+ "Content-Type": file.type || "application/octet-stream",
473
+ "X-RequestDigest": digest
474
+ }
475
+ });
476
+ const responseData = ((_a = response.data) == null ? void 0 : _a.d) ?? response.data;
477
+ const listItemFields = responseData == null ? void 0 : responseData.ListItemAllFields;
478
+ let itemId = (listItemFields == null ? void 0 : listItemFields.Id) ?? (listItemFields == null ? void 0 : listItemFields.ID) ?? (responseData == null ? void 0 : responseData.Id) ?? (responseData == null ? void 0 : responseData.ID);
479
+ if (typeof itemId === "undefined" && ((_b = listItemFields == null ? void 0 : listItemFields.__deferred) == null ? void 0 : _b.uri)) {
480
+ const listItemResponse = await context.instance.get(
481
+ listItemFields.__deferred.uri
482
+ );
483
+ const deferredId = ((_d = (_c = listItemResponse.data) == null ? void 0 : _c.d) == null ? void 0 : _d.Id) ?? ((_e = listItemResponse.data) == null ? void 0 : _e.Id);
484
+ if (typeof deferredId !== "undefined") {
485
+ itemId = deferredId;
486
+ }
487
+ }
488
+ return { response, itemId };
489
+ }
490
+
491
+ // src/root/internal/userRequests.ts
492
+ var PERMISSION_PRIORITY = [
493
+ "Full Control",
494
+ "Edit",
495
+ "Contribute",
496
+ "Read"
497
+ ];
498
+ async function typeAhead(instance2, input, filter) {
499
+ var _a, _b, _c;
500
+ const requestUrl = `_api/SP.UI.ApplicationPages.ClientPeoplePickerWebServiceInterface.clientPeoplePickerSearchUser`;
501
+ const data = {
502
+ queryParams: {
503
+ __metadata: {
504
+ type: "SP.UI.ApplicationPages.ClientPeoplePickerQueryParameters"
505
+ },
506
+ AllowEmailAddresses: true,
507
+ AllowMultipleEntities: false,
508
+ AllUrlZones: false,
509
+ MaximumEntitySuggestions: 10,
510
+ PrincipalSource: 15,
511
+ PrincipalType: 1,
512
+ QueryString: input
513
+ }
514
+ };
515
+ if (filter) {
516
+ data.queryParams.RequiredProperties = [filter];
517
+ }
518
+ const response = await instance2.post(requestUrl, JSON.stringify(data), {
519
+ headers: {
520
+ Accept: "application/json; odata=verbose",
521
+ "Content-Type": "application/json;odata=verbose"
522
+ }
523
+ });
524
+ const searchResults = ((_b = (_a = response.data) == null ? void 0 : _a.d) == null ? void 0 : _b.ClientPeoplePickerSearchUser) ?? ((_c = response.data) == null ? void 0 : _c.ClientPeoplePickerSearchUser);
525
+ if (typeof searchResults === "string") {
526
+ const parsed = JSON.parse(searchResults);
527
+ if (Array.isArray(parsed) && parsed.length > 0) {
528
+ return parsed[0];
529
+ }
530
+ throw new Error("People picker returned no results.");
531
+ }
532
+ if (Array.isArray(searchResults) && searchResults.length > 0) {
533
+ return searchResults[0];
534
+ }
535
+ if (searchResults && typeof searchResults === "object") {
536
+ return searchResults;
537
+ }
538
+ throw new Error("Invalid people picker response.");
539
+ }
540
+ async function currentUserProperties(instance2) {
541
+ var _a, _b, _c;
542
+ const requestUrl = "_api/sp.userprofiles.peoplemanager/getmyproperties";
543
+ const response = await instance2.get(requestUrl);
544
+ const profile = ((_a = response.data) == null ? void 0 : _a.d) ?? response.data;
545
+ if (!profile) {
546
+ throw new Error("Unable to retrieve user profile.");
547
+ }
548
+ (_c = (_b = profile.UserProfileProperties) == null ? void 0 : _b.results) == null ? void 0 : _c.forEach(
549
+ (prop) => {
550
+ if (prop.Key === "FirstName") profile.FirstName = prop.Value;
551
+ else if (prop.Key === "LastName") profile.LastName = prop.Value;
552
+ else if (prop.Key === "Country") profile.Country = prop.Value;
553
+ else if (prop.Key === "UserName") profile.UserName = prop.Value;
554
+ }
555
+ );
556
+ return profile;
557
+ }
558
+ async function getSiteUser(instance2, searchValue) {
559
+ const safeSearchValue = escapeODataStringLiteral(searchValue);
560
+ const response = await instance2.get("_api/web/siteusers", {
561
+ params: {
562
+ $filter: `substringof('${safeSearchValue}', Title) or substringof('${safeSearchValue}', LoginName) or substringof('${safeSearchValue}', Email)`,
563
+ $top: 50,
564
+ $expand: "Groups"
565
+ }
566
+ });
567
+ return response.data.d;
568
+ }
569
+ async function roleDefinitions(instance2) {
570
+ const response = await instance2.get("_api/Web/RoleDefinitions");
571
+ return response.data.d.results;
572
+ }
573
+ async function getEffectiveBasePermissions(instance2) {
574
+ const response = await instance2.get("_api/Web/effectiveBasePermissions");
575
+ return response.data.d.EffectiveBasePermissions;
576
+ }
577
+ async function currentUserPermissions(context) {
578
+ const { High: userHigh, Low: userLow } = await getEffectiveBasePermissions(
579
+ context.instance
580
+ );
581
+ const definitions = await roleDefinitions(context.instance);
582
+ const userHighInt = parseInt(userHigh, 10);
583
+ const userLowInt = parseInt(userLow, 10);
584
+ const matching = definitions.filter(
585
+ (def) => PERMISSION_PRIORITY.includes(def.Name) && (userHighInt & parseInt(def.BasePermissions.High, 10)) === parseInt(def.BasePermissions.High, 10) && (userLowInt & parseInt(def.BasePermissions.Low, 10)) === parseInt(def.BasePermissions.Low, 10)
586
+ ).sort(
587
+ (a, b) => PERMISSION_PRIORITY.indexOf(a.Name) - PERMISSION_PRIORITY.indexOf(b.Name)
588
+ );
589
+ const result = matching[0];
590
+ if (!result) {
591
+ throw new Error(
592
+ `Unable to resolve current user permission. Effective: High="${userHigh}", Low="${userLow}"`
593
+ );
594
+ }
595
+ return result;
596
+ }
597
+
598
+ // src/root/index.ts
599
+ var HTTPSharePointRequests = class {
600
+ baseURL;
601
+ instance;
602
+ listItemEntityTypeCache;
603
+ /**
604
+ * @deprecated SP.Utilities.Utility.SendEmail was retired by Microsoft on 2025-10-31.
605
+ * Migrate to Microsoft Graph API POST /v1.0/me/sendMail
606
+ * https://learn.microsoft.com/en-us/graph/api/user-sendmail
607
+ */
608
+ email;
609
+ constructor(baseURL, config) {
610
+ this.baseURL = baseURL.endsWith("/") ? baseURL : `${baseURL}/`;
611
+ this.instance = instance_default(this.baseURL, config);
612
+ this.listItemEntityTypeCache = /* @__PURE__ */ new Map();
613
+ const buildEmailContext = () => this.createContext({
614
+ endpoint: "_api/SP.Utilities.Utility.SendEmail",
615
+ listName: "",
616
+ sharepointVersion: "2013"
617
+ });
618
+ this.email = Object.assign(
619
+ (from, to, subject, body) => sendEmail(buildEmailContext(), {
620
+ From: from,
621
+ To: to,
622
+ Subject: subject,
623
+ Body: body
624
+ }),
625
+ {
626
+ send: (props) => sendEmail(buildEmailContext(), props)
627
+ }
628
+ );
629
+ }
630
+ createContext({
631
+ endpoint,
632
+ listName,
633
+ sharepointVersion,
634
+ listItemEntityTypeFullName,
635
+ resolveListItemEntityType
636
+ }) {
637
+ return {
638
+ instance: this.instance,
639
+ endpoint,
640
+ listName,
641
+ sharepointVersion,
642
+ listItemEntityTypeFullName,
643
+ resolveListItemEntityType
644
+ };
645
+ }
646
+ async resolveListItemEntityTypeFullName(listName) {
647
+ var _a, _b, _c;
648
+ const cacheKey = listName;
649
+ const cachedType = this.listItemEntityTypeCache.get(cacheKey);
650
+ if (cachedType) {
651
+ return cachedType;
652
+ }
653
+ const response = await this.instance.get(
654
+ `${buildListByTitleEndpoint(listName)}?$select=ListItemEntityTypeFullName`
655
+ );
656
+ const entityType = ((_b = (_a = response.data) == null ? void 0 : _a.d) == null ? void 0 : _b.ListItemEntityTypeFullName) ?? ((_c = response.data) == null ? void 0 : _c.ListItemEntityTypeFullName);
657
+ if (!entityType || typeof entityType !== "string") {
658
+ throw new Error(
659
+ `Unable to resolve ListItemEntityTypeFullName for list "${listName}".`
660
+ );
661
+ }
662
+ this.listItemEntityTypeCache.set(cacheKey, entityType);
663
+ return entityType;
664
+ }
665
+ buildListScope(listName, version) {
666
+ const listNameValue = String(listName);
667
+ const endpoint = version === "2013" ? `${buildListByTitleEndpoint(listNameValue)}/items` : `_vti_bin/ListData.svc/${encodeURIComponent(listNameValue)}`;
668
+ let scopedEntityType;
669
+ const resolveListItemEntityType = async () => {
670
+ if (scopedEntityType) {
671
+ return scopedEntityType;
672
+ }
673
+ if (version !== "2013") {
674
+ scopedEntityType = `SP.Data.${listNameValue}ListItem`;
675
+ return scopedEntityType;
676
+ }
677
+ scopedEntityType = await this.resolveListItemEntityTypeFullName(
678
+ listNameValue
679
+ );
680
+ return scopedEntityType;
681
+ };
682
+ const getContext = () => this.createContext({
683
+ endpoint,
684
+ listName: listNameValue,
685
+ sharepointVersion: version,
686
+ listItemEntityTypeFullName: scopedEntityType,
687
+ resolveListItemEntityType
688
+ });
689
+ const removeItem = (id) => deleteItem(getContext(), id);
690
+ const removeFile = (props) => deleteFile(getContext(), props);
691
+ return {
692
+ create: (data) => createListItem(getContext(), data),
693
+ get: (options) => getListItems(getContext(), options),
694
+ update: (id, data, digest) => updateListItem(getContext(), id, data, digest),
695
+ delete: removeItem,
696
+ remove: removeItem,
697
+ createFile: (props) => createAttachment(getContext(), props),
698
+ uploadFile: (props) => uploadFile(getContext(), props),
699
+ deleteFile: removeFile,
700
+ removeFile,
701
+ fields: (options) => {
702
+ const fieldsEndpoint = !(options == null ? void 0 : options.fieldName) ? `${buildListByTitleEndpoint(listNameValue)}/fields` : `${buildListByTitleEndpoint(
703
+ listNameValue
704
+ )}/fields/getbytitle('${escapeODataStringLiteral(
705
+ String(options.fieldName)
706
+ )}')`;
707
+ return {
708
+ get: () => getOnly(this.instance, fieldsEndpoint, options)
709
+ };
710
+ },
711
+ items: {
712
+ create: (data) => createListItem(getContext(), data),
713
+ get: (options) => getListItems(getContext(), options),
714
+ update: (id, data, digest) => updateListItem(getContext(), id, data, digest),
715
+ delete: removeItem,
716
+ remove: removeItem
717
+ },
718
+ files: {
719
+ attach: (props) => createAttachment(getContext(), props),
720
+ upload: (props) => uploadFile(getContext(), props),
721
+ delete: removeFile,
722
+ remove: removeFile
723
+ }
724
+ };
725
+ }
726
+ /**
727
+ * @deprecated Use `from(listName, '2010')` instead.
728
+ */
729
+ list(listName) {
730
+ return this.from(listName, "2010");
731
+ }
732
+ from(listName, sharepointVersion) {
733
+ const version = sharepointVersion ?? "2013";
734
+ return this.buildListScope(listName, version);
735
+ }
736
+ field(listName, fieldName, sharepointVersion) {
737
+ return this.from(listName, sharepointVersion).fields({
738
+ fieldName
739
+ }).get();
740
+ }
741
+ lists(options) {
742
+ const context = this.createContext({
743
+ endpoint: "_api/web/lists",
744
+ listName: "",
745
+ sharepointVersion: "2013"
746
+ });
747
+ return {
748
+ get: () => getListItems(context, options)
749
+ };
750
+ }
751
+ getDigest() {
752
+ return fetchDigest(this.instance);
753
+ }
754
+ folders(options) {
755
+ const context = this.createContext({
756
+ endpoint: "_api/web/folders",
757
+ listName: "",
758
+ sharepointVersion: "2013"
759
+ });
760
+ return {
761
+ get: () => getFiles(context, options)
762
+ };
763
+ }
764
+ folder(folderName) {
765
+ const defaultPath = ["Shared Documents"];
766
+ const segments = folderName ? Array.isArray(folderName) ? [...defaultPath, ...folderName] : [...defaultPath, ...folderName.split("/").filter(Boolean)] : defaultPath;
767
+ const serverRelativePath = escapeODataStringLiteral(segments.join("/"));
768
+ const baseEndpoint = `_api/web/GetFolderByServerRelativeUrl('${serverRelativePath}')`;
769
+ return {
770
+ get: (options) => {
771
+ const folderType = (options == null ? void 0 : options.type) ?? "Files";
772
+ const endpoint = `${baseEndpoint}/${folderType}`;
773
+ const context = this.createContext({
774
+ endpoint,
775
+ listName: "",
776
+ sharepointVersion: "2013"
777
+ });
778
+ return getFiles(context, {
779
+ ...options,
780
+ type: folderType
781
+ });
782
+ }
783
+ };
784
+ }
785
+ users(options) {
786
+ const context = this.createContext({
787
+ endpoint: "_api/web/SiteUserInfoList/items",
788
+ listName: "",
789
+ sharepointVersion: "2013"
790
+ });
791
+ return {
792
+ get: () => getListItems(context, options)
793
+ };
794
+ }
795
+ /**
796
+ * @deprecated SP.Utilities.Utility.SendEmail was retired by Microsoft on 2025-10-31.
797
+ * Migrate to Microsoft Graph API POST /v1.0/me/sendMail
798
+ * https://learn.microsoft.com/en-us/graph/api/user-sendmail
799
+ */
800
+ sendEmail(props) {
801
+ const context = this.createContext({
802
+ endpoint: "_api/SP.Utilities.Utility.SendEmail",
803
+ listName: "",
804
+ sharepointVersion: "2013"
805
+ });
806
+ return sendEmail(context, props);
807
+ }
808
+ user = {
809
+ properties: () => ({
810
+ get: () => currentUserProperties(this.instance)
811
+ }),
812
+ searchSiteUser: (searchValue) => getSiteUser(this.instance, searchValue),
813
+ searchUser: (input, filter) => typeAhead(this.instance, input, filter),
814
+ current: () => ({
815
+ get: () => getOnly(this.instance, "_api/web/currentUser")
816
+ }),
817
+ currentPermission: () => ({
818
+ get: () => currentUserPermissions(
819
+ this.createContext({
820
+ endpoint: "_api/web/effectiveBasePermissions",
821
+ listName: "",
822
+ sharepointVersion: "2013"
823
+ })
824
+ )
825
+ })
826
+ };
827
+ getCurrentUser() {
828
+ return this.user.current().get();
829
+ }
830
+ currentUserProperties() {
831
+ return this.user.properties().get();
832
+ }
833
+ currentUserPermission() {
834
+ return this.user.currentPermission().get();
835
+ }
836
+ };
837
+ var root_default = HTTPSharePointRequests;
838
+
839
+ // src/index.ts
840
+ function createBase() {
841
+ function create({
842
+ baseURL,
843
+ config
844
+ }) {
845
+ if (!baseURL) {
846
+ throw new Error("A valid SharePoint URL must be provided.");
847
+ }
848
+ return new root_default(baseURL, config);
849
+ }
850
+ return {
851
+ /**
852
+ * @deprecated Use `create` instead.
853
+ */
854
+ baseURL(url, config) {
855
+ return create({ baseURL: url, config });
856
+ },
857
+ create
858
+ };
859
+ }
860
+ export {
861
+ createBase as default
862
+ };
863
+ //# sourceMappingURL=index.mjs.map