@lindle/sharepoint_requests 0.1.11 → 0.1.13
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/dist/index.d.ts +4 -2
- package/dist/root/index.d.ts +17 -217
- package/dist/root/instance.d.ts +2 -2
- package/dist/root/internal/context.d.ts +9 -0
- package/dist/root/internal/digest.d.ts +2 -0
- package/dist/root/internal/emailRequests.d.ts +10 -0
- package/dist/root/internal/listRequests/createAttachment.d.ts +3 -0
- package/dist/root/internal/listRequests/createListItem.d.ts +3 -0
- package/dist/root/internal/listRequests/deleteItem.d.ts +3 -0
- package/dist/root/internal/listRequests/getFiles.d.ts +8 -0
- package/dist/root/internal/listRequests/getListItems.d.ts +8 -0
- package/dist/root/internal/listRequests/getOnly.d.ts +3 -0
- package/dist/root/internal/listRequests/index.d.ts +7 -0
- package/dist/root/internal/listRequests/updateListItem.d.ts +3 -0
- package/dist/root/internal/odata.d.ts +5 -0
- package/dist/root/internal/userRequests.d.ts +15 -0
- package/dist/sharepoint_requests.cjs.development.js +621 -775
- package/dist/sharepoint_requests.cjs.development.js.map +1 -1
- package/dist/sharepoint_requests.cjs.production.min.js +1 -1
- package/dist/sharepoint_requests.cjs.production.min.js.map +1 -1
- package/dist/sharepoint_requests.esm.js +620 -775
- package/dist/sharepoint_requests.esm.js.map +1 -1
- package/dist/types.d.ts +38 -7
- package/package.json +1 -1
- package/src/index.ts +37 -28
- package/src/root/index.ts +201 -600
- package/src/root/instance.ts +22 -13
- package/src/root/internal/context.ts +10 -0
- package/src/root/internal/digest.ts +22 -0
- package/src/root/internal/emailRequests.ts +45 -0
- package/src/root/internal/listRequests/createAttachment.ts +18 -0
- package/src/root/internal/listRequests/createListItem.ts +36 -0
- package/src/root/internal/listRequests/deleteItem.ts +10 -0
- package/src/root/internal/listRequests/getFiles.ts +31 -0
- package/src/root/internal/listRequests/getListItems.ts +50 -0
- package/src/root/internal/listRequests/getOnly.ts +18 -0
- package/src/root/internal/listRequests/index.ts +7 -0
- package/src/root/internal/listRequests/updateListItem.ts +32 -0
- package/src/root/internal/odata.ts +36 -0
- package/src/root/internal/userRequests.ts +109 -0
- package/src/types.ts +76 -36
package/src/root/index.ts
CHANGED
|
@@ -1,600 +1,201 @@
|
|
|
1
|
-
import { AxiosInstance,
|
|
2
|
-
import instance from './instance';
|
|
3
|
-
import {
|
|
4
|
-
CreateFileProps,
|
|
5
|
-
CurrentUser,
|
|
6
|
-
EmailProps,
|
|
7
|
-
Field_Options,
|
|
8
|
-
Folder_Options,
|
|
9
|
-
IListName,
|
|
10
|
-
ItemID,
|
|
11
|
-
ListData,
|
|
12
|
-
Options,
|
|
13
|
-
PersonField,
|
|
14
|
-
SHAREPOINT_VER,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} from '
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
get: () => {
|
|
121
|
-
this.endpoint = '_api/web/folders';
|
|
122
|
-
this.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Executes a GET request without additional metadata wrapping.
|
|
204
|
-
*
|
|
205
|
-
* @param options Optional REST query options.
|
|
206
|
-
* @returns SharePoint response payload or undefined when empty.
|
|
207
|
-
*/
|
|
208
|
-
private async get_only(options?: any) {
|
|
209
|
-
const { expand, orderBy, limit, filter, cols, skip } = options || {};
|
|
210
|
-
|
|
211
|
-
const params = {
|
|
212
|
-
$expand: Array.isArray(expand) ? expand.join(',') : expand,
|
|
213
|
-
$top: limit,
|
|
214
|
-
$skip: skip,
|
|
215
|
-
$select: Array.isArray(cols) ? cols.join(',') : cols,
|
|
216
|
-
$filter: filter,
|
|
217
|
-
$orderby: Array.isArray(orderBy) ? orderBy.join(',') : orderBy,
|
|
218
|
-
};
|
|
219
|
-
const { data } = await this.instance.get(this.endpoint, { params });
|
|
220
|
-
if (data) {
|
|
221
|
-
return data.d.results ? data.d.results : data.d;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Retrieves folder contents with optional filtering and returns the payload with metadata.
|
|
227
|
-
*
|
|
228
|
-
* @param options REST options describing expand/filter/order conditions.
|
|
229
|
-
* @returns Folder data and the fully resolved request URL.
|
|
230
|
-
*/
|
|
231
|
-
private async get_files(options?: Folder_Options<any>) {
|
|
232
|
-
const { expand, orderBy, limit, filter, cols, skip } = options || {};
|
|
233
|
-
const params = {
|
|
234
|
-
$expand: Array.isArray(expand) ? expand.join(',') : expand,
|
|
235
|
-
$top: limit,
|
|
236
|
-
$skip: skip,
|
|
237
|
-
$select: Array.isArray(cols) ? cols.join(',') : cols,
|
|
238
|
-
$filter: filter,
|
|
239
|
-
$orderby: Array.isArray(orderBy) ? orderBy.join(',') : orderBy,
|
|
240
|
-
};
|
|
241
|
-
const url = new URL(this.endpoint, this.instance.defaults.baseURL);
|
|
242
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
243
|
-
if (value !== undefined && value !== null) {
|
|
244
|
-
url.searchParams.append(key, String(value));
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
const response = await this.instance.request({
|
|
249
|
-
url: this.endpoint,
|
|
250
|
-
method: 'GET',
|
|
251
|
-
params,
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
const data =
|
|
255
|
-
limit || !response.data.d.results
|
|
256
|
-
? response.data.d
|
|
257
|
-
: response.data.d.results;
|
|
258
|
-
|
|
259
|
-
const meta = { url };
|
|
260
|
-
|
|
261
|
-
return { data, meta };
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Performs a typed list read against the current endpoint.
|
|
266
|
-
*
|
|
267
|
-
* @param options Optional REST query options governing expand, filter, ordering and pagination.
|
|
268
|
-
* @returns SharePoint list items alongside the fully-resolved request URL.
|
|
269
|
-
*/
|
|
270
|
-
private async get_v2<K = any>(
|
|
271
|
-
options?: Options<K>
|
|
272
|
-
): Promise<{ data: (K & Partial<SPItem>)[]; meta: { url: URL } }> {
|
|
273
|
-
const { expand, orderBy, limit, filter, cols, skip } = options || {};
|
|
274
|
-
|
|
275
|
-
const params = {
|
|
276
|
-
$expand: Array.isArray(expand) ? expand.join(',') : expand,
|
|
277
|
-
$top: limit,
|
|
278
|
-
$skip: skip,
|
|
279
|
-
$select: Array.isArray(cols) ? cols.join(',') : cols,
|
|
280
|
-
$filter: filter,
|
|
281
|
-
$orderby: Array.isArray(orderBy) ? orderBy.join(' ') : orderBy,
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
const url = new URL(this.endpoint, this.instance.defaults.baseURL);
|
|
285
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
286
|
-
if (value !== undefined && value !== null) {
|
|
287
|
-
url.searchParams.append(key, String(value));
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
const response = await this.instance.request({
|
|
292
|
-
url: this.endpoint,
|
|
293
|
-
method: 'GET',
|
|
294
|
-
params,
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
const data =
|
|
298
|
-
limit || !response.data.d.results
|
|
299
|
-
? response.data.d
|
|
300
|
-
: response.data.d.results;
|
|
301
|
-
|
|
302
|
-
const meta = { url };
|
|
303
|
-
|
|
304
|
-
return { data, meta };
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Updates an item in the currently selected list.
|
|
309
|
-
*
|
|
310
|
-
* @param id Target list item identifier.
|
|
311
|
-
* @param data Payload with fields to update.
|
|
312
|
-
* @param digest Optional pre-fetched form digest to reuse.
|
|
313
|
-
* @returns Axios response of the update request.
|
|
314
|
-
*/
|
|
315
|
-
private async update_2(id: ItemID, data: ListData, digest?: string) {
|
|
316
|
-
const formDigestValue = digest || (await this.getDigest());
|
|
317
|
-
|
|
318
|
-
data.__metadata = {
|
|
319
|
-
type: `SP.Data.${this.listName}ListItem`,
|
|
320
|
-
};
|
|
321
|
-
const url = `_api/lists/getbytitle('${this.listName}')/getItemById('${id}')`;
|
|
322
|
-
const response = await this.instance({
|
|
323
|
-
url,
|
|
324
|
-
method: 'POST',
|
|
325
|
-
data: JSON.stringify(data),
|
|
326
|
-
headers: {
|
|
327
|
-
'X-RequestDigest': formDigestValue,
|
|
328
|
-
'IF-MATCH': '*',
|
|
329
|
-
'X-Http-Method': 'PATCH',
|
|
330
|
-
},
|
|
331
|
-
});
|
|
332
|
-
return response;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Creates a new item within the active list.
|
|
337
|
-
*
|
|
338
|
-
* @param listData SharePoint list item payload.
|
|
339
|
-
* @returns Axios response resolved from the create request.
|
|
340
|
-
*/
|
|
341
|
-
private async create_v2(listData: ListData) {
|
|
342
|
-
this.transformArraysToEdmStrings(listData);
|
|
343
|
-
listData.__metadata = {
|
|
344
|
-
type: `SP.Data.${this.listName}ListItem`,
|
|
345
|
-
};
|
|
346
|
-
const response = await this.instance({
|
|
347
|
-
url: this.endpoint,
|
|
348
|
-
method: 'POST',
|
|
349
|
-
data: JSON.stringify(listData),
|
|
350
|
-
headers: {
|
|
351
|
-
'X-RequestDigest': await this.getDigest(),
|
|
352
|
-
'IF-MATCH': '*',
|
|
353
|
-
},
|
|
354
|
-
});
|
|
355
|
-
return response;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Uploads an attachment file to an existing list item.
|
|
360
|
-
*
|
|
361
|
-
* @param props File payload and target item identifier.
|
|
362
|
-
* @returns Axios response describing the upload result.
|
|
363
|
-
*/
|
|
364
|
-
private async createFile({ file, itemId }: CreateFileProps) {
|
|
365
|
-
const requestUrl = `_api/lists/getbytitle('${this.listName}')/items(${itemId})/AttachmentFiles/add(FileName='${file.name}')`;
|
|
366
|
-
const res = await this.instance.post(requestUrl, file, {
|
|
367
|
-
headers: {
|
|
368
|
-
'X-RequestDigest': await this.getDigest(),
|
|
369
|
-
},
|
|
370
|
-
withCredentials: true,
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
return res;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Deletes an item using the current endpoint.
|
|
378
|
-
*
|
|
379
|
-
* @param ID Identifier of the entity to remove.
|
|
380
|
-
* @returns Axios response from the delete call.
|
|
381
|
-
*/
|
|
382
|
-
private async delete(ID: string | number) {
|
|
383
|
-
const response = await this.instance.delete(`${this.endpoint!}(${ID})`);
|
|
384
|
-
return response;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
public email = {
|
|
388
|
-
/**
|
|
389
|
-
* Sends an email using SharePoint's Utility.SendEmail API.
|
|
390
|
-
*
|
|
391
|
-
* @param {EmailProps} param0 - The email properties.
|
|
392
|
-
* @param {string} param0.From - The sender's email address.
|
|
393
|
-
* @example
|
|
394
|
-
* Example of a sender's email address
|
|
395
|
-
* "sender@example.com"
|
|
396
|
-
* @param {string | string[]} param0.To - The recipient's email address or an array of email addresses.
|
|
397
|
-
* @example
|
|
398
|
-
* Example of a single recipient's email address
|
|
399
|
-
* "recipient@example.com"
|
|
400
|
-
* Example of multiple recipients' email addresses
|
|
401
|
-
* ["recipient1@example.com", "recipient2@example.com"]
|
|
402
|
-
* @param {string} param0.Subject - The subject of the email.
|
|
403
|
-
* @example
|
|
404
|
-
* Example of an email subject
|
|
405
|
-
* "Meeting Reminder"
|
|
406
|
-
* @param {string} param0.Body - The body content of the email.
|
|
407
|
-
* @example
|
|
408
|
-
* Example of an email body
|
|
409
|
-
* "Dear team, please be reminded of the meeting scheduled for tomorrow at 10 AM."
|
|
410
|
-
* @returns {Promise<any>} - A promise that resolves to the response of the email send request.
|
|
411
|
-
*/
|
|
412
|
-
send: (props: EmailProps) => this.sendEmail(props),
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
private async sendEmail({
|
|
416
|
-
From,
|
|
417
|
-
To,
|
|
418
|
-
Subject,
|
|
419
|
-
Body,
|
|
420
|
-
}: EmailProps): Promise<
|
|
421
|
-
AxiosResponse<{
|
|
422
|
-
d: {
|
|
423
|
-
SendEmail: null;
|
|
424
|
-
};
|
|
425
|
-
}>
|
|
426
|
-
> {
|
|
427
|
-
const httpRequest = '_api/SP.Utilities.Utility.SendEmail';
|
|
428
|
-
const response = await this.instance({
|
|
429
|
-
url: httpRequest,
|
|
430
|
-
method: 'POST',
|
|
431
|
-
headers: {
|
|
432
|
-
'X-RequestDigest': await this.getDigest(),
|
|
433
|
-
'X-Http-Method': 'POST',
|
|
434
|
-
'Content-Type': 'application/json;odata=verbose',
|
|
435
|
-
},
|
|
436
|
-
data: JSON.stringify({
|
|
437
|
-
properties: {
|
|
438
|
-
__metadata: {
|
|
439
|
-
type: 'SP.Utilities.EmailProperties',
|
|
440
|
-
},
|
|
441
|
-
From,
|
|
442
|
-
To: {
|
|
443
|
-
results: Array.isArray(To) ? To : [To],
|
|
444
|
-
},
|
|
445
|
-
Body,
|
|
446
|
-
Subject,
|
|
447
|
-
},
|
|
448
|
-
}),
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
return response;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* Queries SharePoint's people picker endpoint for matching users.
|
|
456
|
-
*
|
|
457
|
-
* @param input Free-text search term.
|
|
458
|
-
* @param filter Optional additional filter condition.
|
|
459
|
-
* @returns Promise with type-ahead results payload.
|
|
460
|
-
*/
|
|
461
|
-
private async typeAhead(input: string, filter?: string) {
|
|
462
|
-
const filterValue = filter ? ` and ${filter}` : '';
|
|
463
|
-
const response = await this.instance.get(
|
|
464
|
-
`_vti_bin/listdata.svc/UserInformationList?$top=10&$filter=substringof('${input}',Name) or substringof('${input}',WorkEmail)${filterValue}`
|
|
465
|
-
);
|
|
466
|
-
return response.data.d;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Retrieves the current user profile and flattens selected properties.
|
|
471
|
-
*
|
|
472
|
-
* @returns Promise containing the current user's profile information.
|
|
473
|
-
*/
|
|
474
|
-
private async currentUserProperties() {
|
|
475
|
-
const requestUrl = '_api/sp.userprofiles.peoplemanager/getmyproperties';
|
|
476
|
-
let profile: any = await this.instance.get(requestUrl);
|
|
477
|
-
|
|
478
|
-
profile = profile.data.d;
|
|
479
|
-
|
|
480
|
-
profile.UserProfileProperties.results.forEach(
|
|
481
|
-
(prop: { Key: string; Value: string }) => {
|
|
482
|
-
if (prop.Key === 'FirstName') {
|
|
483
|
-
profile.FirstName = prop.Value;
|
|
484
|
-
}
|
|
485
|
-
if (prop.Key === 'LastName') {
|
|
486
|
-
profile.LastName = prop.Value;
|
|
487
|
-
}
|
|
488
|
-
if (prop.Key === 'Country') {
|
|
489
|
-
profile.Country = prop.Value;
|
|
490
|
-
}
|
|
491
|
-
if (prop.Key === 'UserName') {
|
|
492
|
-
profile.UserName = prop.Value;
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
);
|
|
496
|
-
return profile as Promise<UserProfile>;
|
|
497
|
-
}
|
|
498
|
-
public user = {
|
|
499
|
-
/**
|
|
500
|
-
* Provides accessors for the current user's profile information.
|
|
501
|
-
*
|
|
502
|
-
* @returns Helper exposing a `get` function to retrieve profile details.
|
|
503
|
-
*/
|
|
504
|
-
properties: () => {
|
|
505
|
-
return {
|
|
506
|
-
get: () => this.currentUserProperties(),
|
|
507
|
-
};
|
|
508
|
-
},
|
|
509
|
-
/**
|
|
510
|
-
* Searches across site users and their groups.
|
|
511
|
-
*
|
|
512
|
-
* @param searchValue Query string to match site users.
|
|
513
|
-
* @returns Promise resolving to matching site users.
|
|
514
|
-
*/
|
|
515
|
-
searchSiteUser: (searchValue: string) => this.getSiteUser(searchValue),
|
|
516
|
-
/**
|
|
517
|
-
* Performs a people-picker search scoped to the current site collection.
|
|
518
|
-
*
|
|
519
|
-
* @param input Search prefix.
|
|
520
|
-
* @param filter Optional additional filter condition.
|
|
521
|
-
* @returns Promise with type-ahead person results.
|
|
522
|
-
*/
|
|
523
|
-
searchUser: (input: string, filter?: string): Promise<PersonField> =>
|
|
524
|
-
this.typeAhead(input, filter),
|
|
525
|
-
/**
|
|
526
|
-
* Fetches the currently authenticated SharePoint user.
|
|
527
|
-
*
|
|
528
|
-
* @returns Helper exposing `get` that resolves to the current user payload.
|
|
529
|
-
*/
|
|
530
|
-
current: () => {
|
|
531
|
-
this.endpoint = '_api/web/currentUser';
|
|
532
|
-
return {
|
|
533
|
-
get: (): Promise<CurrentUser> => this.get_only(),
|
|
534
|
-
};
|
|
535
|
-
},
|
|
536
|
-
/**
|
|
537
|
-
* Retrieves calculated permission level for the current user.
|
|
538
|
-
*
|
|
539
|
-
* @returns Helper exposing `get` resolving to the user's permission definition.
|
|
540
|
-
*/
|
|
541
|
-
currentPermission: () => {
|
|
542
|
-
return {
|
|
543
|
-
get: () => this.currentUserPermissions(),
|
|
544
|
-
};
|
|
545
|
-
},
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
/**
|
|
549
|
-
* Searches site users based on a substring match across common fields.
|
|
550
|
-
*
|
|
551
|
-
* @param searchValue Term to match against the SharePoint user directory.
|
|
552
|
-
* @returns Promise with matching site users and their groups.
|
|
553
|
-
*/
|
|
554
|
-
private async getSiteUser(searchValue: string) {
|
|
555
|
-
const requestUrl = `_api/web/siteusers?&$filter=substringof('${searchValue}', Title) or substringof('${searchValue}', LoginName) or substringof('${searchValue}', Email)&$top=50&$expand=Groups`;
|
|
556
|
-
const response = await this.instance.get(requestUrl);
|
|
557
|
-
return response.data.d;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
/**
|
|
561
|
-
* Retrieves all available role definitions for the current site.
|
|
562
|
-
*
|
|
563
|
-
* @returns Promise resolving to an array of role definitions.
|
|
564
|
-
*/
|
|
565
|
-
private async roleDefinitions() {
|
|
566
|
-
const requestUrl = `_api/Web/RoleDefinitions`;
|
|
567
|
-
const response = await this.instance.get(requestUrl);
|
|
568
|
-
return response.data.d.results;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Fetches the bit-encoded permissions of the current user.
|
|
573
|
-
*
|
|
574
|
-
* @returns Promise resolving to the `EffectiveBasePermissions` object.
|
|
575
|
-
*/
|
|
576
|
-
private async getEffectiveBasePermissions() {
|
|
577
|
-
const requestUrl = `_api/Web/effectiveBasePermissions`;
|
|
578
|
-
const response = await this.instance.get(requestUrl);
|
|
579
|
-
return response.data.d.EffectiveBasePermissions;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* Matches the current user's effective permissions against known role definitions.
|
|
584
|
-
*
|
|
585
|
-
* @returns The matching SharePoint role definition as a structured permission object.
|
|
586
|
-
*/
|
|
587
|
-
private async currentUserPermissions() {
|
|
588
|
-
const { High } = await this.getEffectiveBasePermissions();
|
|
589
|
-
const roleDefinitions = await this.roleDefinitions();
|
|
590
|
-
|
|
591
|
-
const result = roleDefinitions.find(
|
|
592
|
-
({ BasePermissions }: { BasePermissions: { High: string } }) =>
|
|
593
|
-
BasePermissions.High === High
|
|
594
|
-
);
|
|
595
|
-
|
|
596
|
-
return result as UserPermision;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
export default HTTPSharePointRequests;
|
|
1
|
+
import { AxiosInstance, CreateAxiosDefaults as AxiosConfig } from 'axios';
|
|
2
|
+
import instance from './instance';
|
|
3
|
+
import {
|
|
4
|
+
CreateFileProps,
|
|
5
|
+
CurrentUser,
|
|
6
|
+
EmailProps,
|
|
7
|
+
Field_Options,
|
|
8
|
+
Folder_Options,
|
|
9
|
+
IListName,
|
|
10
|
+
ItemID,
|
|
11
|
+
ListData,
|
|
12
|
+
Options,
|
|
13
|
+
PersonField,
|
|
14
|
+
SHAREPOINT_VER,
|
|
15
|
+
UserPermision,
|
|
16
|
+
UserProfile,
|
|
17
|
+
} from '../types';
|
|
18
|
+
import { RequestContext } from './internal/context';
|
|
19
|
+
import {
|
|
20
|
+
createAttachment,
|
|
21
|
+
createListItem,
|
|
22
|
+
deleteItem,
|
|
23
|
+
getFiles,
|
|
24
|
+
getListItems,
|
|
25
|
+
getOnly,
|
|
26
|
+
updateListItem,
|
|
27
|
+
} from './internal/listRequests';
|
|
28
|
+
import { sendEmail as sendEmailRequest } from './internal/emailRequests';
|
|
29
|
+
import {
|
|
30
|
+
currentUserPermissions as resolveCurrentUserPermissions,
|
|
31
|
+
currentUserProperties as loadCurrentUserProperties,
|
|
32
|
+
getSiteUser as fetchSiteUser,
|
|
33
|
+
typeAhead,
|
|
34
|
+
} from './internal/userRequests';
|
|
35
|
+
|
|
36
|
+
class HTTPSharePointRequests<
|
|
37
|
+
T extends {
|
|
38
|
+
LISTS: Record<string, unknown>;
|
|
39
|
+
FOLDERS?: Record<string, unknown>;
|
|
40
|
+
}
|
|
41
|
+
> {
|
|
42
|
+
private baseURL: string;
|
|
43
|
+
private endpoint: string;
|
|
44
|
+
private listName: string;
|
|
45
|
+
private instance: AxiosInstance;
|
|
46
|
+
private sharepointVersion: SHAREPOINT_VER;
|
|
47
|
+
|
|
48
|
+
constructor(baseURL: string, config?: AxiosConfig) {
|
|
49
|
+
this.baseURL = baseURL.endsWith('/') ? baseURL : `${baseURL}/`;
|
|
50
|
+
this.endpoint = '/api';
|
|
51
|
+
this.listName = '';
|
|
52
|
+
this.instance = instance(this.baseURL, config);
|
|
53
|
+
this.sharepointVersion = '2013';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private getContext<V extends SHAREPOINT_VER = '2013'>(): RequestContext<V> {
|
|
57
|
+
return {
|
|
58
|
+
instance: this.instance,
|
|
59
|
+
baseURL: this.baseURL,
|
|
60
|
+
endpoint: this.endpoint,
|
|
61
|
+
listName: this.listName,
|
|
62
|
+
sharepointVersion: this.sharepointVersion as V,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Use `from` instead.
|
|
68
|
+
*/
|
|
69
|
+
list(listName: IListName<T>) {
|
|
70
|
+
this.listName = listName;
|
|
71
|
+
this.endpoint = `_vti_bin/ListData.svc/${listName}`;
|
|
72
|
+
this.sharepointVersion = '2010';
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public readonly lists = {
|
|
77
|
+
get: () => {
|
|
78
|
+
this.endpoint = '_api/web/lists';
|
|
79
|
+
this.sharepointVersion = '2013';
|
|
80
|
+
return getListItems(this.getContext());
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
from<
|
|
85
|
+
K extends Extract<keyof T['LISTS'], string>,
|
|
86
|
+
V extends SHAREPOINT_VER = '2013'
|
|
87
|
+
>(listName: K, sharepoint_ver?: V) {
|
|
88
|
+
const version = (sharepoint_ver ?? '2013') as V;
|
|
89
|
+
|
|
90
|
+
this.listName = listName;
|
|
91
|
+
this.sharepointVersion = version;
|
|
92
|
+
this.endpoint =
|
|
93
|
+
version === '2013'
|
|
94
|
+
? `_api/web/lists/GetByTitle('${this.listName}')/items`
|
|
95
|
+
: `_vti_bin/ListData.svc/${listName}`;
|
|
96
|
+
|
|
97
|
+
const getContext = () => this.getContext<V>();
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
create: (data: ListData) => createListItem(getContext(), data),
|
|
101
|
+
get: (options?: Options<T['LISTS'][K], V>) =>
|
|
102
|
+
getListItems<T['LISTS'][K], V>(getContext(), options),
|
|
103
|
+
update: (id: ItemID, data: ListData, digest?: string) =>
|
|
104
|
+
updateListItem(getContext(), id, data, digest),
|
|
105
|
+
delete: (id: ItemID) => deleteItem(getContext(), id),
|
|
106
|
+
createFile: (props: CreateFileProps) =>
|
|
107
|
+
createAttachment(getContext(), props),
|
|
108
|
+
fields: (options?: Field_Options<T['LISTS'][K], V>) => {
|
|
109
|
+
this.endpoint = !options?.fieldName
|
|
110
|
+
? `_api/web/lists/GetByTitle('${this.listName}')/fields`
|
|
111
|
+
: `_api/web/lists/GetByTitle('${this.listName}')/fields/getbytitle('${options.fieldName}')`;
|
|
112
|
+
return {
|
|
113
|
+
get: () => getOnly<T['LISTS'][K], V>(this.instance, this.endpoint, options),
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public folders = {
|
|
120
|
+
get: () => {
|
|
121
|
+
this.endpoint = '_api/web/folders';
|
|
122
|
+
this.sharepointVersion = '2013';
|
|
123
|
+
getListItems(this.getContext());
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
public folder(folderName?: string | string[]) {
|
|
128
|
+
let filePath = ['Shared Documents'];
|
|
129
|
+
|
|
130
|
+
if (folderName) {
|
|
131
|
+
filePath = Array.isArray(folderName)
|
|
132
|
+
? [...filePath, ...folderName]
|
|
133
|
+
: [...filePath, folderName];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.endpoint = `_api/web/GetFolderByServerRelativeUrl('${filePath.join(
|
|
137
|
+
'/'
|
|
138
|
+
)}')`;
|
|
139
|
+
this.sharepointVersion = '2013';
|
|
140
|
+
|
|
141
|
+
const getContext = () => this.getContext();
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
get: (options?: Folder_Options<any>) => {
|
|
145
|
+
if (!options?.type) {
|
|
146
|
+
this.endpoint += '/Files';
|
|
147
|
+
} else {
|
|
148
|
+
this.endpoint += `/${options.type}`;
|
|
149
|
+
}
|
|
150
|
+
return getFiles(getContext(), options);
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public users(options?: Options<any>) {
|
|
156
|
+
this.endpoint = '_api/web/SiteUserInfoList/items';
|
|
157
|
+
this.sharepointVersion = '2013';
|
|
158
|
+
return {
|
|
159
|
+
get: () => getListItems(this.getContext(), options),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public email = {
|
|
164
|
+
send: (props: EmailProps) => {
|
|
165
|
+
this.sharepointVersion = '2013';
|
|
166
|
+
return sendEmailRequest(this.getContext(), props);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
public user = {
|
|
171
|
+
properties: () => ({
|
|
172
|
+
get: (): Promise<UserProfile> => {
|
|
173
|
+
this.sharepointVersion = '2013';
|
|
174
|
+
return loadCurrentUserProperties(this.instance);
|
|
175
|
+
},
|
|
176
|
+
}),
|
|
177
|
+
searchSiteUser: (searchValue: string) => {
|
|
178
|
+
this.sharepointVersion = '2013';
|
|
179
|
+
return fetchSiteUser(this.instance, searchValue);
|
|
180
|
+
},
|
|
181
|
+
searchUser: (input: string, filter?: string): Promise<PersonField> => {
|
|
182
|
+
this.sharepointVersion = '2013';
|
|
183
|
+
return typeAhead(this.instance, input, filter);
|
|
184
|
+
},
|
|
185
|
+
current: () => {
|
|
186
|
+
this.endpoint = '_api/web/currentUser';
|
|
187
|
+
this.sharepointVersion = '2013';
|
|
188
|
+
return {
|
|
189
|
+
get: (): Promise<CurrentUser> => getOnly(this.instance, this.endpoint),
|
|
190
|
+
};
|
|
191
|
+
},
|
|
192
|
+
currentPermission: () => ({
|
|
193
|
+
get: (): Promise<UserPermision> => {
|
|
194
|
+
this.sharepointVersion = '2013';
|
|
195
|
+
return resolveCurrentUserPermissions(this.getContext());
|
|
196
|
+
},
|
|
197
|
+
}),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export default HTTPSharePointRequests;
|