kishan
kishan

Reputation: 13

Skip http interceptors for all GET requests

I had a http interceptor service which will hit a toast message on success and failure and it is working fine.

But i want to skip this toasts to get all requests , cos everytime when i open a page it was showing the success toast.

I want to display only when user makes a request manually

import { Jsonp } from '@angular/http';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from "@angular/core"
import { Observable } from "rxjs";
import { tap, catchError } from "rxjs/operators";
import { ToastrService } from 'ngx-toastr';
import { of } from 'rxjs/observable/of';

@Injectable()

export class AppHttpInterceptor implements HttpInterceptor {
    constructor(public toasterService: ToastrService) { }
    intercept(
        req: HttpRequest<any>,
        next: HttpHandler
    ): Observable<HttpEvent<any>> {
        return next.handle(req).pipe(
            tap(evt => {
                if (evt instanceof HttpResponse && evt.body && evt.body.success) {
                    // this.toasterService.success(evt.body.success.message, evt.body.success.title);
                    this.displayToast(evt.body)
                }
            }),
            catchError((err: any) => {
                if (err instanceof HttpErrorResponse) {
                    try {
                        // this.toasterService.error(err.error.message, err.error.title);
                        this.displayToast(err.error)
                    } catch (e) {
                        // this.toasterService.error('An error occurred', '');
                        this.displayToast(err.error)
                    }
                    //log error 
                }
                return of(err);
            }));

    }

    displayToast(response) {
        let message = response.message;

        if (response.code == 200) {
            this.toasterService.success('success');
        } else if (response.code == 201) {
            this.toasterService.success(message, 'Created');
        } else if (response.code == 400) {
            this.toasterService.error(message, 'Bad Request');
        } else if (response.code == 401) {
            this.toasterService.warning(message, 'Unauthorized');
        } else if (response.code == 403) {
            this.toasterService.warning(message, 'Forbidden');
        } else if (response.code == 404) {
            this.toasterService.warning(message, 'Not Found');
        } else if (response.code == 409) {
            this.toasterService.error(message);
        } else if (response.code == 411) {
            this.toasterService.info(message, 'No Records');
        } else if (response.code == 500) {
            this.toasterService.error(message, 'Internal Server Error');
        } else if (response.code == 503) {
            this.toasterService.error(message, 'Service Unavailable');
        }
    }

}

Upvotes: 1

Views: 2619

Answers (1)

Adrita Sharma
Adrita Sharma

Reputation: 22203

You can detect the request method by req.method

Try this:

requestMethod = req.method

if(requestMethod != "GET" {
  this.displayToast(err.error)
} 

Upvotes: 2

Related Questions