Skip to content

Instantly share code, notes, and snippets.

@Shaddix
Last active January 5, 2021 06:30
Show Gist options
  • Save Shaddix/d5585f46334efa86ad3e685541869c18 to your computer and use it in GitHub Desktop.
Save Shaddix/d5585f46334efa86ad3e685541869c18 to your computer and use it in GitHub Desktop.
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 { }
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);
});
}
}
@sebfischer83
Copy link

What is declare var MiniProfiler: any; ?

@davmszd
Copy link

davmszd commented Dec 19, 2019

I am trying to use your code

with DotNetCore 2.2 and Angular 8

I enable CORS like below in my startup class

services.AddCors(options => options.AddPolicy("Cors", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

there is some thing strange in the response

when I console.log(r.headers) at line 22

unfortunately there is no x-miniprofiler-ids in the header object

but in fiddler and chrome network tab the x-miniprofiler-ids do exists in the response object

so UI does not show the miniprofiler detail notifier

is there any thing extra which i have to do ???

is there any specila config for dot net core or angular or httpclient ???

is your code compelete and well tested ????

@x3weird
Copy link

x3weird commented Jan 5, 2021

Hi, thanks for this gist, I am implementing the same thing but I was wondering do I need to install mini profiler npm package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment