@totvs-agro/t-translate
Version:
Componente Totvs para internacionalização
57 lines • 2.62 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);
};
import { Injectable } from "@angular/core";
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
var DataFinder = /** @class */ (function () {
function DataFinder(http) {
this.http = http;
}
/*
Gets data with a promise which will return with the data when the task is complete
Uses generic type T to define schema interface to ensure returning json matches schema template.
*/
DataFinder.prototype.getJSONDataAsync = function (filePath) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.http.get(filePath)
.subscribe(function (res) {
if (!res.ok) {
reject("Failed with status: " + res.status + "\nTrying to find fil at " + filePath);
}
var jsonRes = res.json();
resolve(jsonRes);
});
}).catch(function (reason) { return _this.handleError(reason); });
};
/* Takes an error, logs it to the console, and throws it */
DataFinder.prototype.handleError = function (error) {
var errMsg;
if (error instanceof Response) {
var body = error.json() || '';
var err = JSON.stringify(body);
errMsg = error.status + " - " + (error.statusText || '') + " " + err;
}
else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
};
DataFinder = __decorate([
Injectable(),
__metadata("design:paramtypes", [Http])
], DataFinder);
return DataFinder;
}());
export { DataFinder };
//# sourceMappingURL=datafinder.js.map