🗺️ RouteConfig
Static route configuration helper
Installation
NPM:
$ npm install --save route-config
Yarn:
yarn add route-config
Import
In ES6:
import { Config, RouteConfig } from 'route-config'
Use
Basic
const routeConfig = namespaces: posts: path: '/posts' routes: index: {} show: path: '/:postId' routes: home: path: '/home' routeConfig//=> "/home" routeConfig//=> "/posts/:postId"
API
new RouteConfig(map, options)
Params:
map={ namespaces: {}, routes: {} }
: Map of your routesconfig
: config to applied on sub namspaces and sub routesfullPath=''
: if set then it's used to prefix all sub namespace and sub route path.id=''
: id is used internally to create normalized map.key=''
: if set prefix all sub keys withkey
namespaces={}
: sub namespacespath=''
: root pathroutes={}
: sub routes
options={}
:configs
: options passed toConfigManager
constructor
Ex
const routeConfig = namespaces: posts: path: '/posts' routes: show: path: '/:postId' routes: home: path: '/'
routeConfig.getMap(options)
Return map Params:
options={}
:filter
: used to filter namespaces and routes returned in mapformatRoute=identity
: used to format route returned in map
Ex
routeConfig//=>// {// namespaces: {// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: {// show: {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// }// },// routes: {// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// }// }// }
routeConfig.getNormalizedMap(options)
Return normalized map. For more info see normalizr
Params:
options={}
:filter
: used to filter namespaces and routes returned in mapformatRoute=identity
: used to format route returned in map
routeConfig//=>// {// entities: {// namespaces: {// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: ['posts.show']// }// },// routes: {// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// },// 'posts.show': {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// },// result: {// namespaces: ['posts'],// routes: ['home']// }// }
routeConfig.getRoute(key)
Return route object
Params:
key
(String): a string or dot notation string to find route or nested route
Ex
routeConfig//=>// {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// } routeConfig // nested route//=>// {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }
routeConfig.merge(...maps)
Merge route config map with maps (modify map)
Ex
routeConfig routeConfig//=>// {// namespaces: {// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: {// show: {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// }// },// routes: {// contact: {// config: {},// fullPath: '/contact',// id: 'contact',// key: 'contact',// path: '/contact'// },// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// }// }// }
routeConfig.namespace(namespace)
Returns the new namespace that is a instance of RouteConfig
.
Params:
namespace
(Object):config
: config to applied on sub namspaces and sub routesfullPath
: if set then it's used to prefix all sub namespace and sub route path. Else sub namespace and sub route path are prefixed with parent full path andnamespace
pathid
: id is used internally to create normalized mapkey
(String): namespace name used to get route inside namespacenamespaces={}
: sub namespacespath
(String): namespace pathroutes={}
: sub routes
Ex
const namespace = routeConfignamespacenamespace//=>// {// config: {},// fullPath: '/authors',// id: 'authors',// key: 'authors',// namespaces: {},// path: '/authors',// routes: {// show: {// config: {},// fullPath: '/authors/:authorId',// id: 'authors.show',// key: 'show',// path: '/:authorId'// }// }// } routeConfig//=>// {// namespaces: {// authors: {// config: {},// fullPath: '/authors',// id: 'authors',// key: 'authors',// namespaces: {},// path: '/authors',// routes: {// show: {// config: {},// fullPath: '/authors/:authorId',// id: 'authors.show',// key: 'show',// path: '/:authorId'// }// }// },// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: {// show: {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// }// },// routes: {// contact: {// config: {},// fullPath: '/contact',// id: 'contact',// key: 'contact',// path: '/contact'// },// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// }// }// }
routeConfig.route(route)
Returns routeConfig instance so you can chain call.
Params:
namespace
(Object):
Ex
routeConfig//=>// {// namespaces: {// authors: {// config: {},// fullPath: '/authors',// id: 'authors',// key: 'authors',// namespaces: {},// path: '/authors',// routes: {// show: {// config: {},// fullPath: '/authors/:authorId',// id: 'authors.show',// key: 'show',// path: '/:authorId'// }// }// },// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: {// show: {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// }// },// routes: {// about: {// config: {},// fullPath: '/about',// id: 'about',// key: 'about',// path: '/about'// },// contact: {// config: {},// fullPath: '/contact',// id: 'contact',// key: 'contact',// path: '/contact'// },// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// }// }
routeConfig.url(key, params, options)
Returns route url or null.
Params:
key
(String): route key. You can use dot notation to get route url inside namspaceparams=undefined
: params passed topath-to-regexp
options=undefined
: options passed topath-to-regexp
Ex
routeConfig//=> '/' routeConfig//=> /posts/:postId routeConfig//=> /posts/1
static RouteConfig.fromNormalizedMap(normalizedMap)
Returns new RouteConfig instance created from normalized map.
Params:
normalizedMap
(Object): for the object format seegetNormalizedMap
options
: options passed to RouteConfig constructor
Ex
const routeConfig = RouteConfig routeConfig//=>// {// namespaces: {// posts: {// config: {},// fullPath: '/posts',// id: 'posts',// key: 'posts',// path: '/posts',// routes: {// show: {// config: {},// fullPath: '/posts/:postId',// id: 'posts.show',// key: 'show',// path: '/:postId'// }// }// }// },// routes: {// home: {// config: {},// fullPath: '/',// id: 'home',// key: 'home',// path: '/'// }// }// }