hevy-shared 1.0.820 → 1.0.822

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.
@@ -74,6 +74,16 @@ interface HevyAPIClientConfig {
74
74
  * @default ...
75
75
  */
76
76
  isAccessTokenInvalidResponse?(response: HTTPResponse<unknown>): boolean;
77
+ /**
78
+ * Callback for checking for a new token before calling the refresh endpoint.
79
+ * Used in cases where multiple APIClients are using the same credentials,
80
+ * such as multiple browser tabs with the web app.
81
+ *
82
+ * Optional.
83
+ *
84
+ * @default {undefined}
85
+ */
86
+ localRefresh?(): ClientAuthToken | null;
77
87
  }
78
88
  export declare class HevyAPIClient {
79
89
  private static readonly DEFAULT_TOKEN_EXPIRY_SAFETY_THRESHOLD_MS;
@@ -89,6 +99,7 @@ export declare class HevyAPIClient {
89
99
  constructor(httpClient: HTTPClient, config: HevyAPIClientConfig);
90
100
  private get _authHeaders();
91
101
  private _addAuthHeaders;
102
+ private _isExpired;
92
103
  private refreshExpiredAuthToken;
93
104
  private forceRefreshAuthToken;
94
105
  private waitForTokenRefresh;
@@ -24,6 +24,9 @@ class HevyAPIClient {
24
24
  this._authToken = null;
25
25
  this._legacyAuthToken = null;
26
26
  this._addAuthHeaders = (config) => (Object.assign(Object.assign({}, config), { headers: Object.assign(Object.assign({}, config === null || config === void 0 ? void 0 : config.headers), this._authHeaders) }));
27
+ this._isExpired = ({ now, authToken, }) => now.valueOf() >=
28
+ new Date(authToken.expires_at).valueOf() -
29
+ this._config.tokenExpirySafetyThresholdMs;
27
30
  this._httpClient = httpClient;
28
31
  this._config = Object.assign({ tokenExpirySafetyThresholdMs: HevyAPIClient.DEFAULT_TOKEN_EXPIRY_SAFETY_THRESHOLD_MS, accessTokenMinimumValidAgeMs: HevyAPIClient.DEFAULT_ACCESS_TOKEN_MINIMUM_VALID_AGE_MS, tokenRefreshThrottleMs: HevyAPIClient.DEFAULT_TOKEN_REFRESH_THROTTLE_MS, refreshAuthTokenApiEndpoint: HevyAPIClient.DEFAULT_REFRESH_AUTH_TOKEN_API_ENDPOINT, isAccessTokenExpiredResponse: (response) => response.status === 401 &&
29
32
  (0, types_1.isHTTPErrorResponse)(response) &&
@@ -45,16 +48,28 @@ class HevyAPIClient {
45
48
  }
46
49
  refreshExpiredAuthToken() {
47
50
  return __awaiter(this, void 0, void 0, function* () {
48
- const { _authToken } = this;
49
- if (!_authToken)
51
+ const { _authToken: authToken } = this;
52
+ if (!authToken)
50
53
  return;
51
- if (new Date().valueOf() <=
52
- new Date(_authToken.expires_at).valueOf() -
53
- this._config.tokenExpirySafetyThresholdMs) {
54
+ const now = new Date();
55
+ if (!this._isExpired({ now, authToken })) {
54
56
  // Token is still valid, at least according to the client. Don't refresh.
55
57
  return;
56
58
  }
57
- yield this._refreshAuthToken(_authToken);
59
+ if (this._config.localRefresh !== undefined) {
60
+ const authToken = this._config.localRefresh();
61
+ this._authToken = authToken;
62
+ this._lastTokenRefresh = undefined; // This isn't shared across processes
63
+ if (authToken === null) {
64
+ // We were de-authenticated, there is nothing to refresh.
65
+ return;
66
+ }
67
+ if (!this._isExpired({ now, authToken })) {
68
+ // Token was refreshed by a different process (e.g. other browser tab).
69
+ return;
70
+ }
71
+ }
72
+ yield this._refreshAuthToken(authToken);
58
73
  });
59
74
  }
60
75
  forceRefreshAuthToken() {
package/built/async.d.ts CHANGED
@@ -38,10 +38,4 @@ type MethodDecorator<T> = (target: unknown, propertyKey: string | symbol, descri
38
38
  export declare function synchronized(queue: boolean): MethodDecorator<Promise<any>>;
39
39
  export declare function synchronized(queue: boolean, id: any): MethodDecorator<Promise<any>>;
40
40
  export declare function synchronized(target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>): void;
41
- /** @deprecated Using this on a synchronous function has no effect */
42
- export declare function synchronized(queue: boolean): MethodDecorator<any>;
43
- /** @deprecated Using this on a synchronous function has no effect */
44
- export declare function synchronized(queue: boolean, id: any): MethodDecorator<any>;
45
- /** @deprecated Using this on a synchronous function has no effect */
46
- export declare function synchronized(target: unknown, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>): void;
47
41
  export {};
package/built/index.d.ts CHANGED
@@ -558,7 +558,6 @@ export interface UserProfile {
558
558
  routines: RoutineMetadata[];
559
559
  weekly_workout_durations: WeeklyWorkoutDuration[];
560
560
  mutual_followers: PreviewMutualUser[];
561
- streak_count: number;
562
561
  }
563
562
  export interface PublicUserProfile {
564
563
  private_profile: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hevy-shared",
3
- "version": "1.0.820",
3
+ "version": "1.0.822",
4
4
  "description": "",
5
5
  "main": "built/index.js",
6
6
  "types": "built/index.d.ts",