@smartbit4all/ng-client 2.0.9 → 2.0.11

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,9 +1,9 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, Injectable, Optional, Inject, PLATFORM_ID, NgModule, SkipSelf, Component, Pipe, CUSTOM_ELEMENTS_SCHEMA, ViewChild, Input, ViewChildren } from '@angular/core';
3
- import { Subject, takeUntil, map, catchError, throwError, from } from 'rxjs';
4
- import { SmartSubject } from '@smartbit4all/subject';
5
3
  import * as i1 from '@angular/common/http';
6
- import { HttpHeaders, HttpContext, HttpParams, HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
4
+ import { HttpHeaders, HttpContext, HttpErrorResponse, HttpParams, HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
5
+ import { Subject, lastValueFrom, takeUntil, map, catchError, throwError, from } from 'rxjs';
6
+ import { SmartSubject } from '@smartbit4all/subject';
7
7
  import * as i4 from '@angular/common';
8
8
  import { isPlatformBrowser, DOCUMENT, DatePipe, CommonModule } from '@angular/common';
9
9
  import * as i2 from '@angular/material/button';
@@ -765,7 +765,7 @@ class SmartSessionService {
765
765
  }
766
766
  async handleSessionError(sessionError) {
767
767
  if (this.sessionHandlerService) {
768
- await this.sessionHandlerService.handleSessionError(sessionError);
768
+ return await this.sessionHandlerService.handleSessionError(sessionError);
769
769
  }
770
770
  throw new Error(`sessionHandlerService was not initialized. Error: ${sessionError.code}`);
771
771
  }
@@ -860,11 +860,7 @@ class SmartSessionService {
860
860
  async startSession() {
861
861
  let sessionInfo = await this.apiService.startSession().toPromise();
862
862
  if (sessionInfo) {
863
- this.sessionInfoData = sessionInfo;
864
- this.token = sessionInfo.sid;
865
- this.saveTokenToLocalStorage();
866
- this.setAuthenticationStatus();
867
- this.setUpTokenForSessionService();
863
+ this.updateSessionInfo(sessionInfo);
868
864
  }
869
865
  return this.sessionInfoData;
870
866
  }
@@ -883,26 +879,34 @@ class SmartSessionService {
883
879
  if (!refreshToken) {
884
880
  return await this.startSession();
885
881
  }
886
- let sessionInfo = await this.apiService
887
- .refreshSession({
888
- refreshToken,
889
- })
890
- .toPromise()
891
- .catch(async (error) => {
892
- if (error.error &&
893
- error.error.code &&
894
- error.error.code === 'session.refreshtoken.invalid') {
895
- await this.startSession();
882
+ try {
883
+ const sessionInfo = await lastValueFrom(this.apiService.refreshSession({ refreshToken }));
884
+ if (sessionInfo) {
885
+ this.updateSessionInfo(sessionInfo);
886
+ return sessionInfo;
896
887
  }
897
- });
898
- if (sessionInfo) {
899
- this.sessionInfoData = sessionInfo;
900
- this.token = sessionInfo.sid;
901
- this.saveTokenToLocalStorage();
902
- this.setAuthenticationStatus();
903
- this.setUpTokenForSessionService();
904
888
  }
905
- return this.sessionInfoData;
889
+ catch (error) {
890
+ if (this.isInvalidRefreshTokenError(error)) {
891
+ this.clearAndStartNewSession();
892
+ // still throw the error, so the error can be handled in the caller
893
+ }
894
+ throw error;
895
+ }
896
+ // no sessionInfo returned
897
+ throw new Error('Unable to refresh session');
898
+ }
899
+ updateSessionInfo(sessionInfo) {
900
+ this.sessionInfoData = sessionInfo;
901
+ this.token = sessionInfo.sid;
902
+ this.saveTokenToLocalStorage();
903
+ this.setAuthenticationStatus();
904
+ this.setUpTokenForSessionService();
905
+ }
906
+ isInvalidRefreshTokenError(error) {
907
+ return (error instanceof HttpErrorResponse &&
908
+ error.error &&
909
+ error.error.code === 'session.refreshtoken.invalid');
906
910
  }
907
911
  async getAuthenticationProviders() {
908
912
  let providers = await this.apiService
@@ -3044,6 +3048,7 @@ class SmartDevToolDialogComponent {
3044
3048
  }
3045
3049
  ngOnDestroy() {
3046
3050
  this._destroy$.next();
3051
+ this._destroy$.complete();
3047
3052
  }
3048
3053
  clear() {
3049
3054
  this.service.clear();
@@ -3134,6 +3139,7 @@ class SmartDevToolFloatingButtonComponent {
3134
3139
  }
3135
3140
  ngOnDestroy() {
3136
3141
  this._destroy$.next();
3142
+ this._destroy$.complete();
3137
3143
  }
3138
3144
  openDevToolDialog() {
3139
3145
  this.service.openDialog();
@@ -3284,52 +3290,68 @@ class SmartErrorCatchingInterceptor {
3284
3290
  this.session = session;
3285
3291
  this.sessionErrorCodes = [
3286
3292
  {
3287
- code: "session.expired",
3293
+ code: 'session.expired',
3288
3294
  behaviour: SessionErrorBehaviour.REFRESH,
3289
3295
  },
3290
3296
  {
3291
- code: "session.refreshtoken.invalid",
3297
+ code: 'session.refreshtoken.invalid',
3292
3298
  behaviour: SessionErrorBehaviour.RESTART,
3293
3299
  },
3294
3300
  {
3295
- code: "session.badrequest.missingtoken",
3301
+ code: 'session.badrequest.missingtoken',
3296
3302
  behaviour: SessionErrorBehaviour.RESTART,
3297
3303
  },
3298
3304
  {
3299
- code: "session.security.nocontext",
3305
+ code: 'session.security.nocontext',
3300
3306
  behaviour: SessionErrorBehaviour.RESTART,
3301
3307
  },
3302
3308
  {
3303
- code: "session.invalidsessionuri",
3309
+ code: 'session.invalidsessionuri',
3304
3310
  behaviour: SessionErrorBehaviour.RESTART,
3305
3311
  },
3306
3312
  {
3307
- code: "session.notinitialized",
3313
+ code: 'session.notinitialized',
3308
3314
  behaviour: SessionErrorBehaviour.RESTART,
3309
3315
  },
3310
3316
  {
3311
- code: "session.viewcontext.missing",
3317
+ code: 'session.viewcontext.missing',
3312
3318
  behaviour: SessionErrorBehaviour.RESTART,
3313
3319
  },
3314
3320
  ];
3315
3321
  }
3316
3322
  intercept(request, next) {
3317
- return next.handle(request).pipe(catchError$1((error) => {
3318
- if (error.status === 400 && error.error.code) {
3319
- let sessionError = this.sessionErrorCodes.find((sessionError) => sessionError.code === error.error.code);
3320
- if (sessionError) {
3321
- return this.resolveError(sessionError).pipe(switchMap(() => {
3322
- this.session.headerParams.map((headerParam) => {
3323
- request = request.clone({
3324
- headers: request.headers.set(headerParam.headerName, headerParam.value),
3325
- });
3326
- });
3327
- return next.handle(request);
3328
- }));
3329
- }
3323
+ return next
3324
+ .handle(request)
3325
+ .pipe(catchError$1((error) => this.handleError(error, request, next)));
3326
+ }
3327
+ handleError(error, request, next) {
3328
+ if (this.isSessionError(error)) {
3329
+ const sessionError = this.getSessionError(error);
3330
+ if (sessionError) {
3331
+ return this.resolveError(sessionError).pipe(switchMap((resolved) => {
3332
+ if (resolved && sessionError.behaviour === SessionErrorBehaviour.REFRESH) {
3333
+ return next.handle(this.updateRequestHeaders(request));
3334
+ }
3335
+ return throwError(() => error);
3336
+ }), catchError$1((resolveError) => {
3337
+ // when the resolveError throws a restart error (in the refreshSession), rethrow the original error
3338
+ if (this.isSessionError(resolveError)) {
3339
+ const resolveSessionError = this.getSessionError(resolveError);
3340
+ if (resolveSessionError?.behaviour === SessionErrorBehaviour.RESTART) {
3341
+ return throwError(() => error);
3342
+ }
3343
+ }
3344
+ return throwError(() => resolveError);
3345
+ }));
3330
3346
  }
3331
- return throwError(error);
3332
- }));
3347
+ }
3348
+ return throwError(() => error);
3349
+ }
3350
+ isSessionError(error) {
3351
+ return error.status === 400 && error.error.code != null;
3352
+ }
3353
+ getSessionError(error) {
3354
+ return this.sessionErrorCodes.find((sessionError) => sessionError.code === error.error.code);
3333
3355
  }
3334
3356
  resolveError(sessionError) {
3335
3357
  if (sessionError.behaviour === SessionErrorBehaviour.RESTART) {
@@ -3337,6 +3359,14 @@ class SmartErrorCatchingInterceptor {
3337
3359
  }
3338
3360
  return from(this.session.refreshSession());
3339
3361
  }
3362
+ updateRequestHeaders(request) {
3363
+ this.session.headerParams.forEach((headerParam) => {
3364
+ request = request.clone({
3365
+ headers: request.headers.set(headerParam.headerName, headerParam.value),
3366
+ });
3367
+ });
3368
+ return request;
3369
+ }
3340
3370
  }
3341
3371
  SmartErrorCatchingInterceptor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: SmartErrorCatchingInterceptor, deps: [{ token: SmartSessionService }], target: i0.ɵɵFactoryTarget.Injectable });
3342
3372
  SmartErrorCatchingInterceptor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: SmartErrorCatchingInterceptor });
@@ -4273,6 +4303,7 @@ class UiActionToolbarComponent {
4273
4303
  }
4274
4304
  ngOnDestroy() {
4275
4305
  this._destroy$.next();
4306
+ this._destroy$.complete();
4276
4307
  }
4277
4308
  subscribeToLanguageChange() {
4278
4309
  this.languageChangedSubscription?.unsubscribe();