Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Injectable} from '@angular/core';
- import {Response, RequestOptionsArgs} from '@angular/http';
- import {AuthHttp as JwtAuthHttp} from 'angular2-jwt';
- import {Observable} from 'rxjs/Observable';
- import {ErrorService} from '../error/error.service';
- @Injectable()
- export class CustomAuthHttp {
- constructor(private authHttp: JwtAuthHttp, private errorService: ErrorService) {
- }
- private authIntercept(response: Observable<Response>): Observable<Response> {
- return response
- .do(null, (error) => {
- let errorStatus: number = error.status;
- let errorMessage: string = JSON.stringify(error);
- try {
- errorMessage = JSON.parse(error._body).message;
- } catch (e) {
- }
- this.errorService.requestErrorHandler(errorStatus, errorMessage);
- });
- }
- public get(url: string, options?: RequestOptionsArgs): Observable<Response> {
- return this.authIntercept(this.authHttp.get(url, options));
- }
- public post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
- return this.authIntercept(this.authHttp.post(url, body, options));
- }
- public put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
- return this.authIntercept(this.authHttp.put(url, body, options));
- }
- public delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
- return this.authIntercept(this.authHttp.delete(url, options));
- }
- public patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
- return this.authIntercept(this.authHttp.patch(url, body, options));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement