forked from jasonjoh/node-outlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-2.js
More file actions
286 lines (252 loc) · 9.84 KB
/
version-2.js
File metadata and controls
286 lines (252 loc) · 9.84 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See full license at the bottom of this file.
var request = require('request');
var uuid = require('node-uuid');
var utilities = require('./utilities.js');
var fiddlerEnabled = false;
var traceFunction = undefined;
var endpoint = 'https://outlook.office.com/api/v1.0';
var defaultAnchor = '';
var defaultTimeZone = '';
/**
* @module base
*/
module.exports = {
/**
* Used to do the actual send of a REST request to the REST endpoint.
*
* @param parameters {object} An object containing all of the relevant parameters. Possible values:
* @param parameters.url {string} The full URL of the API endpoint
* @param parameters.token {string} The access token for authentication
* @param [parameters.user.email] {string} The user's SMTP email address, used to set the `X-AnchorMailbox` header.
* @param [parameters.user.timezone] {string} The user's time zone, used to set the `outlook.timezone` `Prefer` header.
* @param [parameters.method] {string} Used to specify the HTTP method. Default is 'GET'.
* @param [parameters.query] {object} An object containing key/value pairs. The pairs will be serialized into a query string.
* @param [parameters.payload] {object} A JSON-serializable object representing the request body.
* @param [parameters.headers] {object} A JSON-serializable object representing custom headers to send with the request.
* @param [callback] {function} A callback function that is called when the function completes. It should have the signature `function (error, result)`.
*/
makeApiCall: function (parameters, callback) {
// Check required parameters
if (parameters.url === undefined || parameters.token === undefined) {
trace('makeApiCall - ERROR: Missing required parameter');
if (typeof callback === 'function') {
callback('ERROR: You must include the \'url\' and \'token\' parameters.');
}
return;
}
var method = parameters.method === undefined ? 'GET' : parameters.method;
trace('url: ' + parameters.url);
trace('token: ' + parameters.token);
trace('method: ' + method);
var auth = {
'bearer': parameters.token
};
var headers = parameters.headers || {};
headers['Accept'] = headers['Accept'] || 'application/json';
headers['User-Agent'] = headers['User-Agent'] || 'node-outlook/2.0';
headers['client-request-id'] = headers['client-request-id'] || uuid.v4();
headers['return-client-request-id'] = headers['return-client-request-id'] || 'true';
// Determine if we have an anchor mailbox to use
// Passed parameter has greater priority than module-level default
var anchorMbx = '';
if (parameters.user && parameters.user.email && parameters.user.email.length > 0) {
anchorMbx = parameters.user.email;
}
else {
anchorMbx = defaultAnchor;
}
if (anchorMbx.length > 0) {
headers['X-Anchor-Mailbox'] = anchorMbx;
}
// Determine if we have a time zone to use
// Passed parameter has greater priority than module-level default
var timezone = '';
if (parameters.user && parameters.user.timezone && parameters.user.timezone.length > 0) {
timezone = parameters.user.timezone;
}
else {
timezone = defaultTimeZone;
}
if (timezone.length > 0) {
headers['Prefer'] = headers['Prefer'] || [];
headers['Prefer'].push('outlook.timezone = "' + timezone + '"');
}
var options = {
method: method,
url: parameters.url,
headers: headers,
auth: auth,
json: true
};
if (parameters.query !== undefined) {
trace('query:' + JSON.stringify(parameters.query));
options['qs'] = parameters.query;
}
if (fiddlerEnabled) {
options['proxy'] = 'http://127.0.0.1:8888';
options['strictSSL'] = false;
}
if (method.toUpperCase() === 'POST' || method.toUpperCase() === 'PATCH') {
if (parameters.payload !== undefined) {
trace('payload:' + JSON.stringify(parameters.payload));
}
options['body'] = parameters.payload;
}
request(options, function(error, response, body) {
if (typeof callback === 'function') {
callback(error, response);
}
});
},
/**
* Used to get information about a user.
*
* @param parameters {object} An object containing all of the relevant parameters. Possible values:
* @param parameters.token {string} The access token.
* @param [parameters.useMe] {boolean} If true, use the `/Me` segment instead of the `/Users/<email>` segment. This parameter defaults to false and is ignored if the `parameters.user.email` parameter isn't provided (the `/Me` segment is always used in this case).
* @param [parameters.user.email] {string} The SMTP address of the user. If absent, the `/Me` segment is used in the API URL.
* @param [parameters.odataParams] {object} An object containing key/value pairs representing OData query parameters. See [Use OData query parameters]{@link https://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#UseODataqueryparameters} for details.
* @param [callback] {function} A callback function that is called when the function completes. It should have the signature `function (error, result)`.
*
* @example var outlook = require('node-outlook');
*
* // Set the API endpoint to use the v2.0 endpoint
* outlook.base.setApiEndpoint('https://outlook.office.com/api/v2.0');
*
* // This is the oAuth token
* var token = 'eyJ0eXAiOiJKV1Q...';
*
* // Set up oData parameters
* var queryParams = {
* '$select': 'DisplayName, EmailAddress',
* };
*
* outlook.base.getUser({token: token, odataParams: queryParams},
* function(error, result) {
* if (error) {
* console.log('getUser returned an error: ' + error);
* }
* else if (result) {
* console.log('User name:', result.DisplayName);
* console.log('User email:', result.EmailAddress);
* }
* });
*/
getUser: function(parameters, callback) {
var userSpec = utilities.getUserSegment(parameters);
var requestUrl = this.apiEndpoint() + userSpec;
var apiOptions = {
url: requestUrl,
token: parameters.token,
user: parameters.user
};
if (parameters.odataParams !== undefined) {
apiOptions['query'] = parameters.odataParams;
}
this.makeApiCall(apiOptions, function(error, response) {
if (error) {
if (typeof callback === 'function') {
callback(error, response);
}
}
else if (response.statusCode !== 200) {
if (typeof callback === 'function') {
callback('REST request returned ' + response.statusCode + '; body: ' + JSON.stringify(response.body), response);
}
}
else {
if (typeof callback === 'function') {
callback(null, response.body);
}
}
});
},
/**
* Used to provide a tracing function.
*
* @param traceFunc {function} A function that takes a string parameter. The string parameter contains the text to add to the trace.
*/
setTraceFunc: function(traceFunc) {
traceFunction = traceFunc;
},
/**
* Used to enable network sniffing with Fiddler.
*
* @param enabled {boolean} `true` to enable default Fiddler proxy and disable SSL verification. `false` to disable proxy and enable SSL verification.
*/
setFiddlerEnabled: function(enabled) {
fiddlerEnabled = enabled;
},
/**
* Gets the API endpoint URL.
* @return {string}
*/
apiEndpoint: function() {
return endpoint;
},
/**
* Sets the API endpoint URL. If not called, the default of `https://outlook.office.com/api/v1.0` is used.
*
* @param newEndPoint {string} The API endpoint URL to use.
*/
setApiEndpoint: function(newEndPoint) {
endpoint = newEndPoint;
},
/**
* Gets the default anchor mailbox address.
* @return {string}
*/
anchorMailbox: function() {
return defaultAnchor;
},
/**
* Sets the default anchor mailbox address.
*
* @param newAnchor {string} The SMTP address to send in the `X-Anchor-Mailbox` header.
*/
setAnchorMailbox: function(newAnchor) {
defaultAnchor = newAnchor;
},
/**
* Gets the default preferred time zone.
* @return {string}
*/
preferredTimeZone: function() {
return defaultTimeZone;
},
/**
* Sets the default preferred time zone.
*
* @param preferredTimeZone {string} The time zone in which the server should return date time values.
*/
setPreferredTimeZone: function(preferredTimeZone) {
defaultTimeZone = preferredTimeZone;
}
};
/**
* @private
*/
function trace(message) {
if (typeof traceFunction === 'function') {
traceFunction(message);
}
}
/*
MIT License:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
""Software""), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/