import { HttpInterceptor , HttpHandler , HttpRequest , HttpEvent , HttpResponse } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { tap , finalize } from 'rxjs/operators' ; import { Injectable } from "@angular/core" ; @ Injectable ( ) export class LogResponseTimeInterceptor implements HttpInterceptor { /**
     * Credit: https://angular.io/guide/http#http-interceptors
     */ intercept ( req , next ) { const started = Date . now ( ) ; let ok ; return next . handle ( req ) . pipe ( tap ( event => ( ok = event instanceof HttpResponse ? 'succeeded' : '' ) , error => ( ok = 'failed' ) ) , finalize ( ( ) => { const elapsed = Date . now ( ) - started ; console . log ( `${ req . method } "${ req . urlWithParams }" \n\t ${ ok } in ${ elapsed } ms.` ) ; } ) ) ; } }