hevy-shared 1.0.845 → 1.0.847
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/built/API/APIClient.d.ts +13 -4
- package/built/API/APIClient.js +34 -27
- package/built/hevyTrainer.d.ts +0 -1
- package/built/index.d.ts +0 -1
- package/package.json +1 -1
package/built/API/APIClient.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ClientAuthToken, DeepReadonly } from '..';
|
|
2
2
|
import { RequestConfig, HTTPResponse, HTTPClient, HTTPErrorHandler } from './types';
|
|
3
|
-
interface HevyAPIClientConfig {
|
|
3
|
+
interface HevyAPIClientConfig<UserContext> {
|
|
4
4
|
/**
|
|
5
5
|
* How long before predicted token expiry to request a new token. We request
|
|
6
6
|
* new tokens a little bit before they expire, so that we don't have to wait
|
|
@@ -55,7 +55,16 @@ interface HevyAPIClientConfig {
|
|
|
55
55
|
*
|
|
56
56
|
* Required.
|
|
57
57
|
*/
|
|
58
|
-
onNewAuthToken(newAuthToken: ClientAuthToken): void;
|
|
58
|
+
onNewAuthToken(newAuthToken: ClientAuthToken, userContext: UserContext): void;
|
|
59
|
+
/**
|
|
60
|
+
* Defines an arbitrary object to be passed back to the `onNewAuthToken`. The
|
|
61
|
+
* value is computed at the time when the request is made. Useful for passing
|
|
62
|
+
* back state that may have gotten changed during the time while the request
|
|
63
|
+
* was being processed, such as holding onto a userId across a logout action.
|
|
64
|
+
*
|
|
65
|
+
* Optional.
|
|
66
|
+
*/
|
|
67
|
+
getUserContext?(): UserContext;
|
|
59
68
|
/**
|
|
60
69
|
* Callback to use when receiving an HTTP error response from the backend, to
|
|
61
70
|
* determine whether it is a response indicating that the token has expired.
|
|
@@ -75,7 +84,7 @@ interface HevyAPIClientConfig {
|
|
|
75
84
|
*/
|
|
76
85
|
isAccessTokenInvalidResponse?(response: HTTPResponse<unknown>): boolean;
|
|
77
86
|
}
|
|
78
|
-
export declare class HevyAPIClient {
|
|
87
|
+
export declare class HevyAPIClient<UserContext = never> {
|
|
79
88
|
private static readonly DEFAULT_TOKEN_EXPIRY_SAFETY_THRESHOLD_MS;
|
|
80
89
|
private static readonly DEFAULT_ACCESS_TOKEN_MINIMUM_VALID_AGE_MS;
|
|
81
90
|
private static readonly DEFAULT_TOKEN_REFRESH_THROTTLE_MS;
|
|
@@ -86,7 +95,7 @@ export declare class HevyAPIClient {
|
|
|
86
95
|
private _legacyAuthToken;
|
|
87
96
|
private _httpClient;
|
|
88
97
|
private _lastTokenRefresh;
|
|
89
|
-
constructor(httpClient: HTTPClient, config: HevyAPIClientConfig);
|
|
98
|
+
constructor(httpClient: HTTPClient, config: HevyAPIClientConfig<UserContext>);
|
|
90
99
|
private get _authToken();
|
|
91
100
|
private get _authHeaders();
|
|
92
101
|
private _addAuthHeaders;
|
package/built/API/APIClient.js
CHANGED
|
@@ -32,7 +32,7 @@ class HevyAPIClient {
|
|
|
32
32
|
(0, types_1.isHTTPErrorResponse)(response) &&
|
|
33
33
|
response.data.error === 'AccessTokenExpired', isAccessTokenInvalidResponse: (response) => response.status === 401 &&
|
|
34
34
|
(0, types_1.isHTTPErrorResponse)(response) &&
|
|
35
|
-
response.data.error === 'InvalidAccessToken' }, config);
|
|
35
|
+
response.data.error === 'InvalidAccessToken', getUserContext: () => undefined }, config);
|
|
36
36
|
const classProperties = Object.getOwnPropertyNames(Object.getPrototypeOf(this)).filter((m) => m in this && typeof m === 'string' && m !== 'constructor');
|
|
37
37
|
classProperties.forEach((m) => {
|
|
38
38
|
if (typeof this[m] === 'function')
|
|
@@ -52,7 +52,7 @@ class HevyAPIClient {
|
|
|
52
52
|
? { authorization: `Bearer ${this._authToken.access_token}` }
|
|
53
53
|
: {})), (this._legacyAuthToken ? { 'auth-token': this._legacyAuthToken } : {}));
|
|
54
54
|
}
|
|
55
|
-
refreshExpiredAuthToken() {
|
|
55
|
+
refreshExpiredAuthToken(userContext) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
57
|
const { _authToken } = this;
|
|
58
58
|
if (!_authToken)
|
|
@@ -63,15 +63,15 @@ class HevyAPIClient {
|
|
|
63
63
|
// Token is still valid, at least according to the client. Don't refresh.
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
|
-
yield this._refreshAuthToken(_authToken);
|
|
66
|
+
yield this._refreshAuthToken(_authToken, userContext);
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
forceRefreshAuthToken() {
|
|
69
|
+
forceRefreshAuthToken(userContext) {
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
const { _authToken } = this;
|
|
72
72
|
if (!_authToken)
|
|
73
73
|
return;
|
|
74
|
-
yield this._refreshAuthToken(_authToken);
|
|
74
|
+
yield this._refreshAuthToken(_authToken, userContext);
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
waitForTokenRefresh() {
|
|
@@ -86,7 +86,7 @@ class HevyAPIClient {
|
|
|
86
86
|
_lastTokenRefresh.valueOf() >
|
|
87
87
|
new Date().valueOf() - this._config.tokenRefreshThrottleMs);
|
|
88
88
|
}
|
|
89
|
-
_refreshAuthToken(authToken) {
|
|
89
|
+
_refreshAuthToken(authToken, userContext) {
|
|
90
90
|
return __awaiter(this, void 0, void 0, function* () {
|
|
91
91
|
if (this._isTokenRecentlyRefreshed) {
|
|
92
92
|
// We have called this function recently. Do not ever spam this endpoint.
|
|
@@ -103,7 +103,7 @@ class HevyAPIClient {
|
|
|
103
103
|
method,
|
|
104
104
|
url,
|
|
105
105
|
try: () => this._httpClient[method](url, { refresh_token }, { headers: { authorization: `Bearer ${access_token}` } }),
|
|
106
|
-
}, false);
|
|
106
|
+
}, userContext, false);
|
|
107
107
|
const newToken = {
|
|
108
108
|
access_token: response.data.access_token,
|
|
109
109
|
refresh_token: response.data.refresh_token,
|
|
@@ -130,11 +130,11 @@ class HevyAPIClient {
|
|
|
130
130
|
if (this._authTokenFactory.type === 'value') {
|
|
131
131
|
this._authTokenFactory.value = newToken;
|
|
132
132
|
}
|
|
133
|
-
this._config.onNewAuthToken(newToken);
|
|
133
|
+
this._config.onNewAuthToken(newToken, userContext);
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
|
-
_handleResponse(request_1) {
|
|
137
|
-
return __awaiter(this, arguments, void 0, function* (request, retryOnFailure = !this._isTokenRecentlyRefreshed) {
|
|
136
|
+
_handleResponse(request_1, userContext_1) {
|
|
137
|
+
return __awaiter(this, arguments, void 0, function* (request, userContext, retryOnFailure = !this._isTokenRecentlyRefreshed) {
|
|
138
138
|
const requestTime = new Date();
|
|
139
139
|
try {
|
|
140
140
|
const response = yield request.try();
|
|
@@ -164,14 +164,14 @@ class HevyAPIClient {
|
|
|
164
164
|
isTokenRefreshedAfterRequest,
|
|
165
165
|
}));
|
|
166
166
|
if (retryOnFailure && this.isAccessTokenExpiredResponse(response)) {
|
|
167
|
-
yield this.forceRefreshAuthToken();
|
|
168
|
-
return yield this._handleResponse(request, false);
|
|
167
|
+
yield this.forceRefreshAuthToken(userContext);
|
|
168
|
+
return yield this._handleResponse(request, userContext, false);
|
|
169
169
|
}
|
|
170
170
|
else if (retryOnFailure &&
|
|
171
171
|
this.isAccessTokenInvalidResponse(response) &&
|
|
172
172
|
isTokenRefreshedAfterRequest) {
|
|
173
173
|
yield this.waitForTokenRefresh();
|
|
174
|
-
return yield this._handleResponse(request, false);
|
|
174
|
+
return yield this._handleResponse(request, userContext, false);
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
177
|
throw e;
|
|
@@ -221,79 +221,86 @@ class HevyAPIClient {
|
|
|
221
221
|
}
|
|
222
222
|
get(url, config) {
|
|
223
223
|
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
-
|
|
224
|
+
const userContext = this._config.getUserContext();
|
|
225
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
225
226
|
const method = 'get';
|
|
226
227
|
return this._handleResponse({
|
|
227
228
|
method,
|
|
228
229
|
url,
|
|
229
230
|
try: () => this._httpClient[method](url, this._addAuthHeaders(config)),
|
|
230
|
-
});
|
|
231
|
+
}, userContext);
|
|
231
232
|
});
|
|
232
233
|
}
|
|
233
234
|
delete(url, config) {
|
|
234
235
|
return __awaiter(this, void 0, void 0, function* () {
|
|
235
|
-
|
|
236
|
+
const userContext = this._config.getUserContext();
|
|
237
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
236
238
|
const method = 'delete';
|
|
237
239
|
return this._handleResponse({
|
|
238
240
|
method,
|
|
239
241
|
url,
|
|
240
242
|
try: () => this._httpClient[method](url, this._addAuthHeaders(config)),
|
|
241
|
-
});
|
|
243
|
+
}, userContext);
|
|
242
244
|
});
|
|
243
245
|
}
|
|
244
246
|
head(url, config) {
|
|
245
247
|
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
-
|
|
248
|
+
const userContext = this._config.getUserContext();
|
|
249
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
247
250
|
const method = 'head';
|
|
248
251
|
return this._handleResponse({
|
|
249
252
|
method,
|
|
250
253
|
url,
|
|
251
254
|
try: () => this._httpClient[method](url, this._addAuthHeaders(config)),
|
|
252
|
-
});
|
|
255
|
+
}, userContext);
|
|
253
256
|
});
|
|
254
257
|
}
|
|
255
258
|
options(url, config) {
|
|
256
259
|
return __awaiter(this, void 0, void 0, function* () {
|
|
257
|
-
|
|
260
|
+
const userContext = this._config.getUserContext();
|
|
261
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
258
262
|
const method = 'options';
|
|
259
263
|
return this._handleResponse({
|
|
260
264
|
method,
|
|
261
265
|
url,
|
|
262
266
|
try: () => this._httpClient[method](url, this._addAuthHeaders(config)),
|
|
263
|
-
});
|
|
267
|
+
}, userContext);
|
|
264
268
|
});
|
|
265
269
|
}
|
|
266
270
|
post(url, data, config) {
|
|
267
271
|
return __awaiter(this, void 0, void 0, function* () {
|
|
268
|
-
|
|
272
|
+
const userContext = this._config.getUserContext();
|
|
273
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
269
274
|
const method = 'post';
|
|
270
275
|
return this._handleResponse({
|
|
271
276
|
method,
|
|
272
277
|
url,
|
|
273
278
|
try: () => this._httpClient[method](url, data, this._addAuthHeaders(config)),
|
|
274
|
-
});
|
|
279
|
+
}, userContext);
|
|
275
280
|
});
|
|
276
281
|
}
|
|
277
282
|
put(url, data, config) {
|
|
278
283
|
return __awaiter(this, void 0, void 0, function* () {
|
|
279
|
-
|
|
284
|
+
const userContext = this._config.getUserContext();
|
|
285
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
280
286
|
const method = 'put';
|
|
281
287
|
return this._handleResponse({
|
|
282
288
|
method,
|
|
283
289
|
url,
|
|
284
290
|
try: () => this._httpClient[method](url, data, this._addAuthHeaders(config)),
|
|
285
|
-
});
|
|
291
|
+
}, userContext);
|
|
286
292
|
});
|
|
287
293
|
}
|
|
288
294
|
patch(url, data, config) {
|
|
289
295
|
return __awaiter(this, void 0, void 0, function* () {
|
|
290
|
-
|
|
296
|
+
const userContext = this._config.getUserContext();
|
|
297
|
+
yield this.refreshExpiredAuthToken(userContext);
|
|
291
298
|
const method = 'patch';
|
|
292
299
|
return this._handleResponse({
|
|
293
300
|
method,
|
|
294
301
|
url,
|
|
295
302
|
try: () => this._httpClient[method](url, data, this._addAuthHeaders(config)),
|
|
296
|
-
});
|
|
303
|
+
}, userContext);
|
|
297
304
|
});
|
|
298
305
|
}
|
|
299
306
|
}
|
package/built/hevyTrainer.d.ts
CHANGED
package/built/index.d.ts
CHANGED
|
@@ -1245,7 +1245,6 @@ export interface HevyTrainerProgram {
|
|
|
1245
1245
|
routines: HevyTrainerRoutine[];
|
|
1246
1246
|
focus_muscle?: SimplifiedMuscleGroup;
|
|
1247
1247
|
next_workout_index: number;
|
|
1248
|
-
workout_tab_ai_insights?: string;
|
|
1249
1248
|
workout_duration_minutes?: WorkoutDurationMinutes;
|
|
1250
1249
|
}
|
|
1251
1250
|
export interface PostHevyTrainerProgramRequestBody {
|