projects/maplander/core/src/lib/interceptors/state/browser-state.interceptor.ts
Methods |
constructor(_platform: Object, _transferState: TransferState)
|
|||||||||
Parameters :
|
intercept | |||||||||
intercept(req: HttpRequest
|
|||||||||
Parameters :
Returns :
Observable<HttpEvent<any>>
|
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { Observable , of } from 'rxjs';
import {isPlatformBrowser} from '@angular/common';
@Injectable()
export class BrowserStateInterceptor implements HttpInterceptor {
constructor(
@Inject(PLATFORM_ID) private _platform: Object,
private _transferState: TransferState
) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const storedResponse: any = this._transferState.get(makeStateKey(req.url), null);
if (storedResponse && !isPlatformBrowser(this._platform)) {
const response = new HttpResponse({ body: storedResponse, status: 200 });
return of(response);
}
return next.handle(req);
}
}