UNPKG

api-know

Version:

API Know provides an easy interface to perform CRUD operations against APIs, localstorage, etc..

416 lines (342 loc) 16 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; var _restApiStoreAdapter = __webpack_require__(1); var _restApiStoreAdapter2 = _interopRequireDefault(_restApiStoreAdapter); var _localStoreAdapter = __webpack_require__(3); var _localStoreAdapter2 = _interopRequireDefault(_localStoreAdapter); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.LocalStoreAdapter = _localStoreAdapter2.default; exports.RestApiStoreAdapter = _restApiStoreAdapter2.default; exports.registerStoreAdapter = function (store, adapter) { store.adapter = adapter; store.create = function (noun, item) { store.adapter.create(item); }; store.update = function (noun, item) { store.adapter.update(item); }; store.read_all = function (noun) { store.adapter.read_all(); }; store.delete = function (noun, id) { store.adapter.delete(id); }; store.read = function (noun, id) { store.adapter.read(id); }; }; /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _storeAdapter = __webpack_require__(2); var _storeAdapter2 = _interopRequireDefault(_storeAdapter); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var RestApiStoreAdapter = function (_StoreAdapter) { _inherits(RestApiStoreAdapter, _StoreAdapter); function RestApiStoreAdapter(store, noun, url) { _classCallCheck(this, RestApiStoreAdapter); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(RestApiStoreAdapter).call(this)); _this.store = store; _this.noun = noun; _this.url = url; return _this; } _createClass(RestApiStoreAdapter, [{ key: 'create', value: function create(item) { console.log('rest adapter create'); var requestStartTime = new Date().getTime(); this.post(requestStartTime, item); } }, { key: 'update', value: function update(item) { console.log('rest adapter update'); this.put(item); } }, { key: 'read', value: function read(id) { console.log('rest adapter read'); } }, { key: 'read_all', value: function read_all() { console.log('Fetching: ', this.url); var requestStartTime = new Date().getTime(); this.get(requestStartTime); } }, { key: 'post', value: function post(requestStartTime, item) { this.request_with_payload(requestStartTime, item, 'post'); } }, { key: 'request_with_payload', value: function request_with_payload(requestStartTime, item, method) { var _this2 = this; var headers = new Headers({ 'Content-type': 'application/json' }); var endpoint = ''; var self = this; if (method == 'post') { endpoint = this.url + '/' + this.noun; } else { endpoint = this.url + '/' + this.noun + '/' + item.id; } fetch(endpoint, { mode: 'cors', method: method, headers: headers, body: JSON.stringify({ article: item }) }).then(function (response) { self.read_all(); }).catch(function (error) { debugger; if (self.store[self.noun].lastRequest != null && requestStartTime < self.store[self.noun].lastRequest) { console.log('STALE ERROR RESPONSE', 'this response is older than another response! ', requestStartTime, self.store[_this2.noun].lastRequest); return; } // do something else }); } }, { key: 'put', value: function put(item) { console.log('putting: ', item); var requestStartTime = new Date().getTime(); this.request_with_payload(requestStartTime, item, 'put'); } }, { key: 'delete_item', value: function delete_item(requestStartTime, id) { var _this3 = this; var self = this; fetch(this.url + '/' + this.noun + '/' + id, { method: 'DELETE', headers: { 'Content-type': 'application/json', 'Accept': 'application/json' } }).then(function (response) { // store[this.noun].data.append(item); self.read_all(); }).catch(function (error) { debugger; if (self.store[_this3.noun].lastRequest != null && requestStartTime < self.store[_this3.noun].lastRequest) { console.log('STALE ERROR RESPONSE', 'this response is older than another response! ', requestStartTime, self.store[_this3.noun].lastRequest); return; } // do something else }); } }, { key: 'get', value: function get(requestStartTime) { var _this4 = this; var self = this; fetch(this.url + '/' + this.noun + '.json').then(function (response) { return response.json(); }).then(function (json) { if (self.store[_this4.noun].lastRequest != null && requestStartTime < self.store[_this4.noun].lastRequest) { console.log('STALE SUCCESS RESPONSE', 'this response is older than another response! ', requestStartTime, self.store[_this4.noun].lastRequest); return; } console.log('success: ', json[_this4.noun]); if (json.error != null || json.error != undefined) { throw new Error('Not Found'); } self.store[_this4.noun] = { data: json[_this4.noun], errors: [], isFetching: false, lastRequest: requestStartTime }; }).catch(function (error) { debugger; if (self.store[_this4.noun].lastRequest != null && requestStartTime < self.store[_this4.noun].lastRequest) { console.log('STALE ERROR RESPONSE', 'this response is older than another response! ', requestStartTime, self.store[_this4.noun].lastRequest); return; } self.store[_this4.noun] = { data: [], errors: error, isFetching: false }; }); } }, { key: 'delete', value: function _delete(id) { console.log('rest adapter delete'); var requestStartTime = new Date().getTime(); this.delete_item(requestStartTime, id); } }]); return RestApiStoreAdapter; }(_storeAdapter2.default); exports.default = RestApiStoreAdapter; /***/ }, /* 2 */ /***/ function(module, exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var StoreAdapter = function StoreAdapter() { _classCallCheck(this, StoreAdapter); if (this.create === undefined) { throw new TypeError('Must override create method'); } if (this.update === undefined) { throw new TypeError('Must override update method'); } if (this.read === undefined) { throw new TypeError('Must override read method'); } if (this.read_all === undefined) { throw new TypeError('Must override read method'); } if (this.delete === undefined) { throw new TypeError('Must override delete method'); } }; exports.default = StoreAdapter; /***/ }, /* 3 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _storeAdapter = __webpack_require__(2); var _storeAdapter2 = _interopRequireDefault(_storeAdapter); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var LocalStoreAdapter = function (_StoreAdapter) { _inherits(LocalStoreAdapter, _StoreAdapter); function LocalStoreAdapter(store, noun) { _classCallCheck(this, LocalStoreAdapter); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(LocalStoreAdapter).call(this)); _this.store = store; _this.noun = noun; _this.global_count = 0; localStorage.setItem(_this.noun, JSON.stringify([])); return _this; } _createClass(LocalStoreAdapter, [{ key: 'create', value: function create(item) { console.log('creating ', this.noun); var currentItems = JSON.parse(localStorage.getItem(this.noun)); var insertItem = Object.assign({}, item, { id: this.global_count++ }); currentItems.push(insertItem); localStorage.setItem(this.noun, JSON.stringify(currentItems)); this.read_all(); return insertItem; } }, { key: 'update', value: function update(item) { var currentItems = JSON.parse(localStorage.getItem(this.noun)); var currentItem = currentItems.filter(function (i) { return item.id == i.id; })[0]; Object.assign(currentItem, {}, item); localStorage.setItem(this.noun, JSON.stringify(currentItems)); this.read_all(); return currentItem; } }, { key: 'read_all', value: function read_all() { this.store[this.noun].data = JSON.parse(localStorage.getItem(this.noun)); return this.store[this.noun].data; } }, { key: 'read', value: function read(id) { console.log('reading ', this.noun, ' with id ', id); var currentItems = JSON.parse(localStorage.getItem(this.noun)); var currentItem = currentItems.filter(function (item) { return item.id == id; })[0]; if (currentItem == null) throw new Error('Item with id ' + id + ' does not exist!'); return currentItem; } }, { key: 'delete', value: function _delete(id) { console.log('delete'); var currentItems = JSON.parse(localStorage.getItem(this.noun)); var deleteItem = currentItems.filter(function (item) { return id == item.id; })[0]; currentItems.splice(currentItems.indexOf(deleteItem), 1); localStorage.setItem(this.noun, JSON.stringify(currentItems)); this.read_all(); return currentItems; } }]); return LocalStoreAdapter; }(_storeAdapter2.default); exports.default = LocalStoreAdapter; /***/ } /******/ ]); //# sourceMappingURL=index.js.map