API.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. 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); } }
  10. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  11. function checkIt(resp) {
  12. if (parseInt(resp.status / 100, 10) === 2 || parseInt(resp.status / 100, 10) === 3) {
  13. return resp;
  14. } else {
  15. throw resp;
  16. }
  17. }
  18. function catchIt(err) {
  19. if (err.message === "Unexpected token O in JSON at position 0") {
  20. return "OK";
  21. }
  22. if (parseInt(err.status / 100, 10) === 4) {
  23. throw err;
  24. } else if (parseInt(err.status / 100, 10) === 5) {
  25. throw err;
  26. } else {
  27. return err;
  28. }
  29. }
  30. var API =
  31. /*#__PURE__*/
  32. function () {
  33. function API() {
  34. var baseURL = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  35. var token = arguments.length > 1 ? arguments[1] : undefined;
  36. _classCallCheck(this, API);
  37. // if (process.env.NODE_ENV === 'production' && !baseURL.includes('http')) {
  38. baseURL = "/api/" + baseURL; // }
  39. this.baseURL = baseURL;
  40. this.token = token;
  41. }
  42. _createClass(API, [{
  43. key: "setToken",
  44. value: function setToken(t) {
  45. this.token = t;
  46. }
  47. }, {
  48. key: "get",
  49. value: function get(url) {
  50. var urb = this.baseURL + url;
  51. return window.fetch(urb, {
  52. method: 'GET',
  53. headers: {
  54. 'Content-Type': 'application/json',
  55. 'Token': this.token
  56. }
  57. }).then(checkIt).then(function (r) {
  58. return r.json();
  59. }).catch(catchIt);
  60. }
  61. }, {
  62. key: "post",
  63. value: function post(url, body) {
  64. var urb = this.baseURL + url;
  65. return window.fetch(urb, {
  66. method: 'POST',
  67. headers: {
  68. 'Content-Type': 'application/json',
  69. 'Token': this.token
  70. },
  71. body: JSON.stringify(body)
  72. }).then(checkIt).then(function (r) {
  73. return r.json();
  74. }).catch(catchIt);
  75. }
  76. }, {
  77. key: "put",
  78. value: function put(url, body) {
  79. var urb = this.baseURL + url;
  80. return window.fetch(urb, {
  81. method: 'PUT',
  82. headers: {
  83. 'Content-Type': 'application/json',
  84. 'Token': this.token
  85. },
  86. body: JSON.stringify(body)
  87. }).then(checkIt).then(function (r) {
  88. return r.json();
  89. }).catch(catchIt);
  90. }
  91. }, {
  92. key: "delete",
  93. value: function _delete(url) {
  94. var urb = this.baseURL + url;
  95. return window.fetch(urb, {
  96. method: 'DELETE',
  97. headers: {
  98. 'Content-Type': 'application/json',
  99. 'Token': this.token
  100. }
  101. }).then(checkIt).then(function (r) {
  102. return r.json();
  103. }).catch(catchIt);
  104. }
  105. }]);
  106. return API;
  107. }();
  108. var _default = API;
  109. exports.default = _default;