Created
August 18, 2018 18:43
-
-
Save RoyiNamir/f81862aec4fe70fb0b6e82e92adddfb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http'; | |
import {Injectable} from '@angular/core'; | |
import {Observable} from 'rxjs'; | |
import {tap} from "rxjs/operators"; | |
@Injectable() | |
export class CustomInterceptor implements HttpInterceptor | |
{ | |
log = (req: HttpRequest<any>) => tap((res: HttpEvent<any>) => | |
{ | |
if ((res instanceof HttpResponse)) | |
{ | |
console.log(this.logJson(req, res)); | |
} | |
}, res => | |
{ | |
if ((res instanceof HttpErrorResponse)) | |
{ | |
console.log(this.logJson(req, res)); | |
} | |
}); | |
logJson(req: HttpRequest<any>, res): string | |
{ | |
return ` | |
Method=${req.method} | |
Url=${req.urlWithParams} | |
Url Params=${JSON.stringify(req.params.keys() | |
.reduce((acc, c) => (acc[c] = req.params.get(c), acc), {}))} | |
Body Params=${JSON.stringify(req.body, null, " ")} | |
Headers=${JSON.stringify(req.headers.keys() | |
.map(x => ({[x]: req.headers.get(x)})), null, " ")} | |
---------------------------------------------------------------------------- | |
Response=${JSON.stringify(res, null, " ")} | |
Headers=${JSON.stringify(res.headers.keys() | |
.map(x => ({[x]: res.headers.get(x)})), null, " ")}`; | |
} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> | |
{ | |
return next.handle(req) | |
.pipe(this.log(req)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment