Created
January 24, 2017 10:32
-
-
Save tomasstankovic/0a747cefe00fc172ff29c3013615f107 to your computer and use it in GitHub Desktop.
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
import { Injectable } from '@angular/core'; | |
import { Http, ConnectionBackend, Request, RequestOptions, RequestOptionsArgs, Response, Headers } from '@angular/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/throw'; | |
import 'rxjs/Rx'; | |
import { PreloaderService } from './preloader-service'; | |
import { Config } from '../config'; | |
@Injectable() | |
export class HttpService extends Http { | |
constructor(backend: ConnectionBackend, | |
defaultOptions: RequestOptions, | |
private preloaderService: PreloaderService) { | |
super(backend, defaultOptions); | |
} | |
/** | |
* Performs a request with `get` http method. | |
* @param url | |
* @param options | |
* @param preloaderType | |
* @returns {Observable<>} | |
*/ | |
get(url: string, options?: RequestOptionsArgs, preloaderType?: string): Observable<any> { | |
this.requestInterceptor(preloaderType); | |
let fullUrl = this.getFullUrl(url); | |
return super.get(fullUrl, this.requestOptions(options)) | |
.catch(this.onCatch) | |
.do((res: Response) => { | |
this.onSubscribeSuccess(res); | |
}, (error: any) => { | |
this.onSubscribeError(error); | |
}) | |
.finally(() => { | |
this.onFinally(preloaderType); | |
}); | |
} | |
/** | |
* Build full URL for request. | |
* @param str | |
* @returns {string} | |
*/ | |
private getFullUrl(str): string { | |
return Config.api + str; | |
} | |
/** | |
* 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(preloaderType = 'full'): void { | |
this.preloaderService.showPreloader(preloaderType); | |
} | |
/** | |
* Response interceptor. | |
*/ | |
private responseInterceptor(preloaderType = 'full'): void { | |
this.preloaderService.hidePreloader(preloaderType); | |
} | |
/** | |
* Error handler. | |
* @param error | |
* @param caught | |
* @returns {ErrorObservable} | |
*/ | |
private onCatch(error: any): Observable<any> { | |
console.log('onCatch'); | |
return Observable.throw(error); | |
} | |
/** | |
* onSubscribeSuccess | |
* @param res | |
*/ | |
private onSubscribeSuccess(res: Response): void { | |
} | |
/** | |
* onSubscribeError | |
* @param error | |
*/ | |
private onSubscribeError(error: any): void { | |
} | |
/** | |
* onFinally | |
*/ | |
private onFinally(preloaderType = 'full'): void { | |
this.responseInterceptor(preloaderType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment