@ethlete/query 0.1.0-next.15 → 0.1.0-next.16

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,7 +1,7 @@
1
+ import { getCookie, deleteCookie, setCookie, DestroyService } from '@ethlete/core';
1
2
  import { Subject, timer, filter, takeWhile, switchMap, of, BehaviorSubject, interval, takeUntil, tap, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
2
3
  import * as i0 from '@angular/core';
3
4
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
4
- import { DestroyService } from '@ethlete/core';
5
5
  import { FormGroup } from '@angular/forms';
6
6
  import { Router, ActivatedRoute } from '@angular/router';
7
7
 
@@ -318,6 +318,7 @@ class BearerAuthProvider {
318
318
  }
319
319
  constructor(_config) {
320
320
  this._config = _config;
321
+ this._token = null;
321
322
  this._refreshToken = null;
322
323
  this._refreshTimerSubscription = null;
323
324
  this._failureCount = 0;
@@ -325,18 +326,41 @@ class BearerAuthProvider {
325
326
  this.onRefreshInitiation$ = new Subject();
326
327
  this.onRefreshSuccess$ = new Subject();
327
328
  this.onRefreshFailure$ = new Subject();
328
- this._token = _config.token;
329
+ this._token = _config.token || null;
329
330
  this._refreshToken = _config.refreshConfig?.token ?? null;
330
- if (this._config.refreshConfig) {
331
+ if (this._config.refreshConfig && this._token) {
331
332
  this._setupRefresh(this._config.refreshConfig);
332
333
  }
334
+ else if (this._config.refreshConfig?.cookieName) {
335
+ const cookie = getCookie(this._config.refreshConfig.cookieName);
336
+ if (!cookie) {
337
+ this._autoDestruct();
338
+ return;
339
+ }
340
+ this._refreshToken = cookie;
341
+ this._refresh(this._config.refreshConfig);
342
+ }
343
+ else {
344
+ this._autoDestruct();
345
+ console.error('A BearerAuthProvider was created without token or refresh token. You should provide at least a cookieName where the refresh token might be stored.');
346
+ }
333
347
  }
334
- cleanUp() {
348
+ cleanUp(config) {
335
349
  this._refreshTimerSubscription?.unsubscribe();
336
350
  this._refreshTimerSubscription = null;
351
+ if (this._config.refreshConfig?.cookieName && !config?.keepCookie) {
352
+ deleteCookie(this._config.refreshConfig.cookieName, '/');
353
+ }
337
354
  }
338
- _setupRefresh(config) {
355
+ _autoDestruct() {
339
356
  this.cleanUp();
357
+ this.queryClient?.clearAuthProvider();
358
+ }
359
+ _setupRefresh(config) {
360
+ this.cleanUp({ keepCookie: true });
361
+ if (!this._token) {
362
+ return;
363
+ }
340
364
  const bearer = decryptBearer(this._token);
341
365
  if (!bearer) {
342
366
  return;
@@ -387,6 +411,9 @@ class BearerAuthProvider {
387
411
  this._token = tokens.token;
388
412
  this._refreshToken = tokens.refreshToken;
389
413
  this.onRefreshSuccess$.next(tokens);
414
+ if (config.cookieName) {
415
+ setCookie(config.cookieName, tokens.refreshToken);
416
+ }
390
417
  this._setupRefresh(config);
391
418
  this._failureCount = 0;
392
419
  }
@@ -401,6 +428,9 @@ class BearerAuthProvider {
401
428
  if (this._failureCount <= (config.maxRefreshAttempts ?? 3)) {
402
429
  this._setupRefresh(config);
403
430
  }
431
+ else {
432
+ this._autoDestruct();
433
+ }
404
434
  }
405
435
  }
406
436
  }
@@ -769,9 +799,9 @@ class QueryDirective {
769
799
  }
770
800
  }
771
801
  }
772
- QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
773
- QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.1", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
774
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: QueryDirective, decorators: [{
802
+ QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
803
+ QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.2", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
804
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: QueryDirective, decorators: [{
775
805
  type: Directive,
776
806
  args: [{
777
807
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1080,9 +1110,9 @@ class InfinityQueryDirective {
1080
1110
  this._data$.next([]);
1081
1111
  }
1082
1112
  }
1083
- InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1084
- InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.1", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1085
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1113
+ InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1114
+ InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.2", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1115
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1086
1116
  type: Directive,
1087
1117
  args: [{
1088
1118
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1132,9 +1162,9 @@ class InfinityQueryTriggerDirective {
1132
1162
  this._observer.observe(this._elementRef.nativeElement);
1133
1163
  }
1134
1164
  }
1135
- InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1136
- InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.1", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1137
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.1", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1165
+ InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1166
+ InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.2", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1167
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.2", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1138
1168
  type: Directive,
1139
1169
  args: [{
1140
1170
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1230,11 +1260,14 @@ class QueryClient {
1230
1260
  return this._clientConfig;
1231
1261
  }
1232
1262
  get authProvider() {
1233
- return this._authProvider;
1263
+ return this._authProvider$.getValue();
1264
+ }
1265
+ get authProvider$() {
1266
+ return this._authProvider$.asObservable();
1234
1267
  }
1235
1268
  constructor(_clientConfig) {
1236
1269
  this._clientConfig = _clientConfig;
1237
- this._authProvider = null;
1270
+ this._authProvider$ = new BehaviorSubject(null);
1238
1271
  this.get = (queryConfig) => this.fetch({
1239
1272
  ...queryConfig,
1240
1273
  method: 'GET',
@@ -1291,15 +1324,15 @@ class QueryClient {
1291
1324
  };
1292
1325
  };
1293
1326
  this.setAuthProvider = (authProvider) => {
1294
- if (this._authProvider) {
1327
+ if (this.authProvider) {
1295
1328
  throw new Error('The auth provider is already set. Please call clearAuthProvider() first.');
1296
1329
  }
1297
- this._authProvider = authProvider;
1330
+ this._authProvider$.next(authProvider);
1298
1331
  authProvider.queryClient = this;
1299
1332
  };
1300
1333
  this.clearAuthProvider = () => {
1301
- this._authProvider?.cleanUp();
1302
- this._authProvider = null;
1334
+ this.authProvider?.cleanUp();
1335
+ this._authProvider$.next(null);
1303
1336
  };
1304
1337
  this._store = new QueryStore({
1305
1338
  enableChangeLogging: _clientConfig.logging?.queryStateChanges,