1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
8 |
|
9 | 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; }; }();
|
10 |
|
11 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
12 |
|
13 | var API = function () {
|
14 | function API(apiURL) {
|
15 | _classCallCheck(this, API);
|
16 |
|
17 | this.apiURL = apiURL;
|
18 | }
|
19 |
|
20 | _createClass(API, [{
|
21 | key: 'headers',
|
22 | value: function headers() {
|
23 | var _headers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
24 |
|
25 | return _extends({
|
26 | 'Content-Type': 'application/json'
|
27 | }, _headers);
|
28 | }
|
29 | }, {
|
30 | key: 'parseJsonResponse',
|
31 | value: function parseJsonResponse(response) {
|
32 | return response.json().then(function (json) {
|
33 | if (!response.ok) {
|
34 | return Promise.reject(json);
|
35 | }
|
36 |
|
37 | return json;
|
38 | });
|
39 | }
|
40 | }, {
|
41 | key: 'request',
|
42 | value: function request(path) {
|
43 | var _this = this;
|
44 |
|
45 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
46 |
|
47 | var headers = this.headers(options.headers || {});
|
48 | return fetch(this.apiURL + path, _extends({}, options, { headers: headers })).then(function (response) {
|
49 | var contentType = response.headers.get('Content-Type');
|
50 | if (contentType && contentType.match(/json/)) {
|
51 | return _this.parseJsonResponse(response);
|
52 | }
|
53 |
|
54 | return response.text().then(function (data) {
|
55 | if (!response.ok) {
|
56 | return Promise.reject({ data: data });
|
57 | }
|
58 |
|
59 | return { data: data };
|
60 | });
|
61 | });
|
62 | }
|
63 | }]);
|
64 |
|
65 | return API;
|
66 | }();
|
67 |
|
68 | exports.default = API; |
\ | No newline at end of file |