1.0.0 • Published 5 years ago

@outcast.by/js-extttaout v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

Router

Router.resources(views, name, routes)

Generates the list of routers based on routes and expands by CRUD routes. Later the list would be used as props for the SwitchRoutes component

import { Router } from '@outcast.by/js-ext'

// more info about settings format see https://github.com/outcastby/awesome-crud
const settings = { cdnServers: { list: {}, gql: { create: 'some query', update: 'some query', show: 'some query' } } }
const components = { New, Edit, Show, List } // the map of universal CRUD components

const views = {
  custom: { cdnServers: { CustomAction } },
  crud: { components, settings },
}
Router.resources(views, 'cdnServers', [{ routeAction: 'customAction' }])

<SwitchRoutes />

This is the extension for react-dom-router <Switch />, which flexible resolves role based control, inner routing and redirects

// You have to implement the function `isPermittedRoute`
const isPermittedRoute = (route) => {
  if (route.skipPermissions) return true
  ...
}
// For CRUD should be `Router.resources` used
const routes = [
  { requiredPermissions: ['cdn_servers', 'edit'], path: '/cdn_servers/new', component: SomeComponent },
  { requiredPermissions: ['cdn_servers', 'edit'], path: '/cdn_servers/:id/edit', component: SomeComponent },
  { redirect: true, path: '/', pathTo: '/build_info' },
]

<SwitchRoutes isPermittedRoute={isPermittedRoute} routes={routes} />

Interface Route

NameTypeExampleDescription
pathstring/cdn_servers/:id/editAny valid URL path (the same like in react-router)
exactbooleantrueWhen true, will only match if the path matches the location.pathname exactly. (the same like in react-router)
pathTostring/build_infoA string representation of the Link location, created by concatenating the location’s pathname, search, and hash properties. (the same like to in react-router)
componentReact.FC() => <div />A React component to render only when the location matches. (the same like in react-router)
isAuthorizedbooleantrueWhen true, the route appears only when the user is authorized
routesRoute[]The list of inner routes
requiredPermissionsstring[]['cdn_servers', 'edit']The list can be used inside isPermittedRoute function in order to filter not permitted routes
redirectbooleantrueWhen true, <Redirect /> will be rendered. Works together with pathTo