UNPKG

react-ssr

Version:

A simplified solution to React server side rendering.

116 lines (90 loc) 3.9 kB
'use strict'; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var Q = require('q'); require('regenerator-runtime/runtime.js'); var executeFetchData = function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(component, match, req, res) { var fetch, keys, props, result, response, updatedKeys, responses; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(typeof component.fetchData !== 'function')) { _context.next = 3; break; } console.info('fetchData is not a function or not static on ' + component.displayName); return _context.abrupt('return', new Error('Fetch data not defined or not a function.')); case 3: fetch = component.fetchData({ match: match, req: req, res: res, isServerRender: !!req }); keys = Object.keys(fetch || {}) || []; props = {}; result = {}; result[component.displayName] = {}; if (keys.length) { _context.next = 29; break; } _context.prev = 9; response = void 0; _context.prev = 11; _context.next = 14; return fetch; case 14: response = _context.sent; updatedKeys = Object.keys(response || {}); updatedKeys.forEach(function (key) { result[component.displayName][key] = response[key]; }); _context.next = 23; break; case 19: _context.prev = 19; _context.t0 = _context['catch'](11); console.info('fetchData failed for ' + component.displayName, _context.t0); props.error = true; case 23: _context.next = 28; break; case 25: _context.prev = 25; _context.t1 = _context['catch'](9); console.info('fetchData failed for ' + component.displayName, _context.t1); case 28: return _context.abrupt('return', result); case 29: _context.prev = 29; _context.next = 32; return Q.allSettled(keys.map(function (key) { return fetch[key]; })); case 32: responses = _context.sent; responses.forEach(function (data, index) { if (data.value) { result[component.displayName][keys[index]] = data.value; } else { result[component.displayName][keys[index]] = data.reason; console.info('fetchData #' + (index + 1) + ' in ' + component.displayName + ' returned undefined.'); } }); _context.next = 39; break; case 36: _context.prev = 36; _context.t2 = _context['catch'](29); console.info('fetchData failed for ' + component.displayName, _context.t2); case 39: return _context.abrupt('return', result); case 40: case 'end': return _context.stop(); } } }, _callee, undefined, [[9, 25], [11, 19], [29, 36]]); })); return function executeFetchData(_x, _x2, _x3, _x4) { return _ref.apply(this, arguments); }; }(); module.exports = executeFetchData;