@lindle/sharepoint_requests 0.1.16 → 0.1.18

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.
@@ -1,4 +1,9 @@
1
- import type { SHAREPOINT_VER, UploadFileProps } from '../../../types';
1
+ import type { AxiosResponse } from 'axios';
2
+ import type {
3
+ ItemID,
4
+ SHAREPOINT_VER,
5
+ UploadFileProps,
6
+ } from '../../../types';
2
7
  import { fetchDigest } from '../digest';
3
8
  import type { RequestContext } from '../context';
4
9
  import { buildLibraryPath, encodeFileName } from './utils';
@@ -8,7 +13,7 @@ const SUPPORTED_VERSION: SHAREPOINT_VER = '2013';
8
13
  export async function uploadFile<V extends SHAREPOINT_VER = '2013'>(
9
14
  context: RequestContext<V>,
10
15
  { file, folderPath, overwrite = true }: UploadFileProps
11
- ) {
16
+ ): Promise<AxiosResponse & { itemId?: ItemID }> {
12
17
  if (context.sharepointVersion !== SUPPORTED_VERSION) {
13
18
  throw new Error(
14
19
  'uploadFile is only supported for SharePoint 2013 REST endpoints.'
@@ -26,17 +31,39 @@ export async function uploadFile<V extends SHAREPOINT_VER = '2013'>(
26
31
  const encodedFileName = encodeFileName(file.name);
27
32
  requestUrl += `/Files/add(url='${encodedFileName}',overwrite=${
28
33
  overwrite ? 'true' : 'false'
29
- })`;
34
+ })?$expand=ListItemAllFields&$select=ListItemAllFields/Id`;
30
35
 
31
36
  const digest = await fetchDigest(context.instance, context.baseURL);
32
37
 
33
- const response = await context.instance.post(requestUrl, file, {
38
+ const response = (await context.instance.post(requestUrl, file, {
34
39
  headers: {
35
40
  'Content-Type': file.type || 'application/octet-stream',
36
41
  'X-RequestDigest': digest,
37
42
  },
38
43
  withCredentials: true,
39
- });
44
+ })) as AxiosResponse & { itemId?: ItemID };
45
+
46
+ const responseData = response.data?.d ?? response.data;
47
+ const listItemFields = responseData?.ListItemAllFields;
48
+ const itemId =
49
+ listItemFields?.Id ??
50
+ listItemFields?.ID ??
51
+ responseData?.Id ??
52
+ responseData?.ID;
53
+
54
+ if (typeof itemId !== 'undefined') {
55
+ response.itemId = itemId;
56
+ } else if (listItemFields?.__deferred?.uri) {
57
+ const listItemResponse = await context.instance.get(
58
+ listItemFields.__deferred.uri
59
+ );
60
+ const deferredId =
61
+ listItemResponse.data?.d?.Id ?? listItemResponse.data?.Id;
62
+
63
+ if (typeof deferredId !== 'undefined') {
64
+ response.itemId = deferredId;
65
+ }
66
+ }
40
67
 
41
68
  return response;
42
69
  }
package/src/types.ts CHANGED
@@ -108,20 +108,11 @@ export type DeleteFileProps = {
108
108
  folderPath?: string | string[];
109
109
  };
110
110
 
111
- export type ListData =
112
- | {
113
- [key: string]: any;
114
- __metadata?: {
115
- type: string;
116
- };
117
- }
118
- | any;
119
-
120
- export type EmailProps = {
121
- From: string;
122
- To: string[] | string;
123
- Subject?: string;
124
- Body: string;
111
+ export type EmailProps = {
112
+ From: string;
113
+ To: string[] | string;
114
+ Subject?: string;
115
+ Body: string;
125
116
  };
126
117
 
127
118
  export type _Deferred = {
@@ -130,16 +121,54 @@ export type _Deferred = {
130
121
  };
131
122
  };
132
123
 
133
- export type LookupField<T> = _Deferred & T;
134
- export type ChoiceField<T> = _Deferred & {
135
- Value: T;
136
- __metadata: Metadata;
137
- };
138
-
139
- export type PersonField = {
140
- AboutMe?: string;
141
- Account: string;
142
- AccountName: string;
124
+ declare const LOOKUP_FIELD_TAG: unique symbol;
125
+ type LookupFieldMarker = {
126
+ [LOOKUP_FIELD_TAG]?: true;
127
+ };
128
+
129
+ export type LookupField<T> = _Deferred & T & LookupFieldMarker;
130
+ export type ChoiceField<T> = _Deferred & {
131
+ Value: T;
132
+ __metadata: Metadata;
133
+ };
134
+
135
+ type MetadataEnvelope = {
136
+ __metadata?: {
137
+ type: string;
138
+ };
139
+ };
140
+
141
+ type EnsureRecord<T> = T extends Record<string, unknown>
142
+ ? T
143
+ : Record<string, unknown>;
144
+
145
+ type LookupCreateValue =
146
+ | number
147
+ | string
148
+ | null
149
+ | {
150
+ lookupId: number | string;
151
+ }
152
+ | {
153
+ LookupId: number | string;
154
+ }
155
+ | {
156
+ results: (number | string)[];
157
+ };
158
+
159
+ type NormalizeField<T> = T extends LookupField<unknown>
160
+ ? LookupCreateValue | LookupField<unknown>
161
+ : T;
162
+
163
+ export type ListData<T = Record<string, unknown>> = Partial<{
164
+ [K in keyof EnsureRecord<T>]: NormalizeField<EnsureRecord<T>[K]>;
165
+ }> &
166
+ MetadataEnvelope;
167
+
168
+ export type PersonField = {
169
+ AboutMe?: string;
170
+ Account: string;
171
+ AccountName: string;
143
172
  AdministrativeSupervisor: string;
144
173
  AskMeAbout?: string;
145
174
  AskMeAbout0?: string;