@lindle/sharepoint_requests 0.1.14 → 0.1.16
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 +9 -0
- package/dist/index.d.ts +17 -17
- package/dist/root/index.d.ts +86 -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/deleteFile.d.ts +3 -0
- 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 +9 -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/listRequests/utils.d.ts +2 -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 +284 -50
- 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 +284 -50
- package/dist/sharepoint_requests.esm.js.map +1 -1
- package/dist/types.d.ts +241 -232
- package/package.json +6 -3
- package/src/root/index.ts +55 -2
- package/src/root/internal/context.ts +2 -0
- package/src/root/internal/listRequests/createListItem.ts +18 -12
- package/src/root/internal/listRequests/deleteFile.ts +35 -0
- package/src/root/internal/listRequests/index.ts +2 -0
- package/src/root/internal/listRequests/updateListItem.ts +21 -3
- package/src/root/internal/listRequests/uploadFile.ts +42 -0
- package/src/root/internal/listRequests/utils.ts +33 -0
- package/src/types.ts +14 -5
|
@@ -2,30 +2,36 @@ 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
37
|
},
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { SHAREPOINT_VER, DeleteFileProps } from '../../../types';
|
|
2
|
+
import { fetchDigest } from '../digest';
|
|
3
|
+
import type { RequestContext } from '../context';
|
|
4
|
+
import { buildLibraryPath, encodeFileName } from './utils';
|
|
5
|
+
|
|
6
|
+
const SUPPORTED_VERSION: SHAREPOINT_VER = '2013';
|
|
7
|
+
|
|
8
|
+
export async function deleteFile<V extends SHAREPOINT_VER = '2013'>(
|
|
9
|
+
context: RequestContext<V>,
|
|
10
|
+
{ fileName, folderPath }: DeleteFileProps
|
|
11
|
+
) {
|
|
12
|
+
if (context.sharepointVersion !== SUPPORTED_VERSION) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
'deleteFile is only supported for SharePoint 2013 REST endpoints.'
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const encodedFileName = encodeFileName(fileName);
|
|
19
|
+
let requestUrl = `_api/web/lists/getbytitle('${context.listName}')/RootFolder`;
|
|
20
|
+
requestUrl += buildLibraryPath(folderPath);
|
|
21
|
+
requestUrl += `/Files('${encodedFileName}')`;
|
|
22
|
+
|
|
23
|
+
const digest = await fetchDigest(context.instance, context.baseURL);
|
|
24
|
+
|
|
25
|
+
const response = await context.instance.post(requestUrl, undefined, {
|
|
26
|
+
headers: {
|
|
27
|
+
'X-HTTP-Method': 'DELETE',
|
|
28
|
+
'IF-MATCH': '*',
|
|
29
|
+
'X-RequestDigest': digest,
|
|
30
|
+
},
|
|
31
|
+
withCredentials: true,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return response;
|
|
35
|
+
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { createListItem } from './createListItem';
|
|
2
2
|
export { createAttachment } from './createAttachment';
|
|
3
3
|
export { deleteItem } from './deleteItem';
|
|
4
|
+
export { deleteFile } from './deleteFile';
|
|
4
5
|
export { getFiles } from './getFiles';
|
|
5
6
|
export { getListItems } from './getListItems';
|
|
6
7
|
export { getOnly } from './getOnly';
|
|
7
8
|
export { updateListItem } from './updateListItem';
|
|
9
|
+
export { uploadFile } from './uploadFile';
|
|
@@ -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,42 @@
|
|
|
1
|
+
import type { SHAREPOINT_VER, UploadFileProps } from '../../../types';
|
|
2
|
+
import { fetchDigest } from '../digest';
|
|
3
|
+
import type { RequestContext } from '../context';
|
|
4
|
+
import { buildLibraryPath, encodeFileName } from './utils';
|
|
5
|
+
|
|
6
|
+
const SUPPORTED_VERSION: SHAREPOINT_VER = '2013';
|
|
7
|
+
|
|
8
|
+
export async function uploadFile<V extends SHAREPOINT_VER = '2013'>(
|
|
9
|
+
context: RequestContext<V>,
|
|
10
|
+
{ file, folderPath, overwrite = true }: UploadFileProps
|
|
11
|
+
) {
|
|
12
|
+
if (context.sharepointVersion !== SUPPORTED_VERSION) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
'uploadFile is only supported for SharePoint 2013 REST endpoints.'
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!file?.name) {
|
|
19
|
+
throw new Error('A file with a valid name must be provided.');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let requestUrl = `_api/web/lists/getbytitle('${context.listName}')/RootFolder`;
|
|
23
|
+
|
|
24
|
+
requestUrl += buildLibraryPath(folderPath);
|
|
25
|
+
|
|
26
|
+
const encodedFileName = encodeFileName(file.name);
|
|
27
|
+
requestUrl += `/Files/add(url='${encodedFileName}',overwrite=${
|
|
28
|
+
overwrite ? 'true' : 'false'
|
|
29
|
+
})`;
|
|
30
|
+
|
|
31
|
+
const digest = await fetchDigest(context.instance, context.baseURL);
|
|
32
|
+
|
|
33
|
+
const response = await context.instance.post(requestUrl, file, {
|
|
34
|
+
headers: {
|
|
35
|
+
'Content-Type': file.type || 'application/octet-stream',
|
|
36
|
+
'X-RequestDigest': digest,
|
|
37
|
+
},
|
|
38
|
+
withCredentials: true,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function buildLibraryPath(folderPath?: string | string[]) {
|
|
2
|
+
if (!folderPath) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const segments = Array.isArray(folderPath)
|
|
7
|
+
? folderPath
|
|
8
|
+
: folderPath.split('/').filter(Boolean);
|
|
9
|
+
|
|
10
|
+
if (!segments.length) {
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const escapedSegments = segments.map((segment) => {
|
|
15
|
+
if (!segment.trim()) {
|
|
16
|
+
throw new Error('Folder path segments cannot be empty.');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const sanitized = segment.replace(/'/g, "''");
|
|
20
|
+
|
|
21
|
+
return `/Folders('${encodeURIComponent(sanitized)}')`;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return escapedSegments.join('');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function encodeFileName(fileName: string) {
|
|
28
|
+
if (!fileName.trim()) {
|
|
29
|
+
throw new Error('File name cannot be empty.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return encodeURIComponent(fileName.replace(/'/g, "''"));
|
|
33
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -93,11 +93,20 @@ 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
|
+
};
|
|
106
|
+
export type DeleteFileProps = {
|
|
107
|
+
fileName: string;
|
|
108
|
+
folderPath?: string | string[];
|
|
109
|
+
};
|
|
101
110
|
|
|
102
111
|
export type ListData =
|
|
103
112
|
| {
|