react-router
Version:
A complete routing library for React.js
63 lines (44 loc) • 2.32 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; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _historyLibCreateMemoryHistory = require('history/lib/createMemoryHistory');
var _historyLibCreateMemoryHistory2 = _interopRequireDefault(_historyLibCreateMemoryHistory);
var _historyLibUseBasename = require('history/lib/useBasename');
var _historyLibUseBasename2 = _interopRequireDefault(_historyLibUseBasename);
var _RouteUtils = require('./RouteUtils');
var _useRoutes = require('./useRoutes');
var _useRoutes2 = _interopRequireDefault(_useRoutes);
var createHistory = _useRoutes2['default'](_historyLibUseBasename2['default'](_historyLibCreateMemoryHistory2['default']));
/**
* A high-level API to be used for server-side rendering.
*
* This function matches a location to a set of routes and calls
* callback(error, redirectLocation, renderProps) when finished.
*
* Note: You probably don't want to use this in a browser. Use
* the history.listen API instead.
*/
function match(_ref, callback) {
var routes = _ref.routes;
var location = _ref.location;
var parseQueryString = _ref.parseQueryString;
var stringifyQuery = _ref.stringifyQuery;
var basename = _ref.basename;
!location ? process.env.NODE_ENV !== 'production' ? _invariant2['default'](false, 'match needs a location') : _invariant2['default'](false) : undefined;
var history = createHistory({
routes: _RouteUtils.createRoutes(routes),
parseQueryString: parseQueryString,
stringifyQuery: stringifyQuery,
basename: basename
});
// Allow match({ location: '/the/path', ... })
if (typeof location === 'string') location = history.createLocation(location);
history.match(location, function (error, redirectLocation, nextState) {
callback(error, redirectLocation, nextState && _extends({}, nextState, { history: history }));
});
}
exports['default'] = match;
module.exports = exports['default'];