Skip to content

Commit

Permalink
Update Router API (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored Mar 25, 2017
1 parent ee588b2 commit 3a593e5
Show file tree
Hide file tree
Showing 24 changed files with 3,428 additions and 372 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- '7'
- '6'
script:
- npm run lint
Expand Down
59 changes: 49 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
## 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]

- Update Router API (BREAKING CHANGE)
```js
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>
```
See #83 for more info and examples
- `context.next()` now iterates only child routes by default (BREAKING CHANGE)<br>
use `context.next(true)` to iterate through all remaining routes
- Add support for URL Generation
```js
import generateUrls from 'universal-router/generate-urls';
const url = generateUrls(router);
url(routeName, params); // => String
```
- Add support for Dynamic Breadcrumbs, use `context.route.parent` to iterate
- Add support for Declarative Routes, `new Router(routes, { resolveRoute: customResolveRouteFn })`
- Add support for Base URL option, `new Router(routes, { baseUrl: '/base' })`
- Add ability to specify custom context properties once, `new Router(routes, { context: { ... } })`
- Remove `babel-runtime` dependency to decrease library size<br>
Now you need to care about these polyfills yourself:
- [Object.assign](https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
- [Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)
- [Map](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map)
- Rewrite `matchRoute` function without usage of
[generators](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*)
to decrease amount of necessary polyfills
- Remove usage of
[String.prototype.startsWith()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith)
- Add `context.url` with the original url passed to `resolve` method
- Add `context` property to `Route not found` error

## [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 +54,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 +98,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
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License
The MIT License

Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC.
Copyright © 2015-present Konstantin Tarkus, Kriasoft LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Visit **[Quickstart Guide](http://slides.com/koistya/universal-router)** (slides

### Documentation

* [Overview](docs/index.md)
* [Getting Started](docs/getting-started.md)
* [Universal Router API](docs/api.md)
* [Overview](https://github.com/kriasoft/universal-router/blob/master/docs/README.md)
* [Getting Started](https://github.com/kriasoft/universal-router/blob/master/docs/getting-started.md)
* [Universal Router API](https://github.com/kriasoft/universal-router/blob/master/docs/api.md)


### Learn ES6
Expand Down Expand Up @@ -54,7 +54,7 @@ Visit **[Quickstart Guide](http://slides.com/koistya/universal-router)** (slides

### License

Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC. This source code is licensed under the MIT
Copyright © 2015-present Konstantin Tarkus, Kriasoft LLC. This source code is licensed under the MIT
license found in the [LICENSE.txt](https://github.com/kriasoft/universal-router/blob/master/LICENSE.txt)
file. The documentation to the project is licensed under the [CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)
license.
Expand Down
14 changes: 8 additions & 6 deletions docs/index.md → docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ server-side applications (e.g. Node.js/Express, Koa).

## Why use Universal Router?

* It has [simple code](https://github.com/kriasoft/universal-router/blob/master/src/resolve.js)
with minimum dependencies (just `path-to-regexp` and `babel-runtime`)
* It has [simple code](https://github.com/kriasoft/universal-router/blob/master/src/Router.js)
with only single [path-to-regexp](https://github.com/pillarjs/path-to-regexp) dependency
* It can be used with any JavaScript framework such as React, Vue.js etc
* It uses the same middleware approach used in Express and Koa, making it easy to learn


## How does it look like?

```js
import { resolve } from 'universal-router';
import Router from 'universal-router';

const routes = [
{
Expand All @@ -36,21 +36,23 @@ const routes = [
},
{
path: '/:id',
action: (context) => `<h1>Post #${context.params.id}`
action: (context) => `<h1>Post #${context.params.id}</h1>`
}
]
},
];

resolve(routes, '/posts').then(html => {
const router = new Router(routes);

router.resolve('/posts').then(html => {
document.body.innerHTML = html;
});
```

**Note**: If you're using the router with Node v5 and below, import it as follows:

```js
import { resolve } from 'universal-router/legacy';
import Router from 'universal-router/legacy';
````


Expand Down
Loading

0 comments on commit 3a593e5

Please sign in to comment.