-
-
Save sebfischer83/9fbf317f13c3f1f6610c22aeb1f7c6a7 to your computer and use it in GitHub Desktop.
Using MiniProfiler with Angular & HttpClient for SPAs http://www.arturdr.ru/net/using-mini-profiler-with-angular-and-httpclient/ (ported from https://gist.github.com/GeorgDangl/2807d11b367540e1de5629502289f5a8)
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 { NgModule } from '@angular/core'; | |
import { HttpClientModule, HTTP_INTERCEPTORS, } from '@angular/common/http'; | |
import { MiniProfilerInterceptor } from './mini-profiler-http'; | |
@NgModule({ | |
providers: [ | |
{ provide: HTTP_INTERCEPTORS, useClass: MiniProfilerInterceptor, multi: true }, | |
] | |
}) | |
export class AppModule { } |
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 { Injectable } from '@angular/core'; | |
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse, HttpHeaders } | |
from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/operator/do'; | |
declare var MiniProfiler: any; | |
@Injectable() | |
export class MiniProfilerInterceptor implements HttpInterceptor { | |
intercept( | |
req: HttpRequest<any>, | |
next: HttpHandler | |
): Observable<HttpEvent<any>> { | |
return next.handle(req).do(evt => { | |
if (evt instanceof HttpResponse) { | |
if (typeof MiniProfiler !== 'undefined' && evt && evt.headers) { | |
this.makeMiniProfilerRequests(evt.headers); | |
} | |
} | |
}); | |
} | |
private makeMiniProfilerRequests(headers: HttpHeaders) { | |
const miniProfilerHeaders = headers.getAll('x-miniprofiler-ids'); | |
if (!miniProfilerHeaders) { | |
return; | |
} | |
miniProfilerHeaders.forEach(miniProfilerIdHeaderValue => { | |
const ids = JSON.parse(miniProfilerIdHeaderValue) as string[]; | |
MiniProfiler.fetchResults(ids); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment