UNPKG

react-refetch

Version:

A simple, declarative, and composable way to fetch data for React components.

78 lines (70 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = checkTypes; var _invariant = _interopRequireDefault(require("invariant")); var _isPlainObject = _interopRequireDefault(require("./isPlainObject")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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); } function typecheck(types, name, obj) { (0, _invariant["default"])(Array.isArray(types) ? types.some(function (t) { return _typeof(obj) === t; }) : _typeof(obj) === types, "".concat(name, " must be ").concat(Array.isArray(types) ? 'one of' : 'a', " ").concat(types, ". Instead received a %s."), _typeof(obj)); } var checks = { buildRequest: function buildRequest(fn) { typecheck('function', 'buildRequest', fn); }, credentials: function credentials(str) { var allowed = ['omit', 'same-origin', 'include']; (0, _invariant["default"])(allowed.indexOf(str) !== -1, "credentials must be one of ".concat(allowed.join(', '), ". Instead got %s."), str ? str.toString() : str); }, fetch: function fetch(fn) { typecheck('function', 'fetch', fn); }, handleResponse: function handleResponse(fn) { typecheck('function', 'handleResponse', fn); }, headers: function headers(obj) { (0, _invariant["default"])((0, _isPlainObject["default"])(obj), 'headers must be a plain object with string values. Instead received a %s.', _typeof(obj)); }, method: function method(str) { typecheck('string', 'method', str); }, redirect: function redirect(str) { var allowed = ['follow', 'error', 'manual']; (0, _invariant["default"])(allowed.indexOf(str) !== -1, "redirect must be one of ".concat(allowed.join(', '), ". Instead got %s."), str ? str.toString() : str); }, mode: function mode(str) { var allowed = ['cors', 'no-cors', 'same-origin', 'navigate']; (0, _invariant["default"])(allowed.indexOf(str) !== -1, "mode must be one of ".concat(allowed.join(', '), ". Instead got %s."), str ? str.toString() : str); }, refreshInterval: function refreshInterval(num) { typecheck('number', 'refreshInterval', num); (0, _invariant["default"])(num >= 0, 'refreshInterval must be positive or 0.'); (0, _invariant["default"])(num !== Infinity, 'refreshInterval must not be Infinity.'); }, Request: function Request(fn) { typecheck('function', 'Request', fn); }, then: function then(fn) { typecheck(['function', 'undefined'], 'then', fn); }, andThen: function andThen(fn) { typecheck(['function', 'undefined'], 'andThen', fn); }, "catch": function _catch(fn) { typecheck(['function', 'undefined'], 'catch', fn); }, andCatch: function andCatch(fn) { typecheck(['function', 'undefined'], 'andCatch', fn); } }; function checkTypes(mapping) { Object.keys(mapping).forEach(function (key) { if (checks[key]) { checks[key](mapping[key]); } }); }