@ethlete/query 2.7.3 → 2.9.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.
@@ -1,5 +1,6 @@
1
- import { BehaviorSubjectWithSubscriberCount, getCookie, setCookie, deleteCookie, DestroyService, equal, clone } from '@ethlete/core';
2
- import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, interval, startWith, finalize, tap, timer, map, fromEvent, debounceTime, take, concat, combineLatest, distinctUntilChanged } from 'rxjs';
1
+ import * as i1 from '@ethlete/core';
2
+ import { BehaviorSubjectWithSubscriberCount, getCookie, setCookie, deleteCookie, DestroyService, DelayableDirective, equal, clone } from '@ethlete/core';
3
+ import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, tap, interval, startWith, finalize, timer, map, combineLatest, fromEvent, debounceTime, take, concat, distinctUntilChanged } from 'rxjs';
3
4
  import * as i0 from '@angular/core';
4
5
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
5
6
  import { FormGroup } from '@angular/forms';
@@ -902,11 +903,34 @@ class Query {
902
903
  });
903
904
  }
904
905
  else if (state.type === 'failure') {
905
- this._state$.next({
906
- type: "FAILURE" /* QueryStateType.Failure */,
907
- error: state.error,
908
- meta,
909
- });
906
+ const failure = () => {
907
+ this._state$.next({
908
+ type: "FAILURE" /* QueryStateType.Failure */,
909
+ error: state.error,
910
+ meta,
911
+ });
912
+ };
913
+ const bearerAuthProvider = this._getBearerAuthProvider();
914
+ if (!bearerAuthProvider || (options === null || options === void 0 ? void 0 : options._isUnauthorizedRetry)) {
915
+ return failure();
916
+ }
917
+ else if (state.error.status === 401 /* HttpStatusCode.Unauthorized */ &&
918
+ bearerAuthProvider.shouldRefreshOnUnauthorizedResponse) {
919
+ const query = bearerAuthProvider._refreshQuery();
920
+ if (!query) {
921
+ return failure();
922
+ }
923
+ query.state$
924
+ .pipe(takeUntilResponse(), takeUntil(this._onAbort$), tap((state) => {
925
+ if (isQueryStateSuccess(state)) {
926
+ this.execute(Object.assign(Object.assign({}, options), { _isUnauthorizedRetry: true }));
927
+ }
928
+ else if (isQueryStateFailure(state)) {
929
+ failure();
930
+ }
931
+ }))
932
+ .subscribe();
933
+ }
910
934
  }
