API.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 _Module2 = _interopRequireDefault(require("../Module"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  10. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  11. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  12. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  13. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  14. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  15. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  16. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  18. 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); } }
  19. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  20. function checkIt(resp) {
  21. if (parseInt(resp.status / 100, 10) === 2) {
  22. return resp;
  23. } else {
  24. throw resp;
  25. }
  26. }
  27. function catchIt(err) {
  28. if (parseInt(err.status / 500, 10) === 5) {
  29. return err;
  30. } else {
  31. return err;
  32. }
  33. }
  34. var APIService =
  35. /*#__PURE__*/
  36. function () {
  37. function APIService(baseURL) {
  38. _classCallCheck(this, APIService);
  39. this.baseURL = baseURL;
  40. }
  41. _createClass(APIService, [{
  42. key: "get",
  43. value: function get(url) {
  44. url = this.baseURL + url;
  45. return window.fetch(url, {
  46. method: 'GET'
  47. }).then(checkIt).then(function (r) {
  48. return r.json();
  49. }).catch(catchIt);
  50. }
  51. }, {
  52. key: "post",
  53. value: function post(url, body) {
  54. var urb = this.baseURL + url;
  55. return window.fetch(urb, {
  56. method: 'POST',
  57. headers: {
  58. 'Content-Type': 'application/json',
  59. 'Token': ''
  60. },
  61. body: JSON.stringify(body)
  62. }).then(checkIt).then(function (r) {
  63. return r.json();
  64. }).catch(catchIt);
  65. }
  66. }]);
  67. return APIService;
  68. }();
  69. var API =
  70. /*#__PURE__*/
  71. function (_Module) {
  72. _inherits(API, _Module);
  73. function API(props) {
  74. var _this;
  75. _classCallCheck(this, API);
  76. _this = _possibleConstructorReturn(this, _getPrototypeOf(API).call(this, props));
  77. _this.baseURL.onUpdate(function (v) {
  78. return _this.setAPI(v);
  79. });
  80. _this.setAPI(_this.baseURL.value);
  81. return _this;
  82. }
  83. _createClass(API, [{
  84. key: "setAPI",
  85. value: function setAPI(v) {
  86. var temp = new APIService(v);
  87. this.api.set(temp);
  88. }
  89. }, {
  90. key: "render",
  91. value: function render() {
  92. return _get(_getPrototypeOf(API.prototype), "render", this).call(this);
  93. }
  94. }]);
  95. return API;
  96. }(_Module2.default);
  97. API.Inputs = {
  98. baseURL: {
  99. type: _Module2.default.Types.Text,
  100. required: true,
  101. comment: "API URL",
  102. defaultValue: '/'
  103. }
  104. };
  105. API.Outputs = {
  106. api: {}
  107. };
  108. API.Category = "Services";
  109. var _default = API;
  110. exports.default = _default;