hevy-shared 1.0.874 → 1.0.876

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.
@@ -95,6 +95,7 @@ export declare class HevyAPIClient<UserContext = never> {
95
95
  private _legacyAuthToken;
96
96
  private _httpClient;
97
97
  private _lastTokenRefresh;
98
+ private _lastSessionDelete;
98
99
  constructor(httpClient: HTTPClient, config: HevyAPIClientConfig<UserContext>);
99
100
  private get _authToken();
100
101
  private get _authHeaders();
@@ -120,9 +121,12 @@ export declare class HevyAPIClient<UserContext = never> {
120
121
  legacyAuthToken: string | null;
121
122
  }): Promise<void>;
122
123
  clearAuthToken(): Promise<void>;
124
+ get isAuthenticated(): boolean;
125
+ markSessionDeleted(): void;
123
126
  attachErrorHandler(onError: HTTPErrorHandler<{
124
127
  willRetry: boolean;
125
128
  isTokenRefreshedAfterRequest: boolean;
129
+ isPreviousSession: boolean;
126
130
  }>): void;
127
131
  removeErrorHandlers(): void;
128
132
  get<T = never>(url: string, config?: RequestConfig): Promise<HTTPResponse<T>>;
@@ -161,9 +161,19 @@ class HevyAPIClient {
161
161
  */
162
162
  const isTokenRefreshedAfterRequest = this._lastTokenRefresh !== undefined &&
163
163
  requestTime < this._lastTokenRefresh;
164
+ /**
165
+ * Similar to the above, but this will be `true` if the user has logged
166
+ * out after the request was made, i.e. the session that the request was
167
+ * made within is is (probably) no longer valid. In this case, we don't
168
+ * want to retry the request, but we also don't want to log the user out,
169
+ * in case that they've logged in to another session in the meantime.
170
+ */
171
+ const isPreviousSession = this._lastSessionDelete !== undefined &&
172
+ requestTime < this._lastSessionDelete;
164
173
  this._errorHandlers.forEach((cb) => cb(response, request, {
165
174
  willRetry: retryOnFailure,
166
175
  isTokenRefreshedAfterRequest,
176
+ isPreviousSession,
167
177
  }));
168
178
  if (retryOnFailure && this.isAccessTokenExpiredResponse(response)) {
169
179
  yield this.forceRefreshAuthToken(userContext);
@@ -171,7 +181,8 @@ class HevyAPIClient {
171
181
  }
172
182
  else if (retryOnFailure &&
173
183
  this.isAccessTokenInvalidResponse(response) &&
174
- isTokenRefreshedAfterRequest) {
184
+ isTokenRefreshedAfterRequest &&
185
+ !isPreviousSession) {
175
186
  yield this.waitForTokenRefresh();
176
187
  return yield this._handleResponse(request, userContext, false);
177
188
  }
@@ -215,6 +226,18 @@ class HevyAPIClient {
215
226
  this._lastTokenRefresh = undefined;
216
227
  });
217
228
  }
229
+ get isAuthenticated() {
230
+ if (this._authTokenFactory.type === 'value' &&
231
+ this._authTokenFactory.value === null) {
232
+ return false;
233
+ }
234
+ else {
235
+ return true;
236
+ }
237
+ }
238
+ markSessionDeleted() {
239
+ this._lastSessionDelete = new Date();
240
+ }
218
241
  attachErrorHandler(onError) {
219
242
  this._errorHandlers.push(onError);
220
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hevy-shared",
3
- "version": "1.0.874",
3
+ "version": "1.0.876",
4
4
  "description": "",
5
5
  "main": "built/index.js",
6
6
  "types": "built/index.d.ts",