File

projects/maplander/core/src/lib/interceptors/state/browser-state.interceptor.ts

Index

Methods

Constructor

constructor(_platform: Object, _transferState: TransferState)
Parameters :
Name Type Optional
_platform Object No
_transferState TransferState No

Methods

intercept
intercept(req: HttpRequest, next: HttpHandler)
Parameters :
Name Type Optional
req HttpRequest<any> No
next HttpHandler No
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);
  }
}

result-matching ""

    No results matching ""