DEPRICATED SINCE Angular 4.3 (HttpInterCeptors are back in 4.3)
You can create your own HTTP class and use the rxjs Subject Service to reuse your custom Http class and implement your actions in a custom class.
Implement your custom Http class using the "HttpSubjectService", which contains some rxjs objects.
import { Injectable } from '@angular/core'; import { Http, ConnectionBackend, Request, RequestOptions, RequestOptionsArgs, Response } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import { HttpSubjectService } from './httpSubject.service'; @Injectable() export class CustomHttp extends Http { constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private httpSubjectService: HttpSubjectService) { super(backend, defaultOptions);
Custom module for registering your CustomHttp class - here you overwrite the default Http implementation from Angular using your own CustomHttp implementation.
import { NgModule, ValueProvider } from '@angular/core'; import { HttpModule, Http, XHRBackend, RequestOptions } from '@angular/http';
now we need an implementation of HttpSubjectService where we can SubScribe for our rxjs users when they are called with the following statement.
import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable() export class HttpSubjectService {
in order to invoke your custom Realizations, we need to Connect to Subjects, for example. "app.component.ts".
import { Component } from '@angular/core'; import { HttpSubjectService } from "../HttpInterception/httpSubject.service"; import { Homeservice } from "../HttpServices/home.service"; @Component({ selector: 'app', templateUrl: './app.component.html', }) export class AppComponent { private locals: AppLocalsModel = new AppLocalsModel(); constructor(private httpSubjectService : HttpSubjectService, private homeService : Homeservice) {} ngOnInit(): void { this.notifications(); this.httpRedirects(); this.spinner(); this.overlay(); } public loadServiceData(): void { this.homeService.getCurrentUsername() .subscribe(result => { this.locals.username = result; }); } private overlay(): void { this.httpSubjectService.overlaySubject.subscribe({ next: () => { console.log("Call Overlay Service"); } }); } private spinner(): void { this.httpSubjectService.spinnerSubject.subscribe({ next: (value: number) => { console.log("Call Spinner Service"); } }); } private notifications(): void { this.httpSubjectService.notificationSubject.subscribe({ next: (json: any) => { console.log("Call Notification Service"); } }); } private httpRedirects(): void { this.httpSubjectService.http500Subject.subscribe({ next: (error: any) => { console.log("Navigate to Error Page"); } }); this.httpSubjectService.http403Subject.subscribe({ next: (error: any) => { console.log("Navigate to Not Authorized Page"); } }); } } class AppLocalsModel { public username : string = "noch nicht abgefragt"; }
SINCE Angular 4.3 you can use InterCeptors
In Angular 4.3, you have built-in Interceptors where you can implement your own things, such as redirecting for a 500 server error
import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/throw'; @Injectable() export class SxpHttp500Interceptor implements HttpInterceptor { constructor(public router: Router) { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(req).do(evt => { }).catch(err => { if (err["status"]) { if (err.status === 500) { this.router.navigate(['/serverError', { fehler: JSON.stringify(err) }]); } } return Observable.throw(err); }); } }
you need to register this in your main module in an array of suppliers
import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { Router } from '@angular/router'; import { SxpHttp500Interceptor } from "./sxpHttp500.interceptor"; .... providers: [ { provide: HTTP_INTERCEPTORS, useFactory: (router: Router) => { return new SxpHttp500Interceptor(router) }, multi: true, deps: [Router] } ]