Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {Response, RequestOptionsArgs} from '@angular/http';
  3. import {AuthHttp as JwtAuthHttp} from 'angular2-jwt';
  4. import {Observable} from 'rxjs/Observable';
  5.  
  6. import {ErrorService} from '../error/error.service';
  7.  
  8.  
  9. @Injectable()
  10. export class CustomAuthHttp {
  11.  
  12. constructor(private authHttp: JwtAuthHttp, private errorService: ErrorService) {
  13. }
  14.  
  15. private authIntercept(response: Observable<Response>): Observable<Response> {
  16. return response
  17. .do(null, (error) => {
  18. let errorStatus: number = error.status;
  19. let errorMessage: string = JSON.stringify(error);
  20. try {
  21. errorMessage = JSON.parse(error._body).message;
  22. } catch (e) {
  23. }
  24. this.errorService.requestErrorHandler(errorStatus, errorMessage);
  25. });
  26. }
  27.  
  28. public get(url: string, options?: RequestOptionsArgs): Observable<Response> {
  29. return this.authIntercept(this.authHttp.get(url, options));
  30. }
  31.  
  32. public post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
  33. return this.authIntercept(this.authHttp.post(url, body, options));
  34. }
  35.  
  36. public put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
  37. return this.authIntercept(this.authHttp.put(url, body, options));
  38. }
  39.  
  40. public delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
  41. return this.authIntercept(this.authHttp.delete(url, options));
  42. }
  43.  
  44. public patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
  45. return this.authIntercept(this.authHttp.patch(url, body, options));
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement