@ethlete/query 0.1.0-next.17 → 0.1.0-next.19

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,4 +1,4 @@
1
- import { getCookie, deleteCookie, setCookie, DestroyService } from '@ethlete/core';
1
+ import { getCookie, setCookie, deleteCookie, DestroyService } from '@ethlete/core';
2
2
  import { filter, takeWhile, switchMap, of, BehaviorSubject, Subject, interval, takeUntil, tap, timer, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
3
3
  import * as i0 from '@angular/core';
4
4
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
@@ -599,7 +599,13 @@ const decryptBearer = (token) => {
599
599
 
600
600
  class BearerAuthProvider {
601
601
  get header() {
602
- return { Authorization: `Bearer ${this._token}` };
602
+ return { Authorization: `Bearer ${this._tokens$.getValue().token}` };
603
+ }
604
+ get tokens$() {
605
+ return this._tokens$.asObservable();
606
+ }
607
+ get tokens() {
608
+ return this._tokens$.getValue();
603
609
  }
604
610
  get currentRefreshQuery$() {
605
611
  return this._currentRefreshQuery$.asObservable();
@@ -611,18 +617,31 @@ class BearerAuthProvider {
611
617
  this._config = _config;
612
618
  this._destroy$ = new Subject();
613
619
  this._currentRefreshQuery$ = new BehaviorSubject(null);
614
- this._token = null;
615
- this._refreshToken = null;
616
- const cookieToken = _config.refreshConfig?.cookieName ? getCookie(_config.refreshConfig.cookieName) ?? null : null;
617
- this._token = _config.token || null;
618
- this._refreshToken = _config.refreshConfig?.token || cookieToken || null;
619
- if (!this._token && !this._refreshToken) {
620
+ this._tokens$ = new BehaviorSubject({
621
+ token: null,
622
+ refreshToken: null,
623
+ });
624
+ const cookieEnabled = _config.refreshConfig?.cookieEnabled ?? true;
625
+ const cookieToken = _config.refreshConfig?.cookieName && cookieEnabled ? getCookie(_config.refreshConfig.cookieName) ?? null : null;
626
+ this._tokens$.next({
627
+ token: _config.token || null,
628
+ refreshToken: _config.refreshConfig?.token || cookieToken || null,
629
+ });
630
+ if (!this.tokens.token && !this.tokens.refreshToken) {
620
631
  if (!_config.refreshConfig?.cookieName) {
621
632
  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.');
622
633
  }
623
634
  return;
624
635
  }
625
- this._prepareForRefresh();
636
+ if (this.tokens.token) {
637
+ this._prepareForRefresh();
638
+ if (this.tokens.refreshToken && this._config.refreshConfig?.cookieName) {
639
+ setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
640
+ }
641
+ }
642
+ else if (this.tokens.refreshToken) {
643
+ this._refreshQuery();
644
+ }
626
645
  }
627
646
  cleanUp() {
628
647
  this._destroy$.next(true);
@@ -632,11 +651,28 @@ class BearerAuthProvider {
632
651
  deleteCookie(this._config.refreshConfig.cookieName, '/');
633
652
  }
634
653
  }
654
+ enableCookie() {
655
+ if (!this._config.refreshConfig?.cookieName) {
656
+ throw new Error('No cookie name was provided');
657
+ }
658
+ if (!this.tokens.refreshToken) {
659
+ throw new Error('No refresh token was provided');
660
+ }
661
+ this._config.refreshConfig.cookieEnabled = true;
662
+ setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
663
+ }
664
+ disableCookie() {
665
+ if (!this._config.refreshConfig?.cookieName) {
666
+ throw new Error('No cookie name was provided');
667
+ }
668
+ this._config.refreshConfig.cookieEnabled = false;
669
+ deleteCookie(this._config.refreshConfig.cookieName, '/');
670
+ }
635
671
  _prepareForRefresh() {
636
- if (!this._token || !this._config.refreshConfig) {
672
+ if (!this.tokens.token || !this._config.refreshConfig) {
637
673
  return;
638
674
  }
639
- const bearer = decryptBearer(this._token);
675
+ const bearer = decryptBearer(this.tokens.token);
640
676
  if (!bearer) {
641
677
  return;
642
678
  }
@@ -658,11 +694,15 @@ class BearerAuthProvider {
658
694
  }
659
695
  }
660
696
  _refreshQuery() {
661
- if (!this._refreshToken || !this._config.refreshConfig?.queryCreator) {
697
+ const currentRefreshToken = this.tokens.refreshToken;
698
+ if (!currentRefreshToken || !this._config.refreshConfig) {
662
699
  return;
663
700
  }
664
- const args = this._config.refreshConfig.requestArgsAdapter?.(this._refreshToken, this._token) ?? {
665
- body: { refreshToken: this._refreshToken },
701
+ const args = this._config.refreshConfig.requestArgsAdapter?.({
702
+ token: this.tokens.token,
703
+ refreshToken: currentRefreshToken,
704
+ }) ?? {
705
+ body: { refreshToken: currentRefreshToken },
666
706
  };
667
707
  const query = this._config.refreshConfig.queryCreator.prepare(args).execute({ skipCache: true });
668
708
  this._currentRefreshQuery$.next(query);
@@ -671,21 +711,22 @@ class BearerAuthProvider {
671
711
  if (isQueryStateSuccess(state)) {
672
712
  if (this._config.refreshConfig?.responseAdapter) {
673
713
  const tokens = this._config.refreshConfig.responseAdapter(state.response);
674
- this._token = tokens.token;
675
- this._refreshToken = tokens.refreshToken || null;
714
+ this._tokens$.next(tokens);
676
715
  }
677
716
  else {
678
- this._token = state.response['token'];
679
- this._refreshToken = state.response['refreshToken'];
717
+ this._tokens$.next({
718
+ token: state.response['token'],
719
+ refreshToken: state.response['refreshToken'],
720
+ });
680
721
  }
681
- if (this._config.refreshConfig?.cookieName && this._refreshToken) {
682
- setCookie(this._config.refreshConfig.cookieName, this._refreshToken);
722
+ const cookieEnabled = this._config.refreshConfig?.cookieEnabled ?? true;
723
+ if (this._config.refreshConfig?.cookieName && this.tokens.refreshToken && cookieEnabled) {
724
+ setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
683
725
  }
684
726
  this._prepareForRefresh();
685
727
  }
686
728
  else if (isQueryStateFailure(state)) {
687
- this._token = null;
688
- this._refreshToken = null;
729
+ this._tokens$.next({ token: null, refreshToken: null });
689
730
  if (this._config.refreshConfig?.cookieName) {
690
731
  deleteCookie(this._config.refreshConfig.cookieName, '/');
691
732
  }
@@ -973,7 +1014,7 @@ class InfinityQueryDirective {
973
1014
  itemsPerPage: null,
974
1015
  };
975
1016
  this._infinityQueryInstance = null;
976
- this._destroy$ = inject(DestroyService).destroy$;
1017
+ this._destroy$ = inject(DestroyService, { self: true }).destroy$;
977
1018
  this._cdr = inject(ChangeDetectorRef);
978
1019
  this._viewContainerRef = inject(ViewContainerRef);
979
1020
  this._mainTemplateRef = inject((TemplateRef));