UNPKG

12.1 kBJavaScriptView Raw
1(function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory(require("./actions.js"), require("./pageUtils.js"), require("./router.js"), require("path-to-regexp"));
4 else if(typeof define === 'function' && define.amd)
5 define(["./actions.js", "./pageUtils.js", "./router.js", "path-to-regexp"], factory);
6 else if(typeof exports === 'object')
7 exports["navigationMiddleware.js"] = factory(require("./actions.js"), require("./pageUtils.js"), require("./router.js"), require("path-to-regexp"));
8 else
9 root["navigationMiddleware.js"] = factory(root["./actions.js"], root["./pageUtils.js"], root["./router.js"], root["path-to-regexp"]);
10})(this, function(__WEBPACK_EXTERNAL_MODULE_13__, __WEBPACK_EXTERNAL_MODULE_14__, __WEBPACK_EXTERNAL_MODULE_15__, __WEBPACK_EXTERNAL_MODULE_321__) {
11return /******/ (function(modules) { // webpackBootstrap
12/******/ // The module cache
13/******/ var installedModules = {};
14
15/******/ // The require function
16/******/ function __webpack_require__(moduleId) {
17
18/******/ // Check if module is in cache
19/******/ if(installedModules[moduleId])
20/******/ return installedModules[moduleId].exports;
21
22/******/ // Create a new module (and put it into the cache)
23/******/ var module = installedModules[moduleId] = {
24/******/ exports: {},
25/******/ id: moduleId,
26/******/ loaded: false
27/******/ };
28
29/******/ // Execute the module function
30/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31
32/******/ // Flag the module as loaded
33/******/ module.loaded = true;
34
35/******/ // Return the exports of the module
36/******/ return module.exports;
37/******/ }
38
39
40/******/ // expose the modules object (__webpack_modules__)
41/******/ __webpack_require__.m = modules;
42
43/******/ // expose the module cache
44/******/ __webpack_require__.c = installedModules;
45
46/******/ // __webpack_public_path__
47/******/ __webpack_require__.p = "";
48
49/******/ // Load entry module and return exports
50/******/ return __webpack_require__(0);
51/******/ })
52/************************************************************************/
53/******/ ({
54
55/***/ 0:
56/***/ function(module, exports, __webpack_require__) {
57
58 'use strict';
59
60 Object.defineProperty(exports, "__esModule", {
61 value: true
62 });
63
64 var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
65
66 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; };
67
68 var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
69
70 exports.matchRoute = matchRoute;
71
72 var _pathToRegexp = __webpack_require__(321);
73
74 var _pathToRegexp2 = _interopRequireDefault(_pathToRegexp);
75
76 var _router = __webpack_require__(15);
77
78 var _actions = __webpack_require__(13);
79
80 var actions = _interopRequireWildcard(_actions);
81
82 var _pageUtils = __webpack_require__(14);
83
84 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
85
86 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
87
88 function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
89
90 function matchRoute(path, routes) {
91 var route = void 0;
92
93 var _iteratorNormalCompletion = true;
94 var _didIteratorError = false;
95 var _iteratorError = undefined;
96
97 try {
98 for (var _iterator = routes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
99 route = _step.value;
100 var _route = route;
101
102 var _route2 = _slicedToArray(_route, 3);
103
104 var url = _route2[0];
105 var handler = _route2[1];
106 var meta = _route2[2];
107
108 var reg = (0, _pathToRegexp2.default)(url);
109 var result = reg.exec(path);
110
111 if (result) {
112 return { handler: handler, reg: reg, result: result, meta: meta };
113 }
114 }
115 } catch (err) {
116 _didIteratorError = true;
117 _iteratorError = err;
118 } finally {
119 try {
120 if (!_iteratorNormalCompletion && _iterator.return) {
121 _iterator.return();
122 }
123 } finally {
124 if (_didIteratorError) {
125 throw _iteratorError;
126 }
127 }
128 }
129 }
130
131 var getRouteMeta = function getRouteMeta(routes, shouldSetPage, data) {
132 var method = data.method;
133 var pathName = data.pathName;
134
135 var route = matchRoute(pathName, routes);
136
137 if (route && route.meta && shouldSetPage && method === _router.METHODS.GET) {
138 return route.meta;
139 }
140 return null;
141 };
142
143 var findAndCallHandler = function findAndCallHandler(store, routes, shouldSetPage, data) {
144 var method = data.method;
145 var pathName = data.pathName;
146 var queryParams = data.queryParams;
147 var hashParams = data.hashParams;
148 var bodyParams = data.bodyParams;
149 var referrer = data.referrer;
150 var dispatch = store.dispatch;
151 var getState = store.getState;
152
153 var route = matchRoute(pathName, routes);
154
155 if (route) {
156 var _ret = function () {
157 var handler = route.handler;
158 var reg = route.reg;
159 var result = route.result;
160
161 var urlParams = reg.keys.reduce(function (prev, cur, index) {
162 return _extends({}, prev, _defineProperty({}, cur.name, result[index + 1]));
163 }, {});
164
165 // set page only if its a HEAD or a GET. setting page data is required
166 // to make sure request rendering and redirects work correctly
167 // The only reason HEAD is included is because its the same as GET but
168 // it doesn't have a response body.
169 if (shouldSetPage && (method === _router.METHODS.GET || method === _router.METHODS.HEAD)) {
170 dispatch(actions.setPage(pathName, {
171 urlParams: urlParams,
172 queryParams: queryParams,
173 hashParams: hashParams,
174 referrer: referrer
175 }));
176 }
177
178 var h = new handler(pathName, urlParams, queryParams, hashParams, bodyParams, dispatch, getState);
179
180 var handlerMethod = method;
181 // HEAD requests are supposed to have the exact same headers and redirects
182 // as a GET request, but they must not send a response body.
183 // To support HEAD requests, we can check if the handler
184 // has a specific HEAD function, otherwise we just use its GET function
185 if (handlerMethod === _router.METHODS.HEAD && !h[_router.METHODS.HEAD]) {
186 handlerMethod = _router.METHODS.GET;
187 }
188
189 if (!h[handlerMethod]) {
190 throw new Error('No method found for ' + method.toUpperCase() + ' ' + pathName);
191 }
192
193 return {
194 v: h[handlerMethod].bind(h)
195 };
196 }();
197
198 if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
199 }
200
201 throw new Error('No route found for ' + method.toUpperCase() + ' ' + pathName);
202 };
203
204 exports.default = {
205 create: function create(routes, isServer, onHandlerComplete) {
206 return function (store) {
207 return function (next) {
208 return function (action) {
209 var shouldSetPage = void 0;
210 var payload = void 0;
211 switch (action.type) {
212 case actions.NAVIGATE_TO_URL:
213 case actions.GOTO_PAGE_INDEX:
214 {
215 var _ret2 = function () {
216 var startTime = new Date().getTime();
217 next(action);
218 if (action.type === actions.NAVIGATE_TO_URL) {
219 shouldSetPage = true;
220 payload = action.payload;
221 } else {
222 shouldSetPage = false;
223 payload = _extends({}, action.payload, { method: _router.METHODS.GET });
224 }
225 var meta = getRouteMeta(routes, shouldSetPage, payload);
226 var handler = findAndCallHandler(store, routes, shouldSetPage, payload);
227 var ret = next(handler);
228
229 // When the handler completes, we get some timing info and pass it
230 // along to onHandlerComplete
231 var timeRoute = function timeRoute() {
232 var endTime = new Date().getTime();
233 var duration = endTime - startTime;
234 onHandlerComplete({ meta: meta, startTime: startTime, endTime: endTime, duration: duration });
235 };
236
237 ret.then(timeRoute).catch(function (e) {
238 timeRoute();
239 throw e;
240 });
241
242 return {
243 v: ret
244 };
245 }();
246
247 if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
248 }
249
250 case actions.REDIRECT:
251 {
252 var url = action.url;
253
254 // We want to redirect the current page somewhere else.
255 // If we're on the server, this should always translate into a SET_PAGE
256 // action, because we should issue a proper 3XX status code to redirect.
257
258 if (isServer) {
259 return store.dispatch(actions.setPage(url));
260 }
261
262 if (url.startsWith('/')) {
263 // We have special logic for relative urls:
264 // Before we route, we want to make sure the app's router supports the
265 // path. It's easy to imagine getting a relative url that isn't in
266 // routes, but is valid, e.g. there might be a load-balancer or proxy
267 // that routes the request the appropriate frontend-application based
268 // on varying criteria such as User-Agent, authentication, path, etc
269 var path = url.split('?')[0];
270 var route = matchRoute(path, routes);
271
272 if (route) {
273 var queryParams = (0, _pageUtils.extractQuery)(url);
274 return store.dispatch(actions.navigateToUrl(_router.METHODS.GET, path, { queryParams: queryParams }));
275 }
276 }
277
278 // base case for client -- hard redirect via window.location.
279 window.location = url;
280
281 return next(action);
282 }
283
284 default:
285 return next(action);
286 }
287 };
288 };
289 };
290 }
291 };
292
293/***/ },
294
295/***/ 13:
296/***/ function(module, exports) {
297
298 module.exports = __WEBPACK_EXTERNAL_MODULE_13__;
299
300/***/ },
301
302/***/ 14:
303/***/ function(module, exports) {
304
305 module.exports = __WEBPACK_EXTERNAL_MODULE_14__;
306
307/***/ },
308
309/***/ 15:
310/***/ function(module, exports) {
311
312 module.exports = __WEBPACK_EXTERNAL_MODULE_15__;
313
314/***/ },
315
316/***/ 321:
317/***/ function(module, exports) {
318
319 module.exports = require("path-to-regexp");
320
321/***/ }
322
323/******/ })
324});
325;
\No newline at end of file