API.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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) {
  13. return resp;
  14. } else {
  15. throw resp;
  16. }
  17. }
  18. function catchIt(err) {
  19. if (parseInt(err.status / 100, 10) === 5 || parseInt(err.status / 100, 10) === 4) {
  20. throw err;
  21. } else {
  22. return err;
  23. }
  24. }
  25. var API =
  26. /*#__PURE__*/
  27. function () {
  28. function API(baseURL) {
  29. _classCallCheck(this, API);
  30. this.baseURL = '';
  31. }
  32. _createClass(API, [{
  33. key: "setUrl",
  34. value: function setUrl(url) {
  35. this.baseURL = url;
  36. }
  37. }, {
  38. key: "get",
  39. value: function get(url, token) {
  40. url = this.baseURL + url;
  41. return window.fetch(url, {
  42. method: 'GET',
  43. headers: {
  44. 'token': token
  45. }
  46. }).then(checkIt).then(function (r) {
  47. return r.json();
  48. }).catch(catchIt);
  49. }
  50. }, {
  51. key: "post",
  52. value: function post(url, body, token) {
  53. var urb = this.baseURL + url;
  54. return window.fetch(urb, {
  55. method: 'POST',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. 'token': token
  59. },
  60. body: JSON.stringify(body)
  61. }).then(checkIt).then(function (r) {
  62. return r.json();
  63. }).catch(catchIt);
  64. }
  65. }]);
  66. return API;
  67. }();
  68. var _default = new API();
  69. exports.default = _default;