0.10.4 • Published 7 years ago
route-config v0.10.4
🗺️ RouteConfig
Static route configuration helper
Installation
NPM:
$ npm install --save route-configYarn:
yarn add route-configImport
In ES6:
import { Config, RouteConfig } from 'route-config'
Use
Basic
import { RouteConfig } from 'route-config'
const routeConfig = new RouteConfig({
namespaces: {
posts: {
path: '/posts',
routes: {
index: {},
show: {
path: '/:postId'
}
}
}
},
routes: {
home: { path: '/home' }
}
})
routeConfig.url('home')
//=> "/home"
routeConfig.url('posts.show')
//=> "/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 withkeynamespaces={}: sub namespacespath='': root pathroutes={}: sub routes
options={}:configs: options passed toConfigManagerconstructor
Ex
const routeConfig = new 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.getMap()
//=>
// {
// 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.getNormalisedMap()
//=>
// {
// 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.getRoute('home')
//=>
// {
// config: {},
// fullPath: '/',
// id: 'home',
// key: 'home',
// path: '/'
// }
routeConfig.getRoute('posts.show') // 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.merge({
routes: {
contact: { path: '/contact' }
}
})
routeConfig.getMap()
//=>
// {
// 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 andnamespacepathid: 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 = routeConfig.namespace({ key: 'authors', path: '/authors' })
namespace.route({ key: 'show', path: '/:authorId' })
namespace.getMap()
//=>
// {
// config: {},
// fullPath: '/authors',
// id: 'authors',
// key: 'authors',
// namespaces: {},
// path: '/authors',
// routes: {
// show: {
// config: {},
// fullPath: '/authors/:authorId',
// id: 'authors.show',
// key: 'show',
// path: '/:authorId'
// }
// }
// }
routeConfig.getMap()
//=>
// {
// 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.route({ key: 'about', path: '/about' }).getMap()
//=>
// {
// 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-regexpoptions=undefined: options passed topath-to-regexp
Ex
routeConfig.url('home')
//=> '/'
routeConfig.url('posts.show')
//=> /posts/:postId
routeConfig.url('posts.show', { postId: 1 })
//=> /posts/1static RouteConfig.fromNormalizedMap(normalizedMap)
Returns new RouteConfig instance created from normalized map.
Params:
normalizedMap(Object): for the object format seegetNormalizedMapoptions: options passed to RouteConfig constructor
Ex
const routeConfig = RouteConfig.fromNormalizedMap({
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.getMap()
//=>
// {
// 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: '/'
// }
// }
// }