Skip to content

Instantly share code, notes, and snippets.

@monostere0
Forked from tomasstankovic/http-service.ts
Created August 18, 2016 11:38
Show Gist options
  • Save monostere0/8528bf36c0ed17a8e41271ad39d8729a to your computer and use it in GitHub Desktop.
Save monostere0/8528bf36c0ed17a8e41271ad39d8729a to your computer and use it in GitHub Desktop.
Angular 2 Http interceptor
import { Injectable } from '@angular/core';
import {
Http,
ConnectionBackend,
RequestOptions,
RequestOptionsArgs,
Response,
Headers
} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { LoaderService } from "./loader-service";
import 'rxjs/Rx';
@Injectable()
export class HttpService extends Http {
constructor(backend: ConnectionBackend,
defaultOptions: RequestOptions,
private loaderService: LoaderService) {
super(backend, defaultOptions);
}
/**
* Performs any type of http request.
* @param url
* @param options
* @returns {Observable<Response>}
*/
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return super.request(url, options);
}
/**
* Performs a request with `get` http method.
* @param url
* @param options
* @returns {Observable<>}
*/
get(url: string, options?: RequestOptionsArgs): Observable<any> {
this.requestInterceptor();
return super.get(this.getFullUrl(url), this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSubscribeSuccess(res);
}, (error: any) => {
this.onSubscribeError(error);
})
.finally(() => {
this.onFinally();
});
}
getLocal(url: string, options?: RequestOptionsArgs): Observable<any> {
return super.get(url, options);
}
/**
* Performs a request with `post` http method.
* @param url
* @param body
* @param options
* @returns {Observable<>}
*/
post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
this.requestInterceptor();
return super.post(this.getFullUrl(url), body, this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSubscribeSuccess(res);
}, (error: any) => {
this.onSubscribeError(error);
})
.finally(() => {
this.onFinally();
});
}
/**
* Performs a request with `put` http method.
* @param url
* @param body
* @param options
* @returns {Observable<>}
*/
put(url: string, body: string, options?: RequestOptionsArgs): Observable<any> {
this.requestInterceptor();
return super.put(this.getFullUrl(url), body, this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSubscribeSuccess(res);
}, (error: any) => {
this.onSubscribeError(error);
})
.finally(() => {
this.onFinally();
});
}
/**
* Performs a request with `delete` http method.
* @param url
* @param options
* @returns {Observable<>}
*/
delete(url: string, options?: RequestOptionsArgs): Observable<any> {
this.requestInterceptor();
return super.delete(this.getFullUrl(url), options)
.catch(this.onCatch)
.do((res: Response) => {
this.onSubscribeSuccess(res);
}, (error: any) => {
this.onSubscribeError(error);
})
.finally(() => {
this.onFinally();
});
}
/**
* Request options.
* @param options
* @returns {RequestOptionsArgs}
*/
private requestOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers();
}
return options;
}
/**
* Request interceptor.
*/
private requestInterceptor(): void {
this.loaderService.showPreloader();
}
/**
* Response interceptor.
*/
private responseInterceptor(): void {
this.loaderService.hidePreloader();
}
/**
* Error handler.
* @param error
* @param caught
* @returns {ErrorObservable}
*/
private onCatch(error: any, caught: Observable<any>): Observable<any> {
return Observable.throw(error);
}
/**
* onSubscribeSuccess
* @param res
*/
private onSubscribeSuccess(res: Response): void {
}
/**
* onSubscribeError
* @param error
*/
private onSubscribeError(error: any): void {
}
/**
* onFinally
*/
private onFinally(): void {
this.responseInterceptor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment