strange-router
Advanced tools
Comparing version 2.0.0 to 2.1.0
146
lib/index.js
const React = require('react'); | ||
const T = require('prop-types'); | ||
const { Switch, Route, Redirect } = require('react-router'); | ||
const { default: Switch } = require('react-router-dom/Switch'); | ||
const { default: Route } = require('react-router-dom/Route'); | ||
const { default: RedirectComponent } = require('react-router-dom/Redirect'); | ||
const internals = {}; | ||
@@ -12,18 +9,20 @@ | ||
static propTypes = { | ||
routes: T.array.isRequired | ||
} | ||
render() { | ||
const { routes } = this.props; | ||
const renderRoute = internals.renderRoute('/'); | ||
return ( | ||
<Switch> | ||
{routes.map(internals.renderRoute('/'))} | ||
</Switch> | ||
); | ||
return Array.isArray(routes) ? | ||
<Switch>{routes.map(renderRoute)}</Switch> : | ||
renderRoute(routes); | ||
} | ||
}; | ||
exports.Routes.propTypes = { | ||
routes: T.oneOfType([ | ||
T.object, | ||
T.arrayOf(T.object) | ||
]).isRequired | ||
}; | ||
internals.renderRoute = (basePath) => { | ||
@@ -39,59 +38,53 @@ | ||
if (Object.keys(rest).length !== 0) { | ||
throw new Error(`No other properties are allowed alongside "redirect" in route configuration. Check childRoutes of "${basePath}"`); | ||
throw new Error(`No other properties are allowed alongside "redirect" in route configuration. Check childRoutes of "${basePath}".`); | ||
} | ||
const redirectClone = { ...redirect }; | ||
const { from, to } = redirect; | ||
const redirectProps = { ...redirect }; | ||
if (typeof redirectClone.from === 'string') { | ||
if (typeof from === 'string') { | ||
// redirect.from must be relative | ||
redirectClone.from = internals.concatPaths(basePath, redirectClone.from); | ||
redirectProps.from = internals.concatPaths(basePath, from); | ||
} | ||
if (typeof redirectClone.to === 'string') { | ||
if (typeof to === 'string') { | ||
// If redirect.to is absolute, leave it be. Otherwise make it relative | ||
redirectClone.to = redirectClone.to.startsWith('/') ? redirectClone.to : internals.concatPaths(basePath, redirectClone.to); | ||
redirectProps.to = to.startsWith('/') ? to : internals.concatPaths(basePath, to); | ||
} | ||
else { | ||
else if (to && typeof to.pathname === 'string') { | ||
// to is an object | ||
if (typeof redirectClone.to.pathname === 'string') { | ||
const pathname = redirectClone.to.pathname.startsWith('/') ? redirectClone.to.pathname : internals.concatPaths(basePath, redirectClone.to.pathname); | ||
redirectClone.to = { | ||
...redirectClone.to, | ||
pathname | ||
}; | ||
} | ||
redirectProps.to = { | ||
...redirectProps.to, | ||
pathname: to.pathname.startsWith('/') ? to.pathname : internals.concatPaths(basePath, to.pathname) | ||
}; | ||
} | ||
return <RedirectComponent {...redirectClone} />; | ||
return <Redirect {...redirectProps} />; | ||
} | ||
const normalizedPath = internals.concatPaths(basePath, route.path); | ||
const RouteComponent = route.component; | ||
const renderRoute = internals.renderRoute(normalizedPath); | ||
const RouteComponent = route.component || route.render; | ||
const { RouteComponentLifecycleWrapper } = internals; | ||
return ( | ||
<Route | ||
exact={route.exact} | ||
key={route.path} | ||
path={normalizedPath} | ||
exact={route.exact} | ||
strict={route.strict} | ||
sensitive={route.sensitive} | ||
render={(props) => { | ||
const switcher = route.childRoutes | ||
? | ||
<Switch> | ||
{route.childRoutes.map(internals.renderRoute(normalizedPath))} | ||
</Switch> | ||
: | ||
null; | ||
const switcher = route.childRoutes ? | ||
<Switch children={route.childRoutes.map(renderRoute)} /> : | ||
null; | ||
return ( | ||
<internals.routeComponentLifecycleWrapper {...props} route={route}> | ||
{RouteComponent | ||
? | ||
<RouteComponent {...props} route={route}>{switcher}</RouteComponent> | ||
: | ||
switcher | ||
} | ||
</internals.routeComponentLifecycleWrapper> | ||
); | ||
const routeComponent = RouteComponent ? | ||
<RouteComponent {...props} route={route} children={switcher} /> : | ||
switcher; | ||
return RouteComponentLifecycleWrapper.nonTrivial(route) ? | ||
<RouteComponentLifecycleWrapper {...props} route={route} children={routeComponent} /> : | ||
routeComponent; | ||
}} | ||
@@ -103,11 +96,4 @@ /> | ||
internals.routeComponentLifecycleWrapper = class RouteComponentLifecycleWrapper extends React.PureComponent { | ||
internals.RouteComponentLifecycleWrapper = class RouteComponentLifecycleWrapper extends React.PureComponent { | ||
static propTypes = { | ||
match: T.object, | ||
location: T.object, | ||
history: T.object, | ||
route: T.object | ||
} | ||
constructor(props) { | ||
@@ -117,36 +103,18 @@ | ||
const { route, match, location, history } = props; | ||
const { route } = props; | ||
if (typeof route.componentDidCatch === 'function') { | ||
this.componentDidCatch = (err, info) => { | ||
route.componentDidCatch({ err, info, route, match, location, history }); | ||
} | ||
if (route.componentDidCatch) { | ||
this.componentDidCatch = (err, info) => route.componentDidCatch({ err, info, ...this.props }); | ||
} | ||
} | ||
componentWillMount() { | ||
const { route, match, location, history } = this.props; | ||
if (typeof route.onWillMount === 'function') { | ||
route.onWillMount({ route, match, location, history }); | ||
if (route.onWillMount) { | ||
this.componentWillMount = () => route.onWillMount(this.props); | ||
} | ||
} | ||
componentDidMount() { | ||
const { route, match, location, history } = this.props; | ||
if (typeof route.onDidMount === 'function') { | ||
route.onDidMount({ route, match, location, history }); | ||
if (route.onDidMount) { | ||
this.componentDidMount = () => route.onDidMount(this.props); | ||
} | ||
} | ||
componentWillUnmount() { | ||
const { route, match, location, history } = this.props; | ||
if (typeof route.onWillUnmount === 'function') { | ||
route.onWillUnmount({ route, match, location, history }); | ||
if (route.onWillUnmount) { | ||
this.componentWillUnmount = () => route.onWillUnmount(this.props); | ||
} | ||
@@ -161,2 +129,14 @@ } | ||
internals.RouteComponentLifecycleWrapper.propTypes = { | ||
route: T.object.isRequired | ||
}; | ||
internals.RouteComponentLifecycleWrapper.nonTrivial = (route) => { | ||
return route.componentDidCatch || | ||
route.onWillMount || | ||
route.onDidMount || | ||
route.onWillUnmount; | ||
}; | ||
internals.concatPaths = (base, path) => { | ||
@@ -169,3 +149,1 @@ | ||
}; | ||
internals.flatten = (arr) => [].concat(...arr); |
{ | ||
"name": "strange-router", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A module to aid bringing configurable routes to react-router v4.x", | ||
"main": "dist/index.js", | ||
"main": "dist/strange-router.js", | ||
"module": "dist/strange-router.module.js", | ||
"directories": { | ||
@@ -11,5 +12,7 @@ "lib": "lib", | ||
"scripts": { | ||
"build": "babel lib --out-dir ./dist", | ||
"build": "npm run clean && rollup -c", | ||
"clean": "rimraf dist", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prepublishOnly": "npm run build" | ||
"lint": "lab -dL", | ||
"coveralls": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
@@ -31,12 +34,22 @@ "repository": { | ||
"homepage": "https://github.com/BigRoomStudios/strange-router#readme", | ||
"peerDependencies": { | ||
"prop-types": "15.x.x", | ||
"react": "16.x.x", | ||
"react-router": ">=4 <6" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "6.x.x", | ||
"babel-plugin-transform-runtime": "6.x.x", | ||
"babel-preset-es2015": "6.x.x", | ||
"babel-preset-react": "6.x.x", | ||
"babel-preset-stage-0": "6.x.x" | ||
}, | ||
"peerDependencies": { | ||
"react-router-dom": "4.x.x" | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"@hapi/lab": "20.x.x", | ||
"react-router": "5.x.x", | ||
"rimraf": "3.x.x", | ||
"rollup": "1.x.x", | ||
"rollup-plugin-babel": "4.x.x", | ||
"rollup-plugin-cjs-es": "0.9.x", | ||
"rollup-plugin-filesize": "6.x.x", | ||
"rollup-plugin-node-resolve": "5.x.x", | ||
"rollup-plugin-peer-deps-external": "2.x.x", | ||
"rollup-plugin-terser": "5.x.x" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59120
10
675
3
13
2
2