@tryvital/vital-node 1.3.1 → 1.3.4
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.
- package/__tests__/activity.test.ts +3 -2
- package/__tests__/arrange.ts +6 -0
- package/__tests__/body.test.ts +3 -2
- package/__tests__/devices.test.ts +3 -2
- package/__tests__/link.test.ts +3 -2
- package/__tests__/profile.test.ts +3 -2
- package/__tests__/sleep.test.ts +18 -2
- package/__tests__/user.test.ts +7 -4
- package/__tests__/vitals.test.ts +3 -2
- package/__tests__/workouts.test.ts +3 -2
- package/client/Link.ts +3 -1
- package/client/Sleep.ts +15 -0
- package/client/models/sleep_models.ts +7 -0
- package/dist/client/Link.d.ts +1 -1
- package/dist/client/Link.js +3 -1
- package/dist/client/Sleep.d.ts +1 -0
- package/dist/client/Sleep.js +15 -0
- package/dist/client/models/sleep_models.d.ts +7 -0
- package/dist/index.js +20 -5
- package/dist/lib/models.d.ts +3 -2
- package/index.ts +15 -4
- package/lib/models.ts +3 -2
- package/package.json +1 -1
@@ -1,11 +1,12 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, testApiKeyClient, getUserId } from "./arrange";
|
3
3
|
|
4
4
|
|
5
5
|
describe('Activity', () => {
|
6
6
|
it.each([
|
7
7
|
["us", testClient],
|
8
|
-
["eu", testEuClient]
|
8
|
+
["eu", testEuClient],
|
9
|
+
["us_api_key", testApiKeyClient],
|
9
10
|
])('should return activity data %p', async (region: string, client: VitalClient) => {
|
10
11
|
const userId = await getUserId(client)
|
11
12
|
const data = await client.Activity.get(
|
package/__tests__/arrange.ts
CHANGED
@@ -17,6 +17,12 @@ export const testEuClient = new VitalClient({
|
|
17
17
|
region: "eu",
|
18
18
|
});
|
19
19
|
|
20
|
+
export const testApiKeyClient = new VitalClient({
|
21
|
+
api_key: process.env.TEST_API_KEY,
|
22
|
+
environment: process.env.TEST_ENVIRONMENT as any,
|
23
|
+
region: "us",
|
24
|
+
});
|
25
|
+
|
20
26
|
export const test_user_id = "test_user_1234";
|
21
27
|
|
22
28
|
export const getUserId = async (client: VitalClient, user_id: string = test_user_id) => {
|
package/__tests__/body.test.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { getUserId, testClient, testEuClient } from "./arrange";
|
2
|
+
import { getUserId, testApiKeyClient, testClient, testEuClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Body', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return body data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Body.get(
|
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, getUserId, testApiKeyClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Devices', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return device data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Devices.get_raw(
|
package/__tests__/link.test.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { getUserId, testClient, testEuClient } from "./arrange";
|
2
|
+
import { getUserId, testApiKeyClient, testClient, testEuClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Link', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should create a link token %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Link.create(
|
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, getUserId, testApiKeyClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Profile', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return profile data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Profile.get(
|
package/__tests__/sleep.test.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, getUserId, testApiKeyClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Sleep', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return sleep data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Sleep.get(
|
@@ -14,4 +15,19 @@ describe('Sleep', () => {
|
|
14
15
|
)
|
15
16
|
expect(data.sleep.length).toBeGreaterThan(0)
|
16
17
|
});
|
18
|
+
|
19
|
+
it.each([
|
20
|
+
["us", testClient],
|
21
|
+
["eu", testEuClient],
|
22
|
+
["us_api_key", testApiKeyClient],
|
23
|
+
])('should return sleep stream data %p', async (region: string, client: VitalClient) => {
|
24
|
+
const userId = await getUserId(client)
|
25
|
+
const data = await client.Sleep.getSleepWithStream(
|
26
|
+
userId,
|
27
|
+
new Date("2021-09-10"),
|
28
|
+
new Date("2021-10-10"),
|
29
|
+
)
|
30
|
+
expect(data.sleep.length).toBeGreaterThan(0)
|
31
|
+
expect(data.sleep[0].sleep_stream).toBeDefined()
|
32
|
+
});
|
17
33
|
})
|
package/__tests__/user.test.ts
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { getUserId, randomString, testClient, testEuClient, test_user_id } from "./arrange";
|
2
|
+
import { getUserId, randomString, testApiKeyClient, testClient, testEuClient, test_user_id } from "./arrange";
|
3
3
|
|
4
4
|
describe('User', () => {
|
5
5
|
const user_id = randomString(10);
|
6
6
|
it.each([
|
7
7
|
["us", testClient],
|
8
|
-
["eu", testEuClient]
|
8
|
+
["eu", testEuClient],
|
9
|
+
["us_api_key", testApiKeyClient],
|
9
10
|
])('should create a user %p', async (region: string, client: VitalClient) => {
|
10
11
|
const user = await client.User.create(
|
11
12
|
user_id,
|
@@ -15,7 +16,8 @@ describe('User', () => {
|
|
15
16
|
|
16
17
|
it.each([
|
17
18
|
testClient,
|
18
|
-
testEuClient
|
19
|
+
testEuClient,
|
20
|
+
testApiKeyClient
|
19
21
|
])('should find a user', async (client: VitalClient) => {
|
20
22
|
const user = await client.User.resolve(
|
21
23
|
test_user_id,
|
@@ -25,7 +27,8 @@ describe('User', () => {
|
|
25
27
|
|
26
28
|
it.each([
|
27
29
|
testClient,
|
28
|
-
testEuClient
|
30
|
+
testEuClient,
|
31
|
+
testApiKeyClient
|
29
32
|
])('should delete a user', async (client: VitalClient) => {
|
30
33
|
const userToDelete = await getUserId(client, user_id);
|
31
34
|
const user = await client.User.delete(
|
package/__tests__/vitals.test.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, getUserId, testApiKeyClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Vitals', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return glucose data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Vitals.glucose(
|
@@ -1,10 +1,11 @@
|
|
1
1
|
import { VitalClient } from "..";
|
2
|
-
import { testClient, testEuClient, getUserId } from "./arrange";
|
2
|
+
import { testClient, testEuClient, getUserId, testApiKeyClient } from "./arrange";
|
3
3
|
|
4
4
|
describe('Workouts', () => {
|
5
5
|
it.each([
|
6
6
|
["us", testClient],
|
7
|
-
["eu", testEuClient]
|
7
|
+
["eu", testEuClient],
|
8
|
+
["us_api_key", testApiKeyClient],
|
8
9
|
])('should return workout data %p', async (region: string, client: VitalClient) => {
|
9
10
|
const userId = await getUserId(client)
|
10
11
|
const data = await client.Workouts.get(
|
package/client/Link.ts
CHANGED
@@ -18,11 +18,13 @@ export class LinkApi {
|
|
18
18
|
|
19
19
|
public async create(
|
20
20
|
userId: string,
|
21
|
-
provider: string = null
|
21
|
+
provider: string = null,
|
22
|
+
redirect_url: string = null
|
22
23
|
): Promise<LinkTokenExchangeResponse> {
|
23
24
|
const resp = await this.client.post(this.baseURL.concat('/link/token/'), {
|
24
25
|
user_key: userId,
|
25
26
|
provider,
|
27
|
+
redirect_url,
|
26
28
|
});
|
27
29
|
return resp.data;
|
28
30
|
}
|
package/client/Sleep.ts
CHANGED
@@ -29,6 +29,21 @@ export class SleepApi {
|
|
29
29
|
return resp.data;
|
30
30
|
}
|
31
31
|
|
32
|
+
public async getSleepWithStream(
|
33
|
+
userId: string,
|
34
|
+
startDate: Date,
|
35
|
+
endDate?: Date,
|
36
|
+
provider?: string
|
37
|
+
): Promise<ClientSleepResponse> {
|
38
|
+
const resp = await this.client.get(
|
39
|
+
this.baseURL.concat(`/summary/sleep/${userId}/stream`),
|
40
|
+
{
|
41
|
+
params: { start_date: startDate, end_date: endDate, provider },
|
42
|
+
}
|
43
|
+
);
|
44
|
+
return resp.data;
|
45
|
+
}
|
46
|
+
|
32
47
|
public async getStream(sleepId: string): Promise<ClientSleepStreamResponse> {
|
33
48
|
const resp = await this.client.get(
|
34
49
|
this.baseURL.concat(`/timeseries/sleep/${sleepId}/stream`)
|
@@ -127,6 +127,13 @@ export interface ClientFacingSleep {
|
|
127
127
|
* @memberof ClientFacingSleep
|
128
128
|
*/
|
129
129
|
user_id: string;
|
130
|
+
/**
|
131
|
+
* UTC time when the sleep period started
|
132
|
+
* @type {ClientSleepStreamResponse}
|
133
|
+
* @memberof ClientFacingSleep
|
134
|
+
*
|
135
|
+
*/
|
136
|
+
sleep_stream: ClientSleepStreamResponse[];
|
130
137
|
}
|
131
138
|
|
132
139
|
export interface ClientSleepResponse {
|
package/dist/client/Link.d.ts
CHANGED
@@ -5,7 +5,7 @@ export declare class LinkApi {
|
|
5
5
|
baseURL: string;
|
6
6
|
client: AxiosInstance;
|
7
7
|
constructor(baseURL: string, axios: AxiosInstance);
|
8
|
-
create(userId: string, provider?: string): Promise<LinkTokenExchangeResponse>;
|
8
|
+
create(userId: string, provider?: string, redirect_url?: string): Promise<LinkTokenExchangeResponse>;
|
9
9
|
connectProvider(linkToken: string, provider: PasswordProviders, username: string, password: string): Promise<ProviderLinkResponse>;
|
10
10
|
connectEmailProvider(linkToken: string, provider: EmailProviders, email: string): Promise<ProviderLinkResponse>;
|
11
11
|
getOAuthLink(linkToken: string, provider: OAuthProviders): Promise<SourceWithLinkInfo>;
|
package/dist/client/Link.js
CHANGED
@@ -42,8 +42,9 @@ var LinkApi = /** @class */ (function () {
|
|
42
42
|
this.baseURL = baseURL;
|
43
43
|
this.client = axios;
|
44
44
|
}
|
45
|
-
LinkApi.prototype.create = function (userId, provider) {
|
45
|
+
LinkApi.prototype.create = function (userId, provider, redirect_url) {
|
46
46
|
if (provider === void 0) { provider = null; }
|
47
|
+
if (redirect_url === void 0) { redirect_url = null; }
|
47
48
|
return __awaiter(this, void 0, void 0, function () {
|
48
49
|
var resp;
|
49
50
|
return __generator(this, function (_a) {
|
@@ -51,6 +52,7 @@ var LinkApi = /** @class */ (function () {
|
|
51
52
|
case 0: return [4 /*yield*/, this.client.post(this.baseURL.concat('/link/token/'), {
|
52
53
|
user_key: userId,
|
53
54
|
provider: provider,
|
55
|
+
redirect_url: redirect_url,
|
54
56
|
})];
|
55
57
|
case 1:
|
56
58
|
resp = _a.sent();
|
package/dist/client/Sleep.d.ts
CHANGED
@@ -6,6 +6,7 @@ export declare class SleepApi {
|
|
6
6
|
client: AxiosInstance;
|
7
7
|
constructor(baseURL: string, axios: AxiosInstance);
|
8
8
|
get(userId: string, startDate: Date, endDate?: Date, provider?: string, with_stream?: boolean): Promise<ClientSleepResponse>;
|
9
|
+
getSleepWithStream(userId: string, startDate: Date, endDate?: Date, provider?: string): Promise<ClientSleepResponse>;
|
9
10
|
getStream(sleepId: string): Promise<ClientSleepStreamResponse>;
|
10
11
|
get_raw(userId: string, startDate: Date, endDate?: Date, provider?: string): Promise<ClientSleepRawResponse>;
|
11
12
|
}
|
package/dist/client/Sleep.js
CHANGED
@@ -58,6 +58,21 @@ var SleepApi = /** @class */ (function () {
|
|
58
58
|
});
|
59
59
|
});
|
60
60
|
};
|
61
|
+
SleepApi.prototype.getSleepWithStream = function (userId, startDate, endDate, provider) {
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
63
|
+
var resp;
|
64
|
+
return __generator(this, function (_a) {
|
65
|
+
switch (_a.label) {
|
66
|
+
case 0: return [4 /*yield*/, this.client.get(this.baseURL.concat("/summary/sleep/" + userId + "/stream"), {
|
67
|
+
params: { start_date: startDate, end_date: endDate, provider: provider },
|
68
|
+
})];
|
69
|
+
case 1:
|
70
|
+
resp = _a.sent();
|
71
|
+
return [2 /*return*/, resp.data];
|
72
|
+
}
|
73
|
+
});
|
74
|
+
});
|
75
|
+
};
|
61
76
|
SleepApi.prototype.getStream = function (sleepId) {
|
62
77
|
return __awaiter(this, void 0, void 0, function () {
|
63
78
|
var resp;
|
@@ -126,6 +126,13 @@ export interface ClientFacingSleep {
|
|
126
126
|
* @memberof ClientFacingSleep
|
127
127
|
*/
|
128
128
|
user_id: string;
|
129
|
+
/**
|
130
|
+
* UTC time when the sleep period started
|
131
|
+
* @type {ClientSleepStreamResponse}
|
132
|
+
* @memberof ClientFacingSleep
|
133
|
+
*
|
134
|
+
*/
|
135
|
+
sleep_stream: ClientSleepStreamResponse[];
|
129
136
|
}
|
130
137
|
export interface ClientSleepResponse {
|
131
138
|
/**
|
package/dist/index.js
CHANGED
@@ -59,7 +59,14 @@ var VitalClient = /** @class */ (function () {
|
|
59
59
|
function VitalClient(config) {
|
60
60
|
var _this = this;
|
61
61
|
this.config = config;
|
62
|
-
|
62
|
+
if (!config.api_key) {
|
63
|
+
try {
|
64
|
+
this.clientCredentials = new credentials_1.ClientCredentials(config);
|
65
|
+
}
|
66
|
+
catch (error) {
|
67
|
+
throw new Error("You must provide either an API key or a client ID and secret");
|
68
|
+
}
|
69
|
+
}
|
63
70
|
var baseURL;
|
64
71
|
if (this.config.region && this.config.region === 'eu') {
|
65
72
|
baseURL = config_1.default.baseEUUrls[config.environment];
|
@@ -73,13 +80,21 @@ var VitalClient = /** @class */ (function () {
|
|
73
80
|
retryDelay: axios_retry_1.default.exponentialDelay,
|
74
81
|
});
|
75
82
|
axiosApiInstance.interceptors.request.use(function (config) { return __awaiter(_this, void 0, void 0, function () {
|
76
|
-
var token;
|
83
|
+
var headers, token;
|
77
84
|
return __generator(this, function (_a) {
|
78
85
|
switch (_a.label) {
|
79
|
-
case 0:
|
80
|
-
|
86
|
+
case 0:
|
87
|
+
headers = config.headers;
|
88
|
+
if (!this.config.api_key) return [3 /*break*/, 1];
|
89
|
+
headers["x-vital-api-key"] = this.config.api_key;
|
90
|
+
return [3 /*break*/, 3];
|
91
|
+
case 1: return [4 /*yield*/, this.clientCredentials.access_token()];
|
92
|
+
case 2:
|
81
93
|
token = _a.sent();
|
82
|
-
|
94
|
+
headers["Authorization"] = "Bearer " + token;
|
95
|
+
_a.label = 3;
|
96
|
+
case 3:
|
97
|
+
config.headers = __assign(__assign({}, headers), { 'x-vital-client-id': this.config.client_id });
|
83
98
|
return [2 /*return*/, config];
|
84
99
|
}
|
85
100
|
});
|
package/dist/lib/models.d.ts
CHANGED
package/index.ts
CHANGED
@@ -36,7 +36,13 @@ export class VitalClient {
|
|
36
36
|
|
37
37
|
constructor(config: ClientConfig) {
|
38
38
|
this.config = config;
|
39
|
-
|
39
|
+
if(!config.api_key){
|
40
|
+
try {
|
41
|
+
this.clientCredentials = new ClientCredentials(config);
|
42
|
+
} catch (error) {
|
43
|
+
throw new Error("You must provide either an API key or a client ID and secret");
|
44
|
+
}
|
45
|
+
}
|
40
46
|
let baseURL;
|
41
47
|
if (this.config.region && this.config.region === 'eu') {
|
42
48
|
baseURL = CONFIG.baseEUUrls[config.environment];
|
@@ -52,11 +58,16 @@ export class VitalClient {
|
|
52
58
|
|
53
59
|
axiosApiInstance.interceptors.request.use(
|
54
60
|
async (config) => {
|
55
|
-
const
|
61
|
+
const headers = config.headers;
|
62
|
+
if(this.config.api_key){
|
63
|
+
headers["x-vital-api-key"] = this.config.api_key;
|
64
|
+
} else {
|
65
|
+
const token = await this.clientCredentials.access_token();
|
66
|
+
headers["Authorization"] = `Bearer ${token}`;
|
67
|
+
}
|
56
68
|
config.headers = {
|
57
|
-
...
|
69
|
+
...headers,
|
58
70
|
'x-vital-client-id': this.config.client_id,
|
59
|
-
Authorization: `Bearer ${token}`,
|
60
71
|
};
|
61
72
|
return config;
|
62
73
|
},
|
package/lib/models.ts
CHANGED