Commit 8e472d218ece7f4fa835c0ea233f6b6de31eb6c0

Authored by Igor Kulikov
Committed by GitHub
2 parents 70412141 5f60c551

Merge pull request #5177 from vvlladd28/improvement/interceptor/counter

[3.3.1] UI: Fixed load counter - requests were not counted when cancel them
@@ -14,21 +14,14 @@ @@ -14,21 +14,14 @@
14 /// limitations under the License. 14 /// limitations under the License.
15 /// 15 ///
16 16
17 -import {  
18 - HttpErrorResponse,  
19 - HttpEvent,  
20 - HttpHandler,  
21 - HttpInterceptor,  
22 - HttpRequest,  
23 - HttpResponseBase  
24 -} from '@angular/common/http'; 17 +import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
25 import { Observable } from 'rxjs/internal/Observable'; 18 import { Observable } from 'rxjs/internal/Observable';
26 import { Inject, Injectable } from '@angular/core'; 19 import { Inject, Injectable } from '@angular/core';
27 import { AuthService } from '@core/auth/auth.service'; 20 import { AuthService } from '@core/auth/auth.service';
28 import { Constants } from '@shared/models/constants'; 21 import { Constants } from '@shared/models/constants';
29 import { InterceptorHttpParams } from './interceptor-http-params'; 22 import { InterceptorHttpParams } from './interceptor-http-params';
30 -import { catchError, delay, mergeMap, switchMap, tap } from 'rxjs/operators';  
31 -import { throwError, of } from 'rxjs'; 23 +import { catchError, delay, finalize, mergeMap, switchMap } from 'rxjs/operators';
  24 +import { of, throwError } from 'rxjs';
32 import { InterceptorConfig } from './interceptor-config'; 25 import { InterceptorConfig } from './interceptor-config';
33 import { Store } from '@ngrx/store'; 26 import { Store } from '@ngrx/store';
34 import { AppState } from '@core/core.state'; 27 import { AppState } from '@core/core.state';
@@ -62,17 +55,25 @@ export class GlobalHttpInterceptor implements HttpInterceptor { @@ -62,17 +55,25 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
62 if (req.url.startsWith('/api/')) { 55 if (req.url.startsWith('/api/')) {
63 const config = this.getInterceptorConfig(req); 56 const config = this.getInterceptorConfig(req);
64 this.updateLoadingState(config, true); 57 this.updateLoadingState(config, true);
  58 + let observable$: Observable<HttpEvent<any>>;
65 if (this.isTokenBasedAuthEntryPoint(req.url)) { 59 if (this.isTokenBasedAuthEntryPoint(req.url)) {
66 if (!AuthService.getJwtToken() && !this.authService.refreshTokenPending()) { 60 if (!AuthService.getJwtToken() && !this.authService.refreshTokenPending()) {
67 - return this.handleResponseError(req, next, new HttpErrorResponse({error: {message: 'Unauthorized!'}, status: 401})); 61 + observable$ = this.handleResponseError(req, next, new HttpErrorResponse({error: {message: 'Unauthorized!'}, status: 401}));
68 } else if (!AuthService.isJwtTokenValid()) { 62 } else if (!AuthService.isJwtTokenValid()) {
69 - return this.handleResponseError(req, next, new HttpErrorResponse({error: {refreshTokenPending: true}})); 63 + observable$ = this.handleResponseError(req, next, new HttpErrorResponse({error: {refreshTokenPending: true}}));
70 } else { 64 } else {
71 - return this.jwtIntercept(req, next); 65 + observable$ = this.jwtIntercept(req, next);
72 } 66 }
73 } else { 67 } else {
74 - return this.handleRequest(req, next); 68 + observable$ = this.handleRequest(req, next);
75 } 69 }
  70 + return observable$.pipe(
  71 + finalize(() => {
  72 + if (req.url.startsWith('/api/')) {
  73 + this.updateLoadingState(config, false);
  74 + }
  75 + })
  76 + );
76 } else { 77 } else {
77 return next.handle(req); 78 return next.handle(req);
78 } 79 }
@@ -83,43 +84,20 @@ export class GlobalHttpInterceptor implements HttpInterceptor { @@ -83,43 +84,20 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
83 if (newReq) { 84 if (newReq) {
84 return this.handleRequest(newReq, next); 85 return this.handleRequest(newReq, next);
85 } else { 86 } else {
86 - return this.handleRequestError(req, new Error('Could not get JWT token from store.')); 87 + return throwError(new Error('Could not get JWT token from store.'));
87 } 88 }
88 } 89 }
89 90
90 private handleRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { 91 private handleRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
91 return next.handle(req).pipe( 92 return next.handle(req).pipe(
92 - tap((event: HttpEvent<any>) => {  
93 - if (event instanceof HttpResponseBase) {  
94 - this.handleResponse(req, event as HttpResponseBase);  
95 - }  
96 - }),  
97 catchError((err) => { 93 catchError((err) => {
98 const errorResponse = err as HttpErrorResponse; 94 const errorResponse = err as HttpErrorResponse;
99 return this.handleResponseError(req, next, errorResponse); 95 return this.handleResponseError(req, next, errorResponse);
100 })); 96 }));
101 } 97 }
102 98
103 - private handleRequestError(req: HttpRequest<any>, err): Observable<HttpEvent<any>> {  
104 - const config = this.getInterceptorConfig(req);  
105 - if (req.url.startsWith('/api/')) {  
106 - this.updateLoadingState(config, false);  
107 - }  
108 - return throwError(err);  
109 - }  
110 -  
111 - private handleResponse(req: HttpRequest<any>, response: HttpResponseBase) {  
112 - const config = this.getInterceptorConfig(req);  
113 - if (req.url.startsWith('/api/')) {  
114 - this.updateLoadingState(config, false);  
115 - }  
116 - }  
117 -  
118 private handleResponseError(req: HttpRequest<any>, next: HttpHandler, errorResponse: HttpErrorResponse): Observable<HttpEvent<any>> { 99 private handleResponseError(req: HttpRequest<any>, next: HttpHandler, errorResponse: HttpErrorResponse): Observable<HttpEvent<any>> {
119 const config = this.getInterceptorConfig(req); 100 const config = this.getInterceptorConfig(req);
120 - if (req.url.startsWith('/api/')) {  
121 - this.updateLoadingState(config, false);  
122 - }  
123 let unhandled = false; 101 let unhandled = false;
124 const ignoreErrors = config.ignoreErrors; 102 const ignoreErrors = config.ignoreErrors;
125 const resendRequest = config.resendRequest; 103 const resendRequest = config.resendRequest;