Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
#12 Example now has dashboard in api root, and improved http logging
Browse files Browse the repository at this point in the history
  • Loading branch information
michadvorak-cen38289 committed Feb 16, 2018
1 parent 0c48245 commit bfd5165
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ <h1>{{title}}</h1>
<ng-container *resourceData="let data of apiLocation" [resourceContext]="data">
<nav>
<!-- TODO -->
<a [resourceLink]="'/api/dashboard'">Dashboard</a>
<a [resourceLink]="'/api'">Dashboard</a>
<a [resourceLink]="'/api/heroes'">Heroes</a>
</nav>

Expand Down
4 changes: 2 additions & 2 deletions src/app/in-memory-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ export class InMemoryDataService implements InMemoryDbService {
'Content-Type': HEROES_TYPE
})
}));
} else if (request.url.endsWith('/api/dashboard')) {
} else if (request.url.endsWith('/api')) {
const collection = (request.utils.getDb() as any).heroes;

return request.utils.createResponse$(() => ({
url: request.url,
body: JSON.stringify(<Heroes>{
items: collection.slice(1, 5).map(hyperHero),
_links: {
self: {href: '/api/dashboard'},
self: {href: '/api'},
}
}),
status: 200,
Expand Down
16 changes: 14 additions & 2 deletions src/app/message-http-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest, HttpResponseBase } from '@angular/common/http';
import {
HttpEvent,
HttpHandler,
HttpHeaders,
HttpInterceptor,
HttpRequest,
HttpResponseBase
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';
import { MessageService } from './message.service';
Expand All @@ -10,13 +17,18 @@ import { MessageService } from './message.service';
@Injectable()
export class MessageHttpInterceptor implements HttpInterceptor {

private static nextId = 1;

constructor(private readonly messageService: MessageService) {
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const id = MessageHttpInterceptor.nextId++;
this.messageService.add(`[${id}] ${req.method} ${req.url} (${dumpHeaders(req.headers)})`);

return next.handle(req).do((res: HttpEvent<any>) => {
if (res instanceof HttpResponseBase) {
this.messageService.add(`${req.method} ${req.url} => ${res.status} ${res.statusText} (${dumpHeaders(res.headers)})`);
this.messageService.add(`[${id}] => ${res.status} ${res.statusText} (${dumpHeaders(res.headers)})`);
}
});
}
Expand Down

0 comments on commit bfd5165

Please sign in to comment.