Repository.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _API = _interopRequireDefault(require("./API"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  10. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. 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); } }
  13. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  14. var Repository =
  15. /*#__PURE__*/
  16. function () {
  17. function Repository(tablename, token) {
  18. var filters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  19. _classCallCheck(this, Repository);
  20. this.tablename = tablename;
  21. this.token = token;
  22. this.filters = filters.map(function (item) {
  23. return _objectSpread({}, item, {
  24. options: item.values
  25. });
  26. });
  27. this.API = new _API.default('/' + this.tablename, token);
  28. this.renderFilters();
  29. }
  30. _createClass(Repository, [{
  31. key: "setToken",
  32. value: function setToken(token) {
  33. this.API.token = token;
  34. this.token = token;
  35. }
  36. }, {
  37. key: "setFilters",
  38. value: function setFilters(fil) {
  39. if (!fil) return;
  40. this.filters = fil;
  41. this.renderFilters();
  42. }
  43. }, {
  44. key: "renderFilters",
  45. value: function renderFilters() {
  46. var fils = [];
  47. this.filters.forEach(function (item) {
  48. if (item.values && item.values.length || ["ascending", "descending"].includes(item.type)) {
  49. fils.push(item);
  50. }
  51. });
  52. this.renderedFilters = JSON.stringify(fils);
  53. }
  54. }, {
  55. key: "count",
  56. value: function count() {
  57. return this.API.get("/count?filters=".concat(this.renderedFilters));
  58. }
  59. }, {
  60. key: "getPage",
  61. value: function getPage(page) {
  62. var perPage = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  63. return this.API.get("?page=".concat(page, "&perPage=").concat(perPage, "&filters=").concat(this.renderedFilters));
  64. }
  65. }, {
  66. key: "get",
  67. value: function get(id) {
  68. return this.API.get('/' + id);
  69. }
  70. }, {
  71. key: "update",
  72. value: function update(id, obj) {
  73. return this.API.put('/' + id, obj);
  74. }
  75. }, {
  76. key: "add",
  77. value: function add(obj) {
  78. return this.API.post('', obj);
  79. }
  80. }, {
  81. key: "delete",
  82. value: function _delete(id) {
  83. return this.API.delete('/' + id);
  84. }
  85. }, {
  86. key: "import",
  87. value: function _import(data) {
  88. return this.API.post('/import', data);
  89. }
  90. }, {
  91. key: "model",
  92. value: function model() {
  93. return this.API.get('/Model');
  94. }
  95. }]);
  96. return Repository;
  97. }();
  98. var _default = Repository;
  99. exports.default = _default;