blips
Version:
State management for the GraphQL heads
56 lines (42 loc) • 2.34 kB
JavaScript
;
exports.__esModule = true;
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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.extendContext = extendContext;
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var STORE_EXTEND_WARNING = exports.STORE_EXTEND_WARNING = 'You cannot extend the context with an object containing a "store" prop. It will be omitted and the other props will be added';
var buildPropExistsWarning = exports.buildPropExistsWarning = function buildPropExistsWarning(propName) {
return 'The "' + propName + '" property already exists in the context object. Make sure you haven\'t replaced anything important';
};
function extendContext(defaultContext, context) {
if (!context || // nil or falsy
(typeof context === 'undefined' ? 'undefined' : _typeof(context)) !== 'object' || // not an object
context.hasOwnProperty('length') || // array
context.hasOwnProperty('size') // map
) {
return defaultContext;
}
var store = context.store,
ctx = _objectWithoutProperties(context, ['store']);
if (store) {
// eslint-disable-next-line
console.warn(STORE_EXTEND_WARNING);
}
for (var _iterator = Object.keys(ctx), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var propName = _ref;
if (defaultContext.hasOwnProperty(propName)) {
// eslint-disable-next-line
console.warn(buildPropExistsWarning(propName));
}
}
return _extends({}, defaultContext, ctx);
}