@tryvital/vital-node 1.3.2 → 1.3.5
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__/providers.test.ts +6 -0
- package/__tests__/sleep.test.ts +8 -6
- 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/dist/client/Link.d.ts +1 -1
- package/dist/client/Link.js +3 -1
- package/dist/index.js +21 -5
- package/dist/lib/models.d.ts +3 -2
- package/index.ts +16 -5
- 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(
|
@@ -17,13 +18,14 @@ describe('Sleep', () => {
|
|
17
18
|
|
18
19
|
it.each([
|
19
20
|
["us", testClient],
|
20
|
-
["eu", testEuClient]
|
21
|
-
|
21
|
+
["eu", testEuClient],
|
22
|
+
["us_api_key", testApiKeyClient],
|
23
|
+
])('should return sleep stream data %p', async (region: string, client: VitalClient) => {
|
22
24
|
const userId = await getUserId(client)
|
23
25
|
const data = await client.Sleep.getSleepWithStream(
|
24
26
|
userId,
|
25
|
-
new Date("2021-
|
26
|
-
new Date("
|
27
|
+
new Date("2021-09-10"),
|
28
|
+
new Date("2021-10-10"),
|
27
29
|
)
|
28
30
|
expect(data.sleep.length).toBeGreaterThan(0)
|
29
31
|
expect(data.sleep[0].sleep_stream).toBeDefined()
|
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/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/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,22 @@ 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
|
+
headers["x-vital-client-id"] = this.config.client_id;
|
96
|
+
_a.label = 3;
|
97
|
+
case 3:
|
98
|
+
config.headers = __assign({}, headers);
|
83
99
|
return [2 /*return*/, config];
|
84
100
|
}
|
85
101
|
});
|
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
|
+
headers["x-vital-client-id"] = this.config.client_id;
|
68
|
+
}
|
56
69
|
config.headers = {
|
57
|
-
...
|
58
|
-
'x-vital-client-id': this.config.client_id,
|
59
|
-
Authorization: `Bearer ${token}`,
|
70
|
+
...headers,
|
60
71
|
};
|
61
72
|
return config;
|
62
73
|
},
|
package/lib/models.ts
CHANGED