all files / modules/ match.js

100% Statements 34/34
75% Branches 12/16
100% Functions 4/4
100% Lines 9/9
6 statements, 1 function, 4 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                               
import invariant from 'invariant'
import createMemoryHistory from 'history/lib/createMemoryHistory'
import useBasename from 'history/lib/useBasename'
import { createRoutes } from './RouteUtils'
import useRoutes from './useRoutes'
 
const createHistory = useRoutes(useBasename(createMemoryHistory))
 
/**
 * 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({
  routes,
  location,
  parseQueryString,
  stringifyQuery,
  basename
}, callback) {
  invariant(
    location,
    'match needs a location'
  )
 
  const history = createHistory({
    routes: createRoutes(routes),
    parseQueryString,
    stringifyQuery,
    basename
  })
 
  // Allow match({ location: '/the/path', ... })
  Eif (typeof location === 'string')
    location = history.createLocation(location)
 
  history.match(location, function (error, redirectLocation, nextState) {
    callback(error, redirectLocation, nextState && { ...nextState, history })
  })
}
 
export default match