Comparing version 2.2.0 to 2.3.0
# Change Log | ||
## 3.0.0 | ||
### Breaking Changes | ||
- `routr` uses native `URLSearchParams` instead of `query-string` | ||
library internally. If you need to support old browsers, you can | ||
either add a `URLSearchParams` polyfill or inject `query-string` | ||
when instantiating `routr`: | ||
```js | ||
router = new Routr(routes, { | ||
queryLib: require('query-string'), | ||
}); | ||
``` | ||
## 2.1.0 | ||
- [#37] Enhance makePath for routes with path array | ||
* [#37] Enhance makePath for routes with path array | ||
## 2.0.2 | ||
- [#36] Bug fix: Add support for question marks in hash fragments | ||
* [#36] Bug fix: Add support for question marks in hash fragments | ||
## 2.0.1 | ||
- [#35] Fix decodeURIComponent of undefined bug | ||
* [#35] Fix decodeURIComponent of undefined bug | ||
@@ -34,3 +19,3 @@ ## 2.0.0 | ||
- [#33] `getRoute` will now `decodeURIComponent` route values, you might need to remove `decodeURIComponent` from your route actions if you were supporting extended characters manually in your routes. | ||
* [#33] `getRoute` will now `decodeURIComponent` route values, you might need to remove `decodeURIComponent` from your route actions if you were supporting extended characters manually in your routes. | ||
@@ -41,23 +26,23 @@ ## 1.0.0 | ||
- [#29] `navigate` is no longer used as part of `router.getRoute` options | ||
- [#29] `route.navigate` has been removed from the matched route object | ||
* [#29] `navigate` is no longer used as part of `router.getRoute` options | ||
* [#29] `route.navigate` has been removed from the matched route object | ||
### Features | ||
- [#30] Route definitions should now be defined as an array of route objects | ||
rather than a map of routes. The old method of defining routes with a map | ||
is still supported, but ordering can not be guaranteed (as per the JavaScript | ||
engine's implementation). | ||
- [#31] Added support for parsing and constructing urls with query strings. | ||
Matched route objects now contain a `query` property containing the map of | ||
query parameters. `router.makePath` now accepts a third `query` parameter | ||
which is a map of query parameters to add to the resulting URL string. e.g. | ||
`router.makePath('home', {}, { foo: 'bar' });` will result in `/?foo=bar`. | ||
Query strings are generated using the `query-string` npm module, but can | ||
be customized by adding the `options.queryLib` to the `Router` constructor. | ||
The replacement should have a `parse` and `stringify` method similar to | ||
`query-string`. An example replacement would be `qs`. | ||
- [#32] Allow routes to match multiple HTTP methods by using an array | ||
for the `route.method` attribute. By default, routes with an undefined | ||
`method` will match ANY method. | ||
* [#30] Route definitions should now be defined as an array of route objects | ||
rather than a map of routes. The old method of defining routes with a map | ||
is still supported, but ordering can not be guaranteed (as per the JavaScript | ||
engine's implementation). | ||
* [#31] Added support for parsing and constructing urls with query strings. | ||
Matched route objects now contain a `query` property containing the map of | ||
query parameters. `router.makePath` now accepts a third `query` parameter | ||
which is a map of query parameters to add to the resulting URL string. e.g. | ||
`router.makePath('home', {}, { foo: 'bar' });` will result in `/?foo=bar`. | ||
Query strings are generated using the `query-string` npm module, but can | ||
be customized by adding the `options.queryLib` to the `Router` constructor. | ||
The replacement should have a `parse` and `stringify` method similar to | ||
`query-string`. An example replacement would be `qs`. | ||
* [#32] Allow routes to match multiple HTTP methods by using an array | ||
for the `route.method` attribute. By default, routes with an undefined | ||
`method` will match ANY method. | ||
@@ -68,3 +53,3 @@ ## 0.1.3 | ||
- [#27] Support for array paths with parameter name collision | ||
* [#27] Support for array paths with parameter name collision | ||
@@ -75,3 +60,3 @@ ## 0.1.2 | ||
- Replace `reverand` with `path-to-regexp` for creation of paths | ||
* Replace `reverand` with `path-to-regexp` for creation of paths | ||
@@ -82,3 +67,3 @@ ## 0.1.1 | ||
- [#22] Make route methods case-insensitive | ||
* [#22] Make route methods case-insensitive | ||
@@ -89,3 +74,3 @@ ## 0.1.0 | ||
- Renamed "path" field to "url" in the return object of getRoute() | ||
* Renamed "path" field to "url" in the return object of getRoute() | ||
@@ -96,3 +81,3 @@ ## 0.0.6 | ||
- Update devDependencies and Readme | ||
* Update devDependencies and Readme | ||
@@ -103,3 +88,3 @@ ## 0.0.5 | ||
- Freeze route objects in non-production environments | ||
* Freeze route objects in non-production environments | ||
@@ -110,3 +95,3 @@ ## 0.0.4 | ||
- Allow matching paths containing query strings | ||
* Allow matching paths containing query strings | ||
@@ -117,3 +102,3 @@ ## 0.0.3 | ||
- Updated dependencies | ||
* Updated dependencies | ||
@@ -124,3 +109,3 @@ ## 0.0.2 | ||
- [#2] Introduction of `navigate` property under route | ||
* [#2] Introduction of `navigate` property under route | ||
@@ -127,0 +112,0 @@ ## 0.0.1 |
@@ -1,6 +0,7 @@ | ||
## Contributing Code to `routr` | ||
Contributing Code to `routr` | ||
------------------------------- | ||
Please be sure to sign our [CLA][] before you submit pull requests or otherwise contribute to `routr`. This protects developers, who rely on [BSD license][]. | ||
[bsd license]: https://github.com/yahoo/routr/blob/master/LICENSE.md | ||
[cla]: https://yahoocla.herokuapp.com/ | ||
[BSD license]: https://github.com/yahoo/routr/blob/master/LICENSE.md | ||
[CLA]: https://yahoocla.herokuapp.com/ |
@@ -7,11 +7,11 @@ # Routr API | ||
- `routes` (optional): Ordered list of routes used for matching. | ||
** `route.name`: Name of the route (used for path making) | ||
** `route.path`: The matching pattern of the route. Follows rules of [path-to-regexp](https://github | ||
.com/pillarjs/path-to-regexp) | ||
\*\* `route.method=undefined`: The method that the path should match to. Will match all methods if `undefined` and no | ||
methods | ||
if `null`. | ||
- `options` (optional): Options for parsing and generating the urls | ||
\*\* `options.queryLib=require('query-string')`: Library to use to `parse` and `stringify` query strings | ||
* `routes` (optional): Ordered list of routes used for matching. | ||
** `route.name`: Name of the route (used for path making) | ||
** `route.path`: The matching pattern of the route. Follows rules of [path-to-regexp](https://github | ||
.com/pillarjs/path-to-regexp) | ||
** `route.method=undefined`: The method that the path should match to. Will match all methods if `undefined` and no | ||
methods | ||
if `null`. | ||
* `options` (optional): Options for parsing and generating the urls | ||
** `options.queryLib=require('query-string')`: Library to use to `parse` and `stringify` query strings | ||
@@ -24,5 +24,5 @@ ## Instance Methods | ||
- `url` (required) The url to be used for route matching. Query strings are **not** considered when performing the match. | ||
- `options` options object | ||
- `options.method` (optional) The case-insensitive HTTP method string. DEFAULT: 'get' | ||
* `url` (required) The url to be used for route matching. Query strings are **not** considered when performing the match. | ||
* `options` options object | ||
* `options.method` (optional) The case-insensitive HTTP method string. DEFAULT: 'get' | ||
@@ -33,4 +33,4 @@ ### makePath(name, params, query) | ||
- `name` (required) The route name | ||
- `params` (required) The route parameters to be used to create the path string | ||
- `query` (optional) The query parameters to be used to create the path string | ||
* `name` (required) The route name | ||
* `params` (required) The route parameters to be used to create the path string | ||
* `query` (optional) The query parameters to be used to create the path string |
@@ -6,30 +6,9 @@ /** | ||
'use strict'; | ||
/*global process:true */ | ||
var pathToRegexp = require('path-to-regexp'); | ||
var queryString = require('query-string'); | ||
var DEFAULT_METHOD = 'GET'; | ||
var cachedCompilers = {}; | ||
var queryString = { | ||
parse: function (string) { | ||
var obj = {}; | ||
var params = new URLSearchParams(string); | ||
params.forEach(function (value, key) { | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
}, | ||
stringify: function (obj) { | ||
var params = new URLSearchParams(); | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
params.append(key, obj[key]); | ||
} | ||
} | ||
params.sort(); | ||
return params.toString(); | ||
}, | ||
}; | ||
/** | ||
@@ -54,3 +33,3 @@ * @class Route | ||
this.methods = {}; | ||
for (var i = 0; i < method.length; ++i) { | ||
for (var i=0; i<method.length; ++i) { | ||
this.methods[method[i].toUpperCase()] = true; | ||
@@ -60,3 +39,3 @@ } | ||
this.keys = []; | ||
this.regexp = pathToRegexp.pathToRegexp(this.config.path, this.keys); | ||
this.regexp = pathToRegexp(this.config.path, this.keys); | ||
this._queryLib = options.queryLib || queryString; | ||
@@ -124,3 +103,3 @@ } | ||
// Leave `pos` at the beginning of the query-string, if any. | ||
['#', '?'].forEach(function (delimiter) { | ||
['#', '?'].forEach(function(delimiter){ | ||
pos = path.indexOf(delimiter); | ||
@@ -143,11 +122,6 @@ if (pos >= 0) { | ||
// name if the path was an array. | ||
if ( | ||
pathMatches[i + 1] !== undefined && | ||
routeParams[self.keys[i].name] === undefined | ||
) { | ||
if (pathMatches[i+1] !== undefined && routeParams[self.keys[i].name] === undefined){ | ||
// Because pathToRegexp encodeURIComponent params values, it is necessary | ||
// to decode when reading from URL | ||
routeParams[self.keys[i].name] = decodeURIComponent( | ||
pathMatches[i + 1] | ||
); | ||
routeParams[self.keys[i].name] = decodeURIComponent(pathMatches[i+1]); | ||
} | ||
@@ -159,3 +133,3 @@ } | ||
if (-1 !== pos) { | ||
queryParams = self._queryLib.parse(url.substring(pos + 1)); | ||
queryParams = self._queryLib.parse(url.substring(pos+1)); | ||
} | ||
@@ -166,3 +140,3 @@ | ||
route: routeParams, | ||
query: queryParams, | ||
query: queryParams | ||
}; | ||
@@ -195,3 +169,3 @@ }; | ||
try { | ||
return this._makePath(routePath, params, query); | ||
return this._makePath(routePath, params, query) | ||
} catch (pathErr) { | ||
@@ -205,3 +179,3 @@ err = pathErr; | ||
Route.prototype._makePath = function (routePath, params, query) { | ||
Route.prototype._makePath = function(routePath, params, query) { | ||
var compiler; | ||
@@ -211,5 +185,3 @@ var url; | ||
if (typeof routePath === 'string') { | ||
compiler = | ||
cachedCompilers[routePath] || | ||
pathToRegexp.compile(routePath, { encode: encodeURIComponent }); | ||
compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath); | ||
cachedCompilers[routePath] = compiler; | ||
@@ -227,3 +199,3 @@ url = compiler(params); | ||
} | ||
}; | ||
} | ||
@@ -284,7 +256,3 @@ /** | ||
self._routeOrder.push(route.name); | ||
self._routes[route.name] = new Route( | ||
route.name, | ||
route, | ||
self._options | ||
); | ||
self._routes[route.name] = new Route(route.name, route, self._options); | ||
}); | ||
@@ -330,3 +298,3 @@ } | ||
config: route.config, | ||
query: match.query, | ||
query: match.query | ||
}; | ||
@@ -347,10 +315,5 @@ } | ||
Router.prototype.makePath = function (name, params, query) { | ||
return ( | ||
(name && | ||
this._routes[name] && | ||
this._routes[name].makePath(params, query)) || | ||
null | ||
); | ||
return (name && this._routes[name] && this._routes[name].makePath(params, query)) || null; | ||
}; | ||
module.exports = Router; |
@@ -1,4 +0,6 @@ | ||
# Software License Agreement (BSD License) | ||
Software License Agreement (BSD License) | ||
======================================== | ||
## Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
Copyright (c) 2014, Yahoo! Inc. All rights reserved. | ||
---------------------------------------------------- | ||
@@ -9,8 +11,8 @@ Redistribution and use of this software in source and binary forms, with or | ||
- Redistributions of source code must retain the above copyright notice, this | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
- Redistributions in binary form must reproduce the above copyright notice, | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
- Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be | ||
* Neither the name of Yahoo! Inc. nor the names of YUI's contributors may be | ||
used to endorse or promote products derived from this software without | ||
@@ -17,0 +19,0 @@ specific prior written permission of Yahoo! Inc. |
{ | ||
"name": "routr", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "A router for both server and client", | ||
@@ -12,5 +12,3 @@ "main": "index.js", | ||
"cover": "node node_modules/istanbul/lib/cli.js cover --dir artifacts -- ./node_modules/mocha/bin/_mocha tests/unit/ --recursive --reporter spec", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check .", | ||
"lint": "eslint . && npm run format:check", | ||
"lint": "eslint .", | ||
"test": "mocha tests/unit/ --recursive --require babel-register --require babel-polyfill --reporter spec" | ||
@@ -26,3 +24,4 @@ }, | ||
"dependencies": { | ||
"path-to-regexp": "^6.2.0" | ||
"path-to-regexp": "^1.1.1", | ||
"query-string": "^5.0.0" | ||
}, | ||
@@ -41,4 +40,3 @@ "devDependencies": { | ||
"pre-commit": "^1.0.7", | ||
"prettier": "^2.3.2", | ||
"sinon": "^11.1.1" | ||
"sinon": "^8.0.0" | ||
}, | ||
@@ -52,7 +50,3 @@ "pre-commit": [ | ||
"router" | ||
], | ||
"prettier": { | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
} | ||
] | ||
} |
@@ -1,7 +0,10 @@ | ||
# Routr | ||
# Routr | ||
[![npm version](https://badge.fury.io/js/routr.svg)](http://badge.fury.io/js/routr) | ||
![github actions](https://github.com/yahoo/routr/actions/workflows/node.js.yml/badge.svg) | ||
[![Build Status](https://travis-ci.org/yahoo/routr.svg?branch=master)](https://travis-ci.org/yahoo/routr) | ||
[![Dependency Status](https://david-dm.org/yahoo/routr.svg)](https://david-dm.org/yahoo/routr) | ||
[![devDependency Status](https://david-dm.org/yahoo/routr/dev-status.svg)](https://david-dm.org/yahoo/routr#info=devDependencies) | ||
[![Coverage Status](https://img.shields.io/coveralls/yahoo/routr.svg)](https://coveralls.io/r/yahoo/routr?branch=master) | ||
Routr library is an implementation of router-related functionalities that can be used for both server and client. It follows the same routing rules as [Express](http://expressjs.com/) by using the same library. This library does not use callbacks for routes, instead just mapping them to string names that can be used as application state and used within your application later. For instance in Flux, the current route would be held as state in a store. | ||
@@ -14,5 +17,5 @@ | ||
```javascript | ||
import Router from 'routr'; | ||
var Router = require('routr'); | ||
const router = new Router([ | ||
var router = new Router([ | ||
{ | ||
@@ -23,4 +26,4 @@ name: 'view_user', | ||
foo: { | ||
bar: 'baz', | ||
}, | ||
bar: 'baz' | ||
} | ||
}, | ||
@@ -30,8 +33,8 @@ { | ||
path: '/user/:id/post/:post', | ||
method: 'get', | ||
}, | ||
method: 'get' | ||
} | ||
]); | ||
// match route | ||
const route = router.getRoute('/user/garfield?foo=bar'); | ||
var route = router.getRoute('/user/garfield?foo=bar'); | ||
if (route) { | ||
@@ -49,11 +52,7 @@ // this will output: | ||
// "path" will be "/user/garfield/post/favoriteFood?meal=breakfast" | ||
const path = router.makePath( | ||
'view_user_post', | ||
{ id: 'garfield', post: 'favoriteFood' }, | ||
{ meal: 'breakfast' } | ||
); | ||
var path = router.makePath('view_user_post', {id: 'garfield', post: 'favoriteFood'}, { meal: 'breakfast' }); | ||
``` | ||
## Object.freeze | ||
We use `Object.freeze` to freeze the router and route objects for non-production environments to ensure the immutability of these objects. | ||
@@ -66,10 +65,7 @@ | ||
### Build with Webpack | ||
Two main utility plugins: | ||
* use [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin) to define the value for `process.env` | ||
* use [UglifyJsPlugin](http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) to remove dead code. | ||
- use [DefinePlugin](http://webpack.github.io/docs/list-of-plugins.html#defineplugin) to define the value for `process.env` | ||
- use [UglifyJsPlugin](http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin) to remove dead code. | ||
Example of the webpack configuration: | ||
```js | ||
@@ -88,10 +84,7 @@ plugins: [ | ||
### Build with Browserify | ||
Similar to webpack, you can also use the following two utils with your favorite build system: | ||
* use [envify](https://github.com/hughsk/envify) to set `process.env.NODE_ENV` to the desired environment | ||
* use [uglifyjs](https://github.com/mishoo/UglifyJS2) to remove dead code. | ||
- use [envify](https://github.com/hughsk/envify) to set `process.env.NODE_ENV` to the desired environment | ||
- use [uglifyjs](https://github.com/mishoo/UglifyJS2) to remove dead code. | ||
Command-line example: | ||
```bash | ||
@@ -101,5 +94,6 @@ $ browserify index.js -t [ envify --NODE_ENV production ] | uglifyjs -c > bundle.js | ||
## API | ||
- [Routr](https://github.com/yahoo/routr/blob/master/docs/routr.md) | ||
- [Routr](https://github.com/yahoo/routr/blob/master/docs/routr.md) | ||
@@ -111,4 +105,5 @@ ## License | ||
[license file]: https://github.com/yahoo/routr/blob/master/LICENSE.md | ||
[LICENSE file]: https://github.com/yahoo/routr/blob/master/LICENSE.md | ||
Third-pary open source code used are listed in our [package.json file](https://github.com/yahoo/routr/blob/master/package.json). | ||
Third-pary open source code used are listed in our [package.json file]( https://github.com/yahoo/routr/blob/master/package.json). | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12
21557
2
11
283
102
+ Addedquery-string@^5.0.0
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedpath-to-regexp@1.9.0(transitive)
+ Addedquery-string@5.1.1(transitive)
+ Addedstrict-uri-encode@1.1.0(transitive)
- Removedpath-to-regexp@6.3.0(transitive)
Updatedpath-to-regexp@^1.1.1