Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Router API #83

Merged
merged 1 commit into from
Mar 25, 2017
Merged

Update Router API #83

merged 1 commit into from
Mar 25, 2017

Conversation

frenzzy
Copy link
Member

@frenzzy frenzzy commented Mar 23, 2017

Changes:

  • Update Router API (BREAKING CHANGE)
    import Router from 'universal-router';
    const router = new Router(routes, options);
    router.resolve({ path, ...context }); // => Promise<any>
    
    // previously
    import { resolve } from 'universal-router';
    resolve(routes, { path, ...context }); // => Promise<any>
  • context.next() now iterates only child routes by default (BREAKING CHANGE)
    use context.next(true) to iterate through all remaining routes
  • Remove babel-runtime dependency to decrease library size (BREAKING CHANGE)
    Now you need to care about these polyfills yourself:
  • Rewrite matchRoute function without usage of generators to decrease amount of necessary polyfills
  • Remove usage of String.prototype.startsWith(), close Fix for compatibility #81
  • Add support for URL Generation, close Generate paths by name #74
    import generateUrls from 'universal-router/generateUrls';
    const url = generateUrls(router);
    url(routeName, params); // => String
  • Add support for Dynamic Breadcrumbs, use context.route.parent to iterate, ref Example of breadcrumbs? #36
  • Add support for Declarative Routes, close Add support for declarative routes #67
    const router = new Router(routes, { resolveRoute: customResolveRouteFn });
  • Add support for Base URL option
    const router = new Router(routes, { baseUrl: '/base' });
  • Add ability to specify custom context property once
    const router = new Router(routes, { context: { store: createStore(), ... } });
  • Add context.url with the original url passed to resolve method, close context.path gets overwritten by route.path while resolving #69
  • Add context property to Route not found error, close On 404 error stack trace is not showing 404 urls. #45

Examples:

import Router from 'universal-router';


import generateUrls from 'universal-router/generateUrls';

 // optional

// route example
const route = {
  path: '/post/:title',           // string, required

  name: 'post',                   // unique string, optional
  parent: null,                   // route or null, optional (filled by router)
  children: [],                   // array of routes or null, optional

  action(context, params) {       // function, optional

    // action method should return anything except `null` or `undefined`
    // to be resolved by router, otherwise router will throw an error
    // if all matched routes returned nothing
    return null;
  }
};

// just array of routes
const routes = [
  { path: '/' },
  { path: '/hello', name: 'hello' },
  { path: '/example', action() {
    return 'Page';
  } },
];

// context object which router supplements and transmits to each route
const context = {
  // your params here

  // plus params filled by the router
  router,                         // current router instance
  route,                          // matched route object
  next,                           // middleware function which can continue resolving
  url: '/base/post/welcome',      // url which was transmitted via router.resolve()
  baseUrl: '/base',               // base url relative to current route
  path: '/post/welcome',          // matched path
  params: { title: 'welcome' },   // matched params
  keys: [{ name: 'title', ... }], // an array of keys found in the path
};

// default router options

const options = {// you can add anything to the context object
  // right when you create a router instance
  // for example `context: { store: createStore() }`
  context,

  // you may define Base URL if your site lives in subfolder
  // like `https://kriasoft.github.io/universal-router/*`
  // baseUrl: '/universal-router'
  baseUrl: '',

  // you can redefine `resolveRoute` function to be able to work with your
  // routes in declarative manner or for any custom route handling logic
  // by default router calls `action` method of matched routes if any
  resolveRoute(context, params) {
    if (typeof context.route.action === 'function') {
      context.route.action(context, params);
    }
    return null;
  },
};



// new constructor based router api
const router = new Router(routes, options);
// or just new Router(route);



// url generation
const url = generateUrls(router);
const routeName = 'post';

const params = { title: 'welcome' };

url(routeName, params);              // => '/post/welcome'

url('world');                        // => '/hello/world'



// resolving routes
const path = '/base/post/welcome';
router.resolve({ path, ...context }) // => Promise<Any>
router.resolve('/example')           // => Promise => 'Page'
router.resolve('/random')            // => Promise
 => Error('Page not found')

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 814f670 on frenzzy:next into ee588b2 on kriasoft:master.

@frenzzy frenzzy merged commit 3a593e5 into kriasoft:master Mar 25, 2017
@frenzzy frenzzy deleted the next branch March 25, 2017 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants