src/lib/core/http/http.service.ts
constructor(backend: XHRBackend, defaultOptions: AuthorizeRequestOptions, configService: ConfigService, loaderService: RequestLoaderService)
|
Defined in src/lib/core/http/http.service.ts:18
|
get |
get(url: string, options: RequestOptionsArgs)
|
Defined in src/lib/core/http/http.service.ts:29
|
Returns :
Observable<any>
|
put |
put(url: string, body: any, options: RequestOptionsArgs)
|
Defined in src/lib/core/http/http.service.ts:44
|
Returns :
Observable<any>
|
post |
post(url: string, body: any, options: RequestOptionsArgs)
|
Defined in src/lib/core/http/http.service.ts:59
|
Returns :
Observable<any>
|
delete |
delete(url: string, options: RequestOptionsArgs)
|
Defined in src/lib/core/http/http.service.ts:74
|
Returns :
Observable<any>
|
Private requestOptions |
requestOptions(options: RequestOptionsArgs)
|
Defined in src/lib/core/http/http.service.ts:89
|
Returns :
RequestOptionsArgs
|
Private getFullUrl |
getFullUrl(url: string)
|
Defined in src/lib/core/http/http.service.ts:101
|
Returns :
string
|
Private onCatch |
onCatch(error: any, caught: Observable
|
Defined in src/lib/core/http/http.service.ts:109
|
Returns :
Observable<any>
|
Private onSuccess |
onSuccess(result: Response)
|
Defined in src/lib/core/http/http.service.ts:113
|
Returns :
void
|
Private onError |
onError(error: )
|
Defined in src/lib/core/http/http.service.ts:122
|
Returns :
void
|
Private onEnd |
onEnd()
|
Defined in src/lib/core/http/http.service.ts:137
|
Returns :
void
|
Private checkError |
checkError(body: )
|
Defined in src/lib/core/http/http.service.ts:141
|
Returns :
void
|
Private showLoader |
showLoader()
|
Defined in src/lib/core/http/http.service.ts:147
|
Returns :
void
|
Private hideLoader |
hideLoader()
|
Defined in src/lib/core/http/http.service.ts:155
|
Returns :
void
|
Private count |
count: |
Default value : 0
|
Defined in src/lib/core/http/http.service.ts:18
|
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import {
RequestOptionsArgs,
Response,
Headers,
XHRBackend
} from '@angular/http';
import {AuthService} from './auth.service';
import {AuthorizeRequestOptions} from './authorize-request-options';
import {ConfigService} from '../config/config.service';
import {RequestLoaderService} from '../request-loader/request-loader.service';
@Injectable()
export class HttpService extends AuthService {
private static count = 0;
constructor(
backend: XHRBackend,
defaultOptions: AuthorizeRequestOptions,
configService: ConfigService,
private loaderService: RequestLoaderService,
) {
super(backend, defaultOptions, configService);
}
get(url: string, options?: RequestOptionsArgs): Observable<any> {
this.showLoader();
return super.get(this.getFullUrl(url), this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSuccess(res);
}, (error: any) => {
this.onError(error);
})
.finally(() => {
this.onEnd();
});
}
put(url: string, body?: any, options?: RequestOptionsArgs): Observable<any> {
this.showLoader();
return super.put(this.getFullUrl(url), body, this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSuccess(res);
}, (error: any) => {
this.onError(error);
})
.finally(() => {
this.onEnd();
});
}
post(url: string, body?: any, options?: RequestOptionsArgs): Observable<any> {
this.showLoader();
return super.post(this.getFullUrl(url), body, this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSuccess(res);
}, (error: any) => {
this.onError(error);
})
.finally(() => {
this.onEnd();
});
}
delete(url: string, options?: RequestOptionsArgs): Observable<any> {
this.showLoader();
return super.delete(this.getFullUrl(url), this.requestOptions(options))
.catch(this.onCatch)
.do((res: Response) => {
this.onSuccess(res);
}, (error: any) => {
this.onError(error);
})
.finally(() => {
this.onEnd();
});
}
private requestOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options == null) {
options = new AuthorizeRequestOptions();
}
if (options.headers == null) {
options.headers = new Headers();
}
return options;
}
private getFullUrl(url: string): string {
if (!this.apiUrl) {
throw new Error('You need to set "apiUrl" inside your "cause" configuration.');
}
return this.apiUrl + url;
}
private onCatch(error: any, caught: Observable<any>): Observable<any> {
return Observable.throw(error);
}
private onSuccess(result: Response): void {
if (result instanceof Response) {
const body = result.json() || {};
this.checkLogin(body);
this.checkError(body);
}
}
private onError(error: Response | any): void {
let errorMessage: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body['error'] || JSON.stringify(body);
errorMessage = error.status + ' - ' + (error.statusText || '') + ' ' + err;
} else {
errorMessage = error.message ? error.message : error.toString();
}
throw new Error(errorMessage);
}
private onEnd(): void {
this.hideLoader();
}
private checkError(body) {
if (body.success === false) {
throw new Error(body.error);
}
}
private showLoader(): void {
HttpService.count++;
if (HttpService.count) {
this.loaderService.show();
}
}
private hideLoader(): void {
HttpService.count--;
if (!HttpService.count) {
this.loaderService.hide();
}
}
}