-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (99 loc) · 4.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _graphqlRequest = require("graphql-request");
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var Request = function Request(props) {
var _useState = (0, _react.useState)(void 0),
_useState2 = _slicedToArray(_useState, 2),
data = _useState2[0],
setData = _useState2[1];
var _useState3 = (0, _react.useState)(void 0),
_useState4 = _slicedToArray(_useState3, 2),
error = _useState4[0],
setError = _useState4[1];
var _useState5 = (0, _react.useState)(false),
_useState6 = _slicedToArray(_useState5, 2),
loading = _useState6[0],
setLoading = _useState6[1];
var reset = function reset() {
setData(void 0);
setError(void 0);
setLoading(false);
};
var requestData =
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(props) {
var res;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
setLoading(true);
if (!props.options) {
_context.next = 7;
break;
}
_context.next = 4;
return new _graphqlRequest.GraphQLClient(props.url, props.options).request(props.query, props.variables || null)["catch"](function (err) {
return setError(err);
});
case 4:
res = _context.sent;
_context.next = 10;
break;
case 7:
_context.next = 9;
return (0, _graphqlRequest.request)(props.url, props.query, props.variables || null)["catch"](function (err) {
return setError(err);
});
case 9:
res = _context.sent;
case 10:
setData(res);
setLoading(false);
case 12:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function requestData(_x) {
return _ref.apply(this, arguments);
};
}();
(0, _react.useEffect)(function () {
reset();
requestData(props);
}, [props]);
if (loading && props.loading) return props.loading();
if (error && props.error) return props.error(error);
if (data) return props.render(data);
return null;
};
Request.propTypes = {
url: _propTypes["default"].string.isRequired,
query: _propTypes["default"].string.isRequired,
render: _propTypes["default"].func.isRequired,
error: _propTypes["default"].func,
loading: _propTypes["default"].func,
variables: _propTypes["default"].object,
options: _propTypes["default"].object
};
var _default = Request;
exports["default"] = _default;