bia-framework
Version:
Bia Framework to work with angular 2
98 lines • 4.5 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var http_1 = require("@angular/http");
var Observable_1 = require("rxjs/Observable");
var ConfigService_1 = require("./ConfigService");
require('rxjs/add/operator/map');
var WebApiService = (function () {
function WebApiService(http, config) {
var _this = this;
this.http = http;
this.config = config;
this._apiAddress = "http://localhost:8787/";
this._urlParameter = "";
this._data = null;
this._resource = "";
this._method = "GET";
this._canSendToken = true;
this._contentType = "application/json; charset=utf-8";
this.setApiAddress = function (newAddress) {
_this._apiAddress = newAddress;
};
this.Content = function (data) {
_this._data = data;
return _this;
};
this.Resource = function (resource) {
_this._resource = resource;
return _this;
};
this.ContentType = function (contentType) {
_this._contentType = contentType;
return _this;
};
this.Method = function (method) {
_this._method = method;
return _this;
};
this.CanSendToken = function (canSendToken) {
_this._canSendToken = canSendToken;
return _this;
};
this.Parameters = function (parameters) {
Object.keys(parameters).map(function (key) {
if (!parameters.hasOwnProperty(key))
return;
_this._resource = _this._resource.replace("{" + key + "}", parameters[key].toString());
});
return _this;
};
this.Execute = function () {
var observable = Observable_1.Observable.create(function (observer) {
var requestOption = new http_1.RequestOptions();
requestOption.method = _this._method;
requestOption.url = _this._apiAddress + _this._resource;
var header = new http_1.Headers();
header.append("Content-Type", _this._contentType);
header.append("Charset", "utf-8");
header.append("Accept", "application/json");
if (_this._canSendToken) {
var tokenString = localStorage.getItem("token").token;
header.append("Authorization", "Bearer " + tokenString);
}
requestOption.headers = header;
if (_this._data != null && typeof _this._data != 'string')
requestOption.body = JSON.stringify(_this._data);
_this._data = null;
_this.http.request(_this._apiAddress + _this._resource, requestOption)
.map(function (res) {
if (res.status < 200 || res.status >= 300) {
throw new Error('This request has failed ' + res.status);
}
else {
observer.next(res.json());
observer.complete();
}
}).subscribe();
});
return observable;
};
this._apiAddress = "http://" + config.getConfig().WebApiAddress + ":" + config.getConfig().WebApiPort + "/";
}
WebApiService = __decorate([
core_1.Injectable(),
__metadata('design:paramtypes', [http_1.Http, ConfigService_1.ConfigService])
], WebApiService);
return WebApiService;
}());
exports.WebApiService = WebApiService;
//# sourceMappingURL=WebApiService.js.map