0

I'm trying to use inheritance on HttpClient and override all its common methods

but Typescript is pointing an error when I try to override the request method

is there an explanation about error pointing out by Typescript

because it didnt have error when I override GET/POST/PUT/DELETE/OPTIONS

this only 'request' method only like this

enter image description here

aj go
  • 637
  • 2
  • 10
  • 27
  • Take a look at your declaration of the `options` attribute, and compare that to the base class. – Simon Sep 28 '20 at 11:47
  • Yep. done comparing it to the base class and I believe those parameter in my screenshot is the right parameters. about the option parameter, I intentionally set its type to 'any' so I could pass the parameters without having trouble in the lint since its type is an anonymous object – aj go Sep 28 '20 at 12:27

2 Answers2

1

This runs through ng build

@Injectable()
export class FoobarHttpClient extends HttpClient {

    request( method: string, url: string, options?: {
        body?: any;
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        observe?: 'body' | 'events' | 'response'; // == HttpObserve;
        reportProgress?: boolean;
        responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
        withCredentials?: boolean;
    } ): Observable<any> {
        return super.request( method, url, options );
    }

}

My project is still on Angular 9, and there http.d.ts doesn't expose the internally used HttpObserve (maybe that's now exported in Angular 10, I didn't check).

If you haven't done so already, I recommend looking into the ng HttpInterceptor. Using an interceptor is the common approach to customize HttpClient-behavior.

Simon
  • 2,994
  • 3
  • 28
  • 37
  • I did something like this approach but it still pointing the same thing on my intellisense. could you provide some link on how I could achieve the same thing using Interceptor? – aj go Sep 29 '20 at 01:21
  • 1
    From SO: https://stackoverflow.com/questions/48348946/angular-5-httpinterceptor-retry-request?rq=1 or https://stackoverflow.com/questions/54733093/angular-http-interceptor-to-retry-requests-with-specific-error-status. From medium eg. https://medium.com/angular-in-depth/top-10-ways-to-use-interceptors-in-angular-db450f8a62d6 – Simon Sep 29 '20 at 11:32
0

This is only hack but I couldn't think of a proper way to solve this

so just by declaring the 'request' method in this way, I could remove that error:

request: any = (method: string, url: string, options?: any): Observable<any> => {

    if (this.authService.config.activateInterception) {
        return this.requestWithRetry(method, url, 0, options);
    }
    else {
        return super.request(method, url, options);
    }
}
aj go
  • 637
  • 2
  • 10
  • 27