UNPKG

simple-providers

Version:
486 lines (472 loc) 16.3 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ionic/storage/index'), require('angular2-jwt'), require('@angular/http'), require('@angular/core'), require('rxjs/Observable'), require('rxjs/add/operator/toPromise'), require('rxjs/add/operator/map')) : typeof define === 'function' && define.amd ? define(['exports', '@ionic/storage/index', 'angular2-jwt', '@angular/http', '@angular/core', 'rxjs/Observable', 'rxjs/add/operator/toPromise', 'rxjs/add/operator/map'], factory) : (factory((global['simple-providers'] = {}),global._ionic_storage_index,global.angular2Jwt,global._angular_http,global._angular_core,global.rxjs_Observable)); }(this, (function (exports,_ionic_storage_index,angular2Jwt,_angular_http,_angular_core,rxjs_Observable) { 'use strict'; 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: _angular_core.Injectable }, ]; /** * @nocollapse */ ProviderDefaults.ctorParameters = function () { return [ { type: _ionic_storage_index.Storage, }, { type: _angular_http.Http, }, { type: angular2Jwt.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 rxjs_Observable.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 rxjs_Observable.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 { rxjs_Observable.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 { rxjs_Observable.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 { rxjs_Observable.Observable.throw(new Error('entity or id undefined')); } }; return CrudProvider; }(EditableProvider)); exports.Entity = Entity; exports.HttpMode = HttpMode; exports.ProviderDefaults = ProviderDefaults; exports.ProviderMethod = ProviderMethod; exports.ProviderProperties = ProviderProperties; exports.RequestParams = RequestParams; exports.Provider = Provider; exports.ReadProvider = ReadProvider; exports.EditableProvider = EditableProvider; exports.CrudProvider = CrudProvider; Object.defineProperty(exports, '__esModule', { value: true }); })));