simple-providers
Version:
475 lines (463 loc) • 15.3 kB
JavaScript
import { Storage } from '@ionic/storage';
import { AuthHttp } from 'angular2-jwt';
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
var Entity = (function () {
function Entity() {
}
return Entity;
}());
var HttpMode = {};
HttpMode.HTTP = 0;
HttpMode.AUTH_HTTP = 1;
HttpMode[HttpMode.HTTP] = "HTTP";
HttpMode[HttpMode.AUTH_HTTP] = "AUTH_HTTP";
var ProviderProperties = (function () {
/**
* @param {?} serverUri
* @param {?} defaultHttp
*/
function ProviderProperties(serverUri, defaultHttp) {
this.serverUri = serverUri;
this.defaultHttp = defaultHttp;
}
return ProviderProperties;
}());
var ProviderDefaults = (function () {
/**
* @param {?} _storage
* @param {?} _http
* @param {?} _authHttp
* @param {?} _providerProperties
*/
function ProviderDefaults(_storage, _http, _authHttp, _providerProperties) {
this._storage = _storage;
this._http = _http;
this._authHttp = _authHttp;
this._providerProperties = _providerProperties;
}
Object.defineProperty(ProviderDefaults.prototype, "http", {
/**
* @return {?}
*/
get: function () {
if (this._providerProperties.defaultHttp === HttpMode.AUTH_HTTP) {
return this._authHttp;
}
else {
return this._http;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(ProviderDefaults.prototype, "basePath", {
/**
* @return {?}
*/
get: function () {
var /** @type {?} */ _basePath = this._providerProperties.serverUri;
return _basePath === undefined ? '' : _basePath;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ProviderDefaults.prototype, "providerProperties", {
/**
* @return {?}
*/
get: function () {
return this._providerProperties;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ProviderDefaults.prototype, "storage", {
/**
* @return {?}
*/
get: function () {
return this._storage;
},
enumerable: true,
configurable: true
});
return ProviderDefaults;
}());
ProviderDefaults.decorators = [
{ type: Injectable },
];
/**
* @nocollapse
*/
ProviderDefaults.ctorParameters = function () { return [
{ type: Storage, },
{ type: Http, },
{ type: AuthHttp, },
{ type: ProviderProperties, },
]; };
var ProviderMethod = {};
ProviderMethod.GET = 0;
ProviderMethod.POST = 1;
ProviderMethod.PUT = 2;
ProviderMethod.DELETE = 3;
ProviderMethod.PATCH = 4;
ProviderMethod[ProviderMethod.GET] = "GET";
ProviderMethod[ProviderMethod.POST] = "POST";
ProviderMethod[ProviderMethod.PUT] = "PUT";
ProviderMethod[ProviderMethod.DELETE] = "DELETE";
ProviderMethod[ProviderMethod.PATCH] = "PATCH";
var __extends = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var RequestParams = (function (_super) {
__extends(RequestParams, _super);
function RequestParams() {
return _super !== null && _super.apply(this, arguments) || this;
}
return RequestParams;
}(Map));
var Provider = (function () {
/**
* @param {?} providerDefaults
* @param {?} resourcePath
*/
function Provider(providerDefaults, resourcePath) {
this.providerDefaults = providerDefaults;
this.resourcePath = resourcePath;
this._defaults = {
methods: [
{
method: ProviderMethod.GET,
config: { forJson: true }
},
{
method: ProviderMethod.POST,
config: { forJson: true }
},
{
method: ProviderMethod.PUT,
config: { forJson: true }
},
{
method: ProviderMethod.PATCH,
config: { forJson: true }
},
{
method: ProviderMethod.DELETE,
config: { forJson: false }
}
]
};
}
/**
* @return {?}
*/
Provider.prototype.getProviderDefaults = function () {
return this.providerDefaults;
};
/**
* @return {?}
*/
Provider.prototype.getAuthHttp = function () {
return this.providerDefaults.http;
};
/**
* @return {?}
*/
Provider.prototype.getPath = function () {
return this.providerDefaults.basePath + (this.resourcePath ? this.resourcePath : '');
};
/**
* @template E
* @param {?} path
* @param {?} requestParams
* @param {?=} options
* @return {?}
*/
Provider.prototype.getWithParams = function (path, requestParams, options) {
return this.get(this.resolveRequestParams(requestParams, path), options);
};
/**
* @template E
* @param {?=} path
* @param {?=} options
* @return {?}
*/
Provider.prototype.get = function (path, options) {
var _this = this;
var /** @type {?} */ promise = new Promise(function (resolve, reject) {
var /** @type {?} */ _path = "" + _this.getPath() + (path ? path : '');
_this.getFromCache(options, _path)
.then(function (cache) { return resolve(cache); })
.catch(function () { return _this.getAuthHttp().get(_path, options)
.map(function (res) {
var /** @type {?} */ result = _this.mapResFor(ProviderMethod.GET, res);
if (options) {
_this.storeGetOnCache(options, _path, result);
}
return result;
}).subscribe(function (result) { return resolve(result); }, function (err) { return reject(err); }); });
});
return Observable.fromPromise(promise);
};
/**
* @template E
* @param {?} path
* @param {?} body
* @param {?=} options
* @return {?}
*/
Provider.prototype.post = function (path, body, options) {
var _this = this;
return this.getAuthHttp().post("" + this.getPath() + (path ? path : ''), body, options)
.map(function (res) { return _this.mapResFor(ProviderMethod.POST, res); });
};
/**
* @template E
* @param {?} path
* @param {?} body
* @param {?=} options
* @return {?}
*/
Provider.prototype.put = function (path, body, options) {
var _this = this;
return this.getAuthHttp().put("" + this.getPath() + (path ? path : ''), body, options)
.map(function (res) { return _this.mapResFor(ProviderMethod.PUT, res); });
};
/**
* @template E
* @param {?} path
* @param {?=} options
* @return {?}
*/
Provider.prototype.del = function (path, options) {
var _this = this;
return this.getAuthHttp().delete("" + this.getPath() + (path ? path : ''), options)
.map(function (res) { return _this.mapResFor(ProviderMethod.DELETE, res); });
};
/**
* @template E
* @param {?} path
* @param {?} body
* @param {?=} options
* @return {?}
*/
Provider.prototype.patch = function (path, body, options) {
var _this = this;
return this.getAuthHttp().patch("" + this.getPath(), body, options)
.map(function (res) { return _this.mapResFor(ProviderMethod.PATCH, res); });
};
/**
* @param {?} method
* @param {?} config
* @return {?}
*/
Provider.prototype.setDefaultFor = function (method, config) {
if (config !== undefined) {
this._defaults.methods[method].config = config;
}
};
/**
* @template E
* @param {?} requestProperties
* @param {?} path
* @return {?}
*/
Provider.prototype.getFromCache = function (requestProperties, path) {
var _this = this;
return new Promise(function (resolve, reject) {
if (requestProperties && requestProperties.cacheable) {
var /** @type {?} */ now_1 = new Date();
_this.providerDefaults.storage.get(path)
.then(function (cr) {
if (!cr) {
reject(undefined);
}
var /** @type {?} */ duration = now_1.getTime() - cr.lastRequest.getTime();
console.log(duration);
if (duration >= requestProperties.cacheDurationMilliseconds) {
reject(undefined);
}
else {
resolve(cr.cache);
}
})
.catch(function () { return reject(undefined); });
}
else {
reject(undefined);
}
});
};
/**
* @template E
* @param {?} requestProperties
* @param {?} path
* @param {?} result
* @return {?}
*/
Provider.prototype.storeGetOnCache = function (requestProperties, path, result) {
if (requestProperties && requestProperties.cacheable) {
this.providerDefaults.storage.set(path, { lastRequest: new Date(), cache: result });
}
};
/**
* @param {?} requestParams
* @param {?=} uri
* @return {?}
*/
Provider.prototype.resolveRequestParams = function (requestParams, uri) {
var /** @type {?} */ uriParam = (uri || '');
if (!requestParams) {
return uriParam;
}
requestParams.forEach(function (value, key) {
uriParam += uriParam.indexOf('?') < 0 ? '?' : '&';
uriParam += value !== undefined ? key + "=" + value : '';
});
return uriParam;
};
/**
* @template E
* @param {?} method
* @param {?} res
* @return {?}
*/
Provider.prototype.mapResFor = function (method, res) {
var /** @type {?} */ config = this._defaults.methods[method].config;
if (config.forJson && res.text() !== undefined) {
return res.json();
}
else {
return res.text();
}
};
Object.defineProperty(Provider.prototype, "defaults", {
/**
* @return {?}
*/
get: function () {
return this._defaults;
},
enumerable: true,
configurable: true
});
return Provider;
}());
var __extends$1 = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var ReadProvider = (function (_super) {
__extends$1(ReadProvider, _super);
function ReadProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @return {?}
*/
ReadProvider.prototype.all = function () {
return this.get();
};
/**
* @param {?} key
* @return {?}
*/
ReadProvider.prototype.one = function (key) {
if (key) {
return this.get("" + key);
}
else {
return Observable.throw(new Error('id invalid'));
}
};
return ReadProvider;
}(Provider));
var __extends$2 = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var EditableProvider = (function (_super) {
__extends$2(EditableProvider, _super);
function EditableProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} t
* @return {?}
*/
EditableProvider.prototype.update = function (t) {
if (t && t.id) {
return this.put("" + t.id, t);
}
else {
Observable.throw(new Error('entity or id undefined'));
}
};
return EditableProvider;
}(ReadProvider));
var __extends$3 = (undefined && undefined.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var CrudProvider = (function (_super) {
__extends$3(CrudProvider, _super);
function CrudProvider() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} t
* @return {?}
*/
CrudProvider.prototype.save = function (t) {
if (t != undefined) {
return this.post("", t);
}
else {
Observable.throw(new Error('entity or id undefined'));
}
};
/**
* @param {?} t
* @return {?}
*/
CrudProvider.prototype.delete = function (t) {
if (t && t.id) {
return this.del("" + t.id);
}
else {
Observable.throw(new Error('entity or id undefined'));
}
};
return CrudProvider;
}(EditableProvider));
export { Entity, HttpMode, ProviderDefaults, ProviderMethod, ProviderProperties, RequestParams, Provider, ReadProvider, EditableProvider, CrudProvider };