@lindle/sharepoint_requests 0.1.13 → 0.1.15
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/README.md +5 -0
- package/dist/index.d.ts +17 -17
- package/dist/root/index.d.ts +85 -81
- package/dist/root/instance.d.ts +3 -3
- package/dist/root/internal/context.d.ts +11 -9
- package/dist/root/internal/digest.d.ts +2 -2
- package/dist/root/internal/emailRequests.d.ts +10 -10
- package/dist/root/internal/listRequests/createAttachment.d.ts +3 -3
- package/dist/root/internal/listRequests/createListItem.d.ts +3 -3
- package/dist/root/internal/listRequests/deleteItem.d.ts +3 -3
- package/dist/root/internal/listRequests/getFiles.d.ts +8 -8
- package/dist/root/internal/listRequests/getListItems.d.ts +8 -8
- package/dist/root/internal/listRequests/getOnly.d.ts +3 -3
- package/dist/root/internal/listRequests/index.d.ts +8 -7
- package/dist/root/internal/listRequests/updateListItem.d.ts +3 -3
- package/dist/root/internal/listRequests/uploadFile.d.ts +3 -0
- package/dist/root/internal/odata.d.ts +5 -5
- package/dist/root/internal/userRequests.d.ts +15 -15
- package/dist/sharepoint_requests.cjs.development.js +232 -52
- 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 +232 -52
- package/dist/sharepoint_requests.esm.js.map +1 -1
- package/dist/types.d.ts +237 -232
- package/package.json +6 -3
- package/src/root/index.ts +51 -2
- package/src/root/internal/context.ts +2 -0
- package/src/root/internal/listRequests/createListItem.ts +18 -13
- package/src/root/internal/listRequests/index.ts +1 -0
- package/src/root/internal/listRequests/updateListItem.ts +21 -3
- package/src/root/internal/listRequests/uploadFile.ts +66 -0
- package/src/types.ts +10 -5
|
@@ -2,33 +2,38 @@ import type { ListData, SHAREPOINT_VER } from '../../../types';
|
|
|
2
2
|
import { fetchDigest } from '../digest';
|
|
3
3
|
import type { RequestContext } from '../context';
|
|
4
4
|
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
async function resolveMetadataType<V extends SHAREPOINT_VER>(
|
|
6
|
+
context: RequestContext<V>
|
|
7
|
+
) {
|
|
8
|
+
if (context.resolveListItemEntityType) {
|
|
9
|
+
return context.resolveListItemEntityType();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (context.listItemEntityTypeFullName) {
|
|
13
|
+
return context.listItemEntityTypeFullName;
|
|
13
14
|
}
|
|
15
|
+
|
|
16
|
+
return `SP.Data.${context.listName}ListItem`;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export async function createListItem<V extends SHAREPOINT_VER = '2013'>(
|
|
17
20
|
context: RequestContext<V>,
|
|
18
21
|
listData: ListData
|
|
19
22
|
) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const metadataType = await resolveMetadataType(context);
|
|
24
|
+
const payload = {
|
|
25
|
+
...listData,
|
|
26
|
+
__metadata: {
|
|
27
|
+
type: metadataType,
|
|
28
|
+
},
|
|
23
29
|
};
|
|
24
30
|
|
|
25
31
|
const response = await context.instance({
|
|
26
32
|
url: context.endpoint,
|
|
27
33
|
method: 'POST',
|
|
28
|
-
data: JSON.stringify(
|
|
34
|
+
data: JSON.stringify(payload),
|
|
29
35
|
headers: {
|
|
30
36
|
'X-RequestDigest': await fetchDigest(context.instance, context.baseURL),
|
|
31
|
-
'IF-MATCH': '*',
|
|
32
37
|
},
|
|
33
38
|
});
|
|
34
39
|
|
|
@@ -2,6 +2,20 @@ import type { ListData, SHAREPOINT_VER } from '../../../types';
|
|
|
2
2
|
import { fetchDigest } from '../digest';
|
|
3
3
|
import type { RequestContext } from '../context';
|
|
4
4
|
|
|
5
|
+
async function resolveMetadataType<V extends SHAREPOINT_VER>(
|
|
6
|
+
context: RequestContext<V>
|
|
7
|
+
) {
|
|
8
|
+
if (context.resolveListItemEntityType) {
|
|
9
|
+
return context.resolveListItemEntityType();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (context.listItemEntityTypeFullName) {
|
|
13
|
+
return context.listItemEntityTypeFullName;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return `SP.Data.${context.listName}ListItem`;
|
|
17
|
+
}
|
|
18
|
+
|
|
5
19
|
export async function updateListItem<V extends SHAREPOINT_VER = '2013'>(
|
|
6
20
|
context: RequestContext<V>,
|
|
7
21
|
id: number | string,
|
|
@@ -11,8 +25,12 @@ export async function updateListItem<V extends SHAREPOINT_VER = '2013'>(
|
|
|
11
25
|
const formDigestValue =
|
|
12
26
|
digest || (await fetchDigest(context.instance, context.baseURL));
|
|
13
27
|
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
const metadataType = await resolveMetadataType(context);
|
|
29
|
+
const payload = {
|
|
30
|
+
...data,
|
|
31
|
+
__metadata: {
|
|
32
|
+
type: metadataType,
|
|
33
|
+
},
|
|
16
34
|
};
|
|
17
35
|
|
|
18
36
|
const url = `_api/lists/getbytitle('${context.listName}')/getItemById('${id}')`;
|
|
@@ -20,7 +38,7 @@ export async function updateListItem<V extends SHAREPOINT_VER = '2013'>(
|
|
|
20
38
|
const response = await context.instance({
|
|
21
39
|
url,
|
|
22
40
|
method: 'POST',
|
|
23
|
-
data: JSON.stringify(
|
|
41
|
+
data: JSON.stringify(payload),
|
|
24
42
|
headers: {
|
|
25
43
|
'X-RequestDigest': formDigestValue,
|
|
26
44
|
'IF-MATCH': '*',
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { SHAREPOINT_VER, UploadFileProps } from '../../../types';
|
|
2
|
+
import { fetchDigest } from '../digest';
|
|
3
|
+
import type { RequestContext } from '../context';
|
|
4
|
+
|
|
5
|
+
const SUPPORTED_VERSION: SHAREPOINT_VER = '2013';
|
|
6
|
+
|
|
7
|
+
function normalizeFolderPath(folderPath: string | string[]) {
|
|
8
|
+
const segments = Array.isArray(folderPath)
|
|
9
|
+
? folderPath
|
|
10
|
+
: folderPath.split('/').filter(Boolean);
|
|
11
|
+
|
|
12
|
+
if (!segments.length) {
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const escapedSegments = segments.map((segment) => {
|
|
17
|
+
if (!segment.trim()) {
|
|
18
|
+
throw new Error('Folder path segments cannot be empty.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// SharePoint expects single quotes to be doubled in OData paths.
|
|
22
|
+
const sanitized = segment.replace(/'/g, "''");
|
|
23
|
+
|
|
24
|
+
return `/Folders('${encodeURIComponent(sanitized)}')`;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return escapedSegments.join('');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function uploadFile<V extends SHAREPOINT_VER = '2013'>(
|
|
31
|
+
context: RequestContext<V>,
|
|
32
|
+
{ file, folderPath, overwrite = true }: UploadFileProps
|
|
33
|
+
) {
|
|
34
|
+
if (context.sharepointVersion !== SUPPORTED_VERSION) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
'uploadFile is only supported for SharePoint 2013 REST endpoints.'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!file?.name) {
|
|
41
|
+
throw new Error('A file with a valid name must be provided.');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let requestUrl = `_api/web/lists/getbytitle('${context.listName}')/RootFolder`;
|
|
45
|
+
|
|
46
|
+
if (folderPath) {
|
|
47
|
+
requestUrl += normalizeFolderPath(folderPath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const encodedFileName = encodeURIComponent(file.name.replace(/'/g, "''"));
|
|
51
|
+
requestUrl += `/Files/add(url='${encodedFileName}',overwrite=${
|
|
52
|
+
overwrite ? 'true' : 'false'
|
|
53
|
+
})`;
|
|
54
|
+
|
|
55
|
+
const digest = await fetchDigest(context.instance, context.baseURL);
|
|
56
|
+
|
|
57
|
+
const response = await context.instance.post(requestUrl, file, {
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': file.type || 'application/octet-stream',
|
|
60
|
+
'X-RequestDigest': digest,
|
|
61
|
+
},
|
|
62
|
+
withCredentials: true,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return response;
|
|
66
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -93,11 +93,16 @@ export type SHAREPOINT_VER = '2010' | '2013';
|
|
|
93
93
|
|
|
94
94
|
export type IListName<T> = Extract<keyof T, string>;
|
|
95
95
|
|
|
96
|
-
export type ItemID = string | number;
|
|
97
|
-
export type CreateFileProps = {
|
|
98
|
-
file: File;
|
|
99
|
-
itemId: ItemID;
|
|
100
|
-
};
|
|
96
|
+
export type ItemID = string | number;
|
|
97
|
+
export type CreateFileProps = {
|
|
98
|
+
file: File;
|
|
99
|
+
itemId: ItemID;
|
|
100
|
+
};
|
|
101
|
+
export type UploadFileProps = {
|
|
102
|
+
file: File;
|
|
103
|
+
folderPath?: string | string[];
|
|
104
|
+
overwrite?: boolean;
|
|
105
|
+
};
|
|
101
106
|
|
|
102
107
|
export type ListData =
|
|
103
108
|
| {
|