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

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,30 @@ 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;
620
+ this._tokens$ = new BehaviorSubject({
621
+ token: null,
622
+ refreshToken: null,
623
+ });
616
624
  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) {
625
+ this._tokens$.next({
626
+ token: _config.token || null,
627
+ refreshToken: _config.refreshConfig?.token || cookieToken || null,
628
+ });
629
+ if (!this.tokens.token && !this.tokens.refreshToken) {
620
630
  if (!_config.refreshConfig?.cookieName) {
621
631
  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
632
  }
623
633
  return;
624
634
  }
625
- this._prepareForRefresh();
635
+ if (this.tokens.token) {
636
+ this._prepareForRefresh();
637
+ if (this.tokens.refreshToken && this._config.refreshConfig?.cookieName) {
638
+ setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
639
+ }
640
+ }
641
+ else if (this.tokens.refreshToken) {
642
+ this._refreshQuery();
643
+ }
626
644
  }
627
645
  cleanUp() {
628
646
  this._destroy$.next(true);
@@ -633,10 +651,10 @@ class BearerAuthProvider {
633
651
  }
634
652
  }
635
653
  _prepareForRefresh() {
636
- if (!this._token || !this._config.refreshConfig) {
654
+ if (!this.tokens.token || !this._config.refreshConfig) {
637
655
  return;
638
656
  }
639
- const bearer = decryptBearer(this._token);
657
+ const bearer = decryptBearer(this.tokens.token);
640
658
  if (!bearer) {
641
659
  return;
642
660
  }
@@ -658,11 +676,11 @@ class BearerAuthProvider {
658
676
  }
659
677
  }
660
678
  _refreshQuery() {
661
- if (!this._refreshToken || !this._config.refreshConfig?.queryCreator) {
679
+ if (!this.tokens.refreshToken || !this._config.refreshConfig) {
662
680
  return;
663
681
  }
664
- const args = this._config.refreshConfig.requestArgsAdapter?.(this._refreshToken, this._token) ?? {
665
- body: { refreshToken: this._refreshToken },
682
+ const args = this._config.refreshConfig.requestArgsAdapter?.(this.tokens) ?? {
683
+ body: { refreshToken: this.tokens.refreshToken },
666
684
  };
667
685
  const query = this._config.refreshConfig.queryCreator.prepare(args).execute({ skipCache: true });
668
686
  this._currentRefreshQuery$.next(query);
@@ -671,21 +689,21 @@ class BearerAuthProvider {
671
689
  if (isQueryStateSuccess(state)) {
672
690
  if (this._config.refreshConfig?.responseAdapter) {
673
691
  const tokens = this._config.refreshConfig.responseAdapter(state.response);
674
- this._token = tokens.token;
675
- this._refreshToken = tokens.refreshToken || null;
692
+ this._tokens$.next(tokens);
676
693
  }
677
694
  else {
678
- this._token = state.response['token'];
679
- this._refreshToken = state.response['refreshToken'];
695
+ this._tokens$.next({
696
+ token: state.response['token'],
697
+ refreshToken: state.response['refreshToken'],
698
+ });
680
699
  }
681
- if (this._config.refreshConfig?.cookieName && this._refreshToken) {
682
- setCookie(this._config.refreshConfig.cookieName, this._refreshToken);
700
+ if (this._config.refreshConfig?.cookieName && this.tokens.refreshToken) {
701
+ setCookie(this._config.refreshConfig.cookieName, this.tokens.refreshToken);
683
702
  }
684
703
  this._prepareForRefresh();
685
704
  }
686
705
  else if (isQueryStateFailure(state)) {
687
- this._token = null;
688
- this._refreshToken = null;
706
+ this._tokens$.next({ token: null, refreshToken: null });
689
707
  if (this._config.refreshConfig?.cookieName) {
690
708
  deleteCookie(this._config.refreshConfig.cookieName, '/');
691
709
  }