@ethlete/query 2.8.0 → 2.9.1

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.
@@ -1,6 +1,6 @@
1
1
  import * as i1 from '@ethlete/core';
2
2
  import { BehaviorSubjectWithSubscriberCount, getCookie, setCookie, deleteCookie, DestroyService, DelayableDirective, equal, clone } from '@ethlete/core';
3
- import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, interval, startWith, finalize, tap, timer, map, combineLatest, fromEvent, debounceTime, take, concat, distinctUntilChanged } from 'rxjs';
3
+ import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, tap, interval, startWith, finalize, timer, map, combineLatest, fromEvent, debounceTime, take, concat, distinctUntilChanged } from 'rxjs';
4
4
  import * as i0 from '@angular/core';
5
5
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
6
6
  import { FormGroup } from '@angular/forms';
@@ -909,11 +909,37 @@ class Query {
909
909
  });
910
910
  }
911
911
  else if (state.type === 'failure') {
912
- this._state$.next({
913
- type: "FAILURE" /* QueryStateType.Failure */,
914
- error: state.error,
915
- meta,
916
- });
912
+ const failure = () => {
913
+ this._state$.next({
914
+ type: "FAILURE" /* QueryStateType.Failure */,
915
+ error: state.error,
916
+ meta,
917
+ });
918
+ };
919
+ const bearerAuthProvider = this._getBearerAuthProvider();
920
+ if (!bearerAuthProvider || options?._isUnauthorizedRetry) {
921
+ return failure();
922
+ }
923
+ else if (state.error.status === 401 /* HttpStatusCode.Unauthorized */ &&
924
+ bearerAuthProvider.shouldRefreshOnUnauthorizedResponse) {
925
+ const query = bearerAuthProvider._refreshQuery();
926
+ if (!query) {
927
+ return failure();
928
+ }
929
+ query.state$
930
+ .pipe(takeUntilResponse(), takeUntil(this._onAbort$), tap((state) => {
931
+ if (isQueryStateSuccess(state)) {
932
+ this.execute({ ...options, _isUnauthorizedRetry: true });
933
+ }
934
+ else if (isQueryStateFailure(state)) {
935
+ failure();
936
+ }
937
+ }))
938
+ .subscribe();
939
+ }
940
+ else {
941
+ failure();
942
+ }
917
943
  }
918
944
  else if (state.type === 'cancel') {
919
945
  this._state$.next({
@@ -995,6 +1021,13 @@ class Query {
995
1021
  this._pollingSubscription?.unsubscribe();
996
1022
  this._entitySubscription?.unsubscribe();
997
1023
  }
1024
+ _getBearerAuthProvider() {
1025
+ const authProvider = this._client.authProvider;
1026
+ if (authProvider && isBearerAuthProvider(authProvider)) {
1027
+ return authProvider;
1028
+ }
1029
+ return null;
1030
+ }
998
1031
  }
999
1032
 
1000
1033
  const isBasicAuthProvider = (authProvider) => authProvider instanceof BasicAuthProvider;
@@ -1032,6 +1065,11 @@ class BearerAuthProvider {
1032
1065
  get currentRefreshQuery() {
1033
1066
  return this._currentRefreshQuery$.getValue();
1034
1067
  }
1068
+ get shouldRefreshOnUnauthorizedResponse() {
1069
+ return (!!this._config.refreshConfig &&
1070
+ (this._config.refreshConfig.refreshOnUnauthorizedResponse === undefined ||
1071
+ this._config.refreshConfig.refreshOnUnauthorizedResponse));
1072
+ }
1035
1073
  constructor(_config) {
1036
1074
  this._config = _config;
1037
1075
  this._destroy$ = new Subject();
@@ -1112,7 +1150,13 @@ class BearerAuthProvider {
1112
1150
  break;
1113
1151
  }
1114
1152
  }
1153
+ /**
1154
+ * @internal
1155
+ */
1115
1156
  _refreshQuery() {
1157
+ if (isQueryStateLoading(this._currentRefreshQuery$.getValue()?.state)) {
1158
+ return;
1159
+ }
1116
1160
  const currentRefreshToken = this.tokens.refreshToken;
1117
1161
  if (!currentRefreshToken || !this._config.refreshConfig) {
1118
1162
  return;
@@ -1152,6 +1196,7 @@ class BearerAuthProvider {
1152
1196
  }
1153
1197
  }), takeUntilResponse(), takeUntil(this._destroy$))
1154
1198
  .subscribe();
1199
+ return query;
1155
1200
  }
1156
1201
  }
1157
1202