import { Injectable } from '@angular/core' ; import { RequestOptionsArgs , Response , Headers } from '@angular/http' ; import { IHttpInterceptor } from '@covalent/http' ; import { TdDialogService } from "@covalent/core" ; import { AuthService } from "../../services/auth.service" ; @ Injectable ( ) export class RequestInterceptor implements IHttpInterceptor { constructor ( private _dialogService , private _auth ) { } onRequest ( requestOptions ) { if ( this . _auth . auth ) { let headers = new Headers ( ) ; headers . append ( 'authorization' , this . _auth . auth . authentication_token ) ; requestOptions . headers = headers ; } return requestOptions ; } onResponse ( error ) { return error ; } ; onResponseError ( error ) { let data ; try { data = error . json ( ) ; } catch ( err ) { data = { } ; } this . openAlert ( data , error . status ) ; return error ; } ; openAlert ( message , status ) { let title = null ; switch ( status ) { case 0 : title = null ; break ; case 400 : title = 'Bad request' ; break ; case 401 : title = 'Unauthorized: you don\'t access' ; break ; case 403 : title = 'Forbidden: access not allowed' ; break ; case 404 : title = null ; break ; case 500 : title = 'Contact Support' ; break ; default : title = 'Contact Support' ; break ; } if ( title ) { this . _dialogService . openAlert ( { message : message [ 'message' ] || 'Unexpected error check network connection!!' , disableClose : false , title : title , closeButton : 'Close' , } ) ; } } }