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

Add support for declarative routes #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## Universal Router Change Log
# Universal Router Change Log

All notable changes to this project will be documented in this file.

### [v2.0.0] - 2016-10-20
## [Unreleased][unreleased]

- Add support for declarative routes ([#67](https://github.com/kriasoft/universal-router/pull/67))

## [v2.0.0] - 2016-10-20

- Preserve `context.params` values from the parent route ([#57](https://github.com/kriasoft/universal-router/pull/57))
- Throws an error if no route found ([#62](https://github.com/kriasoft/universal-router/pull/62))
Expand All @@ -16,39 +20,39 @@ All notable changes to this project will be documented in this file.
- Remove errors handler from core ([#48](https://github.com/kriasoft/universal-router/pull/48))
- Drop support of node.js v5 and below ([#47](https://github.com/kriasoft/universal-router/pull/47))

### [v1.2.2] - 2016-05-31
## [v1.2.2] - 2016-05-31

- Update UMD build to include missing dependencies ([#33](https://github.com/kriasoft/universal-router/pull/33))

### [v1.2.1] - 2016-05-12
## [v1.2.1] - 2016-05-12

- Rename `match()` to `resolve()`. E.g. `import { resovle } from 'universal-router'`
- Fix an issue when the router throws an exception when the top-level route doesn't have `children` property
- Include CommonJS, Harmony Modules, ES5.1 and UMD builds into NPM package
- Include source maps into NPM package

### [v1.1.0-beta.4] - 2016-04-27
## [v1.1.0-beta.4] - 2016-04-27

- Fix optional parameters, e.g. `/products/:id?` ([#27](https://github.com/kriasoft/universal-router/pull/27))

### [v1.1.0-beta.3] - 2016-04-08
## [v1.1.0-beta.3] - 2016-04-08

- Fix `matchRoute()` yielding the same route twice when it matches to both full and base URLs

### [v1.1.0-beta.2] - 2016-04-08
## [v1.1.0-beta.2] - 2016-04-08

- `match(routes, { path, ...context)` now throws an error if a matching route was not found (BREAKING CHANGE)
- If there is a top-level route with path equal to `/error`, it will be used for error handling by convention

### [v1.1.0-beta.1] - 2016-04-05
## [v1.1.0-beta.1] - 2016-04-05

- Remove `Router` class and `router.dispatch()` method in favor of
`match(routes, { path, ...context })`, where `routes` is just a plain JavaScript objects containing
the list of routes (BREAKING CHANGE)
- Add `context.end()` method to be used from inside route actions
- Update documentation and code samples

### [v1.0.0-beta.1] - 2016-03-25
## [v1.0.0-beta.1] - 2016-03-25

- Rename `react-routing` to `universal-router` (BREAKING CHANGE)
- Remove `router.on(path, ...actions)` in favor of `router.route(path, ...actions)` (BREAKING CHANGE)
Expand All @@ -60,10 +64,11 @@ All notable changes to this project will be documented in this file.
- Make the library use `babel-runtime` package instead of an inline runtime
- Add [CHANGELOG.md](CHANGELOG.md) file with the notable changes to this project

### [v0.0.7] - 2015-12-13
## [v0.0.7] - 2015-12-13

- Small bug fixes and improvements

[unreleased]: https://github.com/kriasoft/universal-router/compare/v2.0.0...HEAD
[v2.0.0]: https://github.com/kriasoft/universal-router/compare/v1.2.2...v2.0.0
[v1.2.2]: https://github.com/kriasoft/universal-router/compare/v1.2.1...v1.2.2
[v1.2.1]: https://github.com/kriasoft/universal-router/compare/v1.1.0-beta.4...v1.2.1
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const routes = [
},
{
path: '/:id',
action: (context) => `<h1>Post #${context.params.id}`
action: (context) => `<h1>Post #${context.params.id}</h1>`
}
]
},
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@
"extends": "airbnb-base"
},
"dependencies": {
"babel-runtime": "^6.11.6",
"babel-runtime": "^6.18.0",
"path-to-regexp": "^1.6.0"
},
"devDependencies": {
"babel-cli": "^6.16.0",
"babel-core": "^6.17.0",
"babel-plugin-external-helpers": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.16.0",
"babel-cli": "^6.18.0",
"babel-core": "^6.18.2",
"babel-plugin-external-helpers": "^6.18.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-plugin-uglify": "^1.0.2",
"babel-preset-es2016": "^6.16.0",
"babel-preset-es2017": "^6.16.0",
"babel-preset-latest": "^6.16.0",
"babel-register": "^6.16.3",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"coveralls": "^2.11.14",
"del": "^2.2.2",
"eslint": "^3.8.1",
"eslint": "^3.9.1",
"eslint-config-airbnb-base": "^9.0.0",
"eslint-plugin-import": "^2.0.1",
"istanbul": "^1.1.0-alpha.1",
Expand Down
15 changes: 10 additions & 5 deletions src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

import matchRoute from './matchRoute';

async function resolve(routes, pathOrContext) {
function handleRoute(context, params) {
if (typeof context.route.action === 'function') {
return context.route.action(context, params);
}
return null;
}

async function resolve(routes, pathOrContext, action = handleRoute) {
const context = typeof pathOrContext === 'string' || pathOrContext instanceof String
? { path: pathOrContext }
: pathOrContext;
Expand All @@ -27,10 +34,8 @@ async function resolve(routes, pathOrContext) {
return result;
}

if (value.route.action) {
const newContext = Object.assign({}, context, value);
result = await value.route.action(newContext, newContext.params);
}
const newContext = Object.assign({}, context, value);
result = await action(newContext, newContext.params);

return await next();
}
Expand Down
18 changes: 18 additions & 0 deletions test/resolve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,22 @@ describe('resolve(routes, { path, ...context })', () => {
expect(err).to.be.equal(error);
});

it('should support common action handler for declarative routes', async () => {
const handler = sinon.spy(context => context.route.component || null);
const action = sinon.spy();
const routes = {
path: '/a',
action,
children: [
{ path: '/:b', component: null, action },
{ path: '/c', component: 'c', action },
{ path: '/d', component: 'd', action },
],
};
const result = await resolve(routes, '/a/c', handler);
expect(handler.calledThrice).to.be.true;
expect(action.called).to.be.false;
expect(result).to.be.equal('c');
});

});