Skip to content

Instantly share code, notes, and snippets.

@keithstric
Last active June 28, 2021 15:07
Show Gist options
  • Save keithstric/0e0301a760a8258213ffe2e00d18d19a to your computer and use it in GitHub Desktop.
Save keithstric/0e0301a760a8258213ffe2e00d18d19a to your computer and use it in GitHub Desktop.
import {Injectable} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpResponse, HttpErrorResponse
} from '@angular/common/http';
import {HttpCacheService} from '@core/services/http-cache/http-cache.service';
import {Logger} from '@core/services/logger/logger';
import {Observable, of} from 'rxjs';
import {catchError, finalize, tap} from 'rxjs/operators';
import {LoadingService} from '@layout/services/loading/loading.service';
/**
* Intercept all http requests
* @class {HttpRequestInterceptor}
*/
@Injectable()
export class HttpRequestInterceptor implements HttpRequestInterceptor {
constructor(
private _loading: LoadingService,
private _cache: HttpCacheService
) {}
/**
* When an http request starts, set loading to true. When the request is finished, set loading to false.
* If an error is thrown be sure loading is set to false.
* @param {HttpRequest} request
* @param {HttpHandler} next
* @returns {Observable<HttpEvent<any>>}
*/
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this._loading.setLoading(true, request);
let cachedResponse;
if (request.method === 'GET') {
cachedResponse = this._cache.get(request);
if (cachedResponse) {
Logger.debug(`Response from cache for ${request.urlWithParams}`, cachedResponse);
}
}else if (request.method === 'POST' || request.method === 'PUT' || request.method === 'PATCH' || request.method === 'DELETE') {
const removedFromCache = this._cache.delete(request);
if (removedFromCache) {
Logger.debug(`Cleared ${request.urlWithParams} from the cache`);
}
}
return next.handle(request)
.pipe(
tap<HttpEvent<any>>((httpEvent: HttpEvent<any>) => {
if (httpEvent instanceof HttpResponse) {
this._cache.put(request, httpEvent);
}
return cachedResponse ? cachedResponse : httpEvent;
}),
catchError((err: HttpErrorResponse) => {
throw err;
}),
finalize(() => {
this._loading.setLoading(false, request);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment