flex-template-web/server/dataLoader.js
2017-10-04 13:17:43 +03:00

34 lines
1,023 B
JavaScript

const url = require('url');
const { matchPathname, configureStore, routeConfiguration } = require('./importer');
const log = require('./log');
exports.loadData = function(requestUrl, sdk) {
const { pathname, query } = url.parse(requestUrl);
const matchedRoutes = matchPathname(pathname, routeConfiguration);
const store = configureStore(sdk);
const dataLoadingCalls = matchedRoutes.reduce(
(calls, match) => {
const { route, params } = match;
if (typeof route.loadData === 'function' && !route.auth) {
calls.push(store.dispatch(route.loadData(params, query)));
}
return calls;
},
[]
);
return Promise.all(dataLoadingCalls)
.then(() => {
return store.getState();
})
.catch(e => {
log.error(e, 'server-side-data-load-failed');
// Call to loadData failed, let client handle the data loading errors.
// (It might be recoverable error like lost connection.)
// Return "empty" store.
return store.getState();
});
};