forked from cerealcoder/dexcom-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
583 lines (532 loc) · 19.2 KB
/
index.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* This package provides an API that is a subset of the Dexcom public API. It handles the HTTP layer, as well as the
* refreshing of expired OAuth 2.0 access tokens. The public API for this package is described in the top-level
* README file.
*/
'use strict';
//***********
//* Modules *
//***********
const httpClient = require('axios');
const querystring = require('querystring');
const helpers = require('./helpers.js');
const _ = require('lodash');
//**************
//* Public API *
//**************
const DexcomJS = Object.create({});
module.exports = DexcomJS;
//**********************
//* Private Properties *
//**********************
DexcomJS.options = {
clientId: '',
clientSecret: '',
redirectUri: '',
apiUri: 'https://sandbox-api.dexcom.com',
};
//********************
//* Public Functions *
//********************
/**
* Sets this package's options for accessing the Dexcom platform API.
*
* @param newOptions
* An object that conforms to the following format:
* {
* clientId: string,
* clientSecret: string,
* redirectUri: string,
* apiUri: string
* }
*/
DexcomJS.setOptions = function(newOptions) {
helpers.validateOptions(newOptions);
this.options = newOptions;
};
/**
* Obtains a Dexcom OAuth 2.0 access token for the Dexcom "sandbox" data.
*
* @see https://developer.dexcom.com/sandbox-data
*
* @param user
* the sandbox user name such as 'SandBoxUser1', 'SandBoxser2', ..., 'SandBoxUser6').
* WAS: The Sandbox authorization code ('authcode1', 'authcode2', ..., 'authcode6').
*
* @returns a Promise that wraps an Object of the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* Notes:
*
* 1. Dexcom access tokens are not guaranteed to be JSON Web Tokens (JWTs).
* 2. The timestamp property represents the time, in epoch milliseconds, when the Dexcom access token was obtained.
* Downstream users may use the timestamp and the expires_in values to determine if the Dexcom access token has
* expired and must be refreshed.
*/
DexcomJS.getSandboxAuthenticationToken = async function(user) {
helpers.validateOptions(this.options);
helpers.validateSandboxAuthcode(user);
// map over old method (authcode) to new method that uses username as of 4/27/19
let userId = user;
const origTestApiAuthcode = 'authcode';
const lastLetter = user.substring(user.length - 1);
if (user.includes(origTestApiAuthcode)) {
userId = 'SandboxUser' + lastLetter;
}
// Issue an HTTP POST to the Dexcom system to obtain the sandbox access token.
const form = {
userId: userId,
clientId: this.options.clientId,
redirectURI: this.options.redirectUri,
state: '',
scope: [
'egv',
'calibrations',
'devices',
'dataRange',
'events',
'statistics',
]
};
//console.log(form);
const result = await httpClient.post('https://developer-portal-dot-g5-dexcom-prod-us-5.appspot.com/consent', form);
//console.log(result);
//console.log(result.status);
//console.log(result.data);
const authCode = result.data.authCode;
const urlEncodedForm = querystring.stringify({
client_id: this.options.clientId,
client_secret: this.options.clientSecret,
code: authCode,
grant_type: 'authorization_code',
redirect_uri: this.options.redirectUri,
});
const httpConfig = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
const authResult = await httpClient.post('https://sandbox-api.dexcom.com/v2/oauth2/token', urlEncodedForm);
//console.log(authResult);
//console.log(authResult.data);
return {
timestamp: new Date().getTime(),
dexcomOAuthToken: authResult.data,
};
}
/**
* @brief Gets the Dexcom estimated glucose values for a particular date range.
*
* @param oauthTokens
* An object that conforms to the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* @param startTime
* A number that represents the UTC epoch time, in milliseconds, of the beginning of the time window for which to
* acquire estimated glucose values.
*
* @param endTime
* A number that represents the UTC epoch time, in milliseconds, of the end of the time window for which to
* acquire estimated glucose values.
*
* @returns a Promise that wraps an object of the following format:
* {
* estimatedGlucoseValues: {<object returned by Dexcom API>},
* oauthTokens: {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
* }
*
* Note that the oauthTokens property will exist only if the access token was refreshed.
*
* @see https://developer.dexcom.com/get-egvs
*/
DexcomJS.getEstimatedGlucoseValues = async function(oauthTokens, startTime, endTime) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
helpers.validateTimeWindow(startTime, endTime);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
const startDateString = helpers.dexcomifyEpochTime(startTime);
const endDateString = helpers.dexcomifyEpochTime(endTime);
const parameters = { startDate: startDateString, endDate: endDateString };
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}, params: parameters };
const result = await httpClient.get(`${this.options.apiUri}/v2/users/self/egvs`, httpConfig);
const returnValue = {estimatedGlucoseValues: result.data};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
DexcomJS.getEstimatedGlucoseValuesAnyDateRange = async function(oauthTokens, startTime, endTime) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
helpers.validateTimeWindow(startTime, endTime);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
//
// split the data range up into 89 day ranges (90 is the max, we're skirting around rounding issues by going one less than that)
//
const maxTimeRangeMilliSec = 89 * 86400 * 1000;
let timeIndex = startTime;
const times = [];
while (timeIndex <= endTime) {
let endTimeWindow = timeIndex + maxTimeRangeMilliSec;
endTimeWindow = endTimeWindow <= endTime? endTimeWindow : endTime;
times.push({
startTime: timeIndex,
endTime: endTimeWindow,
});
timeIndex = timeIndex + maxTimeRangeMilliSec;
}
const requests = times.map(el => {
const startDateString = helpers.dexcomifyEpochTime(el.startTime);
const endDateString = helpers.dexcomifyEpochTime(el.endTime);
const parameters = { startDate: startDateString, endDate: endDateString };
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}, params: parameters };
return httpClient.get(`${this.options.apiUri}/v2/users/self/egvs`, httpConfig);
});
const results = await Promise.all(requests);
const combinedEgvs = results.reduce((a, el) => {
// @see https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays
return a.concat(el.data.egvs);
}, []);
const returnValue = {
estimatedGlucoseValues: {
unit: results[0].unit, // ugh, 0 may not exist, and each fetched time period in theory could have different units
rateUnit: results[0].rateUnit, // ugh, 0 may not exist, and each fetched time period in theory could have different units
egvs: combinedEgvs,
}
};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
/*
* @brief Gets the Dexcom user-specified events for a particular date range.
*
* @param oauthTokens
* An object that conforms to the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* @param startTime
* A number that represents the UTC epoch time, in milliseconds, of the beginning of the time window for which to
* acquire user-specified events.
*
* @param endTime
* A number that represents the UTC epoch time, in milliseconds, of the end of the time window for which to
* acquire user-specified events.
*
* @returns a Promise that wraps an object of the following format:
* {
* events: {<object returned by Dexcom API>},
* oauthTokens: {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
* }
*
* Note that the oauthTokens property will exist only if the access token was refreshed.
*
* @see https://developer.dexcom.com/get-events
*/
DexcomJS.getEvents = async function(oauthTokens, startTime, endTime) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
helpers.validateTimeWindow(startTime, endTime);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
const startDateString = helpers.dexcomifyEpochTime(startTime);
const endDateString = helpers.dexcomifyEpochTime(endTime);
const parameters = { startDate: startDateString, endDate: endDateString };
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}, params: parameters };
const result = await httpClient.get(`${this.options.apiUri}/v2/users/self/events`, httpConfig);
const returnValue = {events: result.data};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
/**
* @brief Gets a Dexcom user's earliest and latest times for calibration, EGV, and event records.
*
* @param oauthTokens
* An object that conforms to the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* @returns a Promise that wraps an object of the following format:
* {
* dataRange: {<object returned by Dexcom API>},
* oauthTokens: {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
* }
*
* Note that the oauthTokens property will exist only if the access token was refreshed.
*
* @see https://developer.dexcom.com/get-datarange
*/
DexcomJS.getDataRange = async function(oauthTokens) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}};
const result = await httpClient.get(`${this.options.apiUri}/v2/users/self/dataRange`, httpConfig);
const returnValue = {dataRange: result.data};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
/**
* @brief Gets a Dexcom user's of a user's calibration events.
*
* @param oauthTokens
* An object that conforms to the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* @param startTime
* A number that represents the UTC epoch time, in milliseconds, of the beginning of the time window for which to
* acquire calibration events.
*
* @param endTime
* A number that represents the UTC epoch time, in milliseconds, of the end of the time window for which to
* acquire calibration events.
*
* @returns a Promise that wraps an object of the following format:
* {
* calibrations: {<object returned by Dexcom API>},
* oauthTokens: {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
* }
*
* Note that the oauthTokens property will exist only if the access token was refreshed.
*
* @see https://developer.dexcom.com/get-calibrations
*/
DexcomJS.getCalibrations = async function(oauthTokens, startTime, endTime) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
helpers.validateTimeWindow(startTime, endTime);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
const startDateString = helpers.dexcomifyEpochTime(startTime);
const endDateString = helpers.dexcomifyEpochTime(endTime);
const parameters = { startDate: startDateString, endDate: endDateString };
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}, params: parameters };
const result = await httpClient.get(`${this.options.apiUri}/v2/users/self/calibrations`, httpConfig);
const returnValue = {calibrations: result.data};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
/**
* @brief Gets a Dexcom user's summary statistics, including averages, quartiles, and measures of variance.
*
* @param oauthTokens
* An object that conforms to the following format:
* {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
*
* @param startTime
* A number that represents the UTC epoch time, in milliseconds, of the beginning of the time window for which to
* acquire statistics.
*
* @param endTime
* A number that represents the UTC epoch time, in milliseconds, of the end of the time window for which to
* acquire statistics.
*
* @returns a Promise that wraps an object of the following format:
* {
* statistics: {<object returned by Dexcom API>},
* oauthTokens: {
* "timestamp": epochMilliseconds,
* "dexcomOAuthToken": {
* "access_token": "your access token",
* "expires_in": timeToLiveInSeconds,
* "token_type": "Bearer",
* "refresh_token": "your refresh token"
* }
* }
* }
*
* Note that the oauthTokens property will exist only if the access token was refreshed.
*
* @see https://developer.dexcom.com/post-statistics
*/
DexcomJS.getStatistics = async function(oauthTokens, startTime, endTime) {
helpers.validateOptions(this.options);
helpers.validateOAuthTokens(oauthTokens);
helpers.validateTimeWindow(startTime, endTime);
const possiblyRefreshedOauthTokens = await helpers.refreshAccessToken(this.options, oauthTokens, false);
const startDateString = helpers.dexcomifyEpochTime(startTime);
const endDateString = helpers.dexcomifyEpochTime(endTime);
const parameters = { startDate: startDateString, endDate: endDateString };
const httpConfig = { headers: {Authorization: `Bearer ${possiblyRefreshedOauthTokens.dexcomOAuthToken.access_token}`}, params: parameters };
const requestBody = {
targetRanges: [
{
name: 'day',
startTime: '07:00:00',
endTime: '20:00:00',
egvRanges: [
{
name: 'urgentLow',
bound: 55,
},
{
name: 'low',
bound: 70,
},
{
name: 'high',
bound: 180,
},
]
},
{
name: 'night',
startTime: '20:00:00',
endTime: '07:00:00',
egvRanges: [
{
name: 'urgentLow',
bound: 55,
},
{
name: 'low',
bound: 80,
},
{
name: 'high',
bound: 200,
},
]
},
]
};
const result = await httpClient.post(`${this.options.apiUri}/v2/users/self/statistics`, requestBody, httpConfig);
const returnValue = {statistics: result.data};
if (possiblyRefreshedOauthTokens !== oauthTokens) {
returnValue['oauthTokens'] = possiblyRefreshedOauthTokens;
}
return returnValue;
};
/*
* @brief split data up into an array of daily arrays, sorted by time in forward order
*
* @param input
* Data as received from Dexcom via a function in this modulee
*
* @see https://developer.dexcom.com/get-events
*/
DexcomJS.shardEgvsByDay = function(input) {
helpers.validateOptions(this.options);
const sortedInput = input.sort((e1, e2) => {
const e1ms = new Date(e1.systemTime).getTime();
const e2ms = new Date(e2.systemTime).getTime();
return e1ms - e2ms;
});
const groupedByDay = _.groupBy(sortedInput, el => {
// @see https://stackoverflow.com/questions/3894048/what-is-the-best-way-to-initialize-a-javascript-date-to-midnight
const elDate = new Date(el.systemTime);
return elDate.setHours(0,0,0,0); // last midnight
});
return groupedByDay;
};
/**
* @brief
* calculate a legal fetch range that starts at the midnight prior
* to the requested time, or the midnight following if the request3ed
* time is less than the valid time ranges Dexcom has returned
*/
DexcomJS.rangeInDayIntervals = function(dataRange, endTime, daysPast) {
const returnValue = {
startTime: 0,
endTime: 0,
valid: false,
}
const startDateString = dataRange.start.systemTime;
const endDateString = dataRange.end.systemTime;
let startTime = endTime - daysPast * 86400 * 1000;
// go back to midnight of start time if possible
const midnightStartTime = new Date(startTime).setHours(0,0,0,0);
const availableStartTime = new Date(startDateString);
startTime = midnightStartTime;
if (midnightStartTime < availableStartTime.getTime()) {
startTime = availableStartTime.getTime();
}
returnValue.startTime = startTime;
returnValue.endTime = endTime;
returnValue.valid = true;
return returnValue;
}