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 pathoneNoteApiBase.ts
More file actions
175 lines (146 loc) · 5.27 KB
/
oneNoteApiBase.ts
File metadata and controls
175 lines (146 loc) · 5.27 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
import {ErrorUtils, RequestErrorType, RequestError} from "./errorUtils";
import * as ContentType from "content-type";
import {Promise} from "es6-promise";
export type XHRData = ArrayBufferView | Blob | Document | string | FormData;
export interface ResponsePackage<T> {
parsedResponse: T;
request: XMLHttpRequest;
}
/**
* Base communication layer for talking to the OneNote APIs.
*/
export class OneNoteApiBase {
// Whether or not the OneNote Beta APIs should be used.
public useBetaApi: boolean = false;
private authHeader: string;
private timeout: number;
private headers: { [key: string]: string };
private oneNoteApiHostOverride: string;
private queryParams: { [key: string]: string };
constructor(authHeader: string, timeout: number, headers: { [key: string]: string } = {}, oneNoteApiHostOverride: string = null, queryParams: { [key: string]: string } = null) {
this.authHeader = authHeader;
this.timeout = timeout;
this.headers = headers;
this.oneNoteApiHostOverride = oneNoteApiHostOverride;
this.queryParams = queryParams;
}
protected requestPromise(url: string, data?: XHRData, contentType?: string, httpMethod?: string, isFullUrl?: boolean, urlContainsVersion?: boolean): Promise<ResponsePackage<any>> {
let fullUrl;
if (isFullUrl) {
fullUrl = url;
} else {
fullUrl = this.generateFullUrl(url, urlContainsVersion);
}
if (!contentType) {
contentType = "application/json";
}
return new Promise(((resolve: (responsePackage: ResponsePackage<any>) => void, reject: (error: RequestError) => void) => {
this.makeRequest(fullUrl, data, contentType, httpMethod).then((responsePackage: ResponsePackage<any>) => {
resolve(responsePackage);
}, (error: RequestError) => {
reject(error);
});
}));
}
private appendQueryParams(url: string): string {
let queryParams = this.queryParams;
if (!queryParams) {
return url;
}
let queryParamArray = [];
for (const key in queryParams) {
if (queryParams.hasOwnProperty(key)) {
const queryParamValue = encodeURIComponent(queryParams[key]);
queryParamArray.push(key + "=" + queryParamValue);
}
}
const serializedQueryParams = queryParamArray.join("&");
if (url.indexOf("?") === -1) {
return url + "?" + serializedQueryParams;
} else {
return url + "&" + serializedQueryParams;
}
}
private generateUrlUntilVersion(urlContainsVersion?: boolean) {
let apiHost;
if (this.oneNoteApiHostOverride) {
apiHost = this.oneNoteApiHostOverride;
} else {
apiHost = "https://www.onenote.com";
}
let apiVersionPortion = "";
if (!urlContainsVersion) {
apiVersionPortion = this.useBetaApi ? "/api/beta" : "/api/v1.0";
}
return apiHost + apiVersionPortion;
}
public generateFullUrl(partialUrl: string, urlContainsVersion?: boolean): string {
return this.generateUrlUntilVersion(urlContainsVersion) + partialUrl;
}
public generateFullMeNotesUrl(partialUrl: string, urlContainsVersion?: boolean): string {
return this.generateUrlUntilVersion(urlContainsVersion) + "/me/notes" + partialUrl;
}
private makeRequest(url: string, data?: XHRData, contentType?: string, httpMethod?: string): Promise<ResponsePackage<any>> {
return new Promise((resolve: (responsePackage: ResponsePackage<any>) => void, reject: (error: RequestError) => void) => {
let request = new XMLHttpRequest();
let type: string;
if (!!httpMethod) {
type = httpMethod;
} else {
type = data ? "POST" : "GET";
}
request.open(type, url);
request.timeout = this.timeout;
request.onload = () => {
// TODO: more status code checking
if (request.status === 200 || request.status === 201 || request.status === 204) {
try {
let contentTypeOfResponse: ContentType.MediaType = { type: "" };
try {
contentTypeOfResponse = ContentType.parse(request.getResponseHeader("Content-Type"));
} catch (ex) {
// Patch requests do not return a content type, so this is ok.
}
let response = request.response;
switch (contentTypeOfResponse.type) {
case "application/json":
response = JSON.parse(request.response ? request.response : "{}");
break;
case "text/html":
default:
response = request.response;
}
resolve({ parsedResponse: response, request: request });
} catch (e) {
reject(ErrorUtils.createRequestErrorObject(request, RequestErrorType.UNABLE_TO_PARSE_RESPONSE));
}
} else {
reject(ErrorUtils.createRequestErrorObject(request, RequestErrorType.UNEXPECTED_RESPONSE_STATUS));
}
};
request.onerror = () => {
reject(ErrorUtils.createRequestErrorObject(request, RequestErrorType.NETWORK_ERROR));
};
request.ontimeout = () => {
reject(ErrorUtils.createRequestErrorObject(request, RequestErrorType.REQUEST_TIMED_OUT));
};
if (contentType) {
request.setRequestHeader("Content-Type", contentType);
}
if (this.authHeader) {
request.setRequestHeader("Authorization", this.authHeader);
}
OneNoteApiBase.addHeadersToRequest(request, this.headers);
request.send(data);
});
}
private static addHeadersToRequest(openRequest: XMLHttpRequest, headers: { [key: string]: string }) {
if (headers) {
for (let key in headers) {
if (headers.hasOwnProperty(key)) {
openRequest.setRequestHeader(key, headers[key]);
}
}
}
}
}