@dlwiest/ts-tonal-client 0.0.1 → 0.1.0
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/README.md +404 -38
- package/dist/examples/create-workout.d.ts +1 -0
- package/dist/examples/debug-daily-lifts.d.ts +1 -0
- package/dist/examples/delete-workout.d.ts +1 -0
- package/dist/examples/edit-workout.d.ts +1 -0
- package/dist/examples/estimate-workout.d.ts +1 -0
- package/dist/examples/get-achievement-stats.d.ts +2 -0
- package/dist/examples/get-achievements.d.ts +2 -0
- package/dist/examples/get-activity-summaries.d.ts +2 -0
- package/dist/examples/get-current-streak.d.ts +1 -0
- package/dist/examples/get-daily-lifts.d.ts +1 -0
- package/dist/examples/get-daily-metrics.d.ts +1 -0
- package/dist/examples/get-goal-metrics.d.ts +1 -0
- package/dist/examples/get-goals.d.ts +1 -0
- package/dist/examples/get-home-calendar.d.ts +2 -0
- package/dist/examples/get-metric-scores.d.ts +1 -0
- package/dist/examples/get-movements.d.ts +1 -0
- package/dist/examples/get-muscle-readiness.d.ts +1 -0
- package/dist/examples/get-program-by-id.d.ts +1 -0
- package/dist/examples/get-target-scores.d.ts +1 -0
- package/dist/examples/get-training-effect-goals.d.ts +1 -0
- package/dist/examples/get-training-types.d.ts +1 -0
- package/dist/examples/get-user-info.d.ts +1 -0
- package/dist/examples/get-user-settings.d.ts +1 -0
- package/dist/examples/get-user-statistics.d.ts +2 -0
- package/dist/examples/get-user-workouts.d.ts +1 -0
- package/dist/examples/get-workout-by-id.d.ts +1 -0
- package/dist/examples/get-workout-by-share-url.d.ts +1 -0
- package/dist/examples/raw-daily-lifts-test.d.ts +1 -0
- package/dist/index.d.ts +573 -29
- package/dist/index.esm.js +447 -119
- package/dist/index.js +451 -119
- package/dist/src/auth/auth-manager.d.ts +12 -0
- package/dist/src/client.d.ts +39 -0
- package/dist/src/http/http-client.d.ts +12 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/services/movement-service.d.ts +7 -0
- package/dist/src/services/user-service.d.ts +51 -0
- package/dist/src/services/workout-service.d.ts +14 -0
- package/dist/src/types/auth.d.ts +18 -0
- package/dist/src/types/common.d.ts +1 -0
- package/dist/src/types/index.d.ts +6 -0
- package/dist/src/types/movements.d.ts +30 -0
- package/dist/src/types/programs.d.ts +46 -0
- package/dist/src/types/users.d.ts +418 -0
- package/dist/src/types/workouts.d.ts +120 -0
- package/dist/src/types.d.ts +125 -0
- package/package.json +43 -4
package/dist/index.esm.js
CHANGED
|
@@ -1,147 +1,475 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
19
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
21
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
22
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
23
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
29
|
-
var e = new Error(message);
|
|
30
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
31
|
-
};
|
|
1
|
+
class TonalClientError extends Error {
|
|
2
|
+
constructor(message, statusCode, originalError) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'TonalClientError';
|
|
5
|
+
this.statusCode = statusCode;
|
|
6
|
+
this.originalError = originalError;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
32
9
|
|
|
33
|
-
class
|
|
34
|
-
constructor(
|
|
35
|
-
this.username = username;
|
|
36
|
-
this.password = password;
|
|
10
|
+
class AuthManager {
|
|
11
|
+
constructor(username, password) {
|
|
37
12
|
this.idToken = '';
|
|
38
13
|
this.tokenExpiresAt = 0;
|
|
14
|
+
this.authUrl = 'https://tonal.auth0.com/oauth/token';
|
|
15
|
+
this.clientId = 'ERCyexW-xoVG_Yy3RDe-eV4xsOnRHP6L';
|
|
16
|
+
this.username = username;
|
|
17
|
+
this.password = password;
|
|
39
18
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
const data = {
|
|
19
|
+
async authenticate() {
|
|
20
|
+
if (this.isTokenValid()) {
|
|
21
|
+
return this.idToken;
|
|
22
|
+
}
|
|
23
|
+
const response = await fetch(this.authUrl, {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
},
|
|
28
|
+
body: JSON.stringify({
|
|
52
29
|
username: this.username,
|
|
53
30
|
password: this.password,
|
|
54
|
-
client_id:
|
|
31
|
+
client_id: this.clientId,
|
|
55
32
|
grant_type: 'password',
|
|
56
33
|
scope: 'offline_access',
|
|
57
|
-
}
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const errorText = await response.text();
|
|
38
|
+
let errorData;
|
|
58
39
|
try {
|
|
59
|
-
|
|
60
|
-
method: 'POST',
|
|
61
|
-
headers: {
|
|
62
|
-
'Content-Type': 'application/json',
|
|
63
|
-
},
|
|
64
|
-
body: JSON.stringify(data),
|
|
65
|
-
});
|
|
66
|
-
if (response.status === 403) {
|
|
67
|
-
throw new Error('Invalid username or password');
|
|
68
|
-
}
|
|
69
|
-
const json = (yield response.json());
|
|
70
|
-
this.idToken = json.id_token;
|
|
71
|
-
this.tokenExpiresAt = Date.now() + json.expires_in * 1000;
|
|
40
|
+
errorData = JSON.parse(errorText);
|
|
72
41
|
}
|
|
73
|
-
catch
|
|
74
|
-
|
|
75
|
-
throw new Error('Failed to retrieve access token');
|
|
42
|
+
catch {
|
|
43
|
+
errorData = { error: errorText };
|
|
76
44
|
}
|
|
77
|
-
|
|
45
|
+
throw new TonalClientError(errorData.error_description || errorData.error || 'Authentication failed', response.status, errorData);
|
|
46
|
+
}
|
|
47
|
+
const tokenData = await response.json();
|
|
48
|
+
this.idToken = tokenData.id_token;
|
|
49
|
+
this.tokenExpiresAt = Date.now() + (tokenData.expires_in * 1000);
|
|
50
|
+
return this.idToken;
|
|
78
51
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
52
|
+
getValidToken() {
|
|
53
|
+
if (!this.isTokenValid()) {
|
|
54
|
+
throw new TonalClientError('Token expired. Call authenticate() first.');
|
|
55
|
+
}
|
|
56
|
+
return this.idToken;
|
|
57
|
+
}
|
|
58
|
+
isTokenValid() {
|
|
59
|
+
return !!this.idToken && Date.now() < this.tokenExpiresAt - 60000; // 1 minute buffer
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
class HttpClient {
|
|
64
|
+
constructor(authManager) {
|
|
65
|
+
this.authManager = authManager;
|
|
66
|
+
this.baseUrl = 'https://api.tonal.com/v6';
|
|
67
|
+
this.requestTimeout = 30000;
|
|
68
|
+
this.maxRetries = 3;
|
|
69
|
+
}
|
|
70
|
+
async request(endpoint, options = {}, expectsBody = true) {
|
|
71
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
72
|
+
return this.makeRequestWithRetry(url, options, expectsBody);
|
|
73
|
+
}
|
|
74
|
+
async makeRequest(url, options = {}, expectsBody = true) {
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
const timeoutId = setTimeout(() => controller.abort(), this.requestTimeout);
|
|
77
|
+
try {
|
|
78
|
+
const token = this.authManager.getValidToken();
|
|
79
|
+
const response = await fetch(url, {
|
|
80
|
+
...options,
|
|
81
|
+
headers: {
|
|
82
|
+
Authorization: `Bearer ${token}`,
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
...options.headers,
|
|
85
|
+
},
|
|
86
|
+
signal: controller.signal,
|
|
87
|
+
});
|
|
88
|
+
clearTimeout(timeoutId);
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const errorText = await response.text();
|
|
91
|
+
let errorData;
|
|
92
|
+
try {
|
|
93
|
+
errorData = JSON.parse(errorText);
|
|
85
94
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
});
|
|
91
|
-
return (yield response.json());
|
|
95
|
+
catch {
|
|
96
|
+
errorData = { error: errorText };
|
|
97
|
+
}
|
|
98
|
+
throw new TonalClientError(errorData.error_description || errorData.error || `HTTP ${response.status}`, response.status, errorData);
|
|
92
99
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
throw new Error('Failed to retrieve movements');
|
|
100
|
+
if (expectsBody) {
|
|
101
|
+
return await response.json();
|
|
96
102
|
}
|
|
97
|
-
|
|
103
|
+
else {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
clearTimeout(timeoutId);
|
|
109
|
+
if (error instanceof TonalClientError) {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
113
|
+
throw new TonalClientError('Request timeout', undefined, error);
|
|
114
|
+
}
|
|
115
|
+
throw new TonalClientError('Request failed', undefined, error);
|
|
116
|
+
}
|
|
98
117
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
118
|
+
async makeRequestWithRetry(url, options = {}, expectsBody = true) {
|
|
119
|
+
let lastError;
|
|
120
|
+
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
|
|
102
121
|
try {
|
|
103
|
-
|
|
104
|
-
yield this.refreshToken();
|
|
105
|
-
}
|
|
106
|
-
const response = yield fetch(`https://api.tonal.com/v6/workouts/${id}`, {
|
|
107
|
-
headers: {
|
|
108
|
-
Authorization: `Bearer ${this.idToken}`,
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
const json = (yield response.json());
|
|
112
|
-
return json;
|
|
122
|
+
return await this.makeRequest(url, options, expectsBody);
|
|
113
123
|
}
|
|
114
|
-
catch (
|
|
115
|
-
|
|
116
|
-
|
|
124
|
+
catch (error) {
|
|
125
|
+
lastError = error instanceof TonalClientError ? error : new TonalClientError('Unknown error', undefined, error);
|
|
126
|
+
if (attempt === this.maxRetries || (lastError.statusCode && lastError.statusCode < 500)) {
|
|
127
|
+
throw lastError;
|
|
128
|
+
}
|
|
129
|
+
const delay = Math.pow(2, attempt - 1) * 1000;
|
|
130
|
+
await this.sleep(delay);
|
|
117
131
|
}
|
|
132
|
+
}
|
|
133
|
+
throw lastError;
|
|
134
|
+
}
|
|
135
|
+
async sleep(ms) {
|
|
136
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
class WorkoutService {
|
|
141
|
+
constructor(httpClient) {
|
|
142
|
+
this.httpClient = httpClient;
|
|
143
|
+
}
|
|
144
|
+
async getUserWorkouts(offset = 0, limit = 50) {
|
|
145
|
+
return this.httpClient.request('/user-workouts', {
|
|
146
|
+
method: 'GET',
|
|
147
|
+
headers: {
|
|
148
|
+
'x-paginate-offset': offset.toString(),
|
|
149
|
+
'x-paginate-limit': limit.toString(),
|
|
150
|
+
},
|
|
118
151
|
});
|
|
119
152
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
153
|
+
async getDailyLifts(userInfo, timeZone) {
|
|
154
|
+
const device = userInfo.recentMobileDevice;
|
|
155
|
+
const userAgent = device.platform === 'ios'
|
|
156
|
+
? `Tonal/3004226 CFNetwork/3860.100.1 Darwin/${device.osVersion}`
|
|
157
|
+
: `Tonal/${device.appVersion}`;
|
|
158
|
+
// Use provided timezone, or auto-detect from system, or fall back to UTC
|
|
159
|
+
let detectedTimeZone = timeZone;
|
|
160
|
+
if (!detectedTimeZone) {
|
|
127
161
|
try {
|
|
128
|
-
|
|
129
|
-
yield this.refreshToken();
|
|
130
|
-
}
|
|
131
|
-
const response = yield fetch(`https://api.tonal.com/v6/user-workouts/sharing-records/${shareId}`, {
|
|
132
|
-
headers: {
|
|
133
|
-
Authorization: `Bearer ${this.idToken}`,
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
const json = (yield response.json());
|
|
137
|
-
return json;
|
|
162
|
+
detectedTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
138
163
|
}
|
|
139
|
-
catch
|
|
140
|
-
|
|
141
|
-
throw new Error('Failed to retrieve workout');
|
|
164
|
+
catch {
|
|
165
|
+
detectedTimeZone = 'UTC';
|
|
142
166
|
}
|
|
167
|
+
}
|
|
168
|
+
return this.httpClient.request(`/user-workouts?types=DailyLift`, {
|
|
169
|
+
method: 'GET',
|
|
170
|
+
headers: {
|
|
171
|
+
'Time-Zone': detectedTimeZone,
|
|
172
|
+
'AppVersion': device.appVersion,
|
|
173
|
+
'DeviceId': device.tonalDeviceId,
|
|
174
|
+
'Accept': '*/*',
|
|
175
|
+
'Accept-Encoding': 'gzip, deflate, br',
|
|
176
|
+
'Accept-Language': 'en-US,en;q=0.9',
|
|
177
|
+
'User-Agent': userAgent,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
async getWorkoutById(workoutId) {
|
|
182
|
+
if (!workoutId?.trim()) {
|
|
183
|
+
throw new TonalClientError('Workout ID is required');
|
|
184
|
+
}
|
|
185
|
+
return this.httpClient.request(`/workouts/${workoutId}`);
|
|
186
|
+
}
|
|
187
|
+
async getWorkoutByShareUrl(shareUrl) {
|
|
188
|
+
if (!shareUrl?.trim()) {
|
|
189
|
+
throw new TonalClientError('Share URL is required');
|
|
190
|
+
}
|
|
191
|
+
const urlPattern = /https:\/\/share\.tonal\.com\/workout\/([a-f0-9-]+)/;
|
|
192
|
+
const match = shareUrl.match(urlPattern);
|
|
193
|
+
if (!match) {
|
|
194
|
+
throw new TonalClientError('Invalid share URL format. Expected: https://share.tonal.com/workout/{id}');
|
|
195
|
+
}
|
|
196
|
+
const shareId = match[1];
|
|
197
|
+
return this.httpClient.request(`/user-workouts/sharing-records/${encodeURIComponent(shareId)}`);
|
|
198
|
+
}
|
|
199
|
+
async estimateWorkoutDuration(sets) {
|
|
200
|
+
if (!sets?.length) {
|
|
201
|
+
throw new TonalClientError('At least one set is required for estimation');
|
|
202
|
+
}
|
|
203
|
+
return this.httpClient.request('/user-workouts/estimate', {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
body: JSON.stringify({ sets }),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
async createWorkout(workoutData) {
|
|
209
|
+
if (!workoutData.title?.trim()) {
|
|
210
|
+
throw new TonalClientError('Workout title is required');
|
|
211
|
+
}
|
|
212
|
+
if (!workoutData.sets?.length) {
|
|
213
|
+
throw new TonalClientError('At least one set is required');
|
|
214
|
+
}
|
|
215
|
+
const requestBody = {
|
|
216
|
+
title: workoutData.title,
|
|
217
|
+
sets: workoutData.sets,
|
|
218
|
+
createdSource: workoutData.createdSource || 'WorkoutBuilder',
|
|
219
|
+
shortDescription: workoutData.shortDescription || '',
|
|
220
|
+
description: workoutData.description || '',
|
|
221
|
+
};
|
|
222
|
+
return this.httpClient.request('/user-workouts', {
|
|
223
|
+
method: 'POST',
|
|
224
|
+
body: JSON.stringify(requestBody),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
async updateWorkout(workoutData) {
|
|
228
|
+
if (!workoutData.id) {
|
|
229
|
+
throw new TonalClientError('Workout ID is required for updates');
|
|
230
|
+
}
|
|
231
|
+
if (!workoutData.title?.trim()) {
|
|
232
|
+
throw new TonalClientError('Workout title is required');
|
|
233
|
+
}
|
|
234
|
+
if (!workoutData.sets?.length) {
|
|
235
|
+
throw new TonalClientError('At least one set is required');
|
|
236
|
+
}
|
|
237
|
+
const requestBody = {
|
|
238
|
+
id: workoutData.id,
|
|
239
|
+
title: workoutData.title,
|
|
240
|
+
description: workoutData.description || '',
|
|
241
|
+
coachId: workoutData.coachId || '00000000-0000-0000-0000-000000000000',
|
|
242
|
+
sets: workoutData.sets,
|
|
243
|
+
level: workoutData.level || '',
|
|
244
|
+
assetId: workoutData.assetId,
|
|
245
|
+
createdSource: workoutData.createdSource || 'WorkoutBuilder'
|
|
246
|
+
};
|
|
247
|
+
return this.httpClient.request(`/user-workouts/${workoutData.id}`, {
|
|
248
|
+
method: 'PUT',
|
|
249
|
+
body: JSON.stringify(requestBody),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
async deleteWorkout(workoutId) {
|
|
253
|
+
if (!workoutId?.trim()) {
|
|
254
|
+
throw new TonalClientError('Workout ID is required');
|
|
255
|
+
}
|
|
256
|
+
await this.httpClient.request(`/user-workouts/${workoutId}`, {
|
|
257
|
+
method: 'DELETE',
|
|
258
|
+
}, false);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
class MovementService {
|
|
263
|
+
constructor(httpClient) {
|
|
264
|
+
this.httpClient = httpClient;
|
|
265
|
+
}
|
|
266
|
+
async getMovements() {
|
|
267
|
+
return this.httpClient.request('/movements');
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
class UserService {
|
|
272
|
+
constructor(httpClient) {
|
|
273
|
+
this.httpClient = httpClient;
|
|
274
|
+
}
|
|
275
|
+
async getUserInfo() {
|
|
276
|
+
return this.httpClient.request('/users/userinfo');
|
|
277
|
+
}
|
|
278
|
+
async getGoals() {
|
|
279
|
+
return this.httpClient.request('/goals');
|
|
280
|
+
}
|
|
281
|
+
async getTrainingEffectGoals() {
|
|
282
|
+
return this.httpClient.request('/training-effect-goals');
|
|
283
|
+
}
|
|
284
|
+
async getTrainingTypes() {
|
|
285
|
+
return this.httpClient.request('/training-types');
|
|
286
|
+
}
|
|
287
|
+
async getGoalMetrics() {
|
|
288
|
+
return this.httpClient.request('/goal-metrics');
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Register or update a personal device for the user.
|
|
292
|
+
*
|
|
293
|
+
* NOTE: This endpoint has side effects and should be used with caution.
|
|
294
|
+
* It registers the device with Tonal's system and may affect notification
|
|
295
|
+
* settings and device management. For reverse-engineered clients, this
|
|
296
|
+
* could be considered unauthorized device registration.
|
|
297
|
+
*
|
|
298
|
+
* @internal This method is primarily for API completeness and documentation
|
|
299
|
+
*/
|
|
300
|
+
async registerPersonalDevice(userId, deviceInfo) {
|
|
301
|
+
return this.httpClient.request(`/users/${userId}/personal-devices`, {
|
|
302
|
+
method: 'POST',
|
|
303
|
+
body: JSON.stringify(deviceInfo),
|
|
143
304
|
});
|
|
144
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Get user privacy permissions/settings.
|
|
308
|
+
*
|
|
309
|
+
* Returns what information this user has made public vs private.
|
|
310
|
+
* Useful for understanding social features and respecting privacy preferences.
|
|
311
|
+
*
|
|
312
|
+
* @internal This method is for understanding user privacy settings
|
|
313
|
+
*/
|
|
314
|
+
async getUserPermissions(userId) {
|
|
315
|
+
return this.httpClient.request(`/users/${userId}/permissions`);
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Get comprehensive user settings and preferences.
|
|
319
|
+
*
|
|
320
|
+
* Returns audio/visual settings, feature flags, onboarding states, and user preferences.
|
|
321
|
+
* Provides insight into Tonal's full feature ecosystem and user customization options.
|
|
322
|
+
*
|
|
323
|
+
* @internal This method is for understanding user preferences and feature usage
|
|
324
|
+
*/
|
|
325
|
+
async getUserSettings(userId) {
|
|
326
|
+
return this.httpClient.request(`/users/${userId}/user-settings`);
|
|
327
|
+
}
|
|
328
|
+
async getDailyMetrics(userId, days = 60) {
|
|
329
|
+
return this.httpClient.request(`/users/${userId}/metrics/daily?days=${days}`);
|
|
330
|
+
}
|
|
331
|
+
async getCurrentStreak(userId) {
|
|
332
|
+
return this.httpClient.request(`/users/${userId}/streaks/current`);
|
|
333
|
+
}
|
|
334
|
+
async getActivitySummaries(userId) {
|
|
335
|
+
return this.httpClient.request(`/users/${userId}/activity-summaries`);
|
|
336
|
+
}
|
|
337
|
+
async getUserStatistics(userId) {
|
|
338
|
+
return this.httpClient.request(`/users/${userId}/statistics`);
|
|
339
|
+
}
|
|
340
|
+
async getAchievementStats(userId) {
|
|
341
|
+
return this.httpClient.request(`/users/${userId}/achievement-stats`);
|
|
342
|
+
}
|
|
343
|
+
async getAchievements(userId) {
|
|
344
|
+
return this.httpClient.request(`/users/${userId}/achievements`);
|
|
345
|
+
}
|
|
346
|
+
async getHomeCalendar(userId) {
|
|
347
|
+
return this.httpClient.request(`/users/${userId}/calendar/home`);
|
|
348
|
+
}
|
|
349
|
+
async getMuscleReadiness(userId) {
|
|
350
|
+
return this.httpClient.request(`/users/${userId}/muscle-readiness/current`);
|
|
351
|
+
}
|
|
352
|
+
async getProgramById(programId) {
|
|
353
|
+
return this.httpClient.request(`/programs/${programId}`);
|
|
354
|
+
}
|
|
355
|
+
async getTargetScores(userId) {
|
|
356
|
+
return this.httpClient.request(`/users/${userId}/target-scores`);
|
|
357
|
+
}
|
|
358
|
+
async getMetricScores(userId, startWeek) {
|
|
359
|
+
const url = startWeek
|
|
360
|
+
? `/users/${userId}/metric-scores?startWeek=${startWeek}`
|
|
361
|
+
: `/users/${userId}/metric-scores`;
|
|
362
|
+
return this.httpClient.request(url);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
class TonalClient {
|
|
367
|
+
constructor(username, password) {
|
|
368
|
+
this.authManager = new AuthManager(username, password);
|
|
369
|
+
this.httpClient = new HttpClient(this.authManager);
|
|
370
|
+
this.workoutService = new WorkoutService(this.httpClient);
|
|
371
|
+
this.movementService = new MovementService(this.httpClient);
|
|
372
|
+
this.userService = new UserService(this.httpClient);
|
|
373
|
+
}
|
|
374
|
+
static async create(credentials) {
|
|
375
|
+
const client = new TonalClient(credentials.username, credentials.password);
|
|
376
|
+
await client.authManager.authenticate();
|
|
377
|
+
return client;
|
|
378
|
+
}
|
|
379
|
+
// Movement operations
|
|
380
|
+
async getMovements() {
|
|
381
|
+
return this.movementService.getMovements();
|
|
382
|
+
}
|
|
383
|
+
// User operations
|
|
384
|
+
async getUserInfo() {
|
|
385
|
+
return this.userService.getUserInfo();
|
|
386
|
+
}
|
|
387
|
+
async getGoals() {
|
|
388
|
+
return this.userService.getGoals();
|
|
389
|
+
}
|
|
390
|
+
async getTrainingEffectGoals() {
|
|
391
|
+
return this.userService.getTrainingEffectGoals();
|
|
392
|
+
}
|
|
393
|
+
async getTrainingTypes() {
|
|
394
|
+
return this.userService.getTrainingTypes();
|
|
395
|
+
}
|
|
396
|
+
async getGoalMetrics() {
|
|
397
|
+
return this.userService.getGoalMetrics();
|
|
398
|
+
}
|
|
399
|
+
async getUserSettings() {
|
|
400
|
+
const userInfo = await this.getUserInfo();
|
|
401
|
+
return this.userService.getUserSettings(userInfo.id);
|
|
402
|
+
}
|
|
403
|
+
async getDailyMetrics(days = 60) {
|
|
404
|
+
const userInfo = await this.getUserInfo();
|
|
405
|
+
return this.userService.getDailyMetrics(userInfo.id, days);
|
|
406
|
+
}
|
|
407
|
+
async getCurrentStreak() {
|
|
408
|
+
const userInfo = await this.getUserInfo();
|
|
409
|
+
return this.userService.getCurrentStreak(userInfo.id);
|
|
410
|
+
}
|
|
411
|
+
async getActivitySummaries() {
|
|
412
|
+
const userInfo = await this.getUserInfo();
|
|
413
|
+
return this.userService.getActivitySummaries(userInfo.id);
|
|
414
|
+
}
|
|
415
|
+
async getUserStatistics() {
|
|
416
|
+
const userInfo = await this.getUserInfo();
|
|
417
|
+
return this.userService.getUserStatistics(userInfo.id);
|
|
418
|
+
}
|
|
419
|
+
async getAchievementStats() {
|
|
420
|
+
const userInfo = await this.getUserInfo();
|
|
421
|
+
return this.userService.getAchievementStats(userInfo.id);
|
|
422
|
+
}
|
|
423
|
+
async getAchievements() {
|
|
424
|
+
const userInfo = await this.getUserInfo();
|
|
425
|
+
return this.userService.getAchievements(userInfo.id);
|
|
426
|
+
}
|
|
427
|
+
async getHomeCalendar() {
|
|
428
|
+
const userInfo = await this.getUserInfo();
|
|
429
|
+
return this.userService.getHomeCalendar(userInfo.id);
|
|
430
|
+
}
|
|
431
|
+
async getMuscleReadiness() {
|
|
432
|
+
const userInfo = await this.getUserInfo();
|
|
433
|
+
return this.userService.getMuscleReadiness(userInfo.id);
|
|
434
|
+
}
|
|
435
|
+
async getProgramById(programId) {
|
|
436
|
+
return this.userService.getProgramById(programId);
|
|
437
|
+
}
|
|
438
|
+
async getTargetScores() {
|
|
439
|
+
const userInfo = await this.getUserInfo();
|
|
440
|
+
return this.userService.getTargetScores(userInfo.id);
|
|
441
|
+
}
|
|
442
|
+
async getMetricScores(startWeek) {
|
|
443
|
+
const userInfo = await this.getUserInfo();
|
|
444
|
+
return this.userService.getMetricScores(userInfo.id, startWeek);
|
|
445
|
+
}
|
|
446
|
+
// Workout operations
|
|
447
|
+
async getUserWorkouts(offset = 0, limit = 50) {
|
|
448
|
+
return this.workoutService.getUserWorkouts(offset, limit);
|
|
449
|
+
}
|
|
450
|
+
async getDailyLifts(timeZone) {
|
|
451
|
+
// Get user info to populate device-specific headers
|
|
452
|
+
const userInfo = await this.getUserInfo();
|
|
453
|
+
return this.workoutService.getDailyLifts(userInfo, timeZone);
|
|
454
|
+
}
|
|
455
|
+
async getWorkoutById(workoutId) {
|
|
456
|
+
return this.workoutService.getWorkoutById(workoutId);
|
|
457
|
+
}
|
|
458
|
+
async getWorkoutByShareUrl(shareUrl) {
|
|
459
|
+
return this.workoutService.getWorkoutByShareUrl(shareUrl);
|
|
460
|
+
}
|
|
461
|
+
async estimateWorkoutDuration(sets) {
|
|
462
|
+
return this.workoutService.estimateWorkoutDuration(sets);
|
|
463
|
+
}
|
|
464
|
+
async createWorkout(workoutData) {
|
|
465
|
+
return this.workoutService.createWorkout(workoutData);
|
|
466
|
+
}
|
|
467
|
+
async updateWorkout(workoutData) {
|
|
468
|
+
return this.workoutService.updateWorkout(workoutData);
|
|
469
|
+
}
|
|
470
|
+
async deleteWorkout(workoutId) {
|
|
471
|
+
return this.workoutService.deleteWorkout(workoutId);
|
|
472
|
+
}
|
|
145
473
|
}
|
|
146
474
|
|
|
147
|
-
export { TonalClient as default };
|
|
475
|
+
export { TonalClient, TonalClientError, TonalClient as default };
|