/**
 * Reference:
 * https://blog.tomasandtomas.com/angular-2-http-interceptors-7e2d74b7f14e#.ph17wjbiu
 */ import { Injectable } from '@angular/core' ; import { Http , ConnectionBackend , RequestOptions , RequestOptionsArgs , Response , Headers , Request } from '@angular/http' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/Rx' ; @ Injectable ( ) export class HttpInterceptor extends Http { constructor ( backend , defaultOptions ) { super ( backend , defaultOptions ) ; } param url param options returns { Observable < Response > } request ( url , options ? ) { return super . request ( url , options ) ; } param url param options returns { Observable < > } get ( url , options ? ) { this . requestInterceptor ( ) ; return super . get ( url , this . requestOptions ( options ) ) . catch ( this . onCatch ) . do ( ( res ) => { this . onSubscribeSuccess ( res ) ; } , ( error ) => { this . onSubscribeError ( error ) ; } ) . finally ( ( ) => { this . onFinally ( ) ; } ) ; } getLocal ( url , options ? ) { return super . get ( url , options ) ; } param url param body param options returns { Observable < > } post ( url , body , options ? ) { this . requestInterceptor ( ) ; return super . post ( url , body , this . requestOptions ( options ) ) . catch ( this . onCatch ) . do ( ( res ) => { this . onSubscribeSuccess ( res ) ; } , ( error ) => { this . onSubscribeError ( error ) ; } ) . finally ( ( ) => { this . onFinally ( ) ; } ) ; } param url param body param options returns { Observable < > } put ( url , body , options ? ) { this . requestInterceptor ( ) ; return super . put ( url , body , this . requestOptions ( options ) ) . catch ( this . onCatch ) . do ( ( res ) => { this . onSubscribeSuccess ( res ) ; } , ( error ) => { this . onSubscribeError ( error ) ; } ) . finally ( ( ) => { this . onFinally ( ) ; } ) ; } param url param options returns { Observable < > } delete ( url , options ? ) { this . requestInterceptor ( ) ; return super . delete ( url , options ) . catch ( this . onCatch ) . do ( ( res ) => { this . onSubscribeSuccess ( res ) ; } , ( error ) => { this . onSubscribeError ( error ) ; } ) . finally ( ( ) => { this . onFinally ( ) ; } ) ; } param options returns { RequestOptionsArgs } private requestOptions ( options ? ) { if ( options == null ) { options = new RequestOptions ( ) ; } if ( options . headers == null ) { options . headers = new Headers ( ) ; } return options ; } /**
     * Request interceptor.
     */ private requestInterceptor ( ) { } /**
     * Response interceptor.
     */ private responseInterceptor ( ) { } param error param caught returns { ErrorObservable } private onCatch ( error , caught ) { return Observable . throw ( error ) ; } param res private onSubscribeSuccess ( res ) { } param error private onSubscribeError ( error ) { } /**
     * onFinally
     */ private onFinally ( ) { this . responseInterceptor ( ) ; } }