Home > Mobile >  Interceptor Type 'Observable<HttpResponse>' is not assignable to type 'Observab
Interceptor Type 'Observable<HttpResponse>' is not assignable to type 'Observab

Time:02-24

I'm having trouble handling the response of a cached query.

In order to improve the performance of my application, I investigated that using a TransferStateInterceptor was the best solution. But unfortunately I have a little problem to return the cached response.

in my research this answer seemed to be the solution https://stackoverflow.com/a/53492870/8747207

I also wanted to implement the solution in this video, but I always get the same problem when returning the cached response

The problem seems to be the type with which the response is returned, but I haven't found any useful information on how to solve this issue.

The error is specifically in this line

return of(new HttpResponse<any>({ body: cachedResponse })); // here is the type fail

the error message is this:

Type 'Observable<HttpResponse>' is not assignable to type 'Observable<HttpEvent<any>>'.
  Type 'HttpResponse' is not assignable to type 'HttpEvent<any>'.
    Type 'HttpResponse' is missing the following properties from type 'HttpResponse<any>': type, clone, status, statusText, and 2 more.

in both solutions i try to implement, just when i have to return the HttpResponse is where it fails.

This is a sample of my code in the interceptor.

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor} from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { TransferStateService } from '../internal/transfer-state.service';
import { HttpResponse } from 'aws-sdk';
import { tap } from 'rxjs/operators';

@Injectable()
export class TransferStateInterceptor implements HttpInterceptor {

  constructor(private transferStateService: TransferStateService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.method !== 'GET') {
      return next.handle(req);
    }

    const cachedResponse = this.transferStateService.getCache(req.url);
    if (cachedResponse) {
       return of(new HttpResponse<any>({ body: cachedResponse })); // here is the type fail
    }

    return next.handle(req).pipe(
      tap(event => {
        if (event instanceof HttpResponse) {
          this.transferStateService.setCache(req.url, event.body);
        }
      })
    );
   }
 }

and this is my TransferStateService

import { isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { makeStateKey, TransferState } from '@angular/platform-browser';
const transferStateCache: String[] = [];
@Injectable({
  providedIn: 'root'
})
export class TransferStateService {

  constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId) {}

  setCache(key: string, data: any) {
    if (!isPlatformBrowser(this.platformId)) {
      transferStateCache[key] = makeStateKey<any>(key);
      this.transferState.set(transferStateCache[key], data);
    }
  }


  getCache(key: string): any {
    if (isPlatformBrowser(this.platformId)) {
      const cachedData: any = this.transferState['store'][key];
      delete this.transferState['store'][key];
      return cachedData;
    }
  }
}

how can i fix this problem in the interceptor so i can get the stored response?

CodePudding user response:

In your TransferStateInterceptor,

import { HttpResponse } from 'aws-sdk';

While I believe you need to import HttpResponse from '@angular/common/http'.

Union types for HttpEvent

import { ..., HttpResponse } from '@angular/common/http';
  • Related