troch / route-node

A package to create a tree of named routes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to extend a node object

DmitryKamynin opened this issue · comments

Hi, I made a library for router5 that uses your library. My library is responsible for private access to certain pages and I would like to extend it so that it can take on roles and restrict / grant access to certain application pages in accordance with them. My library also takes your nodes and recursively marks them private, but that's all I have. I would like to do something similar

import createRouter, {PluginFactory, Route} from "router5";
import accessPlugin, {AccessPluginRouter} from "./index";

const path: Route[] = [
  {
    name: 'root',
    path: '/',
    role: 'public', // transfer the role like this 
    public: true,  // or like this
  },
  {
    name: 'admin',
    path: '/admin',
    role: 'admin', // transfer the role like this
    children: [
      {
        name: 'adminPage',
        path: '/adminPage',
      },
    ]
  },
  {
    name: 'moderator',
    path: '/moderator',
    role: 'moderator', // transfer the role like this
    children: [
      {
        name: 'moderPage',
        path: '/moderatorPage',
      },
    ]
  }
];

const test = () => {
  const router = createRouter(path)
  router.usePlugin(accessPlugin({log: true}) as PluginFactory);
  (router as AccessPluginRouter).createPrivateAccess(path);
  (router as AccessPluginRouter).checkAccess('admin'); // or 'moderator'   // either true or 'public' for open access

  router.start();

  console.log('plugin test is successful')
}

test();

so that my plugin captures these roles and works with them. What would you advise me to do?