This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathoneNoteApi.ts
More file actions
261 lines (219 loc) · 7.87 KB
/
oneNoteApi.ts
File metadata and controls
261 lines (219 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import {IOneNoteApi} from "./iOneNoteApi";
import {OneNoteApiBase, ResponsePackage, XHRData} from "./oneNoteApiBase";
import {OneNotePage} from "./oneNotePage";
import {BatchRequest} from "./batchRequest";
import {Revision} from "./structuredTypes";
/**
* Wrapper for easier calling of the OneNote APIs.
*/
export class OneNoteApi extends OneNoteApiBase implements IOneNoteApi {
constructor(authHeader: string, timeout = 30000, headers: { [key: string]: string } = {}, oneNoteApiHostOverride: string = null, queryParams: { [key: string]: string } = null) {
super(authHeader, timeout, headers, oneNoteApiHostOverride, queryParams);
}
/**
* CreateNotebook
*/
public createNotebook(name: string): Promise<ResponsePackage<any>> {
let data = JSON.stringify({ name: name });
return this.requestPromise(this.getNotebooksUrl(), data);
}
/**
* CreatePage
*/
public createPage(page: OneNotePage, sectionId?: string): Promise<ResponsePackage<any>> {
let sectionPath = sectionId ? "/sections/" + sectionId : "";
let url = sectionPath + "/pages";
let form = page.getTypedFormData();
return this.requestPromise(url, form.asBlob(), form.getContentType());
}
/**
* GetRecentNotebooks
*/
public getRecentNotebooks(includePersonal: boolean): Promise<ResponsePackage<any>> {
let url = "/me/notes/notebooks/Microsoft.OneNote.Api.GetRecentNotebooks(includePersonalNotebooks=" + includePersonal + ")";
return this.requestPromise(url);
}
/**
* GetWopiProperties
*/
public getNotebookWopiProperties(notebookSelfPath: string, frameAction: string): Promise<ResponsePackage<any>> {
let url = notebookSelfPath + "/Microsoft.OneNote.Api.GetWopiProperties(frameAction='" + frameAction + "')";
return this.requestPromise(url, null, null, null, null, true /* URL contains version */);
}
/**
* GetNotebooksFromWebUrls
*/
public getNotebooksFromWebUrls(notebookWebUrls: string[]): Promise<ResponsePackage<any>> {
let url = "/me/notes/notebooks/Microsoft.OneNote.Api.GetNotebooksFromWebUrls()";
const payload = {
webUrls: notebookWebUrls
};
const oldUseBetaApi = this.useBetaApi;
this.useBetaApi = true; // This API is only supported in beta
const returnValue = this.requestPromise(url, JSON.stringify(payload));
this.useBetaApi = oldUseBetaApi;
return returnValue;
}
/**
* SendBatchRequest
**/
public sendBatchRequest(batchRequest: BatchRequest): Promise<ResponsePackage<any>> {
this.enableBetaApi();
return this.requestPromise("/$batch", batchRequest.getRequestBody(), batchRequest.getContentType(), "POST").then(this.disableBetaApi.bind(this));
}
/**
* GetPage
*/
public getPage(pageId: string): Promise<ResponsePackage<any>> {
let pagePath = "/me/notes/pages/" + pageId;
return this.requestPromise(pagePath);
}
/**
* GetPageContent
*/
public getPageContent(pageId: string): Promise<ResponsePackage<any>> {
let pagePath = "/me/notes/pages/" + pageId + "/content";
return this.requestPromise(pagePath);
}
/**
* GetPages
*/
public getPages(options: { top?: number, sectionId?: string }): Promise<ResponsePackage<any>> {
let pagePath = "/pages";
if (options.top > 0 && options.top === Math.floor(options.top)) {
pagePath += "?top=" + options.top;
}
if (options.sectionId) {
pagePath = "/me/notes/sections/" + options.sectionId + pagePath;
}
return this.requestPromise(pagePath);
}
/**
* UpdatePage
*/
public updatePage(pageId: string, revisions: Revision[]): Promise<ResponsePackage<any>> {
let pagePath = "/me/notes/pages/" + pageId;
let url = pagePath + "/content";
return this.requestPromise(url, JSON.stringify(revisions), "application/json", "PATCH");
}
/**
* CreateSection
*/
public createSection(notebookId: string, name: string): Promise<ResponsePackage<any>> {
let obj: Object = { name: name };
let data = JSON.stringify(obj);
return this.requestPromise("/me/notes/notebooks/" + notebookId + "/sections/", data);
}
/**
* CreateSectionUnderSectionGroup
*/
public createSectionUnderSectionGroup(sectionGroupId: string, name: string): Promise<ResponsePackage<any>> {
let obj: Object = { name: name };
let data = JSON.stringify(obj);
return this.requestPromise("/me/notes/sectionGroups/" + sectionGroupId + "/sections/", data);
}
/**
* GetNotebooks
*/
public getNotebooks(excludeReadOnlyNotebooks = true): Promise<ResponsePackage<any>> {
return this.requestPromise(this.getNotebooksUrl(null /*expands*/, excludeReadOnlyNotebooks));
}
/**
* GetNotebooksWithExpandedSections
*/
public getNotebooksWithExpandedSections(expands = 2, excludeReadOnlyNotebooks = true): Promise<ResponsePackage<any>> {
return this.requestPromise(this.getNotebooksUrl(expands, excludeReadOnlyNotebooks));
}
/**
* GetNotebooksWithExpandedSections
*/
public getNotebookBySelfUrl(selfUrl: string, expands = 2): Promise<ResponsePackage<any>> {
return this.requestPromise(selfUrl + "?" + this.getExpands(expands), null, null, null, true /* isFullUrl */);
}
/**
* GetNotebookbyName
*/
public getNotebookByName(name: string): Promise<ResponsePackage<any>> {
return this.requestPromise("/me/notes/notebooks?filter=name%20eq%20%27" + encodeURI(name) + "%27&orderby=createdTime");
}
/**
* GetDefaultNotebook
*/
public getDefaultNotebook(): Promise<ResponsePackage<any>> {
return this.requestPromise("/me/notes/notebooks?filter=isDefault%20eq%20true%20");
}
/**
* PagesSearch
*/
public pagesSearch(query: string): Promise<ResponsePackage<any>> {
return this.requestPromise(this.getSearchUrl(query));
}
/**
* Method that can be used to send any HTTP request
*/
public performApiCall(url: string, data?: XHRData, contentType?: string, httpMethod?: string, isFullUrl?: boolean, urlContainsVersion?: boolean): Promise<ResponsePackage<any>> {
return this.requestPromise(url, data, contentType, httpMethod, isFullUrl, urlContainsVersion);
}
/**
* Get site information for a site
*/
public getSiteLocationFromUrl(url: string): Promise<ResponsePackage<any>> {
const escapeAposForOData = url.replace(/'/g, "\"");
const encodeUriComponent = encodeURIComponent(escapeAposForOData);
const endpointUrl = "/myOrganization/siteCollections/FromUrl(url='" + encodeUriComponent + "')";
return this.requestPromise(endpointUrl);
}
/**
* create a group notebook
*/
public createGroupNotebook(name: string, groupId: string): Promise<ResponsePackage<any>> {
const data = JSON.stringify({ name: name });
return this.requestPromise("/myOrganization/groups/" + groupId + "/notes/notebooks", data);
}
/**
* GetExpands
*
* Nest expands so we can get notebook elements (sections and section groups) in
* the same call that we get notebooks.
*
* expands specifies how many levels deep to return.
*/
private getExpands(expands: number): string {
if (expands <= 0) {
return "";
}
let s = "$expand=sections,sectionGroups";
return expands === 1 ? s : s + "(" + this.getExpands(expands - 1) + ")";
}
/**
* GetNotebooksUrl
*/
private getNotebooksUrl(numExpands = 0, excludeReadOnlyNotebooks = true): string {
// Since this url is most often used to save content to a specific notebook, by default
// it does not include a notebook where user has Read only permissions.
let filter = (excludeReadOnlyNotebooks) ? "$filter=userRole%20ne%20Microsoft.OneNote.Api.UserRole'Reader'" : "";
return "/me/notes/notebooks?" + filter + (numExpands ? "&" + this.getExpands(numExpands) : "");
}
/**
* GetSearchUrl
*/
private getSearchUrl(query: string): string {
return "/me/notes/pages?search=" + query;
}
/**
* Helper Method to use beta features OR to use beta endpoints
*/
private enableBetaApi() {
this.useBetaApi = true;
}
/**
* Helper method to turn off beta features OR endpoints
*/
private disableBetaApi() {
this.useBetaApi = false;
}
}
export {ContentType} from "./contentType";
export {OneNotePage} from "./oneNotePage";
export {BatchRequest} from "./batchRequest";
export { ErrorUtils, RequestErrorType } from "./errorUtils";