911
935
  else if (state.type === 'cancel') {
912
936
  this._state$.next({
@@ -993,6 +1017,13 @@ class Query {
993
1017
  (_a = this._pollingSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
994
1018
  (_b = this._entitySubscription) === null || _b === void 0 ? void 0 : _b.unsubscribe();
995
1019
  }
1020
+ _getBearerAuthProvider() {
1021
+ const authProvider = this._client.authProvider;
1022
+ if (authProvider && isBearerAuthProvider(authProvider)) {
1023
+ return authProvider;
1024
+ }
1025
+ return null;
1026
+ }
996
1027
  }
997
1028
 
998
1029
  const isBasicAuthProvider = (authProvider) => authProvider instanceof BasicAuthProvider;
@@ -1030,6 +1061,11 @@ class BearerAuthProvider {
1030
1061
  get currentRefreshQuery() {
1031
1062
  return this._currentRefreshQuery$.getValue();
1032
1063
  }
1064
+ get shouldRefreshOnUnauthorizedResponse() {
1065
+ return (!!this._config.refreshConfig &&
1066
+ (this._config.refreshConfig.refreshOnUnauthorizedResponse === undefined ||
1067
+ this._config.refreshConfig.refreshOnUnauthorizedResponse));
1068
+ }
1033
1069
  constructor(_config) {
1034
1070
  var _a, _b, _c, _d, _e, _f, _g;
1035
1071
  this._config = _config;
@@ -1115,16 +1151,22 @@ class BearerAuthProvider {
1115
1151
  break;
1116
1152
  }
1117
1153
  }
1154
+ /**
1155
+ * @internal
1156
+ */
1118
1157
  _refreshQuery() {
1119
- var _a, _b, _c;
1158
+ var _a, _b, _c, _d;
1159
+ if (isQueryStateLoading((_a = this._currentRefreshQuery$.getValue()) === null || _a === void 0 ? void 0 : _a.state)) {
1160
+ return;
1161
+ }
1120
1162
  const currentRefreshToken = this.tokens.refreshToken;
1121
1163
  if (!currentRefreshToken || !this._config.refreshConfig) {
1122
1164
  return;
1123
1165
  }
1124
- const args = (_c = (_b = (_a = this._config.refreshConfig).requestArgsAdapter) === null || _b === void 0 ? void 0 : _b.call(_a, {
1166
+ const args = (_d = (_c = (_b = this._config.refreshConfig).requestArgsAdapter) === null || _c === void 0 ? void 0 : _c.call(_b, {
1125
1167
  token: this.tokens.token,
1126
1168
  refreshToken: currentRefreshToken,
1127
- })) !== null && _c !== void 0 ? _c : {
1169
+ })) !== null && _d !== void 0 ? _d : {
1128
1170
  body: { refreshToken: currentRefreshToken },
1129
1171
  };
1130
1172
  const query = this._config.refreshConfig.queryCreator.prepare(args).execute({ skipCache: true });
@@ -1157,6 +1199,7 @@ class BearerAuthProvider {
1157
1199
  }
1158
1200
  }), takeUntilResponse(), takeUntil(this._destroy$))
1159
1201
  .subscribe();
1202
+ return query;
1160
1203
  }
1161
1204
  }
1162
1205
 
@@ -1458,6 +1501,7 @@ class InfinityQueryDirective {
1458
1501
  this._viewContainerRef = inject(ViewContainerRef);
1459
1502
  this._mainTemplateRef = inject((TemplateRef));
1460
1503
  this._errorHandler = inject(ErrorHandler);
1504
+ this._delayable = inject(DelayableDirective, { host: true });
1461
1505
  this._data$ = new BehaviorSubject([]);
1462
1506
  this._isMainViewCreated = false;
1463
1507
  }
@@ -1494,15 +1538,15 @@ class InfinityQueryDirective {
1494
1538
  }
1495
1539
  _setupInfinityQuery(config) {
1496
1540
  const instance = new InfinityQuery(config);
1497
- instance.currentQuery$
1498
- .pipe(switchMap((q) => { var _a; return (_a = q === null || q === void 0 ? void 0 : q.state$) !== null && _a !== void 0 ? _a : of(null); }), tap((state) => {
1541
+ combineLatest([instance.currentQuery$.pipe(switchQueryState()), this._delayable.isDelayed$])
1542
+ .pipe(tap(([state, isDelayed]) => {
1499
1543
  this._viewContext.currentPage = instance.currentPage;
1500
1544
  this._viewContext.totalPages = instance.totalPages;
1501
1545
  this._viewContext.itemsPerPage = instance.itemsPerPage;
1502
1546
  this._viewContext.canLoadMore =
1503
1547
  (instance.totalPages && instance.currentPage && instance.totalPages > instance.currentPage) || false;
1504
1548
  this._viewContext.currentCalculatedPage = instance.currentCalculatedPage;
1505
- if (isQueryStateLoading(state)) {
1549
+ if (isQueryStateLoading(state) || isDelayed) {
1506
1550
  this._viewContext.loading = true;
1507
1551
  this._viewContext.error = null;
1508
1552
  this._viewContext.isFirstLoad = this.context.infinityQuery === null;
@@ -1553,6 +1597,9 @@ class InfinityQueryDirective {
1553
1597
  if (!this._infinityQueryInstance) {
1554
1598
  return;
1555
1599
  }
1600
+ if (this._viewContext.loading) {
1601
+ return;
1602
+ }
1556
1603
  this._infinityQueryInstance.nextPage();
1557
1604
  }
1558
1605
  reset(newConfig) {
@@ -1584,7 +1631,7 @@ class InfinityQueryDirective {
1584
1631
  }
1585
1632
  }
1586
1633
  InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1587
- InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.5", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1634
+ InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.5", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], hostDirectives: [{ directive: i1.DelayableDirective }], ngImport: i0 });
1588
1635
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1589
1636
  type: Directive,
1590
1637
  args: [{
@@ -1593,6 +1640,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.5", ngImpor
1593
1640
  exportAs: 'infinityQuery',
1594
1641
  standalone: true,
1595
1642
  providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService],
1643
+ hostDirectives: [DelayableDirective],
1596
1644
  }]
1597
1645
  }], propDecorators: { infinityQuery: [{
1598
1646
  type: Input