This file contains hidden or 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
@Component({ | |
selector: 'my-app', | |
template: ` | |
<div><h3>Response</h3>{{response|async|json}}</div> | |
<button (click)="request()">Make request</button>` | |
, | |
}) | |
export class AppComponent { | |
response: Observable<any>; | |
constructor(private http: HttpClient) {} |
This file contains hidden or 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
export abstract class HttpHandler { | |
abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>>; | |
} |
This file contains hidden or 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
@NgModule({ | |
providers: [ | |
HttpXhrBackend, | |
{provide: HttpBackend, useExisting: HttpXhrBackend} | |
] | |
}) | |
export class HttpClientModule {} |
This file contains hidden or 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
export abstract class HttpHandler { | |
abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>>; | |
} | |
export abstract class HttpBackend implements HttpHandler { | |
abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>>; | |
} | |
export class HttpXhrBackend implements HttpBackend { | |
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {} |
This file contains hidden or 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
@NgModule({ | |
providers: [ | |
HttpXhrBackend, | |
{ provide: HttpBackend, useExisting: HttpXhrBackend } | |
] | |
}) | |
export class HttpClientModule {} |
This file contains hidden or 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
export class AppComponent { | |
response: Observable<any>; | |
constructor(private backend: HttpXhrBackend) {} | |
request() { | |
const req = new HttpRequest('GET', 'https://jsonplaceholder.typicode.com/posts/1'); | |
this.response = this.backend.handle(req); | |
} | |
} |
This file contains hidden or 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
export class AppComponent { | |
response: Observable<any>; | |
constructor(private http: HttpClient) {} | |
request() { | |
const url = 'https://jsonplaceholder.typicode.com/posts/1'; | |
this.response = this.http.get(url, {observe: 'body'}); | |
} | |
} |
This file contains hidden or 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
export class AppComponent { | |
response: Observable<any>; | |
constructor(private backend: HttpXhrBackend) {} | |
request() { | |
const req = new HttpRequest('GET', 'https://jsonplaceholder.typicode.com/posts/1'); | |
this.response = this.backend.handle(req); | |
} | |
} |
This file contains hidden or 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
export class HttpInterceptorHandler implements HttpHandler { | |
constructor(private next: HttpHandler, private interceptor: HttpInterceptor) {} | |
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> { | |
// execute an interceptor and pass the reference to the next handler | |
return this.interceptor.intercept(req, this.next); | |
} | |
} |
This file contains hidden or 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
export class AppComponent { | |
response: Observable<any>; | |
constructor(private backend: HttpXhrBackend) {} | |
request() { | |
const req = new HttpRequest('GET', 'https://jsonplaceholder.typicode.com/posts/1'); | |
const handler = new HttpInterceptorHandler(this.backend, new I1()); | |
this.response = handler.handle(req); | |
} | |
} |