route-config

0.10.4 • Public • Published

🗺️ 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

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:

Ex

const routeConfig = new RouteConfig({
  namespaces: {
    posts: {
      path: '/posts',
      routes: {
        show: { path: '/:postId' }
      }
    }
  },
  routes: {
    home: { path: '/' }
  }
})

routeConfig.getMap(options)

Return map Params:

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:

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 routes
    • fullPath: 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 and namespace path
    • id: id is used internally to create normalized map
    • key (String): namespace name used to get route inside namespace
    • namespaces={}: sub namespaces
    • path (String): namespace path
    • routes={}: 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):
    • config: route config
    • fullPath: route full path
    • id: id is used internally to create normalized map
    • key (String): route name
    • path (String): route path

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:

Ex

routeConfig.url('home')
//=> '/'
 
routeConfig.url('posts.show')
//=> /posts/:postId
 
routeConfig.url('posts.show', { postId: 1 })
//=> /posts/1

static RouteConfig.fromNormalizedMap(normalizedMap)

Returns new RouteConfig instance created from normalized map.

Params:

  • normalizedMap (Object): for the object format see getNormalizedMap
  • options: 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: '/'
//     }
//   }
// }

Readme

Keywords

Package Sidebar

Install

npm i route-config

Weekly Downloads

58

Version

0.10.4

License

MIT

Unpacked Size

148 kB

Total Files

22

Last publish

Collaborators

  • lobor
  • c0r3y8