UNPKG

@tdb/web

Version:

Common condiguration for serving a web-site and testing web-based UI components.

133 lines (105 loc) 3.73 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.execOnce = execOnce; exports.getDisplayName = getDisplayName; exports.isResSent = isResSent; exports.loadGetInitialProps = loadGetInitialProps; exports.getLocationOrigin = getLocationOrigin; exports.getURL = getURL; var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator")); function execOnce(fn) { var _this = this; var used = false; return function () { if (!used) { used = true; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } fn.apply(_this, args); } }; } function getDisplayName(Component) { if (typeof Component === 'string') { return Component; } return Component.displayName || Component.name || 'Unknown'; } function isResSent(res) { return res.finished || res.headersSent; } function loadGetInitialProps(_x, _x2) { return _loadGetInitialProps.apply(this, arguments); } function _loadGetInitialProps() { _loadGetInitialProps = (0, _asyncToGenerator2.default)( /*#__PURE__*/ _regenerator.default.mark(function _callee(Component, ctx) { var compName, message, props, _compName, _message; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(process.env.NODE_ENV !== 'production')) { _context.next = 5; break; } if (!(Component.prototype && Component.prototype.getInitialProps)) { _context.next = 5; break; } compName = getDisplayName(Component); message = "\"".concat(compName, ".getInitialProps()\" is defined as an instance method - visit https://err.sh/zeit/next.js/get-initial-props-as-an-instance-method for more information."); throw new Error(message); case 5: if (Component.getInitialProps) { _context.next = 7; break; } return _context.abrupt("return", {}); case 7: _context.next = 9; return Component.getInitialProps(ctx); case 9: props = _context.sent; if (!(ctx.res && isResSent(ctx.res))) { _context.next = 12; break; } return _context.abrupt("return", props); case 12: if (props) { _context.next = 16; break; } _compName = getDisplayName(Component); _message = "\"".concat(_compName, ".getInitialProps()\" should resolve to an object. But found \"").concat(props, "\" instead."); throw new Error(_message); case 16: return _context.abrupt("return", props); case 17: case "end": return _context.stop(); } } }, _callee, this); })); return _loadGetInitialProps.apply(this, arguments); } function getLocationOrigin() { var _window$location = window.location, protocol = _window$location.protocol, hostname = _window$location.hostname, port = _window$location.port; return "".concat(protocol, "//").concat(hostname).concat(port ? ':' + port : ''); } function getURL() { var href = window.location.href; var origin = getLocationOrigin(); return href.substring(origin.length); }