import {Injectable} from "@angular/core"; import {Headers, Http, RequestOptions} from "@angular/http"; import {Message} from "../../../../common/entities/Message"; import "rxjs/Rx"; import {SlimLoadingBarService} from "ng2-slim-loading-bar"; @Injectable() export class NetworkService { _baseUrl = "/api"; constructor(protected _http: Http, private slimLoadingBarService: SlimLoadingBarService) { } private callJson(method: string, url: string, data: any = {}): Promise { let body = JSON.stringify(data); let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); this.slimLoadingBarService.visible = true; this.slimLoadingBarService.start(() => { this.slimLoadingBarService.visible = false; }); const process = (res: any) => { this.slimLoadingBarService.complete(); return > res.json(); }; const err = (err) => { NetworkService.handleError(err); this.slimLoadingBarService.complete(); }; if (method == "get" || method == "delete") { return this._http[method](this._baseUrl + url, options) .toPromise() .then(process) .catch(err); } return this._http[method](this._baseUrl + url, body, options) .toPromise() .then(process) .catch(err); } public postJson(url: string, data: any = {}): Promise { return this.callJson("post", url, data); } public putJson(url: string, data: any = {}): Promise { return this.callJson("put", url, data); } public getJson(url: string): Promise { return this.callJson("get", url); } public deleteJson(url: string): Promise { return this.callJson("delete", url); } private static handleError(error: any) { // TODO: in a real world app do smthing better // instead of just logging it to the console console.error(error); return Promise.reject(error.message || error.json().error || 'Server error'); } }